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/sctp.h> 19 #include <linux/ctype.h> 20 #include <linux/sysctl.h> 21 #include <linux/sysfs.h> 22 #include <linux/audit.h> 23 #include <linux/user_namespace.h> 24 #include <linux/netfilter_ipv4.h> 25 #include <linux/netfilter_ipv6.h> 26 #include <linux/zstd.h> 27 #include <net/sock.h> 28 #include <uapi/linux/mount.h> 29 #include <uapi/linux/lsm.h> 30 31 #include "include/af_unix.h" 32 #include "include/af_inet.h" 33 #include "include/apparmor.h" 34 #include "include/apparmorfs.h" 35 #include "include/audit.h" 36 #include "include/capability.h" 37 #include "include/cred.h" 38 #include "include/crypto.h" 39 #include "include/file.h" 40 #include "include/ipc.h" 41 #include "include/net.h" 42 #include "include/path.h" 43 #include "include/label.h" 44 #include "include/policy.h" 45 #include "include/policy_ns.h" 46 #include "include/procattr.h" 47 #include "include/mount.h" 48 #include "include/secid.h" 49 50 /* Flag indicating whether initialization completed */ 51 int apparmor_initialized; 52 53 union aa_buffer { 54 struct list_head list; 55 DECLARE_FLEX_ARRAY(char, buffer); 56 }; 57 58 struct aa_local_cache { 59 unsigned int hold; 60 unsigned int count; 61 struct list_head head; 62 }; 63 64 #define RESERVE_COUNT 2 65 static int reserve_count = RESERVE_COUNT; 66 static int buffer_count; 67 68 static LIST_HEAD(aa_global_buffers); 69 static DEFINE_SPINLOCK(aa_buffers_lock); 70 static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers); 71 72 /* 73 * LSM hook functions 74 */ 75 76 /* 77 * put the associated labels 78 */ 79 static void apparmor_cred_free(struct cred *cred) 80 { 81 aa_put_label(cred_label(cred)); 82 set_cred_label(cred, NULL); 83 } 84 85 /* 86 * allocate the apparmor part of blank credentials 87 */ 88 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) 89 { 90 set_cred_label(cred, NULL); 91 return 0; 92 } 93 94 /* 95 * prepare new cred label for modification by prepare_cred block 96 */ 97 static int apparmor_cred_prepare(struct cred *new, const struct cred *old, 98 gfp_t gfp) 99 { 100 set_cred_label(new, aa_get_newest_label(cred_label(old))); 101 return 0; 102 } 103 104 /* 105 * transfer the apparmor data to a blank set of creds 106 */ 107 static void apparmor_cred_transfer(struct cred *new, const struct cred *old) 108 { 109 set_cred_label(new, aa_get_newest_label(cred_label(old))); 110 } 111 112 static void apparmor_task_free(struct task_struct *task) 113 { 114 115 aa_free_task_ctx(task_ctx(task)); 116 } 117 118 static int apparmor_task_alloc(struct task_struct *task, 119 u64 clone_flags) 120 { 121 struct aa_task_ctx *new = task_ctx(task); 122 123 aa_dup_task_ctx(new, task_ctx(current)); 124 125 return 0; 126 } 127 128 static int apparmor_ptrace_access_check(struct task_struct *child, 129 unsigned int mode) 130 { 131 struct aa_label *tracer, *tracee; 132 const struct cred *cred; 133 int error; 134 bool needput; 135 136 cred = get_task_cred(child); 137 tracee = cred_label(cred); /* ref count on cred */ 138 tracer = __begin_current_label_crit_section(&needput); 139 error = aa_may_ptrace(current_cred(), tracer, cred, tracee, 140 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ 141 : AA_PTRACE_TRACE); 142 __end_current_label_crit_section(tracer, needput); 143 put_cred(cred); 144 145 return error; 146 } 147 148 static int apparmor_ptrace_traceme(struct task_struct *parent) 149 { 150 struct aa_label *tracer, *tracee; 151 const struct cred *cred; 152 int error; 153 bool needput; 154 155 tracee = __begin_current_label_crit_section(&needput); 156 cred = get_task_cred(parent); 157 tracer = cred_label(cred); /* ref count on cred */ 158 error = aa_may_ptrace(cred, tracer, current_cred(), tracee, 159 AA_PTRACE_TRACE); 160 put_cred(cred); 161 __end_current_label_crit_section(tracee, needput); 162 163 return error; 164 } 165 166 /* Derived from security/commoncap.c:cap_capget */ 167 static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective, 168 kernel_cap_t *inheritable, kernel_cap_t *permitted) 169 { 170 struct aa_label *label; 171 const struct cred *cred; 172 173 rcu_read_lock(); 174 cred = __task_cred(target); 175 label = aa_get_newest_cred_label(cred); 176 177 /* 178 * cap_capget is stacked ahead of this and will 179 * initialize effective and permitted. 180 */ 181 if (!unconfined(label)) { 182 struct aa_profile *profile; 183 struct label_it i; 184 185 label_for_each_confined(i, label, profile) { 186 kernel_cap_t allowed; 187 188 allowed = aa_profile_capget(profile); 189 *effective = cap_intersect(*effective, allowed); 190 *permitted = cap_intersect(*permitted, allowed); 191 } 192 } 193 rcu_read_unlock(); 194 aa_put_label(label); 195 196 return 0; 197 } 198 199 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, 200 int cap, unsigned int opts) 201 { 202 struct aa_label *label; 203 int error = 0; 204 205 label = aa_get_newest_cred_label(cred); 206 if (!unconfined(label)) 207 error = aa_capable(cred, label, cap, opts); 208 aa_put_label(label); 209 210 return error; 211 } 212 213 /** 214 * common_perm - basic common permission check wrapper fn for paths 215 * @op: operation being checked 216 * @path: path to check permission of (NOT NULL) 217 * @mask: requested permissions mask 218 * @cond: conditional info for the permission request (NOT NULL) 219 * 220 * Returns: %0 else error code if error or permission denied 221 */ 222 static int common_perm(const char *op, const struct path *path, u32 mask, 223 struct path_cond *cond) 224 { 225 struct aa_label *label; 226 int error = 0; 227 bool needput; 228 229 label = __begin_current_label_crit_section(&needput); 230 if (!unconfined(label)) 231 error = aa_path_perm(op, current_cred(), label, path, 0, mask, 232 cond); 233 __end_current_label_crit_section(label, needput); 234 235 return error; 236 } 237 238 /** 239 * common_perm_cond - common permission wrapper around inode cond 240 * @op: operation being checked 241 * @path: location to check (NOT NULL) 242 * @mask: requested permissions mask 243 * 244 * Returns: %0 else error code if error or permission denied 245 */ 246 static int common_perm_cond(const char *op, const struct path *path, u32 mask) 247 { 248 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt), 249 d_backing_inode(path->dentry)); 250 struct path_cond cond = { 251 vfsuid_into_kuid(vfsuid), 252 d_backing_inode(path->dentry)->i_mode 253 }; 254 255 if (!path_mediated_fs(path->dentry)) 256 return 0; 257 258 return common_perm(op, path, mask, &cond); 259 } 260 261 /** 262 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry 263 * @op: operation being checked 264 * @dir: directory of the dentry (NOT NULL) 265 * @dentry: dentry to check (NOT NULL) 266 * @mask: requested permissions mask 267 * @cond: conditional info for the permission request (NOT NULL) 268 * 269 * Returns: %0 else error code if error or permission denied 270 */ 271 static int common_perm_dir_dentry(const char *op, const struct path *dir, 272 struct dentry *dentry, u32 mask, 273 struct path_cond *cond) 274 { 275 struct path path = { .mnt = dir->mnt, .dentry = dentry }; 276 277 return common_perm(op, &path, mask, cond); 278 } 279 280 /** 281 * common_perm_rm - common permission wrapper for operations doing rm 282 * @op: operation being checked 283 * @dir: directory that the dentry is in (NOT NULL) 284 * @dentry: dentry being rm'd (NOT NULL) 285 * @mask: requested permission mask 286 * 287 * Returns: %0 else error code if error or permission denied 288 */ 289 static int common_perm_rm(const char *op, const struct path *dir, 290 struct dentry *dentry, u32 mask) 291 { 292 struct inode *inode = d_backing_inode(dentry); 293 struct path_cond cond = { }; 294 vfsuid_t vfsuid; 295 296 if (!inode || !path_mediated_fs(dentry)) 297 return 0; 298 299 vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode); 300 cond.uid = vfsuid_into_kuid(vfsuid); 301 cond.mode = inode->i_mode; 302 303 return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 304 } 305 306 /** 307 * common_perm_create - common permission wrapper for operations doing create 308 * @op: operation being checked 309 * @dir: directory that dentry will be created in (NOT NULL) 310 * @dentry: dentry to create (NOT NULL) 311 * @mask: request permission mask 312 * @mode: created file mode 313 * 314 * Returns: %0 else error code if error or permission denied 315 */ 316 static int common_perm_create(const char *op, const struct path *dir, 317 struct dentry *dentry, u32 mask, umode_t mode) 318 { 319 struct path_cond cond = { current_fsuid(), mode }; 320 321 if (!path_mediated_fs(dir->dentry)) 322 return 0; 323 324 return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 325 } 326 327 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) 328 { 329 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); 330 } 331 332 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, 333 umode_t mode) 334 { 335 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, 336 S_IFDIR); 337 } 338 339 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) 340 { 341 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); 342 } 343 344 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, 345 umode_t mode, unsigned int dev) 346 { 347 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); 348 } 349 350 static int apparmor_path_truncate(const struct path *path) 351 { 352 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); 353 } 354 355 static int apparmor_file_truncate(struct file *file) 356 { 357 return apparmor_path_truncate(&file->f_path); 358 } 359 360 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, 361 const char *old_name) 362 { 363 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, 364 S_IFLNK); 365 } 366 367 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, 368 struct dentry *new_dentry) 369 { 370 struct aa_label *label; 371 int error = 0; 372 bool needput; 373 374 if (!path_mediated_fs(old_dentry)) 375 return 0; 376 377 label = begin_current_label_crit_section(&needput); 378 if (!unconfined(label)) 379 error = aa_path_link(current_cred(), label, old_dentry, new_dir, 380 new_dentry); 381 end_current_label_crit_section(label, needput); 382 383 return error; 384 } 385 386 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, 387 const struct path *new_dir, struct dentry *new_dentry, 388 const unsigned int flags) 389 { 390 struct aa_label *label; 391 int error = 0; 392 bool needput; 393 394 if (!path_mediated_fs(old_dentry)) 395 return 0; 396 if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry)) 397 return 0; 398 399 label = begin_current_label_crit_section(&needput); 400 if (!unconfined(label)) { 401 const struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt); 402 vfsuid_t vfsuid; 403 struct path old_path = { .mnt = old_dir->mnt, 404 .dentry = old_dentry }; 405 struct path new_path = { .mnt = new_dir->mnt, 406 .dentry = new_dentry }; 407 struct path_cond cond = { 408 .mode = d_backing_inode(old_dentry)->i_mode 409 }; 410 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry)); 411 cond.uid = vfsuid_into_kuid(vfsuid); 412 413 if (flags & RENAME_EXCHANGE) { 414 struct path_cond cond_exchange = { 415 .mode = d_backing_inode(new_dentry)->i_mode, 416 }; 417 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(new_dentry)); 418 cond_exchange.uid = vfsuid_into_kuid(vfsuid); 419 420 error = aa_path_perm(OP_RENAME_SRC, current_cred(), 421 label, &new_path, 0, 422 MAY_READ | AA_MAY_GETATTR | MAY_WRITE | 423 AA_MAY_SETATTR | AA_MAY_DELETE, 424 &cond_exchange); 425 if (!error) 426 error = aa_path_perm(OP_RENAME_DEST, current_cred(), 427 label, &old_path, 428 0, MAY_WRITE | AA_MAY_SETATTR | 429 AA_MAY_CREATE, &cond_exchange); 430 } 431 432 if (!error) 433 error = aa_path_perm(OP_RENAME_SRC, current_cred(), 434 label, &old_path, 0, 435 MAY_READ | AA_MAY_GETATTR | MAY_WRITE | 436 AA_MAY_SETATTR | AA_MAY_DELETE, 437 &cond); 438 if (!error) 439 error = aa_path_perm(OP_RENAME_DEST, current_cred(), 440 label, &new_path, 441 0, MAY_WRITE | AA_MAY_SETATTR | 442 AA_MAY_CREATE, &cond); 443 444 } 445 end_current_label_crit_section(label, needput); 446 447 return error; 448 } 449 450 static int apparmor_path_chmod(const struct path *path, umode_t mode) 451 { 452 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); 453 } 454 455 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) 456 { 457 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); 458 } 459 460 static int apparmor_inode_getattr(const struct path *path) 461 { 462 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); 463 } 464 465 static int apparmor_file_open(struct file *file) 466 { 467 struct aa_file_ctx *fctx = file_ctx(file); 468 struct aa_label *label; 469 int error = 0; 470 bool needput; 471 472 if (!path_mediated_fs(file->f_path.dentry)) 473 return 0; 474 475 /* If in exec, permission is handled by bprm hooks. 476 * Cache permissions granted by the previous exec check, with 477 * implicit read and executable mmap which are required to 478 * actually execute the image. 479 * 480 * Illogically, FMODE_EXEC is in f_flags, not f_mode. 481 */ 482 if (file->f_flags & __FMODE_EXEC) { 483 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; 484 return 0; 485 } 486 487 label = aa_get_newest_cred_label_condref(file->f_cred, &needput); 488 if (!unconfined(label)) { 489 const struct mnt_idmap *idmap = file_mnt_idmap(file); 490 struct inode *inode = file_inode(file); 491 vfsuid_t vfsuid; 492 struct path_cond cond = { 493 .mode = inode->i_mode, 494 }; 495 vfsuid = i_uid_into_vfsuid(idmap, inode); 496 cond.uid = vfsuid_into_kuid(vfsuid); 497 498 error = aa_path_perm(OP_OPEN, file->f_cred, 499 label, &file->f_path, 0, 500 aa_map_file_to_perms(file), &cond); 501 /* todo cache full allowed permissions set and state */ 502 fctx->allow = aa_map_file_to_perms(file); 503 } 504 aa_put_label_condref(label, needput); 505 506 return error; 507 } 508 509 static int apparmor_file_alloc_security(struct file *file) 510 { 511 struct aa_file_ctx *ctx = file_ctx(file); 512 bool needput; 513 struct aa_label *label = begin_current_label_crit_section(&needput); 514 515 spin_lock_init(&ctx->lock); 516 rcu_assign_pointer(ctx->label, aa_get_label(label)); 517 end_current_label_crit_section(label, needput); 518 return 0; 519 } 520 521 static void apparmor_file_free_security(struct file *file) 522 { 523 struct aa_file_ctx *ctx = file_ctx(file); 524 525 if (ctx) 526 aa_put_label(rcu_access_pointer(ctx->label)); 527 } 528 529 static int common_file_perm(const char *op, struct file *file, u32 mask) 530 { 531 struct aa_label *label; 532 bool needput; 533 int error = 0; 534 535 label = begin_current_label_crit_section(&needput); 536 error = aa_file_perm(op, current_cred(), label, file, mask, false); 537 end_current_label_crit_section(label, needput); 538 539 return error; 540 } 541 542 static int apparmor_file_receive(struct file *file) 543 { 544 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file)); 545 } 546 547 static int apparmor_file_permission(struct file *file, int mask) 548 { 549 return common_file_perm(OP_FPERM, file, mask); 550 } 551 552 static int apparmor_file_lock(struct file *file, unsigned int cmd) 553 { 554 u32 mask = AA_MAY_LOCK; 555 556 if (cmd == F_WRLCK) 557 mask |= MAY_WRITE; 558 559 return common_file_perm(OP_FLOCK, file, mask); 560 } 561 562 static int common_mmap(const char *op, struct file *file, unsigned long prot, 563 unsigned long flags) 564 { 565 int mask = 0; 566 567 if (!file || !file_ctx(file)) 568 return 0; 569 570 if (prot & PROT_READ) 571 mask |= MAY_READ; 572 /* 573 * Private mappings don't require write perms since they don't 574 * write back to the files 575 */ 576 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) 577 mask |= MAY_WRITE; 578 if (prot & PROT_EXEC) 579 mask |= AA_EXEC_MMAP; 580 581 return common_file_perm(op, file, mask); 582 } 583 584 static int apparmor_mmap_file(struct file *file, unsigned long reqprot, 585 unsigned long prot, unsigned long flags) 586 { 587 return common_mmap(OP_FMMAP, file, prot, flags); 588 } 589 590 static int apparmor_file_mprotect(struct vm_area_struct *vma, 591 unsigned long reqprot, unsigned long prot) 592 { 593 return common_mmap(OP_FMPROT, vma->vm_file, prot, 594 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0); 595 } 596 597 #ifdef CONFIG_IO_URING 598 static const char *audit_uring_mask(u32 mask) 599 { 600 if (mask & AA_MAY_CREATE_SQPOLL) 601 return "sqpoll"; 602 if (mask & AA_MAY_OVERRIDE_CRED) 603 return "override_creds"; 604 return ""; 605 } 606 607 static void audit_uring_cb(struct audit_buffer *ab, void *va) 608 { 609 struct apparmor_audit_data *ad = aad_of_va(va); 610 611 if (ad->request & AA_URING_PERM_MASK) { 612 audit_log_format(ab, " requested=\"%s\"", 613 audit_uring_mask(ad->request)); 614 if (ad->denied & AA_URING_PERM_MASK) { 615 audit_log_format(ab, " denied=\"%s\"", 616 audit_uring_mask(ad->denied)); 617 } 618 } 619 if (ad->uring.target) { 620 audit_log_format(ab, " tcontext="); 621 aa_label_xaudit(ab, labels_ns(ad->subj_label), 622 ad->uring.target, 623 FLAGS_NONE, GFP_ATOMIC); 624 } 625 } 626 627 static int profile_uring(struct aa_profile *profile, u32 request, 628 struct aa_label *new, int cap, 629 struct apparmor_audit_data *ad) 630 { 631 unsigned int state; 632 struct aa_ruleset *rules; 633 int error = 0; 634 635 AA_BUG(!profile); 636 637 rules = profile->label.rules[0]; 638 state = RULE_MEDIATES(rules, AA_CLASS_IO_URING); 639 if (state) { 640 struct aa_perms perms = { }; 641 642 if (new) { 643 aa_label_match(profile, rules, new, state, 644 false, request, &perms); 645 } else { 646 perms = *aa_lookup_perms(rules->policy, state); 647 } 648 aa_apply_modes_to_perms(profile, &perms); 649 error = aa_check_perms(profile, &perms, request, ad, 650 audit_uring_cb); 651 } 652 653 return error; 654 } 655 656 /** 657 * apparmor_uring_override_creds - check the requested cred override 658 * @new: the target creds 659 * 660 * Check to see if the current task is allowed to override it's credentials 661 * to service an io_uring operation. 662 */ 663 static int apparmor_uring_override_creds(const struct cred *new) 664 { 665 struct aa_profile *profile; 666 struct aa_label *label; 667 int error; 668 bool needput; 669 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING, 670 OP_URING_OVERRIDE); 671 672 ad.uring.target = cred_label(new); 673 label = __begin_current_label_crit_section(&needput); 674 error = fn_for_each(label, profile, 675 profile_uring(profile, AA_MAY_OVERRIDE_CRED, 676 cred_label(new), CAP_SYS_ADMIN, &ad)); 677 __end_current_label_crit_section(label, needput); 678 679 return error; 680 } 681 682 /** 683 * apparmor_uring_sqpoll - check if a io_uring polling thread can be created 684 * 685 * Check to see if the current task is allowed to create a new io_uring 686 * kernel polling thread. 687 */ 688 static int apparmor_uring_sqpoll(void) 689 { 690 struct aa_profile *profile; 691 struct aa_label *label; 692 int error; 693 bool needput; 694 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING, 695 OP_URING_SQPOLL); 696 697 label = __begin_current_label_crit_section(&needput); 698 error = fn_for_each(label, profile, 699 profile_uring(profile, AA_MAY_CREATE_SQPOLL, 700 NULL, CAP_SYS_ADMIN, &ad)); 701 __end_current_label_crit_section(label, needput); 702 703 return error; 704 } 705 #endif /* CONFIG_IO_URING */ 706 707 static int apparmor_sb_mount(const char *dev_name, const struct path *path, 708 const char *type, unsigned long flags, void *data) 709 { 710 struct aa_label *label; 711 int error = 0; 712 bool needput; 713 714 /* Discard magic */ 715 if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 716 flags &= ~MS_MGC_MSK; 717 718 flags &= ~AA_MS_IGNORE_MASK; 719 720 label = __begin_current_label_crit_section(&needput); 721 if (!unconfined(label)) { 722 if (flags & MS_REMOUNT) 723 error = aa_remount(current_cred(), label, path, flags, 724 data); 725 else if (flags & MS_BIND) 726 error = aa_bind_mount(current_cred(), label, path, 727 dev_name, flags); 728 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | 729 MS_UNBINDABLE)) 730 error = aa_mount_change_type(current_cred(), label, 731 path, flags); 732 else if (flags & MS_MOVE) 733 error = aa_move_mount_old(current_cred(), label, path, 734 dev_name); 735 else 736 error = aa_new_mount(current_cred(), label, dev_name, 737 path, type, flags, data); 738 } 739 __end_current_label_crit_section(label, needput); 740 741 return error; 742 } 743 744 static int apparmor_move_mount(const struct path *from_path, 745 const struct path *to_path) 746 { 747 struct aa_label *label; 748 int error = 0; 749 bool needput; 750 751 label = __begin_current_label_crit_section(&needput); 752 if (!unconfined(label)) 753 error = aa_move_mount(current_cred(), label, from_path, 754 to_path); 755 __end_current_label_crit_section(label, needput); 756 757 return error; 758 } 759 760 static int apparmor_sb_umount(struct vfsmount *mnt, int flags) 761 { 762 struct aa_label *label; 763 int error = 0; 764 bool needput; 765 766 label = __begin_current_label_crit_section(&needput); 767 if (!unconfined(label)) 768 error = aa_umount(current_cred(), label, mnt, flags); 769 __end_current_label_crit_section(label, needput); 770 771 return error; 772 } 773 774 static int apparmor_sb_pivotroot(const struct path *old_path, 775 const struct path *new_path) 776 { 777 struct aa_label *label; 778 int error = 0; 779 780 label = aa_get_current_label(); 781 if (!unconfined(label)) 782 error = aa_pivotroot(current_cred(), label, old_path, new_path); 783 aa_put_label(label); 784 785 return error; 786 } 787 788 static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx, 789 u32 *size, u32 flags) 790 { 791 int error = -ENOENT; 792 struct aa_task_ctx *ctx = task_ctx(current); 793 struct aa_label *label = NULL; 794 char *value = NULL; 795 796 switch (attr) { 797 case LSM_ATTR_CURRENT: 798 label = aa_get_newest_label(cred_label(current_cred())); 799 break; 800 case LSM_ATTR_PREV: 801 if (ctx->previous) 802 label = aa_get_newest_label(ctx->previous); 803 break; 804 case LSM_ATTR_EXEC: 805 if (ctx->onexec) 806 label = aa_get_newest_label(ctx->onexec); 807 break; 808 default: 809 error = -EOPNOTSUPP; 810 break; 811 } 812 813 if (label) { 814 error = aa_getprocattr(label, &value, false); 815 if (error > 0) 816 error = lsm_fill_user_ctx(lx, size, value, error, 817 LSM_ID_APPARMOR, 0); 818 kfree(value); 819 } 820 821 aa_put_label(label); 822 823 if (error < 0) 824 return error; 825 return 1; 826 } 827 828 static int apparmor_getprocattr(struct task_struct *task, const char *name, 829 char **value) 830 { 831 int error = -ENOENT; 832 struct aa_label *label = NULL; 833 834 rcu_read_lock(); 835 if (strcmp(name, "current") == 0) 836 label = aa_get_newest_cred_label(__task_cred(task)); 837 else if (strcmp(name, "prev") == 0 && task_ctx(task)->previous) 838 label = aa_get_newest_label(task_ctx(task)->previous); 839 else if (strcmp(name, "exec") == 0 && task_ctx(task)->onexec) 840 label = aa_get_newest_label(task_ctx(task)->onexec); 841 else 842 error = -EINVAL; 843 rcu_read_unlock(); 844 845 if (label) 846 error = aa_getprocattr(label, value, true); 847 848 aa_put_label(label); 849 850 return error; 851 } 852 853 static int do_setattr(u64 attr, void *value, size_t size) 854 { 855 char *command, *largs = NULL, *args = value; 856 size_t arg_size; 857 int error; 858 bool needput; 859 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, 860 OP_SETPROCATTR); 861 862 if (size == 0) 863 return -EINVAL; 864 865 /* AppArmor requires that the buffer must be null terminated atm */ 866 if (args[size - 1] != '\0') { 867 largs = args = kmemdup_nul(value, size, GFP_KERNEL); 868 if (!args) 869 return -ENOMEM; 870 } 871 872 error = -EINVAL; 873 args = strim(args); 874 command = strsep(&args, " "); 875 if (!args) 876 goto out; 877 args = skip_spaces(args); 878 if (!*args) 879 goto out; 880 881 arg_size = size - (args - (largs ? largs : (char *) value)); 882 if (attr == LSM_ATTR_CURRENT) { 883 if (strcmp(command, "changehat") == 0) { 884 error = aa_setprocattr_changehat(args, arg_size, 885 AA_CHANGE_NOFLAGS); 886 } else if (strcmp(command, "permhat") == 0) { 887 error = aa_setprocattr_changehat(args, arg_size, 888 AA_CHANGE_TEST); 889 } else if (strcmp(command, "changeprofile") == 0) { 890 error = aa_change_profile(args, AA_CHANGE_NOFLAGS); 891 } else if (strcmp(command, "permprofile") == 0) { 892 error = aa_change_profile(args, AA_CHANGE_TEST); 893 } else if (strcmp(command, "stack") == 0) { 894 error = aa_change_profile(args, AA_CHANGE_STACK); 895 } else 896 goto fail; 897 } else if (attr == LSM_ATTR_EXEC) { 898 if (strcmp(command, "exec") == 0) 899 error = aa_change_profile(args, AA_CHANGE_ONEXEC); 900 else if (strcmp(command, "stack") == 0) 901 error = aa_change_profile(args, (AA_CHANGE_ONEXEC | 902 AA_CHANGE_STACK)); 903 else 904 goto fail; 905 } else 906 /* only support the "current" and "exec" process attributes */ 907 goto fail; 908 909 if (!error) 910 error = size; 911 out: 912 kfree(largs); 913 return error; 914 915 fail: 916 ad.subj_label = begin_current_label_crit_section(&needput); 917 if (attr == LSM_ATTR_CURRENT) 918 ad.info = "current"; 919 else if (attr == LSM_ATTR_EXEC) 920 ad.info = "exec"; 921 else 922 ad.info = "invalid"; 923 ad.error = error = -EINVAL; 924 aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL); 925 end_current_label_crit_section(ad.subj_label, needput); 926 goto out; 927 } 928 929 static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx, 930 u32 size, u32 flags) 931 { 932 int rc; 933 934 if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC) 935 return -EOPNOTSUPP; 936 937 rc = do_setattr(attr, ctx->ctx, ctx->ctx_len); 938 if (rc > 0) 939 return 0; 940 return rc; 941 } 942 943 static int apparmor_setprocattr(const char *name, void *value, 944 size_t size) 945 { 946 int attr = lsm_name_to_attr(name); 947 948 if (attr) 949 return do_setattr(attr, value, size); 950 return -EINVAL; 951 } 952 953 /** 954 * apparmor_bprm_committing_creds - do task cleanup on committing new creds 955 * @bprm: binprm for the exec (NOT NULL) 956 */ 957 static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm) 958 { 959 struct aa_label *label = aa_current_raw_label(); 960 struct aa_label *new_label = cred_label(bprm->cred); 961 962 /* bail out if unconfined or not changing profile */ 963 if ((new_label->proxy == label->proxy) || 964 (unconfined(new_label))) 965 return; 966 967 aa_inherit_files(bprm->cred, current->files); 968 969 current->pdeath_signal = 0; 970 971 /* reset soft limits and set hard limits for the new label */ 972 __aa_transition_rlimits(label, new_label); 973 } 974 975 /** 976 * apparmor_bprm_committed_creds() - do cleanup after new creds committed 977 * @bprm: binprm for the exec (NOT NULL) 978 */ 979 static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm) 980 { 981 /* clear out temporary/transitional state from the context */ 982 aa_clear_task_ctx_trans(task_ctx(current)); 983 984 return; 985 } 986 987 static void apparmor_current_getlsmprop_subj(struct lsm_prop *prop) 988 { 989 struct aa_label *label; 990 bool needput; 991 992 label = __begin_current_label_crit_section(&needput); 993 prop->apparmor.label = label; 994 __end_current_label_crit_section(label, needput); 995 } 996 997 static void apparmor_task_getlsmprop_obj(struct task_struct *p, 998 struct lsm_prop *prop) 999 { 1000 struct aa_label *label = aa_get_task_label(p); 1001 1002 prop->apparmor.label = label; 1003 aa_put_label(label); 1004 } 1005 1006 static int apparmor_task_setrlimit(struct task_struct *task, 1007 unsigned int resource, struct rlimit *new_rlim) 1008 { 1009 struct aa_label *label; 1010 int error = 0; 1011 bool needput; 1012 1013 label = __begin_current_label_crit_section(&needput); 1014 1015 if (!unconfined(label)) 1016 error = aa_task_setrlimit(current_cred(), label, task, 1017 resource, new_rlim); 1018 __end_current_label_crit_section(label, needput); 1019 1020 return error; 1021 } 1022 1023 static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, 1024 int sig, const struct cred *cred) 1025 { 1026 const struct cred *tc; 1027 struct aa_label *cl, *tl; 1028 int error; 1029 bool needput; 1030 1031 tc = get_task_cred(target); 1032 tl = aa_get_newest_cred_label(tc); 1033 if (cred) { 1034 /* 1035 * Dealing with USB IO specific behavior 1036 */ 1037 cl = aa_get_newest_cred_label(cred); 1038 error = aa_may_signal(cred, cl, tc, tl, sig); 1039 aa_put_label(cl); 1040 } else { 1041 cl = __begin_current_label_crit_section(&needput); 1042 error = aa_may_signal(current_cred(), cl, tc, tl, sig); 1043 __end_current_label_crit_section(cl, needput); 1044 } 1045 aa_put_label(tl); 1046 put_cred(tc); 1047 1048 return error; 1049 } 1050 1051 static int apparmor_userns_create(const struct cred *cred) 1052 { 1053 struct aa_label *label; 1054 struct aa_profile *profile; 1055 int error = 0; 1056 bool needput; 1057 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS, 1058 OP_USERNS_CREATE); 1059 1060 ad.subj_cred = current_cred(); 1061 1062 label = begin_current_label_crit_section(&needput); 1063 if (!unconfined(label)) { 1064 error = fn_for_each(label, profile, 1065 aa_profile_ns_perm(profile, &ad, 1066 AA_USERNS_CREATE)); 1067 } 1068 end_current_label_crit_section(label, needput); 1069 1070 return error; 1071 } 1072 1073 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t gfp) 1074 { 1075 struct aa_sk_ctx *ctx = aa_sock(sk); 1076 struct aa_label *label; 1077 bool needput; 1078 1079 label = __begin_current_label_crit_section(&needput); 1080 //spin_lock_init(&ctx->lock); 1081 rcu_assign_pointer(ctx->label, aa_get_label(label)); 1082 rcu_assign_pointer(ctx->peer, NULL); 1083 rcu_assign_pointer(ctx->peer_lastupdate, NULL); 1084 __end_current_label_crit_section(label, needput); 1085 return 0; 1086 } 1087 1088 static void apparmor_sk_free_security(struct sock *sk) 1089 { 1090 struct aa_sk_ctx *ctx = aa_sock(sk); 1091 1092 /* dead these won't be updated any more */ 1093 aa_put_label(rcu_dereference_protected(ctx->label, true)); 1094 aa_put_label(rcu_dereference_protected(ctx->peer, true)); 1095 aa_put_label(rcu_dereference_protected(ctx->peer_lastupdate, true)); 1096 } 1097 1098 /** 1099 * apparmor_sk_clone_security - clone the sk_security field 1100 * @sk: sock to have security cloned 1101 * @newsk: sock getting clone 1102 */ 1103 static void apparmor_sk_clone_security(const struct sock *sk, 1104 struct sock *newsk) 1105 { 1106 struct aa_sk_ctx *ctx = aa_sock(sk); 1107 struct aa_sk_ctx *new = aa_sock(newsk); 1108 1109 /* not actually in use yet */ 1110 if (rcu_access_pointer(ctx->label) != rcu_access_pointer(new->label)) { 1111 aa_put_label(rcu_dereference_protected(new->label, true)); 1112 rcu_assign_pointer(new->label, aa_get_label_rcu(&ctx->label)); 1113 } 1114 1115 if (rcu_access_pointer(ctx->peer) != rcu_access_pointer(new->peer)) { 1116 aa_put_label(rcu_dereference_protected(new->peer, true)); 1117 rcu_assign_pointer(new->peer, aa_get_label_rcu(&ctx->peer)); 1118 } 1119 1120 if (rcu_access_pointer(ctx->peer_lastupdate) != rcu_access_pointer(new->peer_lastupdate)) { 1121 aa_put_label(rcu_dereference_protected(new->peer_lastupdate, true)); 1122 rcu_assign_pointer(new->peer_lastupdate, 1123 aa_get_label_rcu(&ctx->peer_lastupdate)); 1124 } 1125 } 1126 1127 static int unix_connect_perm(const struct cred *cred, struct aa_label *label, 1128 struct sock *sk, struct sock *peer_sk) 1129 { 1130 struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk); 1131 int error; 1132 1133 error = aa_unix_peer_perm(cred, label, OP_CONNECT, 1134 (AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE), 1135 sk, peer_sk, 1136 rcu_dereference_protected(peer_ctx->label, 1137 lockdep_is_held(&unix_sk(peer_sk)->lock))); 1138 if (!is_unix_fs(peer_sk)) { 1139 last_error(error, 1140 aa_unix_peer_perm(cred, 1141 rcu_dereference_protected(peer_ctx->label, 1142 lockdep_is_held(&unix_sk(peer_sk)->lock)), 1143 OP_CONNECT, 1144 (AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE), 1145 peer_sk, sk, label)); 1146 } 1147 1148 return error; 1149 } 1150 1151 /* lockdep check in unix_connect_perm - push sks here to check */ 1152 static void unix_connect_peers(struct aa_sk_ctx *sk_ctx, 1153 struct aa_sk_ctx *peer_ctx) 1154 { 1155 /* Cross reference the peer labels for SO_PEERSEC */ 1156 struct aa_label *label = rcu_dereference_protected(sk_ctx->label, true); 1157 1158 aa_get_label(label); 1159 aa_put_label(rcu_dereference_protected(peer_ctx->peer, 1160 true)); 1161 rcu_assign_pointer(peer_ctx->peer, label); /* transfer cnt */ 1162 1163 label = aa_get_label(rcu_dereference_protected(peer_ctx->label, 1164 true)); 1165 //spin_unlock(&peer_ctx->lock); 1166 1167 //spin_lock(&sk_ctx->lock); 1168 aa_put_label(rcu_dereference_protected(sk_ctx->peer, 1169 true)); 1170 aa_put_label(rcu_dereference_protected(sk_ctx->peer_lastupdate, 1171 true)); 1172 1173 rcu_assign_pointer(sk_ctx->peer, aa_get_label(label)); 1174 rcu_assign_pointer(sk_ctx->peer_lastupdate, label); /* transfer cnt */ 1175 //spin_unlock(&sk_ctx->lock); 1176 } 1177 1178 /** 1179 * apparmor_unix_stream_connect - check perms before making unix domain conn 1180 * @sk: sk attempting to connect 1181 * @peer_sk: sk that is accepting the connection 1182 * @newsk: new sk created for this connection 1183 * peer is locked when this hook is called 1184 * 1185 * Return: 1186 * 0 if connection is permitted 1187 * error code on denial or failure 1188 */ 1189 static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk, 1190 struct sock *newsk) 1191 { 1192 struct aa_sk_ctx *sk_ctx = aa_sock(sk); 1193 struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk); 1194 struct aa_sk_ctx *new_ctx = aa_sock(newsk); 1195 struct aa_label *label; 1196 int error; 1197 bool needput; 1198 1199 label = __begin_current_label_crit_section(&needput); 1200 error = unix_connect_perm(current_cred(), label, sk, peer_sk); 1201 __end_current_label_crit_section(label, needput); 1202 1203 if (error) 1204 return error; 1205 1206 /* newsk doesn't go through post_create, but does go through 1207 * security_sk_alloc() 1208 */ 1209 rcu_assign_pointer(new_ctx->label, 1210 aa_get_label(rcu_dereference_protected(peer_ctx->label, 1211 true))); 1212 1213 /* Cross reference the peer labels for SO_PEERSEC */ 1214 unix_connect_peers(sk_ctx, new_ctx); 1215 1216 return 0; 1217 } 1218 1219 /** 1220 * apparmor_unix_may_send - check perms before conn or sending unix dgrams 1221 * @sock: socket sending the message 1222 * @peer: socket message is being send to 1223 * 1224 * Performs bidirectional permission checks for Unix domain socket communication: 1225 * 1. Verifies sender has AA_MAY_SEND to target socket 1226 * 2. Verifies receiver has AA_MAY_RECEIVE from source socket 1227 * 1228 * sock and peer are locked when this hook is called 1229 * called by: dgram_connect peer setup but path not copied to newsk 1230 * 1231 * Return: 1232 * 0 if transmission is permitted 1233 * error code on denial or failure 1234 */ 1235 static int apparmor_unix_may_send(struct socket *sock, struct socket *peer) 1236 { 1237 struct aa_sk_ctx *peer_ctx = aa_sock(peer->sk); 1238 struct aa_label *label; 1239 int error; 1240 bool needput; 1241 1242 label = __begin_current_label_crit_section(&needput); 1243 error = xcheck(aa_unix_peer_perm(current_cred(), 1244 label, OP_SENDMSG, AA_MAY_SEND, 1245 sock->sk, peer->sk, 1246 rcu_dereference_protected(peer_ctx->label, 1247 true)), 1248 aa_unix_peer_perm(peer->file ? peer->file->f_cred : NULL, 1249 rcu_dereference_protected(peer_ctx->label, 1250 true), 1251 OP_SENDMSG, AA_MAY_RECEIVE, peer->sk, 1252 sock->sk, label)); 1253 __end_current_label_crit_section(label, needput); 1254 1255 return error; 1256 } 1257 1258 static int apparmor_socket_create(int family, int type, int protocol, int kern) 1259 { 1260 struct aa_label *label; 1261 int error = 0; 1262 bool needput; 1263 1264 AA_BUG(in_interrupt()); 1265 1266 if (kern) 1267 return 0; 1268 1269 label = begin_current_label_crit_section(&needput); 1270 if (!unconfined(label)) { 1271 switch (family) { 1272 case PF_UNIX: 1273 error = aa_unix_create_perm(label, family, type, 1274 protocol); 1275 break; 1276 case PF_INET: 1277 case PF_INET6: 1278 error = aa_inet_create_perm(label, family, type, 1279 protocol); 1280 break; 1281 default: 1282 error = aa_af_perm(current_cred(), label, OP_CREATE, 1283 AA_MAY_CREATE, family, type, 1284 protocol); 1285 } 1286 } 1287 end_current_label_crit_section(label, needput); 1288 1289 return error; 1290 } 1291 1292 /** 1293 * apparmor_socket_post_create - setup the per-socket security struct 1294 * @sock: socket that is being setup 1295 * @family: family of socket being created 1296 * @type: type of the socket 1297 * @protocol: protocol of the socket 1298 * @kern: socket is a special kernel socket 1299 * 1300 * Note: 1301 * - kernel sockets labeled kernel_t used to use unconfined 1302 * - socket may not have sk here if created with sock_create_lite or 1303 * sock_alloc. These should be accept cases which will be handled in 1304 * sock_graft. 1305 */ 1306 static int apparmor_socket_post_create(struct socket *sock, int family, 1307 int type, int protocol, int kern) 1308 { 1309 struct aa_label *label; 1310 1311 if (kern) { 1312 label = aa_get_label(kernel_t); 1313 } else 1314 label = aa_get_current_label(); 1315 1316 if (sock->sk) { 1317 struct aa_sk_ctx *ctx = aa_sock(sock->sk); 1318 1319 /* still not live */ 1320 aa_put_label(rcu_dereference_protected(ctx->label, true)); 1321 rcu_assign_pointer(ctx->label, aa_get_label(label)); 1322 } 1323 aa_put_label(label); 1324 1325 return 0; 1326 } 1327 1328 static int apparmor_socket_socketpair(struct socket *socka, 1329 struct socket *sockb) 1330 { 1331 struct aa_sk_ctx *a_ctx = aa_sock(socka->sk); 1332 struct aa_sk_ctx *b_ctx = aa_sock(sockb->sk); 1333 struct aa_label *label; 1334 bool needput; 1335 1336 /* socks not live yet - initial values set in sk_alloc */ 1337 label = begin_current_label_crit_section(&needput); 1338 if (rcu_access_pointer(a_ctx->label) != label) { 1339 AA_BUG("a_ctx != label"); 1340 aa_put_label(rcu_dereference_protected(a_ctx->label, true)); 1341 rcu_assign_pointer(a_ctx->label, aa_get_label(label)); 1342 } 1343 if (rcu_access_pointer(b_ctx->label) != label) { 1344 AA_BUG("b_ctx != label"); 1345 aa_put_label(rcu_dereference_protected(b_ctx->label, true)); 1346 rcu_assign_pointer(b_ctx->label, aa_get_label(label)); 1347 } 1348 1349 if (socka->sk->sk_family == PF_UNIX) { 1350 /* unix socket pairs by-pass unix_stream_connect */ 1351 unix_connect_peers(a_ctx, b_ctx); 1352 } 1353 end_current_label_crit_section(label, needput); 1354 1355 return 0; 1356 } 1357 1358 /** 1359 * apparmor_socket_bind - check perms before bind addr to socket 1360 * @sock: socket to bind the address to (must be non-NULL) 1361 * @address: address that is being bound (must be non-NULL) 1362 * @addrlen: length of @address 1363 * 1364 * Performs security checks before allowing a socket to bind to an address. 1365 * Handles Unix domain sockets specially through aa_unix_bind_perm(). 1366 * For other socket families, uses generic permission check via aa_sk_perm(). 1367 * 1368 * Return: 1369 * 0 if binding is permitted 1370 * error code on denial or invalid parameters 1371 */ 1372 static int apparmor_socket_bind(struct socket *sock, 1373 struct sockaddr *address, int addrlen) 1374 { 1375 AA_BUG(!sock); 1376 AA_BUG(!sock->sk); 1377 AA_BUG(!address); 1378 AA_BUG(in_interrupt()); 1379 1380 switch (sock->sk->sk_family) { 1381 case PF_UNIX: 1382 return aa_unix_bind_perm(sock, address, addrlen); 1383 case PF_INET: 1384 case PF_INET6: 1385 return aa_inet_bind_perm(sock, address, addrlen); 1386 } 1387 return aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk); 1388 } 1389 1390 static int apparmor_socket_connect(struct socket *sock, 1391 struct sockaddr *address, int addrlen) 1392 { 1393 AA_BUG(!sock); 1394 AA_BUG(!sock->sk); 1395 AA_BUG(!address); 1396 AA_BUG(in_interrupt()); 1397 1398 /* PF_UNIX goes through unix_stream_connect && unix_may_send */ 1399 switch (sock->sk->sk_family) { 1400 case PF_UNIX: 1401 return 0; 1402 case PF_INET: 1403 case PF_INET6: 1404 return aa_inet_connect_perm(sock, address, addrlen); 1405 } 1406 return aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk); 1407 } 1408 1409 static int apparmor_sctp_bind_connect(struct sock *sk, int optname, 1410 struct sockaddr *address, int addrlen) 1411 { 1412 switch (optname) { 1413 /* Bind checks */ 1414 case SCTP_SET_PEER_PRIMARY_ADDR: 1415 case SCTP_SOCKOPT_BINDX_ADD: 1416 return aa_sk_perm(OP_BIND, AA_MAY_BIND, sk); 1417 /* Connect checks */ 1418 case SCTP_SOCKOPT_CONNECTX: 1419 case SCTP_SENDMSG_CONNECT: 1420 return aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sk); 1421 } 1422 1423 return 0; 1424 } 1425 1426 static int apparmor_socket_listen(struct socket *sock, int backlog) 1427 { 1428 AA_BUG(!sock); 1429 AA_BUG(!sock->sk); 1430 AA_BUG(in_interrupt()); 1431 1432 switch (sock->sk->sk_family) { 1433 case PF_UNIX: 1434 return aa_unix_listen_perm(sock, backlog); 1435 case PF_INET: 1436 case PF_INET6: 1437 return aa_inet_listen_perm(sock, backlog); 1438 } 1439 return aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk); 1440 } 1441 1442 /* 1443 * Note: while @newsock is created and has some information, the accept 1444 * has not been done. 1445 */ 1446 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) 1447 { 1448 AA_BUG(!sock); 1449 AA_BUG(!sock->sk); 1450 AA_BUG(!newsock); 1451 AA_BUG(in_interrupt()); 1452 1453 switch (sock->sk->sk_family) { 1454 case PF_UNIX: 1455 return aa_unix_accept_perm(sock, newsock); 1456 case PF_INET: 1457 case PF_INET6: 1458 return aa_inet_accept_perm(sock, newsock); 1459 } 1460 return aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk); 1461 } 1462 1463 static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, 1464 struct msghdr *msg, int size) 1465 { 1466 AA_BUG(!sock); 1467 AA_BUG(!sock->sk); 1468 AA_BUG(!msg); 1469 AA_BUG(in_interrupt()); 1470 1471 /* PF_UNIX goes through unix_may_send */ 1472 switch (sock->sk->sk_family) { 1473 case PF_UNIX: 1474 return 0; 1475 case PF_INET: 1476 case PF_INET6: 1477 return aa_inet_msg_perm(op, request, sock, msg, size); 1478 } 1479 1480 return aa_sk_perm(op, request, sock->sk); 1481 } 1482 1483 static int apparmor_socket_sendmsg(struct socket *sock, 1484 struct msghdr *msg, int size) 1485 { 1486 int error = aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); 1487 1488 if (error) 1489 return error; 1490 1491 /* TCP fast open carries connect() semantics in sendmsg(); mediate 1492 * the implicit connect so it cannot bypass the connect permission. 1493 */ 1494 if ((msg->msg_flags & MSG_FASTOPEN) && msg->msg_name && 1495 (sk_is_tcp(sock->sk) || 1496 (sk_is_inet(sock->sk) && sock->sk->sk_type == SOCK_STREAM && 1497 sock->sk->sk_protocol == IPPROTO_MPTCP))) 1498 error = aa_sock_msg_perm(OP_CONNECT, AA_MAY_CONNECT, sock, 1499 msg, size); 1500 1501 return error; 1502 } 1503 1504 static int apparmor_socket_recvmsg(struct socket *sock, 1505 struct msghdr *msg, int size, int flags) 1506 { 1507 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); 1508 } 1509 1510 /* revaliation, get/set attr, shutdown */ 1511 static int aa_sock_perm(const char *op, u32 request, struct socket *sock) 1512 { 1513 AA_BUG(!sock); 1514 AA_BUG(!sock->sk); 1515 AA_BUG(in_interrupt()); 1516 1517 switch (sock->sk->sk_family) { 1518 case PF_UNIX: 1519 return aa_unix_sock_perm(op, request, sock); 1520 case PF_INET: 1521 case PF_INET6: 1522 return aa_inet_sock_perm(op, request, sock); 1523 } 1524 return aa_sk_perm(op, request, sock->sk); 1525 } 1526 1527 static int apparmor_socket_getsockname(struct socket *sock) 1528 { 1529 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); 1530 } 1531 1532 static int apparmor_socket_getpeername(struct socket *sock) 1533 { 1534 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); 1535 } 1536 1537 /* revaliation, get/set attr, opt */ 1538 static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, 1539 int level, int optname) 1540 { 1541 AA_BUG(!sock); 1542 AA_BUG(!sock->sk); 1543 AA_BUG(in_interrupt()); 1544 1545 switch (sock->sk->sk_family) { 1546 case PF_UNIX: 1547 return aa_unix_opt_perm(op, request, sock, level, optname); 1548 case PF_INET: 1549 case PF_INET6: 1550 return aa_inet_opt_perm(op, request, sock, level, optname); 1551 } 1552 return aa_sk_perm(op, request, sock->sk); 1553 } 1554 1555 static int apparmor_socket_getsockopt(struct socket *sock, int level, 1556 int optname) 1557 { 1558 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, 1559 level, optname); 1560 } 1561 1562 static int apparmor_socket_setsockopt(struct socket *sock, int level, 1563 int optname) 1564 { 1565 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, 1566 level, optname); 1567 } 1568 1569 static int apparmor_socket_shutdown(struct socket *sock, int how) 1570 { 1571 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); 1572 } 1573 1574 static struct aa_label *sk_peer_get_label(struct sock *sk) 1575 { 1576 struct aa_sk_ctx *ctx = aa_sock(sk); 1577 1578 if (rcu_access_pointer(ctx->peer)) 1579 return aa_get_label_rcu(&ctx->peer); 1580 1581 return ERR_PTR(-ENOPROTOOPT); 1582 } 1583 1584 /** 1585 * apparmor_socket_getpeersec_stream - get security context of peer 1586 * @sock: socket that we are trying to get the peer context of 1587 * @optval: output - buffer to copy peer name to 1588 * @optlen: output - size of copied name in @optval 1589 * @len: size of @optval buffer 1590 * Returns: 0 on success, -errno of failure 1591 * 1592 * Note: for tcp only valid if using ipsec or cipso on lan 1593 */ 1594 static int apparmor_socket_getpeersec_stream(struct socket *sock, 1595 sockptr_t optval, sockptr_t optlen, 1596 unsigned int len) 1597 { 1598 char *name = NULL; 1599 int slen, error = 0; 1600 struct aa_label *label; 1601 struct aa_label *peer; 1602 bool needput; 1603 1604 peer = sk_peer_get_label(sock->sk); 1605 if (IS_ERR(peer)) { 1606 error = PTR_ERR(peer); 1607 goto done; 1608 } 1609 label = begin_current_label_crit_section(&needput); 1610 slen = aa_label_asxprint(&name, labels_ns(label), peer, 1611 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | 1612 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); 1613 /* don't include terminating \0 in slen, it breaks some apps */ 1614 if (slen < 0) { 1615 error = -ENOMEM; 1616 goto done_put; 1617 } 1618 if (slen > len) { 1619 error = -ERANGE; 1620 goto done_len; 1621 } 1622 1623 if (copy_to_sockptr(optval, name, slen)) 1624 error = -EFAULT; 1625 done_len: 1626 if (copy_to_sockptr(optlen, &slen, sizeof(slen))) 1627 error = -EFAULT; 1628 1629 done_put: 1630 end_current_label_crit_section(label, needput); 1631 aa_put_label(peer); 1632 done: 1633 kfree(name); 1634 return error; 1635 } 1636 1637 /** 1638 * apparmor_socket_getpeersec_dgram - get security label of packet 1639 * @sock: the peer socket 1640 * @skb: packet data 1641 * @secid: pointer to where to put the secid of the packet 1642 * 1643 * Sets the netlabel socket state on sk from parent 1644 */ 1645 static int apparmor_socket_getpeersec_dgram(struct socket *sock, 1646 struct sk_buff *skb, u32 *secid) 1647 1648 { 1649 /* TODO: requires secid support */ 1650 return -ENOPROTOOPT; 1651 } 1652 1653 /** 1654 * apparmor_sock_graft - Initialize newly created socket 1655 * @sk: child sock 1656 * @parent: parent socket 1657 * 1658 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can 1659 * just set sk security information off of current creating process label 1660 * Labeling of sk for accept case - probably should be sock based 1661 * instead of task, because of the case where an implicitly labeled 1662 * socket is shared by different tasks. 1663 */ 1664 static void apparmor_sock_graft(struct sock *sk, struct socket *parent) 1665 { 1666 struct aa_sk_ctx *ctx = aa_sock(sk); 1667 1668 /* setup - not live */ 1669 if (!rcu_access_pointer(ctx->label)) 1670 rcu_assign_pointer(ctx->label, aa_get_current_label()); 1671 } 1672 1673 #ifdef CONFIG_NETWORK_SECMARK 1674 /** 1675 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk 1676 * @sk: sk to associate @skb with 1677 * @skb: skb to check for perms 1678 * 1679 * Note: can not sleep may be called with locks held 1680 * 1681 * don't want protocol specific in __skb_recv_datagram() 1682 * to deny an incoming connection socket_sock_rcv_skb() 1683 */ 1684 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 1685 { 1686 struct aa_sk_ctx *ctx = aa_sock(sk); 1687 int error; 1688 1689 if (!skb->secmark) 1690 return 0; 1691 1692 /* 1693 * If reach here before socket_post_create hook is called, in which 1694 * case label is null, drop the packet. 1695 */ 1696 if (!rcu_access_pointer(ctx->label)) 1697 return -EACCES; 1698 1699 rcu_read_lock(); 1700 error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_RECVMSG, 1701 AA_MAY_RECEIVE, skb->secmark, sk); 1702 rcu_read_unlock(); 1703 1704 return error; 1705 } 1706 1707 static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb, 1708 struct request_sock *req) 1709 { 1710 struct aa_sk_ctx *ctx = aa_sock(sk); 1711 int error; 1712 1713 if (!skb->secmark) 1714 return 0; 1715 1716 rcu_read_lock(); 1717 error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_CONNECT, 1718 AA_MAY_CONNECT, skb->secmark, sk); 1719 rcu_read_unlock(); 1720 1721 return error; 1722 } 1723 #endif 1724 1725 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) 1726 static unsigned int apparmor_ip_postroute(void *priv, 1727 struct sk_buff *skb, 1728 const struct nf_hook_state *state) 1729 { 1730 struct aa_sk_ctx *ctx; 1731 struct sock *sk; 1732 int error; 1733 1734 if (!skb->secmark) 1735 return NF_ACCEPT; 1736 1737 sk = skb_to_full_sk(skb); 1738 if (sk == NULL) 1739 return NF_ACCEPT; 1740 1741 ctx = aa_sock(sk); 1742 rcu_read_lock(); 1743 error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_SENDMSG, 1744 AA_MAY_SEND, skb->secmark, sk); 1745 rcu_read_unlock(); 1746 if (!error) 1747 return NF_ACCEPT; 1748 1749 return NF_DROP_ERR(-ECONNREFUSED); 1750 } 1751 1752 static const struct nf_hook_ops apparmor_nf_ops[] = { 1753 { 1754 .hook = apparmor_ip_postroute, 1755 .pf = NFPROTO_IPV4, 1756 .hooknum = NF_INET_POST_ROUTING, 1757 .priority = NF_IP_PRI_SELINUX_FIRST, 1758 }, 1759 #if IS_ENABLED(CONFIG_IPV6) 1760 { 1761 .hook = apparmor_ip_postroute, 1762 .pf = NFPROTO_IPV6, 1763 .hooknum = NF_INET_POST_ROUTING, 1764 .priority = NF_IP6_PRI_SELINUX_FIRST, 1765 }, 1766 #endif 1767 }; 1768 #endif 1769 1770 /* 1771 * The cred blob is a pointer to, not an instance of, an aa_label. 1772 */ 1773 struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = { 1774 .lbs_cred = sizeof(struct aa_label *), 1775 .lbs_file = sizeof(struct aa_file_ctx), 1776 .lbs_task = sizeof(struct aa_task_ctx), 1777 .lbs_sock = sizeof(struct aa_sk_ctx), 1778 }; 1779 1780 static const struct lsm_id apparmor_lsmid = { 1781 .name = "apparmor", 1782 .id = LSM_ID_APPARMOR, 1783 }; 1784 1785 static struct security_hook_list apparmor_hooks[] __ro_after_init = { 1786 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), 1787 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), 1788 LSM_HOOK_INIT(capget, apparmor_capget), 1789 LSM_HOOK_INIT(capable, apparmor_capable), 1790 1791 LSM_HOOK_INIT(move_mount, apparmor_move_mount), 1792 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), 1793 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), 1794 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), 1795 1796 LSM_HOOK_INIT(path_link, apparmor_path_link), 1797 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), 1798 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), 1799 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), 1800 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), 1801 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), 1802 LSM_HOOK_INIT(path_rename, apparmor_path_rename), 1803 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), 1804 LSM_HOOK_INIT(path_chown, apparmor_path_chown), 1805 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), 1806 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), 1807 1808 LSM_HOOK_INIT(file_open, apparmor_file_open), 1809 LSM_HOOK_INIT(file_receive, apparmor_file_receive), 1810 LSM_HOOK_INIT(file_permission, apparmor_file_permission), 1811 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), 1812 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), 1813 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), 1814 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), 1815 LSM_HOOK_INIT(file_lock, apparmor_file_lock), 1816 LSM_HOOK_INIT(file_truncate, apparmor_file_truncate), 1817 1818 LSM_HOOK_INIT(getselfattr, apparmor_getselfattr), 1819 LSM_HOOK_INIT(setselfattr, apparmor_setselfattr), 1820 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), 1821 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), 1822 1823 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security), 1824 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), 1825 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), 1826 1827 LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect), 1828 LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send), 1829 1830 LSM_HOOK_INIT(socket_create, apparmor_socket_create), 1831 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), 1832 LSM_HOOK_INIT(socket_socketpair, apparmor_socket_socketpair), 1833 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), 1834 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), 1835 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), 1836 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), 1837 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), 1838 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), 1839 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), 1840 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), 1841 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), 1842 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), 1843 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), 1844 LSM_HOOK_INIT(sctp_bind_connect, apparmor_sctp_bind_connect), 1845 #ifdef CONFIG_NETWORK_SECMARK 1846 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), 1847 #endif 1848 LSM_HOOK_INIT(socket_getpeersec_stream, 1849 apparmor_socket_getpeersec_stream), 1850 LSM_HOOK_INIT(socket_getpeersec_dgram, 1851 apparmor_socket_getpeersec_dgram), 1852 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), 1853 #ifdef CONFIG_NETWORK_SECMARK 1854 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), 1855 #endif 1856 1857 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), 1858 LSM_HOOK_INIT(cred_free, apparmor_cred_free), 1859 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), 1860 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), 1861 1862 LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec), 1863 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), 1864 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), 1865 1866 LSM_HOOK_INIT(task_free, apparmor_task_free), 1867 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc), 1868 LSM_HOOK_INIT(current_getlsmprop_subj, 1869 apparmor_current_getlsmprop_subj), 1870 LSM_HOOK_INIT(task_getlsmprop_obj, apparmor_task_getlsmprop_obj), 1871 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), 1872 LSM_HOOK_INIT(task_kill, apparmor_task_kill), 1873 LSM_HOOK_INIT(userns_create, apparmor_userns_create), 1874 1875 #ifdef CONFIG_AUDIT 1876 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init), 1877 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known), 1878 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match), 1879 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free), 1880 #endif 1881 1882 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx), 1883 LSM_HOOK_INIT(lsmprop_to_secctx, apparmor_lsmprop_to_secctx), 1884 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid), 1885 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx), 1886 1887 #ifdef CONFIG_IO_URING 1888 LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds), 1889 LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll), 1890 #endif 1891 }; 1892 1893 /* 1894 * AppArmor sysfs module parameters 1895 */ 1896 1897 static int param_set_aabool(const char *val, const struct kernel_param *kp); 1898 static int param_get_aabool(char *buffer, const struct kernel_param *kp); 1899 #define param_check_aabool param_check_bool 1900 static const struct kernel_param_ops param_ops_aabool = { 1901 .flags = KERNEL_PARAM_OPS_FL_NOARG, 1902 .set = param_set_aabool, 1903 .get = param_get_aabool 1904 }; 1905 1906 static int param_set_aauint(const char *val, const struct kernel_param *kp); 1907 static int param_get_aauint(char *buffer, const struct kernel_param *kp); 1908 #define param_check_aauint param_check_uint 1909 static const struct kernel_param_ops param_ops_aauint = { 1910 .set = param_set_aauint, 1911 .get = param_get_aauint 1912 }; 1913 1914 static int param_set_aacompressionlevel(const char *val, 1915 const struct kernel_param *kp); 1916 static int param_get_aacompressionlevel(char *buffer, 1917 const struct kernel_param *kp); 1918 #define param_check_aacompressionlevel param_check_int 1919 static const struct kernel_param_ops param_ops_aacompressionlevel = { 1920 .set = param_set_aacompressionlevel, 1921 .get = param_get_aacompressionlevel 1922 }; 1923 1924 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); 1925 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); 1926 #define param_check_aalockpolicy param_check_bool 1927 static const struct kernel_param_ops param_ops_aalockpolicy = { 1928 .flags = KERNEL_PARAM_OPS_FL_NOARG, 1929 .set = param_set_aalockpolicy, 1930 .get = param_get_aalockpolicy 1931 }; 1932 1933 static int param_set_debug(const char *val, const struct kernel_param *kp); 1934 static int param_get_debug(char *buffer, const struct kernel_param *kp); 1935 1936 static int param_set_audit(const char *val, const struct kernel_param *kp); 1937 static int param_get_audit(char *buffer, const struct kernel_param *kp); 1938 1939 static int param_set_mode(const char *val, const struct kernel_param *kp); 1940 static int param_get_mode(char *buffer, const struct kernel_param *kp); 1941 1942 /* Flag values, also controllable via /sys/module/apparmor/parameters 1943 * We define special types as we want to do additional mediation. 1944 */ 1945 1946 /* AppArmor global enforcement switch - complain, enforce, kill */ 1947 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; 1948 module_param_call(mode, param_set_mode, param_get_mode, 1949 &aa_g_profile_mode, S_IRUSR | S_IWUSR); 1950 1951 /* whether policy verification hashing is enabled */ 1952 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); 1953 #ifdef CONFIG_SECURITY_APPARMOR_HASH 1954 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); 1955 #endif 1956 1957 /* whether policy exactly as loaded is retained for debug and checkpointing */ 1958 bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY); 1959 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 1960 module_param_named(export_binary, aa_g_export_binary, aabool, 0600); 1961 #endif 1962 1963 /* policy loaddata compression level */ 1964 int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL; 1965 module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level, 1966 aacompressionlevel, 0400); 1967 1968 /* Debug mode */ 1969 int aa_g_debug; 1970 module_param_call(debug, param_set_debug, param_get_debug, 1971 &aa_g_debug, 0600); 1972 1973 /* Audit mode */ 1974 enum audit_mode aa_g_audit; 1975 module_param_call(audit, param_set_audit, param_get_audit, 1976 &aa_g_audit, S_IRUSR | S_IWUSR); 1977 1978 /* Determines if audit header is included in audited messages. This 1979 * provides more context if the audit daemon is not running 1980 */ 1981 bool aa_g_audit_header = true; 1982 module_param_named(audit_header, aa_g_audit_header, aabool, 1983 S_IRUSR | S_IWUSR); 1984 1985 /* lock out loading/removal of policy 1986 * TODO: add in at boot loading of policy, which is the only way to 1987 * load policy, if lock_policy is set 1988 */ 1989 bool aa_g_lock_policy; 1990 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, 1991 S_IRUSR | S_IWUSR); 1992 1993 /* Syscall logging mode */ 1994 bool aa_g_logsyscall; 1995 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); 1996 1997 /* Maximum pathname length before accesses will start getting rejected */ 1998 unsigned int aa_g_path_max = 2 * PATH_MAX; 1999 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); 2000 2001 /* Determines how paranoid loading of policy is and how much verification 2002 * on the loaded policy is done. 2003 * DEPRECATED: read only as strict checking of load is always done now 2004 * that none root users (user namespaces) can load policy. 2005 */ 2006 bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD); 2007 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); 2008 2009 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp); 2010 static int param_set_aaintbool(const char *val, const struct kernel_param *kp); 2011 #define param_check_aaintbool param_check_int 2012 static const struct kernel_param_ops param_ops_aaintbool = { 2013 .set = param_set_aaintbool, 2014 .get = param_get_aaintbool 2015 }; 2016 /* Boot time disable flag */ 2017 static int apparmor_enabled __ro_after_init = 1; 2018 module_param_named(enabled, apparmor_enabled, aaintbool, 0444); 2019 2020 static int __init apparmor_enabled_setup(char *str) 2021 { 2022 unsigned long enabled; 2023 int error = kstrtoul(str, 0, &enabled); 2024 if (!error) 2025 apparmor_enabled = enabled ? 1 : 0; 2026 return 1; 2027 } 2028 2029 __setup("apparmor=", apparmor_enabled_setup); 2030 2031 /* set global flag turning off the ability to load policy */ 2032 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) 2033 { 2034 if (!apparmor_enabled) 2035 return -EINVAL; 2036 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 2037 return -EPERM; 2038 return param_set_bool(val, kp); 2039 } 2040 2041 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) 2042 { 2043 if (!apparmor_enabled) 2044 return -EINVAL; 2045 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 2046 return -EPERM; 2047 return param_get_bool(buffer, kp); 2048 } 2049 2050 static int param_set_aabool(const char *val, const struct kernel_param *kp) 2051 { 2052 if (!apparmor_enabled) 2053 return -EINVAL; 2054 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 2055 return -EPERM; 2056 return param_set_bool(val, kp); 2057 } 2058 2059 static int param_get_aabool(char *buffer, const struct kernel_param *kp) 2060 { 2061 if (!apparmor_enabled) 2062 return -EINVAL; 2063 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 2064 return -EPERM; 2065 return param_get_bool(buffer, kp); 2066 } 2067 2068 static int param_set_aauint(const char *val, const struct kernel_param *kp) 2069 { 2070 int error; 2071 2072 if (!apparmor_enabled) 2073 return -EINVAL; 2074 /* file is ro but enforce 2nd line check */ 2075 if (apparmor_initialized) 2076 return -EPERM; 2077 2078 error = param_set_uint(val, kp); 2079 aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer)); 2080 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); 2081 2082 return error; 2083 } 2084 2085 static int param_get_aauint(char *buffer, const struct kernel_param *kp) 2086 { 2087 if (!apparmor_enabled) 2088 return -EINVAL; 2089 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 2090 return -EPERM; 2091 return param_get_uint(buffer, kp); 2092 } 2093 2094 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */ 2095 static int param_set_aaintbool(const char *val, const struct kernel_param *kp) 2096 { 2097 struct kernel_param kp_local; 2098 bool value; 2099 int error; 2100 2101 if (apparmor_initialized) 2102 return -EPERM; 2103 2104 /* Create local copy, with arg pointing to bool type. */ 2105 value = !!*((int *)kp->arg); 2106 memcpy(&kp_local, kp, sizeof(kp_local)); 2107 kp_local.arg = &value; 2108 2109 error = param_set_bool(val, &kp_local); 2110 if (!error) 2111 *((int *)kp->arg) = *((bool *)kp_local.arg); 2112 return error; 2113 } 2114 2115 /* 2116 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to 2117 * 1/0, this converts the "int that is actually bool" back to bool for 2118 * display in the /sys filesystem, while keeping it "int" for the LSM 2119 * infrastructure. 2120 */ 2121 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp) 2122 { 2123 struct kernel_param kp_local; 2124 bool value; 2125 2126 /* Create local copy, with arg pointing to bool type. */ 2127 value = !!*((int *)kp->arg); 2128 memcpy(&kp_local, kp, sizeof(kp_local)); 2129 kp_local.arg = &value; 2130 2131 return param_get_bool(buffer, &kp_local); 2132 } 2133 2134 static int param_set_aacompressionlevel(const char *val, 2135 const struct kernel_param *kp) 2136 { 2137 int error; 2138 2139 if (!apparmor_enabled) 2140 return -EINVAL; 2141 if (apparmor_initialized) 2142 return -EPERM; 2143 2144 error = param_set_int(val, kp); 2145 2146 aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level, 2147 AA_MIN_CLEVEL, AA_MAX_CLEVEL); 2148 pr_info("AppArmor: policy rawdata compression level set to %d\n", 2149 aa_g_rawdata_compression_level); 2150 2151 return error; 2152 } 2153 2154 static int param_get_aacompressionlevel(char *buffer, 2155 const struct kernel_param *kp) 2156 { 2157 if (!apparmor_enabled) 2158 return -EINVAL; 2159 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 2160 return -EPERM; 2161 return param_get_int(buffer, kp); 2162 } 2163 2164 static int param_get_debug(char *buffer, const struct kernel_param *kp) 2165 { 2166 if (!apparmor_enabled) 2167 return -EINVAL; 2168 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 2169 return -EPERM; 2170 return aa_print_debug_params(buffer); 2171 } 2172 2173 static int param_set_debug(const char *val, const struct kernel_param *kp) 2174 { 2175 int i; 2176 2177 if (!apparmor_enabled) 2178 return -EINVAL; 2179 if (!val) 2180 return -EINVAL; 2181 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 2182 return -EPERM; 2183 2184 i = aa_parse_debug_params(val); 2185 if (i == DEBUG_PARSE_ERROR) 2186 return -EINVAL; 2187 2188 aa_g_debug = i; 2189 return 0; 2190 } 2191 2192 static int param_get_audit(char *buffer, const struct kernel_param *kp) 2193 { 2194 if (!apparmor_enabled) 2195 return -EINVAL; 2196 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 2197 return -EPERM; 2198 return sysfs_emit(buffer, "%s\n", audit_mode_names[aa_g_audit]); 2199 } 2200 2201 static int param_set_audit(const char *val, const struct kernel_param *kp) 2202 { 2203 int i; 2204 2205 if (!apparmor_enabled) 2206 return -EINVAL; 2207 if (!val) 2208 return -EINVAL; 2209 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 2210 return -EPERM; 2211 2212 i = match_string(audit_mode_names, AUDIT_MODE_NAMES_COUNT, val); 2213 if (i < 0) 2214 return -EINVAL; 2215 2216 aa_g_audit = i; 2217 return 0; 2218 } 2219 2220 static int param_get_mode(char *buffer, const struct kernel_param *kp) 2221 { 2222 if (!apparmor_enabled) 2223 return -EINVAL; 2224 if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 2225 return -EPERM; 2226 return sysfs_emit(buffer, "%s\n", aa_profile_mode_names[aa_g_profile_mode]); 2227 } 2228 2229 static int param_set_mode(const char *val, const struct kernel_param *kp) 2230 { 2231 int i; 2232 2233 if (!apparmor_enabled) 2234 return -EINVAL; 2235 if (!val) 2236 return -EINVAL; 2237 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 2238 return -EPERM; 2239 2240 i = match_string(aa_profile_mode_names, PROFILE_MODE_NAMES_COUNT, 2241 val); 2242 if (i < 0) 2243 return -EINVAL; 2244 2245 aa_g_profile_mode = i; 2246 return 0; 2247 } 2248 2249 /* arbitrary cap on how long to hold buffer because contention was 2250 * encountered before trying to put it back into the global pool 2251 */ 2252 #define MAX_HOLD_COUNT 64 2253 2254 /* the hold count is a heuristic for lock contention, and can be 2255 * incremented async to actual buffer alloc/free. Because buffers 2256 * may be put back onto a percpu cache different than the ->hold was 2257 * added to the counts can be out of sync. Guard against underflow 2258 * and overflow 2259 */ 2260 static void cache_hold_inc(unsigned int *hold) 2261 { 2262 if (*hold < MAX_HOLD_COUNT) 2263 (*hold)++; 2264 } 2265 2266 char *aa_get_buffer(bool in_atomic) 2267 { 2268 union aa_buffer *aa_buf; 2269 struct aa_local_cache *cache; 2270 bool try_again = true; 2271 gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 2272 2273 /* use per cpu cached buffers first */ 2274 cache = get_cpu_ptr(&aa_local_buffers); 2275 if (!list_empty(&cache->head)) { 2276 aa_buf = list_first_entry(&cache->head, union aa_buffer, list); 2277 list_del(&aa_buf->list); 2278 if (cache->hold) 2279 cache->hold--; 2280 cache->count--; 2281 put_cpu_ptr(&aa_local_buffers); 2282 return &aa_buf->buffer[0]; 2283 } 2284 /* exit percpu as spinlocks may sleep on realtime kernels */ 2285 put_cpu_ptr(&aa_local_buffers); 2286 2287 if (!spin_trylock(&aa_buffers_lock)) { 2288 /* had contention on lock so increase hold count. Doesn't 2289 * really matter if recorded before or after the spin lock 2290 * as there is no way to guarantee the buffer will be put 2291 * back on the same percpu cache. Instead rely on holds 2292 * roughly averaging out over time. 2293 */ 2294 cache = get_cpu_ptr(&aa_local_buffers); 2295 cache_hold_inc(&cache->hold); 2296 put_cpu_ptr(&aa_local_buffers); 2297 spin_lock(&aa_buffers_lock); 2298 } 2299 retry: 2300 if (buffer_count > reserve_count || 2301 (in_atomic && !list_empty(&aa_global_buffers))) { 2302 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 2303 list); 2304 list_del(&aa_buf->list); 2305 buffer_count--; 2306 spin_unlock(&aa_buffers_lock); 2307 return aa_buf->buffer; 2308 } 2309 if (in_atomic) { 2310 /* 2311 * out of reserve buffers and in atomic context so increase 2312 * how many buffers to keep in reserve 2313 */ 2314 reserve_count++; 2315 flags = GFP_ATOMIC; 2316 } 2317 spin_unlock(&aa_buffers_lock); 2318 2319 if (!in_atomic) 2320 might_sleep(); 2321 aa_buf = kmalloc(aa_g_path_max, flags); 2322 if (!aa_buf) { 2323 if (try_again) { 2324 try_again = false; 2325 spin_lock(&aa_buffers_lock); 2326 goto retry; 2327 } 2328 pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n"); 2329 return NULL; 2330 } 2331 return aa_buf->buffer; 2332 } 2333 2334 void aa_put_buffer(char *buf) 2335 { 2336 union aa_buffer *aa_buf; 2337 struct aa_local_cache *cache; 2338 2339 if (!buf) 2340 return; 2341 aa_buf = container_of(buf, union aa_buffer, buffer[0]); 2342 2343 cache = get_cpu_ptr(&aa_local_buffers); 2344 if (!cache->hold) { 2345 put_cpu_ptr(&aa_local_buffers); 2346 2347 if (spin_trylock(&aa_buffers_lock)) { 2348 /* put back on global list */ 2349 list_add(&aa_buf->list, &aa_global_buffers); 2350 buffer_count++; 2351 spin_unlock(&aa_buffers_lock); 2352 return; 2353 } 2354 /* contention on global list, fallback to percpu */ 2355 cache = get_cpu_ptr(&aa_local_buffers); 2356 cache_hold_inc(&cache->hold); 2357 } 2358 2359 /* cache in percpu list */ 2360 list_add(&aa_buf->list, &cache->head); 2361 cache->count++; 2362 put_cpu_ptr(&aa_local_buffers); 2363 } 2364 2365 /* 2366 * AppArmor init functions 2367 */ 2368 2369 /** 2370 * set_init_ctx - set a task context and profile on the first task. 2371 * 2372 * TODO: allow setting an alternate profile than unconfined 2373 */ 2374 static int __init set_init_ctx(void) 2375 { 2376 struct cred *cred = (__force struct cred *)current->real_cred; 2377 2378 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); 2379 2380 return 0; 2381 } 2382 2383 static void destroy_buffers(void) 2384 { 2385 union aa_buffer *aa_buf; 2386 2387 spin_lock(&aa_buffers_lock); 2388 while (!list_empty(&aa_global_buffers)) { 2389 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 2390 list); 2391 list_del(&aa_buf->list); 2392 spin_unlock(&aa_buffers_lock); 2393 kfree(aa_buf); 2394 spin_lock(&aa_buffers_lock); 2395 } 2396 spin_unlock(&aa_buffers_lock); 2397 } 2398 2399 static int __init alloc_buffers(void) 2400 { 2401 union aa_buffer *aa_buf; 2402 int i, num; 2403 2404 /* 2405 * per cpu set of cached allocated buffers used to help reduce 2406 * lock contention 2407 */ 2408 for_each_possible_cpu(i) { 2409 per_cpu(aa_local_buffers, i).hold = 0; 2410 per_cpu(aa_local_buffers, i).count = 0; 2411 INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head); 2412 } 2413 /* 2414 * A function may require two buffers at once. Usually the buffers are 2415 * used for a short period of time and are shared. On UP kernel buffers 2416 * two should be enough, with more CPUs it is possible that more 2417 * buffers will be used simultaneously. The preallocated pool may grow. 2418 * This preallocation has also the side-effect that AppArmor will be 2419 * disabled early at boot if aa_g_path_max is extremely high. 2420 */ 2421 if (num_online_cpus() > 1) 2422 num = 4 + RESERVE_COUNT; 2423 else 2424 num = 2 + RESERVE_COUNT; 2425 2426 for (i = 0; i < num; i++) { 2427 2428 aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL | 2429 __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 2430 if (!aa_buf) { 2431 destroy_buffers(); 2432 return -ENOMEM; 2433 } 2434 aa_put_buffer(aa_buf->buffer); 2435 } 2436 return 0; 2437 } 2438 2439 #ifdef CONFIG_SYSCTL 2440 static int apparmor_dointvec(const struct ctl_table *table, int write, 2441 void *buffer, size_t *lenp, loff_t *ppos) 2442 { 2443 if (!aa_current_policy_admin_capable(NULL)) 2444 return -EPERM; 2445 if (!apparmor_enabled) 2446 return -EINVAL; 2447 2448 return proc_dointvec(table, write, buffer, lenp, ppos); 2449 } 2450 2451 static const struct ctl_table apparmor_sysctl_table[] = { 2452 #ifdef CONFIG_USER_NS 2453 { 2454 .procname = "unprivileged_userns_apparmor_policy", 2455 .data = &unprivileged_userns_apparmor_policy, 2456 .maxlen = sizeof(int), 2457 .mode = 0600, 2458 .proc_handler = apparmor_dointvec, 2459 }, 2460 #endif /* CONFIG_USER_NS */ 2461 { 2462 .procname = "apparmor_display_secid_mode", 2463 .data = &apparmor_display_secid_mode, 2464 .maxlen = sizeof(int), 2465 .mode = 0600, 2466 .proc_handler = apparmor_dointvec, 2467 }, 2468 { 2469 .procname = "apparmor_restrict_unprivileged_unconfined", 2470 .data = &aa_unprivileged_unconfined_restricted, 2471 .maxlen = sizeof(int), 2472 .mode = 0600, 2473 .proc_handler = apparmor_dointvec, 2474 }, 2475 }; 2476 2477 static int __init apparmor_init_sysctl(void) 2478 { 2479 return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM; 2480 } 2481 #else 2482 static inline int apparmor_init_sysctl(void) 2483 { 2484 return 0; 2485 } 2486 #endif /* CONFIG_SYSCTL */ 2487 2488 2489 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) 2490 static int __net_init apparmor_nf_register(struct net *net) 2491 { 2492 return nf_register_net_hooks(net, apparmor_nf_ops, 2493 ARRAY_SIZE(apparmor_nf_ops)); 2494 } 2495 2496 static void __net_exit apparmor_nf_unregister(struct net *net) 2497 { 2498 nf_unregister_net_hooks(net, apparmor_nf_ops, 2499 ARRAY_SIZE(apparmor_nf_ops)); 2500 } 2501 2502 static struct pernet_operations apparmor_net_ops = { 2503 .init = apparmor_nf_register, 2504 .exit = apparmor_nf_unregister, 2505 }; 2506 2507 static int __init apparmor_nf_ip_init(void) 2508 { 2509 int err; 2510 2511 if (!apparmor_enabled) 2512 return 0; 2513 2514 err = register_pernet_subsys(&apparmor_net_ops); 2515 if (err) 2516 panic("Apparmor: register_pernet_subsys: error %d\n", err); 2517 2518 return 0; 2519 } 2520 #endif 2521 2522 static const char nulldfa_src[] __aligned(8) = { 2523 #include "nulldfa.in" 2524 }; 2525 static struct aa_dfa *nulldfa __ro_after_init; 2526 2527 static char stacksplitdfa_src[] __aligned(8) = { 2528 #include "stacksplitdfa.in" 2529 }; 2530 struct aa_dfa *stacksplitdfa __ro_after_init; 2531 struct aa_policydb *nullpdb __ro_after_init; 2532 2533 static int __init aa_setup_dfa_engine(void) 2534 { 2535 int error = -ENOMEM; 2536 2537 nullpdb = aa_alloc_pdb(GFP_KERNEL); 2538 if (!nullpdb) 2539 return -ENOMEM; 2540 2541 nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src), 2542 TO_ACCEPT1_FLAG(YYTD_DATA32) | 2543 TO_ACCEPT2_FLAG(YYTD_DATA32)); 2544 if (IS_ERR(nulldfa)) { 2545 error = PTR_ERR(nulldfa); 2546 nulldfa = NULL; 2547 goto fail; 2548 } 2549 nullpdb->dfa = aa_get_dfa(nulldfa); 2550 nullpdb->perms = kzalloc_objs(struct aa_perms, 2); 2551 if (!nullpdb->perms) 2552 goto fail; 2553 nullpdb->size = 2; 2554 2555 stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src, 2556 sizeof(stacksplitdfa_src), 2557 TO_ACCEPT1_FLAG(YYTD_DATA32) | 2558 TO_ACCEPT2_FLAG(YYTD_DATA32)); 2559 if (IS_ERR(stacksplitdfa)) { 2560 error = PTR_ERR(stacksplitdfa); 2561 goto fail; 2562 } 2563 2564 return 0; 2565 2566 fail: 2567 aa_put_pdb(nullpdb); 2568 aa_put_dfa(nulldfa); 2569 nullpdb = NULL; 2570 nulldfa = NULL; 2571 stacksplitdfa = NULL; 2572 2573 return error; 2574 } 2575 2576 static void __init aa_teardown_dfa_engine(void) 2577 { 2578 aa_put_dfa(stacksplitdfa); 2579 aa_put_dfa(nulldfa); 2580 aa_put_pdb(nullpdb); 2581 nullpdb = NULL; 2582 stacksplitdfa = NULL; 2583 nulldfa = NULL; 2584 } 2585 2586 static int __init apparmor_init(void) 2587 { 2588 int error; 2589 2590 error = aa_setup_dfa_engine(); 2591 if (error) { 2592 AA_ERROR("Unable to setup dfa engine\n"); 2593 goto alloc_out; 2594 } 2595 2596 error = aa_alloc_root_ns(); 2597 if (error) { 2598 AA_ERROR("Unable to allocate default profile namespace\n"); 2599 goto alloc_out; 2600 } 2601 2602 error = apparmor_init_sysctl(); 2603 if (error) { 2604 AA_ERROR("Unable to register sysctls\n"); 2605 goto alloc_out; 2606 2607 } 2608 2609 error = alloc_buffers(); 2610 if (error) { 2611 AA_ERROR("Unable to allocate work buffers\n"); 2612 goto alloc_out; 2613 } 2614 2615 error = set_init_ctx(); 2616 if (error) { 2617 AA_ERROR("Failed to set context on init task\n"); 2618 aa_free_root_ns(); 2619 goto buffers_out; 2620 } 2621 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), 2622 &apparmor_lsmid); 2623 2624 /* Inform the audit system that secctx is used */ 2625 audit_cfg_lsm(&apparmor_lsmid, AUDIT_CFG_LSM_SECCTX_SUBJECT); 2626 2627 /* Report that AppArmor successfully initialized */ 2628 apparmor_initialized = 1; 2629 if (aa_g_profile_mode == APPARMOR_COMPLAIN) 2630 aa_info_message("AppArmor initialized: complain mode enabled"); 2631 else if (aa_g_profile_mode == APPARMOR_KILL) 2632 aa_info_message("AppArmor initialized: kill mode enabled"); 2633 else 2634 aa_info_message("AppArmor initialized"); 2635 2636 return error; 2637 2638 buffers_out: 2639 destroy_buffers(); 2640 alloc_out: 2641 aa_destroy_aafs(); 2642 aa_teardown_dfa_engine(); 2643 2644 apparmor_enabled = false; 2645 return error; 2646 } 2647 2648 DEFINE_LSM(apparmor) = { 2649 .id = &apparmor_lsmid, 2650 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 2651 .enabled = &apparmor_enabled, 2652 .blobs = &apparmor_blob_sizes, 2653 .init = apparmor_init, 2654 .initcall_fs = aa_create_aafs, 2655 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) 2656 .initcall_device = apparmor_nf_ip_init, 2657 #endif 2658 #ifdef CONFIG_SECURITY_APPARMOR_HASH 2659 .initcall_late = init_profile_hash, 2660 #endif 2661 }; 2662