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