1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor LSM hooks. 6 * 7 * Copyright (C) 1998-2008 Novell/SUSE 8 * Copyright 2009-2010 Canonical Ltd. 9 */ 10 11 #include <linux/lsm_hooks.h> 12 #include <linux/moduleparam.h> 13 #include <linux/mm.h> 14 #include <linux/mman.h> 15 #include <linux/mount.h> 16 #include <linux/namei.h> 17 #include <linux/ptrace.h> 18 #include <linux/ctype.h> 19 #include <linux/sysctl.h> 20 #include <linux/audit.h> 21 #include <linux/user_namespace.h> 22 #include <linux/netfilter_ipv4.h> 23 #include <linux/netfilter_ipv6.h> 24 #include <linux/zstd.h> 25 #include <net/sock.h> 26 #include <uapi/linux/mount.h> 27 28 #include "include/apparmor.h" 29 #include "include/apparmorfs.h" 30 #include "include/audit.h" 31 #include "include/capability.h" 32 #include "include/cred.h" 33 #include "include/file.h" 34 #include "include/ipc.h" 35 #include "include/net.h" 36 #include "include/path.h" 37 #include "include/label.h" 38 #include "include/policy.h" 39 #include "include/policy_ns.h" 40 #include "include/procattr.h" 41 #include "include/mount.h" 42 #include "include/secid.h" 43 44 /* Flag indicating whether initialization completed */ 45 int apparmor_initialized; 46 47 union aa_buffer { 48 struct list_head list; 49 char buffer[1]; 50 }; 51 52 #define RESERVE_COUNT 2 53 static int reserve_count = RESERVE_COUNT; 54 static int buffer_count; 55 56 static LIST_HEAD(aa_global_buffers); 57 static DEFINE_SPINLOCK(aa_buffers_lock); 58 59 /* 60 * LSM hook functions 61 */ 62 63 /* 64 * put the associated labels 65 */ 66 static void apparmor_cred_free(struct cred *cred) 67 { 68 aa_put_label(cred_label(cred)); 69 set_cred_label(cred, NULL); 70 } 71 72 /* 73 * allocate the apparmor part of blank credentials 74 */ 75 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) 76 { 77 set_cred_label(cred, NULL); 78 return 0; 79 } 80 81 /* 82 * prepare new cred label for modification by prepare_cred block 83 */ 84 static int apparmor_cred_prepare(struct cred *new, const struct cred *old, 85 gfp_t gfp) 86 { 87 set_cred_label(new, aa_get_newest_label(cred_label(old))); 88 return 0; 89 } 90 91 /* 92 * transfer the apparmor data to a blank set of creds 93 */ 94 static void apparmor_cred_transfer(struct cred *new, const struct cred *old) 95 { 96 set_cred_label(new, aa_get_newest_label(cred_label(old))); 97 } 98 99 static void apparmor_task_free(struct task_struct *task) 100 { 101 102 aa_free_task_ctx(task_ctx(task)); 103 } 104 105 static int apparmor_task_alloc(struct task_struct *task, 106 unsigned long clone_flags) 107 { 108 struct aa_task_ctx *new = task_ctx(task); 109 110 aa_dup_task_ctx(new, task_ctx(current)); 111 112 return 0; 113 } 114 115 static int apparmor_ptrace_access_check(struct task_struct *child, 116 unsigned int mode) 117 { 118 struct aa_label *tracer, *tracee; 119 int error; 120 121 tracer = __begin_current_label_crit_section(); 122 tracee = aa_get_task_label(child); 123 error = aa_may_ptrace(tracer, tracee, 124 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ 125 : AA_PTRACE_TRACE); 126 aa_put_label(tracee); 127 __end_current_label_crit_section(tracer); 128 129 return error; 130 } 131 132 static int apparmor_ptrace_traceme(struct task_struct *parent) 133 { 134 struct aa_label *tracer, *tracee; 135 int error; 136 137 tracee = __begin_current_label_crit_section(); 138 tracer = aa_get_task_label(parent); 139 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE); 140 aa_put_label(tracer); 141 __end_current_label_crit_section(tracee); 142 143 return error; 144 } 145 146 /* Derived from security/commoncap.c:cap_capget */ 147 static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, 148 kernel_cap_t *inheritable, kernel_cap_t *permitted) 149 { 150 struct aa_label *label; 151 const struct cred *cred; 152 153 rcu_read_lock(); 154 cred = __task_cred(target); 155 label = aa_get_newest_cred_label(cred); 156 157 /* 158 * cap_capget is stacked ahead of this and will 159 * initialize effective and permitted. 160 */ 161 if (!unconfined(label)) { 162 struct aa_profile *profile; 163 struct label_it i; 164 165 label_for_each_confined(i, label, profile) { 166 struct aa_ruleset *rules; 167 if (COMPLAIN_MODE(profile)) 168 continue; 169 rules = list_first_entry(&profile->rules, 170 typeof(*rules), list); 171 *effective = cap_intersect(*effective, 172 rules->caps.allow); 173 *permitted = cap_intersect(*permitted, 174 rules->caps.allow); 175 } 176 } 177 rcu_read_unlock(); 178 aa_put_label(label); 179 180 return 0; 181 } 182 183 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, 184 int cap, unsigned int opts) 185 { 186 struct aa_label *label; 187 int error = 0; 188 189 label = aa_get_newest_cred_label(cred); 190 if (!unconfined(label)) 191 error = aa_capable(label, cap, opts); 192 aa_put_label(label); 193 194 return error; 195 } 196 197 /** 198 * common_perm - basic common permission check wrapper fn for paths 199 * @op: operation being checked 200 * @path: path to check permission of (NOT NULL) 201 * @mask: requested permissions mask 202 * @cond: conditional info for the permission request (NOT NULL) 203 * 204 * Returns: %0 else error code if error or permission denied 205 */ 206 static int common_perm(const char *op, const struct path *path, u32 mask, 207 struct path_cond *cond) 208 { 209 struct aa_label *label; 210 int error = 0; 211 212 label = __begin_current_label_crit_section(); 213 if (!unconfined(label)) 214 error = aa_path_perm(op, label, path, 0, mask, cond); 215 __end_current_label_crit_section(label); 216 217 return error; 218 } 219 220 /** 221 * common_perm_cond - common permission wrapper around inode cond 222 * @op: operation being checked 223 * @path: location to check (NOT NULL) 224 * @mask: requested permissions mask 225 * 226 * Returns: %0 else error code if error or permission denied 227 */ 228 static int common_perm_cond(const char *op, const struct path *path, u32 mask) 229 { 230 struct user_namespace *mnt_userns = mnt_user_ns(path->mnt); 231 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, 232 d_backing_inode(path->dentry)); 233 struct path_cond cond = { 234 vfsuid_into_kuid(vfsuid), 235 d_backing_inode(path->dentry)->i_mode 236 }; 237 238 if (!path_mediated_fs(path->dentry)) 239 return 0; 240 241 return common_perm(op, path, mask, &cond); 242 } 243 244 /** 245 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry 246 * @op: operation being checked 247 * @dir: directory of the dentry (NOT NULL) 248 * @dentry: dentry to check (NOT NULL) 249 * @mask: requested permissions mask 250 * @cond: conditional info for the permission request (NOT NULL) 251 * 252 * Returns: %0 else error code if error or permission denied 253 */ 254 static int common_perm_dir_dentry(const char *op, const struct path *dir, 255 struct dentry *dentry, u32 mask, 256 struct path_cond *cond) 257 { 258 struct path path = { .mnt = dir->mnt, .dentry = dentry }; 259 260 return common_perm(op, &path, mask, cond); 261 } 262 263 /** 264 * common_perm_rm - common permission wrapper for operations doing rm 265 * @op: operation being checked 266 * @dir: directory that the dentry is in (NOT NULL) 267 * @dentry: dentry being rm'd (NOT NULL) 268 * @mask: requested permission mask 269 * 270 * Returns: %0 else error code if error or permission denied 271 */ 272 static int common_perm_rm(const char *op, const struct path *dir, 273 struct dentry *dentry, u32 mask) 274 { 275 struct inode *inode = d_backing_inode(dentry); 276 struct user_namespace *mnt_userns = mnt_user_ns(dir->mnt); 277 struct path_cond cond = { }; 278 vfsuid_t vfsuid; 279 280 if (!inode || !path_mediated_fs(dentry)) 281 return 0; 282 283 vfsuid = i_uid_into_vfsuid(mnt_userns, inode); 284 cond.uid = vfsuid_into_kuid(vfsuid); 285 cond.mode = inode->i_mode; 286 287 return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 288 } 289 290 /** 291 * common_perm_create - common permission wrapper for operations doing create 292 * @op: operation being checked 293 * @dir: directory that dentry will be created in (NOT NULL) 294 * @dentry: dentry to create (NOT NULL) 295 * @mask: request permission mask 296 * @mode: created file mode 297 * 298 * Returns: %0 else error code if error or permission denied 299 */ 300 static int common_perm_create(const char *op, const struct path *dir, 301 struct dentry *dentry, u32 mask, umode_t mode) 302 { 303 struct path_cond cond = { current_fsuid(), mode }; 304 305 if (!path_mediated_fs(dir->dentry)) 306 return 0; 307 308 return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 309 } 310 311 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) 312 { 313 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); 314 } 315 316 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, 317 umode_t mode) 318 { 319 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, 320 S_IFDIR); 321 } 322 323 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) 324 { 325 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); 326 } 327 328 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, 329 umode_t mode, unsigned int dev) 330 { 331 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); 332 } 333 334 static int apparmor_path_truncate(const struct path *path) 335 { 336 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); 337 } 338 339 static int apparmor_file_truncate(struct file *file) 340 { 341 return apparmor_path_truncate(&file->f_path); 342 } 343 344 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, 345 const char *old_name) 346 { 347 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, 348 S_IFLNK); 349 } 350 351 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, 352 struct dentry *new_dentry) 353 { 354 struct aa_label *label; 355 int error = 0; 356 357 if (!path_mediated_fs(old_dentry)) 358 return 0; 359 360 label = begin_current_label_crit_section(); 361 if (!unconfined(label)) 362 error = aa_path_link(label, old_dentry, new_dir, new_dentry); 363 end_current_label_crit_section(label); 364 365 return error; 366 } 367 368 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, 369 const struct path *new_dir, struct dentry *new_dentry, 370 const unsigned int flags) 371 { 372 struct aa_label *label; 373 int error = 0; 374 375 if (!path_mediated_fs(old_dentry)) 376 return 0; 377 if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry)) 378 return 0; 379 380 label = begin_current_label_crit_section(); 381 if (!unconfined(label)) { 382 struct user_namespace *mnt_userns = mnt_user_ns(old_dir->mnt); 383 vfsuid_t vfsuid; 384 struct path old_path = { .mnt = old_dir->mnt, 385 .dentry = old_dentry }; 386 struct path new_path = { .mnt = new_dir->mnt, 387 .dentry = new_dentry }; 388 struct path_cond cond = { 389 .mode = d_backing_inode(old_dentry)->i_mode 390 }; 391 vfsuid = i_uid_into_vfsuid(mnt_userns, d_backing_inode(old_dentry)); 392 cond.uid = vfsuid_into_kuid(vfsuid); 393 394 if (flags & RENAME_EXCHANGE) { 395 struct path_cond cond_exchange = { 396 .mode = d_backing_inode(new_dentry)->i_mode, 397 }; 398 vfsuid = i_uid_into_vfsuid(mnt_userns, d_backing_inode(old_dentry)); 399 cond_exchange.uid = vfsuid_into_kuid(vfsuid); 400 401 error = aa_path_perm(OP_RENAME_SRC, label, &new_path, 0, 402 MAY_READ | AA_MAY_GETATTR | MAY_WRITE | 403 AA_MAY_SETATTR | AA_MAY_DELETE, 404 &cond_exchange); 405 if (!error) 406 error = aa_path_perm(OP_RENAME_DEST, label, &old_path, 407 0, MAY_WRITE | AA_MAY_SETATTR | 408 AA_MAY_CREATE, &cond_exchange); 409 } 410 411 if (!error) 412 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, 413 MAY_READ | AA_MAY_GETATTR | MAY_WRITE | 414 AA_MAY_SETATTR | AA_MAY_DELETE, 415 &cond); 416 if (!error) 417 error = aa_path_perm(OP_RENAME_DEST, label, &new_path, 418 0, MAY_WRITE | AA_MAY_SETATTR | 419 AA_MAY_CREATE, &cond); 420 421 } 422 end_current_label_crit_section(label); 423 424 return error; 425 } 426 427 static int apparmor_path_chmod(const struct path *path, umode_t mode) 428 { 429 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); 430 } 431 432 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) 433 { 434 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); 435 } 436 437 static int apparmor_inode_getattr(const struct path *path) 438 { 439 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); 440 } 441 442 static int apparmor_file_open(struct file *file) 443 { 444 struct aa_file_ctx *fctx = file_ctx(file); 445 struct aa_label *label; 446 int error = 0; 447 448 if (!path_mediated_fs(file->f_path.dentry)) 449 return 0; 450 451 /* If in exec, permission is handled by bprm hooks. 452 * Cache permissions granted by the previous exec check, with 453 * implicit read and executable mmap which are required to 454 * actually execute the image. 455 */ 456 if (current->in_execve) { 457 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; 458 return 0; 459 } 460 461 label = aa_get_newest_cred_label(file->f_cred); 462 if (!unconfined(label)) { 463 struct user_namespace *mnt_userns = file_mnt_user_ns(file); 464 struct inode *inode = file_inode(file); 465 vfsuid_t vfsuid; 466 struct path_cond cond = { 467 .mode = inode->i_mode, 468 }; 469 vfsuid = i_uid_into_vfsuid(mnt_userns, inode); 470 cond.uid = vfsuid_into_kuid(vfsuid); 471 472 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0, 473 aa_map_file_to_perms(file), &cond); 474 /* todo cache full allowed permissions set and state */ 475 fctx->allow = aa_map_file_to_perms(file); 476 } 477 aa_put_label(label); 478 479 return error; 480 } 481 482 static int apparmor_file_alloc_security(struct file *file) 483 { 484 struct aa_file_ctx *ctx = file_ctx(file); 485 struct aa_label *label = begin_current_label_crit_section(); 486 487 spin_lock_init(&ctx->lock); 488 rcu_assign_pointer(ctx->label, aa_get_label(label)); 489 end_current_label_crit_section(label); 490 return 0; 491 } 492 493 static void apparmor_file_free_security(struct file *file) 494 { 495 struct aa_file_ctx *ctx = file_ctx(file); 496 497 if (ctx) 498 aa_put_label(rcu_access_pointer(ctx->label)); 499 } 500 501 static int common_file_perm(const char *op, struct file *file, u32 mask, 502 bool in_atomic) 503 { 504 struct aa_label *label; 505 int error = 0; 506 507 /* don't reaudit files closed during inheritance */ 508 if (file->f_path.dentry == aa_null.dentry) 509 return -EACCES; 510 511 label = __begin_current_label_crit_section(); 512 error = aa_file_perm(op, label, file, mask, in_atomic); 513 __end_current_label_crit_section(label); 514 515 return error; 516 } 517 518 static int apparmor_file_receive(struct file *file) 519 { 520 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file), 521 false); 522 } 523 524 static int apparmor_file_permission(struct file *file, int mask) 525 { 526 return common_file_perm(OP_FPERM, file, mask, false); 527 } 528 529 static int apparmor_file_lock(struct file *file, unsigned int cmd) 530 { 531 u32 mask = AA_MAY_LOCK; 532 533 if (cmd == F_WRLCK) 534 mask |= MAY_WRITE; 535 536 return common_file_perm(OP_FLOCK, file, mask, false); 537 } 538 539 static int common_mmap(const char *op, struct file *file, unsigned long prot, 540 unsigned long flags, bool in_atomic) 541 { 542 int mask = 0; 543 544 if (!file || !file_ctx(file)) 545 return 0; 546 547 if (prot & PROT_READ) 548 mask |= MAY_READ; 549 /* 550 * Private mappings don't require write perms since they don't 551 * write back to the files 552 */ 553 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) 554 mask |= MAY_WRITE; 555 if (prot & PROT_EXEC) 556 mask |= AA_EXEC_MMAP; 557 558 return common_file_perm(op, file, mask, in_atomic); 559 } 560 561 static int apparmor_mmap_file(struct file *file, unsigned long reqprot, 562 unsigned long prot, unsigned long flags) 563 { 564 return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC); 565 } 566 567 static int apparmor_file_mprotect(struct vm_area_struct *vma, 568 unsigned long reqprot, unsigned long prot) 569 { 570 return common_mmap(OP_FMPROT, vma->vm_file, prot, 571 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0, 572 false); 573 } 574 575 static int apparmor_sb_mount(const char *dev_name, const struct path *path, 576 const char *type, unsigned long flags, void *data) 577 { 578 struct aa_label *label; 579 int error = 0; 580 581 /* Discard magic */ 582 if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 583 flags &= ~MS_MGC_MSK; 584 585 flags &= ~AA_MS_IGNORE_MASK; 586 587 label = __begin_current_label_crit_section(); 588 if (!unconfined(label)) { 589 if (flags & MS_REMOUNT) 590 error = aa_remount(label, path, flags, data); 591 else if (flags & MS_BIND) 592 error = aa_bind_mount(label, path, dev_name, flags); 593 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | 594 MS_UNBINDABLE)) 595 error = aa_mount_change_type(label, path, flags); 596 else if (flags & MS_MOVE) 597 error = aa_move_mount(label, path, dev_name); 598 else 599 error = aa_new_mount(label, dev_name, path, type, 600 flags, data); 601 } 602 __end_current_label_crit_section(label); 603 604 return error; 605 } 606 607 static int apparmor_sb_umount(struct vfsmount *mnt, int flags) 608 { 609 struct aa_label *label; 610 int error = 0; 611 612 label = __begin_current_label_crit_section(); 613 if (!unconfined(label)) 614 error = aa_umount(label, mnt, flags); 615 __end_current_label_crit_section(label); 616 617 return error; 618 } 619 620 static int apparmor_sb_pivotroot(const struct path *old_path, 621 const struct path *new_path) 622 { 623 struct aa_label *label; 624 int error = 0; 625 626 label = aa_get_current_label(); 627 if (!unconfined(label)) 628 error = aa_pivotroot(label, old_path, new_path); 629 aa_put_label(label); 630 631 return error; 632 } 633 634 static int apparmor_getprocattr(struct task_struct *task, const char *name, 635 char **value) 636 { 637 int error = -ENOENT; 638 /* released below */ 639 const struct cred *cred = get_task_cred(task); 640 struct aa_task_ctx *ctx = task_ctx(current); 641 struct aa_label *label = NULL; 642 643 if (strcmp(name, "current") == 0) 644 label = aa_get_newest_label(cred_label(cred)); 645 else if (strcmp(name, "prev") == 0 && ctx->previous) 646 label = aa_get_newest_label(ctx->previous); 647 else if (strcmp(name, "exec") == 0 && ctx->onexec) 648 label = aa_get_newest_label(ctx->onexec); 649 else 650 error = -EINVAL; 651 652 if (label) 653 error = aa_getprocattr(label, value); 654 655 aa_put_label(label); 656 put_cred(cred); 657 658 return error; 659 } 660 661 static int apparmor_setprocattr(const char *name, void *value, 662 size_t size) 663 { 664 char *command, *largs = NULL, *args = value; 665 size_t arg_size; 666 int error; 667 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, 668 OP_SETPROCATTR); 669 670 if (size == 0) 671 return -EINVAL; 672 673 /* AppArmor requires that the buffer must be null terminated atm */ 674 if (args[size - 1] != '\0') { 675 /* null terminate */ 676 largs = args = kmalloc(size + 1, GFP_KERNEL); 677 if (!args) 678 return -ENOMEM; 679 memcpy(args, value, size); 680 args[size] = '\0'; 681 } 682 683 error = -EINVAL; 684 args = strim(args); 685 command = strsep(&args, " "); 686 if (!args) 687 goto out; 688 args = skip_spaces(args); 689 if (!*args) 690 goto out; 691 692 arg_size = size - (args - (largs ? largs : (char *) value)); 693 if (strcmp(name, "current") == 0) { 694 if (strcmp(command, "changehat") == 0) { 695 error = aa_setprocattr_changehat(args, arg_size, 696 AA_CHANGE_NOFLAGS); 697 } else if (strcmp(command, "permhat") == 0) { 698 error = aa_setprocattr_changehat(args, arg_size, 699 AA_CHANGE_TEST); 700 } else if (strcmp(command, "changeprofile") == 0) { 701 error = aa_change_profile(args, AA_CHANGE_NOFLAGS); 702 } else if (strcmp(command, "permprofile") == 0) { 703 error = aa_change_profile(args, AA_CHANGE_TEST); 704 } else if (strcmp(command, "stack") == 0) { 705 error = aa_change_profile(args, AA_CHANGE_STACK); 706 } else 707 goto fail; 708 } else if (strcmp(name, "exec") == 0) { 709 if (strcmp(command, "exec") == 0) 710 error = aa_change_profile(args, AA_CHANGE_ONEXEC); 711 else if (strcmp(command, "stack") == 0) 712 error = aa_change_profile(args, (AA_CHANGE_ONEXEC | 713 AA_CHANGE_STACK)); 714 else 715 goto fail; 716 } else 717 /* only support the "current" and "exec" process attributes */ 718 goto fail; 719 720 if (!error) 721 error = size; 722 out: 723 kfree(largs); 724 return error; 725 726 fail: 727 aad(&sa)->label = begin_current_label_crit_section(); 728 aad(&sa)->info = name; 729 aad(&sa)->error = error = -EINVAL; 730 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL); 731 end_current_label_crit_section(aad(&sa)->label); 732 goto out; 733 } 734 735 /** 736 * apparmor_bprm_committing_creds - do task cleanup on committing new creds 737 * @bprm: binprm for the exec (NOT NULL) 738 */ 739 static void apparmor_bprm_committing_creds(struct linux_binprm *bprm) 740 { 741 struct aa_label *label = aa_current_raw_label(); 742 struct aa_label *new_label = cred_label(bprm->cred); 743 744 /* bail out if unconfined or not changing profile */ 745 if ((new_label->proxy == label->proxy) || 746 (unconfined(new_label))) 747 return; 748 749 aa_inherit_files(bprm->cred, current->files); 750 751 current->pdeath_signal = 0; 752 753 /* reset soft limits and set hard limits for the new label */ 754 __aa_transition_rlimits(label, new_label); 755 } 756 757 /** 758 * apparmor_bprm_committed_creds() - do cleanup after new creds committed 759 * @bprm: binprm for the exec (NOT NULL) 760 */ 761 static void apparmor_bprm_committed_creds(struct linux_binprm *bprm) 762 { 763 /* clear out temporary/transitional state from the context */ 764 aa_clear_task_ctx_trans(task_ctx(current)); 765 766 return; 767 } 768 769 static void apparmor_current_getsecid_subj(u32 *secid) 770 { 771 struct aa_label *label = aa_get_current_label(); 772 *secid = label->secid; 773 aa_put_label(label); 774 } 775 776 static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid) 777 { 778 struct aa_label *label = aa_get_task_label(p); 779 *secid = label->secid; 780 aa_put_label(label); 781 } 782 783 static int apparmor_task_setrlimit(struct task_struct *task, 784 unsigned int resource, struct rlimit *new_rlim) 785 { 786 struct aa_label *label = __begin_current_label_crit_section(); 787 int error = 0; 788 789 if (!unconfined(label)) 790 error = aa_task_setrlimit(label, task, resource, new_rlim); 791 __end_current_label_crit_section(label); 792 793 return error; 794 } 795 796 static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, 797 int sig, const struct cred *cred) 798 { 799 struct aa_label *cl, *tl; 800 int error; 801 802 if (cred) { 803 /* 804 * Dealing with USB IO specific behavior 805 */ 806 cl = aa_get_newest_cred_label(cred); 807 tl = aa_get_task_label(target); 808 error = aa_may_signal(cl, tl, sig); 809 aa_put_label(cl); 810 aa_put_label(tl); 811 return error; 812 } 813 814 cl = __begin_current_label_crit_section(); 815 tl = aa_get_task_label(target); 816 error = aa_may_signal(cl, tl, sig); 817 aa_put_label(tl); 818 __end_current_label_crit_section(cl); 819 820 return error; 821 } 822 823 /** 824 * apparmor_sk_alloc_security - allocate and attach the sk_security field 825 */ 826 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags) 827 { 828 struct aa_sk_ctx *ctx; 829 830 ctx = kzalloc(sizeof(*ctx), flags); 831 if (!ctx) 832 return -ENOMEM; 833 834 SK_CTX(sk) = ctx; 835 836 return 0; 837 } 838 839 /** 840 * apparmor_sk_free_security - free the sk_security field 841 */ 842 static void apparmor_sk_free_security(struct sock *sk) 843 { 844 struct aa_sk_ctx *ctx = SK_CTX(sk); 845 846 SK_CTX(sk) = NULL; 847 aa_put_label(ctx->label); 848 aa_put_label(ctx->peer); 849 kfree(ctx); 850 } 851 852 /** 853 * apparmor_sk_clone_security - clone the sk_security field 854 */ 855 static void apparmor_sk_clone_security(const struct sock *sk, 856 struct sock *newsk) 857 { 858 struct aa_sk_ctx *ctx = SK_CTX(sk); 859 struct aa_sk_ctx *new = SK_CTX(newsk); 860 861 if (new->label) 862 aa_put_label(new->label); 863 new->label = aa_get_label(ctx->label); 864 865 if (new->peer) 866 aa_put_label(new->peer); 867 new->peer = aa_get_label(ctx->peer); 868 } 869 870 /** 871 * apparmor_socket_create - check perms before creating a new socket 872 */ 873 static int apparmor_socket_create(int family, int type, int protocol, int kern) 874 { 875 struct aa_label *label; 876 int error = 0; 877 878 AA_BUG(in_interrupt()); 879 880 label = begin_current_label_crit_section(); 881 if (!(kern || unconfined(label))) 882 error = af_select(family, 883 create_perm(label, family, type, protocol), 884 aa_af_perm(label, OP_CREATE, AA_MAY_CREATE, 885 family, type, protocol)); 886 end_current_label_crit_section(label); 887 888 return error; 889 } 890 891 /** 892 * apparmor_socket_post_create - setup the per-socket security struct 893 * 894 * Note: 895 * - kernel sockets currently labeled unconfined but we may want to 896 * move to a special kernel label 897 * - socket may not have sk here if created with sock_create_lite or 898 * sock_alloc. These should be accept cases which will be handled in 899 * sock_graft. 900 */ 901 static int apparmor_socket_post_create(struct socket *sock, int family, 902 int type, int protocol, int kern) 903 { 904 struct aa_label *label; 905 906 if (kern) { 907 label = aa_get_label(kernel_t); 908 } else 909 label = aa_get_current_label(); 910 911 if (sock->sk) { 912 struct aa_sk_ctx *ctx = SK_CTX(sock->sk); 913 914 aa_put_label(ctx->label); 915 ctx->label = aa_get_label(label); 916 } 917 aa_put_label(label); 918 919 return 0; 920 } 921 922 /** 923 * apparmor_socket_bind - check perms before bind addr to socket 924 */ 925 static int apparmor_socket_bind(struct socket *sock, 926 struct sockaddr *address, int addrlen) 927 { 928 AA_BUG(!sock); 929 AA_BUG(!sock->sk); 930 AA_BUG(!address); 931 AA_BUG(in_interrupt()); 932 933 return af_select(sock->sk->sk_family, 934 bind_perm(sock, address, addrlen), 935 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk)); 936 } 937 938 /** 939 * apparmor_socket_connect - check perms before connecting @sock to @address 940 */ 941 static int apparmor_socket_connect(struct socket *sock, 942 struct sockaddr *address, int addrlen) 943 { 944 AA_BUG(!sock); 945 AA_BUG(!sock->sk); 946 AA_BUG(!address); 947 AA_BUG(in_interrupt()); 948 949 return af_select(sock->sk->sk_family, 950 connect_perm(sock, address, addrlen), 951 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk)); 952 } 953 954 /** 955 * apparmor_socket_listen - check perms before allowing listen 956 */ 957 static int apparmor_socket_listen(struct socket *sock, int backlog) 958 { 959 AA_BUG(!sock); 960 AA_BUG(!sock->sk); 961 AA_BUG(in_interrupt()); 962 963 return af_select(sock->sk->sk_family, 964 listen_perm(sock, backlog), 965 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk)); 966 } 967 968 /** 969 * apparmor_socket_accept - check perms before accepting a new connection. 970 * 971 * Note: while @newsock is created and has some information, the accept 972 * has not been done. 973 */ 974 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) 975 { 976 AA_BUG(!sock); 977 AA_BUG(!sock->sk); 978 AA_BUG(!newsock); 979 AA_BUG(in_interrupt()); 980 981 return af_select(sock->sk->sk_family, 982 accept_perm(sock, newsock), 983 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk)); 984 } 985 986 static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, 987 struct msghdr *msg, int size) 988 { 989 AA_BUG(!sock); 990 AA_BUG(!sock->sk); 991 AA_BUG(!msg); 992 AA_BUG(in_interrupt()); 993 994 return af_select(sock->sk->sk_family, 995 msg_perm(op, request, sock, msg, size), 996 aa_sk_perm(op, request, sock->sk)); 997 } 998 999 /** 1000 * apparmor_socket_sendmsg - check perms before sending msg to another socket 1001 */ 1002 static int apparmor_socket_sendmsg(struct socket *sock, 1003 struct msghdr *msg, int size) 1004 { 1005 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); 1006 } 1007 1008 /** 1009 * apparmor_socket_recvmsg - check perms before receiving a message 1010 */ 1011 static int apparmor_socket_recvmsg(struct socket *sock, 1012 struct msghdr *msg, int size, int flags) 1013 { 1014 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); 1015 } 1016 1017 /* revaliation, get/set attr, shutdown */ 1018 static int aa_sock_perm(const char *op, u32 request, struct socket *sock) 1019 { 1020 AA_BUG(!sock); 1021 AA_BUG(!sock->sk); 1022 AA_BUG(in_interrupt()); 1023 1024 return af_select(sock->sk->sk_family, 1025 sock_perm(op, request, sock), 1026 aa_sk_perm(op, request, sock->sk)); 1027 } 1028 1029 /** 1030 * apparmor_socket_getsockname - check perms before getting the local address 1031 */ 1032 static int apparmor_socket_getsockname(struct socket *sock) 1033 { 1034 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); 1035 } 1036 1037 /** 1038 * apparmor_socket_getpeername - check perms before getting remote address 1039 */ 1040 static int apparmor_socket_getpeername(struct socket *sock) 1041 { 1042 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); 1043 } 1044 1045 /* revaliation, get/set attr, opt */ 1046 static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, 1047 int level, int optname) 1048 { 1049 AA_BUG(!sock); 1050 AA_BUG(!sock->sk); 1051 AA_BUG(in_interrupt()); 1052 1053 return af_select(sock->sk->sk_family, 1054 opt_perm(op, request, sock, level, optname), 1055 aa_sk_perm(op, request, sock->sk)); 1056 } 1057 1058 /** 1059 * apparmor_socket_getsockopt - check perms before getting socket options 1060 */ 1061 static int apparmor_socket_getsockopt(struct socket *sock, int level, 1062 int optname) 1063 { 1064 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, 1065 level, optname); 1066 } 1067 1068 /** 1069 * apparmor_socket_setsockopt - check perms before setting socket options 1070 */ 1071 static int apparmor_socket_setsockopt(struct socket *sock, int level, 1072 int optname) 1073 { 1074 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, 1075 level, optname); 1076 } 1077 1078 /** 1079 * apparmor_socket_shutdown - check perms before shutting down @sock conn 1080 */ 1081 static int apparmor_socket_shutdown(struct socket *sock, int how) 1082 { 1083 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); 1084 } 1085 1086 #ifdef CONFIG_NETWORK_SECMARK 1087 /** 1088 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk 1089 * 1090 * Note: can not sleep may be called with locks held 1091 * 1092 * dont want protocol specific in __skb_recv_datagram() 1093 * to deny an incoming connection socket_sock_rcv_skb() 1094 */ 1095 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 1096 { 1097 struct aa_sk_ctx *ctx = SK_CTX(sk); 1098 1099 if (!skb->secmark) 1100 return 0; 1101 1102 return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, 1103 skb->secmark, sk); 1104 } 1105 #endif 1106 1107 1108 static struct aa_label *sk_peer_label(struct sock *sk) 1109 { 1110 struct aa_sk_ctx *ctx = SK_CTX(sk); 1111 1112 if (ctx->peer) 1113 return ctx->peer; 1114 1115 return ERR_PTR(-ENOPROTOOPT); 1116 } 1117 1118 /** 1119 * apparmor_socket_getpeersec_stream - get security context of peer 1120 * 1121 * Note: for tcp only valid if using ipsec or cipso on lan 1122 */ 1123 static int apparmor_socket_getpeersec_stream(struct socket *sock, 1124 sockptr_t optval, sockptr_t optlen, 1125 unsigned int len) 1126 { 1127 char *name = NULL; 1128 int slen, error = 0; 1129 struct aa_label *label; 1130 struct aa_label *peer; 1131 1132 label = begin_current_label_crit_section(); 1133 peer = sk_peer_label(sock->sk); 1134 if (IS_ERR(peer)) { 1135 error = PTR_ERR(peer); 1136 goto done; 1137 } 1138 slen = aa_label_asxprint(&name, labels_ns(label), peer, 1139 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | 1140 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); 1141 /* don't include terminating \0 in slen, it breaks some apps */ 1142 if (slen < 0) { 1143 error = -ENOMEM; 1144 goto done; 1145 } 1146 if (slen > len) { 1147 error = -ERANGE; 1148 goto done_len; 1149 } 1150 1151 if (copy_to_sockptr(optval, name, slen)) 1152 error = -EFAULT; 1153 done_len: 1154 if (copy_to_sockptr(optlen, &slen, sizeof(slen))) 1155 error = -EFAULT; 1156 done: 1157 end_current_label_crit_section(label); 1158 kfree(name); 1159 return error; 1160 } 1161 1162 /** 1163 * apparmor_socket_getpeersec_dgram - get security label of packet 1164 * @sock: the peer socket 1165 * @skb: packet data 1166 * @secid: pointer to where to put the secid of the packet 1167 * 1168 * Sets the netlabel socket state on sk from parent 1169 */ 1170 static int apparmor_socket_getpeersec_dgram(struct socket *sock, 1171 struct sk_buff *skb, u32 *secid) 1172 1173 { 1174 /* TODO: requires secid support */ 1175 return -ENOPROTOOPT; 1176 } 1177 1178 /** 1179 * apparmor_sock_graft - Initialize newly created socket 1180 * @sk: child sock 1181 * @parent: parent socket 1182 * 1183 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can 1184 * just set sk security information off of current creating process label 1185 * Labeling of sk for accept case - probably should be sock based 1186 * instead of task, because of the case where an implicitly labeled 1187 * socket is shared by different tasks. 1188 */ 1189 static void apparmor_sock_graft(struct sock *sk, struct socket *parent) 1190 { 1191 struct aa_sk_ctx *ctx = SK_CTX(sk); 1192 1193 if (!ctx->label) 1194 ctx->label = aa_get_current_label(); 1195 } 1196 1197 #ifdef CONFIG_NETWORK_SECMARK 1198 static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb, 1199 struct request_sock *req) 1200 { 1201 struct aa_sk_ctx *ctx = SK_CTX(sk); 1202 1203 if (!skb->secmark) 1204 return 0; 1205 1206 return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, 1207 skb->secmark, sk); 1208 } 1209 #endif 1210 1211 /* 1212 * The cred blob is a pointer to, not an instance of, an aa_label. 1213 */ 1214 struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = { 1215 .lbs_cred = sizeof(struct aa_label *), 1216 .lbs_file = sizeof(struct aa_file_ctx), 1217 .lbs_task = sizeof(struct aa_task_ctx), 1218 }; 1219 1220 static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { 1221 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), 1222 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), 1223 LSM_HOOK_INIT(capget, apparmor_capget), 1224 LSM_HOOK_INIT(capable, apparmor_capable), 1225 1226 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), 1227 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), 1228 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), 1229 1230 LSM_HOOK_INIT(path_link, apparmor_path_link), 1231 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), 1232 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), 1233 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), 1234 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), 1235 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), 1236 LSM_HOOK_INIT(path_rename, apparmor_path_rename), 1237 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), 1238 LSM_HOOK_INIT(path_chown, apparmor_path_chown), 1239 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), 1240 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), 1241 1242 LSM_HOOK_INIT(file_open, apparmor_file_open), 1243 LSM_HOOK_INIT(file_receive, apparmor_file_receive), 1244 LSM_HOOK_INIT(file_permission, apparmor_file_permission), 1245 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), 1246 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), 1247 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), 1248 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), 1249 LSM_HOOK_INIT(file_lock, apparmor_file_lock), 1250 LSM_HOOK_INIT(file_truncate, apparmor_file_truncate), 1251 1252 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), 1253 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), 1254 1255 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security), 1256 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), 1257 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), 1258 1259 LSM_HOOK_INIT(socket_create, apparmor_socket_create), 1260 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), 1261 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), 1262 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), 1263 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), 1264 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), 1265 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), 1266 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), 1267 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), 1268 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), 1269 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), 1270 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), 1271 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), 1272 #ifdef CONFIG_NETWORK_SECMARK 1273 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), 1274 #endif 1275 LSM_HOOK_INIT(socket_getpeersec_stream, 1276 apparmor_socket_getpeersec_stream), 1277 LSM_HOOK_INIT(socket_getpeersec_dgram, 1278 apparmor_socket_getpeersec_dgram), 1279 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), 1280 #ifdef CONFIG_NETWORK_SECMARK 1281 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), 1282 #endif 1283 1284 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), 1285 LSM_HOOK_INIT(cred_free, apparmor_cred_free), 1286 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), 1287 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), 1288 1289 LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec), 1290 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), 1291 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), 1292 1293 LSM_HOOK_INIT(task_free, apparmor_task_free), 1294 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc), 1295 LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj), 1296 LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj), 1297 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), 1298 LSM_HOOK_INIT(task_kill, apparmor_task_kill), 1299 1300 #ifdef CONFIG_AUDIT 1301 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init), 1302 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known), 1303 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match), 1304 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free), 1305 #endif 1306 1307 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx), 1308 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid), 1309 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx), 1310 }; 1311 1312 /* 1313 * AppArmor sysfs module parameters 1314 */ 1315 1316 static int param_set_aabool(const char *val, const struct kernel_param *kp); 1317 static int param_get_aabool(char *buffer, const struct kernel_param *kp); 1318 #define param_check_aabool param_check_bool 1319 static const struct kernel_param_ops param_ops_aabool = { 1320 .flags = KERNEL_PARAM_OPS_FL_NOARG, 1321 .set = param_set_aabool, 1322 .get = param_get_aabool 1323 }; 1324 1325 static int param_set_aauint(const char *val, const struct kernel_param *kp); 1326 static int param_get_aauint(char *buffer, const struct kernel_param *kp); 1327 #define param_check_aauint param_check_uint 1328 static const struct kernel_param_ops param_ops_aauint = { 1329 .set = param_set_aauint, 1330 .get = param_get_aauint 1331 }; 1332 1333 static int param_set_aacompressionlevel(const char *val, 1334 const struct kernel_param *kp); 1335 static int param_get_aacompressionlevel(char *buffer, 1336 const struct kernel_param *kp); 1337 #define param_check_aacompressionlevel param_check_int 1338 static const struct kernel_param_ops param_ops_aacompressionlevel = { 1339 .set = param_set_aacompressionlevel, 1340 .get = param_get_aacompressionlevel 1341 }; 1342 1343 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); 1344 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); 1345 #define param_check_aalockpolicy param_check_bool 1346 static const struct kernel_param_ops param_ops_aalockpolicy = { 1347 .flags = KERNEL_PARAM_OPS_FL_NOARG, 1348 .set = param_set_aalockpolicy, 1349 .get = param_get_aalockpolicy 1350 }; 1351 1352 static int param_set_audit(const char *val, const struct kernel_param *kp); 1353 static int param_get_audit(char *buffer, const struct kernel_param *kp); 1354 1355 static int param_set_mode(const char *val, const struct kernel_param *kp); 1356 static int param_get_mode(char *buffer, const struct kernel_param *kp); 1357 1358 /* Flag values, also controllable via /sys/module/apparmor/parameters 1359 * We define special types as we want to do additional mediation. 1360 */ 1361 1362 /* AppArmor global enforcement switch - complain, enforce, kill */ 1363 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; 1364 module_param_call(mode, param_set_mode, param_get_mode, 1365 &aa_g_profile_mode, S_IRUSR | S_IWUSR); 1366 1367 /* whether policy verification hashing is enabled */ 1368 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); 1369 #ifdef CONFIG_SECURITY_APPARMOR_HASH 1370 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); 1371 #endif 1372 1373 /* whether policy exactly as loaded is retained for debug and checkpointing */ 1374 bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY); 1375 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 1376 module_param_named(export_binary, aa_g_export_binary, aabool, 0600); 1377 #endif 1378 1379 /* policy loaddata compression level */ 1380 int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL; 1381 module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level, 1382 aacompressionlevel, 0400); 1383 1384 /* Debug mode */ 1385 bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); 1386 module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR); 1387 1388 /* Audit mode */ 1389 enum audit_mode aa_g_audit; 1390 module_param_call(audit, param_set_audit, param_get_audit, 1391 &aa_g_audit, S_IRUSR | S_IWUSR); 1392 1393 /* Determines if audit header is included in audited messages. This 1394 * provides more context if the audit daemon is not running 1395 */ 1396 bool aa_g_audit_header = true; 1397 module_param_named(audit_header, aa_g_audit_header, aabool, 1398 S_IRUSR | S_IWUSR); 1399 1400 /* lock out loading/removal of policy 1401 * TODO: add in at boot loading of policy, which is the only way to 1402 * load policy, if lock_policy is set 1403 */ 1404 bool aa_g_lock_policy; 1405 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, 1406 S_IRUSR | S_IWUSR); 1407 1408 /* Syscall logging mode */ 1409 bool aa_g_logsyscall; 1410 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); 1411 1412 /* Maximum pathname length before accesses will start getting rejected */ 1413 unsigned int aa_g_path_max = 2 * PATH_MAX; 1414 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); 1415 1416 /* Determines how paranoid loading of policy is and how much verification 1417 * on the loaded policy is done. 1418 * DEPRECATED: read only as strict checking of load is always done now 1419 * that none root users (user namespaces) can load policy. 1420 */ 1421 bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD); 1422 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); 1423 1424 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp); 1425 static int param_set_aaintbool(const char *val, const struct kernel_param *kp); 1426 #define param_check_aaintbool param_check_int 1427 static const struct kernel_param_ops param_ops_aaintbool = { 1428 .set = param_set_aaintbool, 1429 .get = param_get_aaintbool 1430 }; 1431 /* Boot time disable flag */ 1432 static int apparmor_enabled __lsm_ro_after_init = 1; 1433 module_param_named(enabled, apparmor_enabled, aaintbool, 0444); 1434 1435 static int __init apparmor_enabled_setup(char *str) 1436 { 1437 unsigned long enabled; 1438 int error = kstrtoul(str, 0, &enabled); 1439 if (!error) 1440 apparmor_enabled = enabled ? 1 : 0; 1441 return 1; 1442 } 1443 1444 __setup("apparmor=", apparmor_enabled_setup); 1445 1446 /* set global flag turning off the ability to load policy */ 1447 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) 1448 { 1449 if (!apparmor_enabled) 1450 return -EINVAL; 1451 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1452 return -EPERM; 1453 return param_set_bool(val, kp); 1454 } 1455 1456 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) 1457 { 1458 if (!apparmor_enabled) 1459 return -EINVAL; 1460 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1461 return -EPERM; 1462 return param_get_bool(buffer, kp); 1463 } 1464 1465 static int param_set_aabool(const char *val, const struct kernel_param *kp) 1466 { 1467 if (!apparmor_enabled) 1468 return -EINVAL; 1469 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1470 return -EPERM; 1471 return param_set_bool(val, kp); 1472 } 1473 1474 static int param_get_aabool(char *buffer, const struct kernel_param *kp) 1475 { 1476 if (!apparmor_enabled) 1477 return -EINVAL; 1478 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1479 return -EPERM; 1480 return param_get_bool(buffer, kp); 1481 } 1482 1483 static int param_set_aauint(const char *val, const struct kernel_param *kp) 1484 { 1485 int error; 1486 1487 if (!apparmor_enabled) 1488 return -EINVAL; 1489 /* file is ro but enforce 2nd line check */ 1490 if (apparmor_initialized) 1491 return -EPERM; 1492 1493 error = param_set_uint(val, kp); 1494 aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer)); 1495 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); 1496 1497 return error; 1498 } 1499 1500 static int param_get_aauint(char *buffer, const struct kernel_param *kp) 1501 { 1502 if (!apparmor_enabled) 1503 return -EINVAL; 1504 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1505 return -EPERM; 1506 return param_get_uint(buffer, kp); 1507 } 1508 1509 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */ 1510 static int param_set_aaintbool(const char *val, const struct kernel_param *kp) 1511 { 1512 struct kernel_param kp_local; 1513 bool value; 1514 int error; 1515 1516 if (apparmor_initialized) 1517 return -EPERM; 1518 1519 /* Create local copy, with arg pointing to bool type. */ 1520 value = !!*((int *)kp->arg); 1521 memcpy(&kp_local, kp, sizeof(kp_local)); 1522 kp_local.arg = &value; 1523 1524 error = param_set_bool(val, &kp_local); 1525 if (!error) 1526 *((int *)kp->arg) = *((bool *)kp_local.arg); 1527 return error; 1528 } 1529 1530 /* 1531 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to 1532 * 1/0, this converts the "int that is actually bool" back to bool for 1533 * display in the /sys filesystem, while keeping it "int" for the LSM 1534 * infrastructure. 1535 */ 1536 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp) 1537 { 1538 struct kernel_param kp_local; 1539 bool value; 1540 1541 /* Create local copy, with arg pointing to bool type. */ 1542 value = !!*((int *)kp->arg); 1543 memcpy(&kp_local, kp, sizeof(kp_local)); 1544 kp_local.arg = &value; 1545 1546 return param_get_bool(buffer, &kp_local); 1547 } 1548 1549 static int param_set_aacompressionlevel(const char *val, 1550 const struct kernel_param *kp) 1551 { 1552 int error; 1553 1554 if (!apparmor_enabled) 1555 return -EINVAL; 1556 if (apparmor_initialized) 1557 return -EPERM; 1558 1559 error = param_set_int(val, kp); 1560 1561 aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level, 1562 AA_MIN_CLEVEL, AA_MAX_CLEVEL); 1563 pr_info("AppArmor: policy rawdata compression level set to %d\n", 1564 aa_g_rawdata_compression_level); 1565 1566 return error; 1567 } 1568 1569 static int param_get_aacompressionlevel(char *buffer, 1570 const struct kernel_param *kp) 1571 { 1572 if (!apparmor_enabled) 1573 return -EINVAL; 1574 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1575 return -EPERM; 1576 return param_get_int(buffer, kp); 1577 } 1578 1579 static int param_get_audit(char *buffer, const struct kernel_param *kp) 1580 { 1581 if (!apparmor_enabled) 1582 return -EINVAL; 1583 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1584 return -EPERM; 1585 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]); 1586 } 1587 1588 static int param_set_audit(const char *val, const struct kernel_param *kp) 1589 { 1590 int i; 1591 1592 if (!apparmor_enabled) 1593 return -EINVAL; 1594 if (!val) 1595 return -EINVAL; 1596 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1597 return -EPERM; 1598 1599 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val); 1600 if (i < 0) 1601 return -EINVAL; 1602 1603 aa_g_audit = i; 1604 return 0; 1605 } 1606 1607 static int param_get_mode(char *buffer, const struct kernel_param *kp) 1608 { 1609 if (!apparmor_enabled) 1610 return -EINVAL; 1611 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1612 return -EPERM; 1613 1614 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]); 1615 } 1616 1617 static int param_set_mode(const char *val, const struct kernel_param *kp) 1618 { 1619 int i; 1620 1621 if (!apparmor_enabled) 1622 return -EINVAL; 1623 if (!val) 1624 return -EINVAL; 1625 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1626 return -EPERM; 1627 1628 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, 1629 val); 1630 if (i < 0) 1631 return -EINVAL; 1632 1633 aa_g_profile_mode = i; 1634 return 0; 1635 } 1636 1637 char *aa_get_buffer(bool in_atomic) 1638 { 1639 union aa_buffer *aa_buf; 1640 bool try_again = true; 1641 gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 1642 1643 retry: 1644 spin_lock(&aa_buffers_lock); 1645 if (buffer_count > reserve_count || 1646 (in_atomic && !list_empty(&aa_global_buffers))) { 1647 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 1648 list); 1649 list_del(&aa_buf->list); 1650 buffer_count--; 1651 spin_unlock(&aa_buffers_lock); 1652 return &aa_buf->buffer[0]; 1653 } 1654 if (in_atomic) { 1655 /* 1656 * out of reserve buffers and in atomic context so increase 1657 * how many buffers to keep in reserve 1658 */ 1659 reserve_count++; 1660 flags = GFP_ATOMIC; 1661 } 1662 spin_unlock(&aa_buffers_lock); 1663 1664 if (!in_atomic) 1665 might_sleep(); 1666 aa_buf = kmalloc(aa_g_path_max, flags); 1667 if (!aa_buf) { 1668 if (try_again) { 1669 try_again = false; 1670 goto retry; 1671 } 1672 pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n"); 1673 return NULL; 1674 } 1675 return &aa_buf->buffer[0]; 1676 } 1677 1678 void aa_put_buffer(char *buf) 1679 { 1680 union aa_buffer *aa_buf; 1681 1682 if (!buf) 1683 return; 1684 aa_buf = container_of(buf, union aa_buffer, buffer[0]); 1685 1686 spin_lock(&aa_buffers_lock); 1687 list_add(&aa_buf->list, &aa_global_buffers); 1688 buffer_count++; 1689 spin_unlock(&aa_buffers_lock); 1690 } 1691 1692 /* 1693 * AppArmor init functions 1694 */ 1695 1696 /** 1697 * set_init_ctx - set a task context and profile on the first task. 1698 * 1699 * TODO: allow setting an alternate profile than unconfined 1700 */ 1701 static int __init set_init_ctx(void) 1702 { 1703 struct cred *cred = (__force struct cred *)current->real_cred; 1704 1705 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); 1706 1707 return 0; 1708 } 1709 1710 static void destroy_buffers(void) 1711 { 1712 union aa_buffer *aa_buf; 1713 1714 spin_lock(&aa_buffers_lock); 1715 while (!list_empty(&aa_global_buffers)) { 1716 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 1717 list); 1718 list_del(&aa_buf->list); 1719 spin_unlock(&aa_buffers_lock); 1720 kfree(aa_buf); 1721 spin_lock(&aa_buffers_lock); 1722 } 1723 spin_unlock(&aa_buffers_lock); 1724 } 1725 1726 static int __init alloc_buffers(void) 1727 { 1728 union aa_buffer *aa_buf; 1729 int i, num; 1730 1731 /* 1732 * A function may require two buffers at once. Usually the buffers are 1733 * used for a short period of time and are shared. On UP kernel buffers 1734 * two should be enough, with more CPUs it is possible that more 1735 * buffers will be used simultaneously. The preallocated pool may grow. 1736 * This preallocation has also the side-effect that AppArmor will be 1737 * disabled early at boot if aa_g_path_max is extremly high. 1738 */ 1739 if (num_online_cpus() > 1) 1740 num = 4 + RESERVE_COUNT; 1741 else 1742 num = 2 + RESERVE_COUNT; 1743 1744 for (i = 0; i < num; i++) { 1745 1746 aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL | 1747 __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 1748 if (!aa_buf) { 1749 destroy_buffers(); 1750 return -ENOMEM; 1751 } 1752 aa_put_buffer(&aa_buf->buffer[0]); 1753 } 1754 return 0; 1755 } 1756 1757 #ifdef CONFIG_SYSCTL 1758 static int apparmor_dointvec(struct ctl_table *table, int write, 1759 void *buffer, size_t *lenp, loff_t *ppos) 1760 { 1761 if (!aa_current_policy_admin_capable(NULL)) 1762 return -EPERM; 1763 if (!apparmor_enabled) 1764 return -EINVAL; 1765 1766 return proc_dointvec(table, write, buffer, lenp, ppos); 1767 } 1768 1769 static struct ctl_path apparmor_sysctl_path[] = { 1770 { .procname = "kernel", }, 1771 { } 1772 }; 1773 1774 static struct ctl_table apparmor_sysctl_table[] = { 1775 { 1776 .procname = "unprivileged_userns_apparmor_policy", 1777 .data = &unprivileged_userns_apparmor_policy, 1778 .maxlen = sizeof(int), 1779 .mode = 0600, 1780 .proc_handler = apparmor_dointvec, 1781 }, 1782 { 1783 .procname = "apparmor_display_secid_mode", 1784 .data = &apparmor_display_secid_mode, 1785 .maxlen = sizeof(int), 1786 .mode = 0600, 1787 .proc_handler = apparmor_dointvec, 1788 }, 1789 1790 { } 1791 }; 1792 1793 static int __init apparmor_init_sysctl(void) 1794 { 1795 return register_sysctl_paths(apparmor_sysctl_path, 1796 apparmor_sysctl_table) ? 0 : -ENOMEM; 1797 } 1798 #else 1799 static inline int apparmor_init_sysctl(void) 1800 { 1801 return 0; 1802 } 1803 #endif /* CONFIG_SYSCTL */ 1804 1805 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) 1806 static unsigned int apparmor_ip_postroute(void *priv, 1807 struct sk_buff *skb, 1808 const struct nf_hook_state *state) 1809 { 1810 struct aa_sk_ctx *ctx; 1811 struct sock *sk; 1812 1813 if (!skb->secmark) 1814 return NF_ACCEPT; 1815 1816 sk = skb_to_full_sk(skb); 1817 if (sk == NULL) 1818 return NF_ACCEPT; 1819 1820 ctx = SK_CTX(sk); 1821 if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, 1822 skb->secmark, sk)) 1823 return NF_ACCEPT; 1824 1825 return NF_DROP_ERR(-ECONNREFUSED); 1826 1827 } 1828 1829 static const struct nf_hook_ops apparmor_nf_ops[] = { 1830 { 1831 .hook = apparmor_ip_postroute, 1832 .pf = NFPROTO_IPV4, 1833 .hooknum = NF_INET_POST_ROUTING, 1834 .priority = NF_IP_PRI_SELINUX_FIRST, 1835 }, 1836 #if IS_ENABLED(CONFIG_IPV6) 1837 { 1838 .hook = apparmor_ip_postroute, 1839 .pf = NFPROTO_IPV6, 1840 .hooknum = NF_INET_POST_ROUTING, 1841 .priority = NF_IP6_PRI_SELINUX_FIRST, 1842 }, 1843 #endif 1844 }; 1845 1846 static int __net_init apparmor_nf_register(struct net *net) 1847 { 1848 return nf_register_net_hooks(net, apparmor_nf_ops, 1849 ARRAY_SIZE(apparmor_nf_ops)); 1850 } 1851 1852 static void __net_exit apparmor_nf_unregister(struct net *net) 1853 { 1854 nf_unregister_net_hooks(net, apparmor_nf_ops, 1855 ARRAY_SIZE(apparmor_nf_ops)); 1856 } 1857 1858 static struct pernet_operations apparmor_net_ops = { 1859 .init = apparmor_nf_register, 1860 .exit = apparmor_nf_unregister, 1861 }; 1862 1863 static int __init apparmor_nf_ip_init(void) 1864 { 1865 int err; 1866 1867 if (!apparmor_enabled) 1868 return 0; 1869 1870 err = register_pernet_subsys(&apparmor_net_ops); 1871 if (err) 1872 panic("Apparmor: register_pernet_subsys: error %d\n", err); 1873 1874 return 0; 1875 } 1876 __initcall(apparmor_nf_ip_init); 1877 #endif 1878 1879 static int __init apparmor_init(void) 1880 { 1881 int error; 1882 1883 error = aa_setup_dfa_engine(); 1884 if (error) { 1885 AA_ERROR("Unable to setup dfa engine\n"); 1886 goto alloc_out; 1887 } 1888 1889 error = aa_alloc_root_ns(); 1890 if (error) { 1891 AA_ERROR("Unable to allocate default profile namespace\n"); 1892 goto alloc_out; 1893 } 1894 1895 error = apparmor_init_sysctl(); 1896 if (error) { 1897 AA_ERROR("Unable to register sysctls\n"); 1898 goto alloc_out; 1899 1900 } 1901 1902 error = alloc_buffers(); 1903 if (error) { 1904 AA_ERROR("Unable to allocate work buffers\n"); 1905 goto alloc_out; 1906 } 1907 1908 error = set_init_ctx(); 1909 if (error) { 1910 AA_ERROR("Failed to set context on init task\n"); 1911 aa_free_root_ns(); 1912 goto buffers_out; 1913 } 1914 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), 1915 "apparmor"); 1916 1917 /* Report that AppArmor successfully initialized */ 1918 apparmor_initialized = 1; 1919 if (aa_g_profile_mode == APPARMOR_COMPLAIN) 1920 aa_info_message("AppArmor initialized: complain mode enabled"); 1921 else if (aa_g_profile_mode == APPARMOR_KILL) 1922 aa_info_message("AppArmor initialized: kill mode enabled"); 1923 else 1924 aa_info_message("AppArmor initialized"); 1925 1926 return error; 1927 1928 buffers_out: 1929 destroy_buffers(); 1930 alloc_out: 1931 aa_destroy_aafs(); 1932 aa_teardown_dfa_engine(); 1933 1934 apparmor_enabled = false; 1935 return error; 1936 } 1937 1938 DEFINE_LSM(apparmor) = { 1939 .name = "apparmor", 1940 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 1941 .enabled = &apparmor_enabled, 1942 .blobs = &apparmor_blob_sizes, 1943 .init = apparmor_init, 1944 }; 1945