1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor policy attachment and domain transitions 6 * 7 * Copyright (C) 2002-2008 Novell/SUSE 8 * Copyright 2009-2010 Canonical Ltd. 9 */ 10 11 #include <linux/errno.h> 12 #include <linux/fs.h> 13 #include <linux/file.h> 14 #include <linux/mount.h> 15 #include <linux/syscalls.h> 16 #include <linux/personality.h> 17 #include <linux/xattr.h> 18 #include <linux/user_namespace.h> 19 20 #include "include/audit.h" 21 #include "include/apparmorfs.h" 22 #include "include/cred.h" 23 #include "include/domain.h" 24 #include "include/file.h" 25 #include "include/ipc.h" 26 #include "include/match.h" 27 #include "include/path.h" 28 #include "include/policy.h" 29 #include "include/policy_ns.h" 30 31 /** 32 * may_change_ptraced_domain - check if can change profile on ptraced task 33 * @to_cred: cred of task changing domain 34 * @to_label: profile to change to (NOT NULL) 35 * @info: message if there is an error 36 * 37 * Check if current is ptraced and if so if the tracing task is allowed 38 * to trace the new domain 39 * 40 * Returns: %0 or error if change not allowed 41 */ 42 static int may_change_ptraced_domain(const struct cred *to_cred, 43 struct aa_label *to_label, 44 const char **info) 45 { 46 struct task_struct *tracer; 47 struct aa_label *tracerl = NULL; 48 const struct cred *tracer_cred = NULL; 49 50 int error = 0; 51 52 rcu_read_lock(); 53 tracer = ptrace_parent(current); 54 if (tracer) { 55 /* released below */ 56 tracerl = aa_get_task_label(tracer); 57 tracer_cred = get_task_cred(tracer); 58 } 59 /* not ptraced */ 60 if (!tracer || unconfined(tracerl)) 61 goto out; 62 63 error = aa_may_ptrace(tracer_cred, tracerl, to_cred, to_label, 64 PTRACE_MODE_ATTACH); 65 66 out: 67 rcu_read_unlock(); 68 aa_put_label(tracerl); 69 put_cred(tracer_cred); 70 71 if (error) 72 *info = "ptrace prevents transition"; 73 return error; 74 } 75 76 /**** TODO: dedup to aa_label_match - needs perm and dfa, merging 77 * specifically this is an exact copy of aa_label_match except 78 * aa_compute_perms is replaced with aa_compute_fperms 79 * and policy->dfa with file->dfa 80 ****/ 81 /* match a profile and its associated ns component if needed 82 * Assumes visibility test has already been done. 83 * If a subns profile is not to be matched should be prescreened with 84 * visibility test. 85 */ 86 static inline aa_state_t match_component(struct aa_profile *profile, 87 struct aa_profile *tp, 88 bool stack, aa_state_t state) 89 { 90 struct aa_ruleset *rules = list_first_entry(&profile->rules, 91 typeof(*rules), list); 92 const char *ns_name; 93 94 if (stack) 95 state = aa_dfa_match(rules->file->dfa, state, "&"); 96 if (profile->ns == tp->ns) 97 return aa_dfa_match(rules->file->dfa, state, tp->base.hname); 98 99 /* try matching with namespace name and then profile */ 100 ns_name = aa_ns_name(profile->ns, tp->ns, true); 101 state = aa_dfa_match_len(rules->file->dfa, state, ":", 1); 102 state = aa_dfa_match(rules->file->dfa, state, ns_name); 103 state = aa_dfa_match_len(rules->file->dfa, state, ":", 1); 104 return aa_dfa_match(rules->file->dfa, state, tp->base.hname); 105 } 106 107 /** 108 * label_compound_match - find perms for full compound label 109 * @profile: profile to find perms for 110 * @label: label to check access permissions for 111 * @stack: whether this is a stacking request 112 * @state: state to start match in 113 * @subns: whether to do permission checks on components in a subns 114 * @request: permissions to request 115 * @perms: perms struct to set 116 * 117 * Returns: 0 on success else ERROR 118 * 119 * For the label A//&B//&C this does the perm match for A//&B//&C 120 * @perms should be preinitialized with allperms OR a previous permission 121 * check to be stacked. 122 */ 123 static int label_compound_match(struct aa_profile *profile, 124 struct aa_label *label, bool stack, 125 aa_state_t state, bool subns, u32 request, 126 struct aa_perms *perms) 127 { 128 struct aa_ruleset *rules = list_first_entry(&profile->rules, 129 typeof(*rules), list); 130 struct aa_profile *tp; 131 struct label_it i; 132 struct path_cond cond = { }; 133 134 /* find first subcomponent that is visible */ 135 label_for_each(i, label, tp) { 136 if (!aa_ns_visible(profile->ns, tp->ns, subns)) 137 continue; 138 state = match_component(profile, tp, stack, state); 139 if (!state) 140 goto fail; 141 goto next; 142 } 143 144 /* no component visible */ 145 *perms = allperms; 146 return 0; 147 148 next: 149 label_for_each_cont(i, label, tp) { 150 if (!aa_ns_visible(profile->ns, tp->ns, subns)) 151 continue; 152 state = aa_dfa_match(rules->file->dfa, state, "//&"); 153 state = match_component(profile, tp, false, state); 154 if (!state) 155 goto fail; 156 } 157 *perms = *(aa_lookup_fperms(rules->file, state, &cond)); 158 aa_apply_modes_to_perms(profile, perms); 159 if ((perms->allow & request) != request) 160 return -EACCES; 161 162 return 0; 163 164 fail: 165 *perms = nullperms; 166 return -EACCES; 167 } 168 169 /** 170 * label_components_match - find perms for all subcomponents of a label 171 * @profile: profile to find perms for 172 * @label: label to check access permissions for 173 * @stack: whether this is a stacking request 174 * @start: state to start match in 175 * @subns: whether to do permission checks on components in a subns 176 * @request: permissions to request 177 * @perms: an initialized perms struct to add accumulation to 178 * 179 * Returns: 0 on success else ERROR 180 * 181 * For the label A//&B//&C this does the perm match for each of A and B and C 182 * @perms should be preinitialized with allperms OR a previous permission 183 * check to be stacked. 184 */ 185 static int label_components_match(struct aa_profile *profile, 186 struct aa_label *label, bool stack, 187 aa_state_t start, bool subns, u32 request, 188 struct aa_perms *perms) 189 { 190 struct aa_ruleset *rules = list_first_entry(&profile->rules, 191 typeof(*rules), list); 192 struct aa_profile *tp; 193 struct label_it i; 194 struct aa_perms tmp; 195 struct path_cond cond = { }; 196 aa_state_t state = 0; 197 198 /* find first subcomponent to test */ 199 label_for_each(i, label, tp) { 200 if (!aa_ns_visible(profile->ns, tp->ns, subns)) 201 continue; 202 state = match_component(profile, tp, stack, start); 203 if (!state) 204 goto fail; 205 goto next; 206 } 207 208 /* no subcomponents visible - no change in perms */ 209 return 0; 210 211 next: 212 tmp = *(aa_lookup_fperms(rules->file, state, &cond)); 213 aa_apply_modes_to_perms(profile, &tmp); 214 aa_perms_accum(perms, &tmp); 215 label_for_each_cont(i, label, tp) { 216 if (!aa_ns_visible(profile->ns, tp->ns, subns)) 217 continue; 218 state = match_component(profile, tp, stack, start); 219 if (!state) 220 goto fail; 221 tmp = *(aa_lookup_fperms(rules->file, state, &cond)); 222 aa_apply_modes_to_perms(profile, &tmp); 223 aa_perms_accum(perms, &tmp); 224 } 225 226 if ((perms->allow & request) != request) 227 return -EACCES; 228 229 return 0; 230 231 fail: 232 *perms = nullperms; 233 return -EACCES; 234 } 235 236 /** 237 * label_match - do a multi-component label match 238 * @profile: profile to match against (NOT NULL) 239 * @label: label to match (NOT NULL) 240 * @stack: whether this is a stacking request 241 * @state: state to start in 242 * @subns: whether to match subns components 243 * @request: permission request 244 * @perms: Returns computed perms (NOT NULL) 245 * 246 * Returns: the state the match finished in, may be the none matching state 247 */ 248 static int label_match(struct aa_profile *profile, struct aa_label *label, 249 bool stack, aa_state_t state, bool subns, u32 request, 250 struct aa_perms *perms) 251 { 252 int error; 253 254 *perms = nullperms; 255 error = label_compound_match(profile, label, stack, state, subns, 256 request, perms); 257 if (!error) 258 return error; 259 260 *perms = allperms; 261 return label_components_match(profile, label, stack, state, subns, 262 request, perms); 263 } 264 265 /******* end TODO: dedup *****/ 266 267 /** 268 * change_profile_perms - find permissions for change_profile 269 * @profile: the current profile (NOT NULL) 270 * @target: label to transition to (NOT NULL) 271 * @stack: whether this is a stacking request 272 * @request: requested perms 273 * @start: state to start matching in 274 * @perms: Returns computed perms (NOT NULL) 275 * 276 * 277 * Returns: permission set 278 * 279 * currently only matches full label A//&B//&C or individual components A, B, C 280 * not arbitrary combinations. Eg. A//&B, C 281 */ 282 static int change_profile_perms(struct aa_profile *profile, 283 struct aa_label *target, bool stack, 284 u32 request, aa_state_t start, 285 struct aa_perms *perms) 286 { 287 if (profile_unconfined(profile)) { 288 perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC; 289 perms->audit = perms->quiet = perms->kill = 0; 290 return 0; 291 } 292 293 /* TODO: add profile in ns screening */ 294 return label_match(profile, target, stack, start, true, request, perms); 295 } 296 297 /** 298 * aa_xattrs_match - check whether a file matches the xattrs defined in profile 299 * @bprm: binprm struct for the process to validate 300 * @profile: profile to match against (NOT NULL) 301 * @state: state to start match in 302 * 303 * Returns: number of extended attributes that matched, or < 0 on error 304 */ 305 static int aa_xattrs_match(const struct linux_binprm *bprm, 306 struct aa_profile *profile, aa_state_t state) 307 { 308 int i; 309 struct dentry *d; 310 char *value = NULL; 311 struct aa_attachment *attach = &profile->attach; 312 int size, value_size = 0, ret = attach->xattr_count; 313 314 if (!bprm || !attach->xattr_count) 315 return 0; 316 might_sleep(); 317 318 /* transition from exec match to xattr set */ 319 state = aa_dfa_outofband_transition(attach->xmatch->dfa, state); 320 d = bprm->file->f_path.dentry; 321 322 for (i = 0; i < attach->xattr_count; i++) { 323 size = vfs_getxattr_alloc(&nop_mnt_idmap, d, attach->xattrs[i], 324 &value, value_size, GFP_KERNEL); 325 if (size >= 0) { 326 u32 index, perm; 327 328 /* 329 * Check the xattr presence before value. This ensure 330 * that not present xattr can be distinguished from a 0 331 * length value or rule that matches any value 332 */ 333 state = aa_dfa_null_transition(attach->xmatch->dfa, 334 state); 335 /* Check xattr value */ 336 state = aa_dfa_match_len(attach->xmatch->dfa, state, 337 value, size); 338 index = ACCEPT_TABLE(attach->xmatch->dfa)[state]; 339 perm = attach->xmatch->perms[index].allow; 340 if (!(perm & MAY_EXEC)) { 341 ret = -EINVAL; 342 goto out; 343 } 344 } 345 /* transition to next element */ 346 state = aa_dfa_outofband_transition(attach->xmatch->dfa, state); 347 if (size < 0) { 348 /* 349 * No xattr match, so verify if transition to 350 * next element was valid. IFF so the xattr 351 * was optional. 352 */ 353 if (!state) { 354 ret = -EINVAL; 355 goto out; 356 } 357 /* don't count missing optional xattr as matched */ 358 ret--; 359 } 360 } 361 362 out: 363 kfree(value); 364 return ret; 365 } 366 367 /** 368 * find_attach - do attachment search for unconfined processes 369 * @bprm: binprm structure of transitioning task 370 * @ns: the current namespace (NOT NULL) 371 * @head: profile list to walk (NOT NULL) 372 * @name: to match against (NOT NULL) 373 * @info: info message if there was an error (NOT NULL) 374 * 375 * Do a linear search on the profiles in the list. There is a matching 376 * preference where an exact match is preferred over a name which uses 377 * expressions to match, and matching expressions with the greatest 378 * xmatch_len are preferred. 379 * 380 * Requires: @head not be shared or have appropriate locks held 381 * 382 * Returns: label or NULL if no match found 383 */ 384 static struct aa_label *find_attach(const struct linux_binprm *bprm, 385 struct aa_ns *ns, struct list_head *head, 386 const char *name, const char **info) 387 { 388 int candidate_len = 0, candidate_xattrs = 0; 389 bool conflict = false; 390 struct aa_profile *profile, *candidate = NULL; 391 392 AA_BUG(!name); 393 AA_BUG(!head); 394 395 rcu_read_lock(); 396 restart: 397 list_for_each_entry_rcu(profile, head, base.list) { 398 struct aa_attachment *attach = &profile->attach; 399 400 if (profile->label.flags & FLAG_NULL && 401 &profile->label == ns_unconfined(profile->ns)) 402 continue; 403 404 /* Find the "best" matching profile. Profiles must 405 * match the path and extended attributes (if any) 406 * associated with the file. A more specific path 407 * match will be preferred over a less specific one, 408 * and a match with more matching extended attributes 409 * will be preferred over one with fewer. If the best 410 * match has both the same level of path specificity 411 * and the same number of matching extended attributes 412 * as another profile, signal a conflict and refuse to 413 * match. 414 */ 415 if (attach->xmatch->dfa) { 416 unsigned int count; 417 aa_state_t state; 418 u32 index, perm; 419 420 state = aa_dfa_leftmatch(attach->xmatch->dfa, 421 attach->xmatch->start[AA_CLASS_XMATCH], 422 name, &count); 423 index = ACCEPT_TABLE(attach->xmatch->dfa)[state]; 424 perm = attach->xmatch->perms[index].allow; 425 /* any accepting state means a valid match. */ 426 if (perm & MAY_EXEC) { 427 int ret = 0; 428 429 if (count < candidate_len) 430 continue; 431 432 if (bprm && attach->xattr_count) { 433 long rev = READ_ONCE(ns->revision); 434 435 if (!aa_get_profile_not0(profile)) 436 goto restart; 437 rcu_read_unlock(); 438 ret = aa_xattrs_match(bprm, profile, 439 state); 440 rcu_read_lock(); 441 aa_put_profile(profile); 442 if (rev != 443 READ_ONCE(ns->revision)) 444 /* policy changed */ 445 goto restart; 446 /* 447 * Fail matching if the xattrs don't 448 * match 449 */ 450 if (ret < 0) 451 continue; 452 } 453 /* 454 * TODO: allow for more flexible best match 455 * 456 * The new match isn't more specific 457 * than the current best match 458 */ 459 if (count == candidate_len && 460 ret <= candidate_xattrs) { 461 /* Match is equivalent, so conflict */ 462 if (ret == candidate_xattrs) 463 conflict = true; 464 continue; 465 } 466 467 /* Either the same length with more matching 468 * xattrs, or a longer match 469 */ 470 candidate = profile; 471 candidate_len = max(count, attach->xmatch_len); 472 candidate_xattrs = ret; 473 conflict = false; 474 } 475 } else if (!strcmp(profile->base.name, name)) { 476 /* 477 * old exact non-re match, without conditionals such 478 * as xattrs. no more searching required 479 */ 480 candidate = profile; 481 goto out; 482 } 483 } 484 485 if (!candidate || conflict) { 486 if (conflict) 487 *info = "conflicting profile attachments"; 488 rcu_read_unlock(); 489 return NULL; 490 } 491 492 out: 493 candidate = aa_get_newest_profile(candidate); 494 rcu_read_unlock(); 495 496 return &candidate->label; 497 } 498 499 static const char *next_name(int xtype, const char *name) 500 { 501 return NULL; 502 } 503 504 /** 505 * x_table_lookup - lookup an x transition name via transition table 506 * @profile: current profile (NOT NULL) 507 * @xindex: index into x transition table 508 * @name: returns: name tested to find label (NOT NULL) 509 * 510 * Returns: refcounted label, or NULL on failure (MAYBE NULL) 511 */ 512 struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex, 513 const char **name) 514 { 515 struct aa_ruleset *rules = list_first_entry(&profile->rules, 516 typeof(*rules), list); 517 struct aa_label *label = NULL; 518 u32 xtype = xindex & AA_X_TYPE_MASK; 519 int index = xindex & AA_X_INDEX_MASK; 520 521 AA_BUG(!name); 522 523 /* index is guaranteed to be in range, validated at load time */ 524 /* TODO: move lookup parsing to unpack time so this is a straight 525 * index into the resultant label 526 */ 527 for (*name = rules->file->trans.table[index]; !label && *name; 528 *name = next_name(xtype, *name)) { 529 if (xindex & AA_X_CHILD) { 530 struct aa_profile *new_profile; 531 /* release by caller */ 532 new_profile = aa_find_child(profile, *name); 533 if (new_profile) 534 label = &new_profile->label; 535 continue; 536 } 537 label = aa_label_parse(&profile->label, *name, GFP_KERNEL, 538 true, false); 539 if (IS_ERR(label)) 540 label = NULL; 541 } 542 543 /* released by caller */ 544 545 return label; 546 } 547 548 /** 549 * x_to_label - get target label for a given xindex 550 * @profile: current profile (NOT NULL) 551 * @bprm: binprm structure of transitioning task 552 * @name: name to lookup (NOT NULL) 553 * @xindex: index into x transition table 554 * @lookupname: returns: name used in lookup if one was specified (NOT NULL) 555 * @info: info message if there was an error (NOT NULL) 556 * 557 * find label for a transition index 558 * 559 * Returns: refcounted label or NULL if not found available 560 */ 561 static struct aa_label *x_to_label(struct aa_profile *profile, 562 const struct linux_binprm *bprm, 563 const char *name, u32 xindex, 564 const char **lookupname, 565 const char **info) 566 { 567 struct aa_ruleset *rules = list_first_entry(&profile->rules, 568 typeof(*rules), list); 569 struct aa_label *new = NULL; 570 struct aa_ns *ns = profile->ns; 571 u32 xtype = xindex & AA_X_TYPE_MASK; 572 const char *stack = NULL; 573 574 switch (xtype) { 575 case AA_X_NONE: 576 /* fail exec unless ix || ux fallback - handled by caller */ 577 *lookupname = NULL; 578 break; 579 case AA_X_TABLE: 580 /* TODO: fix when perm mapping done at unload */ 581 stack = rules->file->trans.table[xindex & AA_X_INDEX_MASK]; 582 if (*stack != '&') { 583 /* released by caller */ 584 new = x_table_lookup(profile, xindex, lookupname); 585 stack = NULL; 586 break; 587 } 588 fallthrough; /* to X_NAME */ 589 case AA_X_NAME: 590 if (xindex & AA_X_CHILD) 591 /* released by caller */ 592 new = find_attach(bprm, ns, &profile->base.profiles, 593 name, info); 594 else 595 /* released by caller */ 596 new = find_attach(bprm, ns, &ns->base.profiles, 597 name, info); 598 *lookupname = name; 599 break; 600 } 601 602 if (!new) { 603 if (xindex & AA_X_INHERIT) { 604 /* (p|c|n)ix - don't change profile but do 605 * use the newest version 606 */ 607 *info = "ix fallback"; 608 /* no profile && no error */ 609 new = aa_get_newest_label(&profile->label); 610 } else if (xindex & AA_X_UNCONFINED) { 611 new = aa_get_newest_label(ns_unconfined(profile->ns)); 612 *info = "ux fallback"; 613 } 614 } 615 616 if (new && stack) { 617 /* base the stack on post domain transition */ 618 struct aa_label *base = new; 619 620 new = aa_label_parse(base, stack, GFP_KERNEL, true, false); 621 if (IS_ERR(new)) 622 new = NULL; 623 aa_put_label(base); 624 } 625 626 /* released by caller */ 627 return new; 628 } 629 630 static struct aa_label *profile_transition(const struct cred *subj_cred, 631 struct aa_profile *profile, 632 const struct linux_binprm *bprm, 633 char *buffer, struct path_cond *cond, 634 bool *secure_exec) 635 { 636 struct aa_ruleset *rules = list_first_entry(&profile->rules, 637 typeof(*rules), list); 638 struct aa_label *new = NULL; 639 const char *info = NULL, *name = NULL, *target = NULL; 640 aa_state_t state = rules->file->start[AA_CLASS_FILE]; 641 struct aa_perms perms = {}; 642 bool nonewprivs = false; 643 int error = 0; 644 645 AA_BUG(!profile); 646 AA_BUG(!bprm); 647 AA_BUG(!buffer); 648 649 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer, 650 &name, &info, profile->disconnected); 651 if (error) { 652 if (profile_unconfined(profile) || 653 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) { 654 AA_DEBUG("name lookup ix on error"); 655 error = 0; 656 new = aa_get_newest_label(&profile->label); 657 } 658 name = bprm->filename; 659 goto audit; 660 } 661 662 if (profile_unconfined(profile)) { 663 new = find_attach(bprm, profile->ns, 664 &profile->ns->base.profiles, name, &info); 665 if (new) { 666 AA_DEBUG("unconfined attached to new label"); 667 return new; 668 } 669 AA_DEBUG("unconfined exec no attachment"); 670 return aa_get_newest_label(&profile->label); 671 } 672 673 /* find exec permissions for name */ 674 state = aa_str_perms(rules->file, state, name, cond, &perms); 675 if (perms.allow & MAY_EXEC) { 676 /* exec permission determine how to transition */ 677 new = x_to_label(profile, bprm, name, perms.xindex, &target, 678 &info); 679 if (new && new->proxy == profile->label.proxy && info) { 680 /* hack ix fallback - improve how this is detected */ 681 goto audit; 682 } else if (!new) { 683 error = -EACCES; 684 info = "profile transition not found"; 685 /* remove MAY_EXEC to audit as failure */ 686 perms.allow &= ~MAY_EXEC; 687 } 688 } else if (COMPLAIN_MODE(profile)) { 689 /* no exec permission - learning mode */ 690 struct aa_profile *new_profile = NULL; 691 692 new_profile = aa_new_learning_profile(profile, false, name, 693 GFP_KERNEL); 694 if (!new_profile) { 695 error = -ENOMEM; 696 info = "could not create null profile"; 697 } else { 698 error = -EACCES; 699 new = &new_profile->label; 700 } 701 perms.xindex |= AA_X_UNSAFE; 702 } else 703 /* fail exec */ 704 error = -EACCES; 705 706 if (!new) 707 goto audit; 708 709 710 if (!(perms.xindex & AA_X_UNSAFE)) { 711 if (DEBUG_ON) { 712 dbg_printk("apparmor: scrubbing environment variables" 713 " for %s profile=", name); 714 aa_label_printk(new, GFP_KERNEL); 715 dbg_printk("\n"); 716 } 717 *secure_exec = true; 718 } 719 720 audit: 721 aa_audit_file(subj_cred, profile, &perms, OP_EXEC, MAY_EXEC, name, 722 target, new, 723 cond->uid, info, error); 724 if (!new || nonewprivs) { 725 aa_put_label(new); 726 return ERR_PTR(error); 727 } 728 729 return new; 730 } 731 732 static int profile_onexec(const struct cred *subj_cred, 733 struct aa_profile *profile, struct aa_label *onexec, 734 bool stack, const struct linux_binprm *bprm, 735 char *buffer, struct path_cond *cond, 736 bool *secure_exec) 737 { 738 struct aa_ruleset *rules = list_first_entry(&profile->rules, 739 typeof(*rules), list); 740 aa_state_t state = rules->file->start[AA_CLASS_FILE]; 741 struct aa_perms perms = {}; 742 const char *xname = NULL, *info = "change_profile onexec"; 743 int error = -EACCES; 744 745 AA_BUG(!profile); 746 AA_BUG(!onexec); 747 AA_BUG(!bprm); 748 AA_BUG(!buffer); 749 750 if (profile_unconfined(profile)) { 751 /* change_profile on exec already granted */ 752 /* 753 * NOTE: Domain transitions from unconfined are allowed 754 * even when no_new_privs is set because this aways results 755 * in a further reduction of permissions. 756 */ 757 return 0; 758 } 759 760 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer, 761 &xname, &info, profile->disconnected); 762 if (error) { 763 if (profile_unconfined(profile) || 764 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) { 765 AA_DEBUG("name lookup ix on error"); 766 error = 0; 767 } 768 xname = bprm->filename; 769 goto audit; 770 } 771 772 /* find exec permissions for name */ 773 state = aa_str_perms(rules->file, state, xname, cond, &perms); 774 if (!(perms.allow & AA_MAY_ONEXEC)) { 775 info = "no change_onexec valid for executable"; 776 goto audit; 777 } 778 /* test if this exec can be paired with change_profile onexec. 779 * onexec permission is linked to exec with a standard pairing 780 * exec\0change_profile 781 */ 782 state = aa_dfa_null_transition(rules->file->dfa, state); 783 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC, 784 state, &perms); 785 if (error) { 786 perms.allow &= ~AA_MAY_ONEXEC; 787 goto audit; 788 } 789 790 if (!(perms.xindex & AA_X_UNSAFE)) { 791 if (DEBUG_ON) { 792 dbg_printk("apparmor: scrubbing environment " 793 "variables for %s label=", xname); 794 aa_label_printk(onexec, GFP_KERNEL); 795 dbg_printk("\n"); 796 } 797 *secure_exec = true; 798 } 799 800 audit: 801 return aa_audit_file(subj_cred, profile, &perms, OP_EXEC, 802 AA_MAY_ONEXEC, xname, 803 NULL, onexec, cond->uid, info, error); 804 } 805 806 /* ensure none ns domain transitions are correctly applied with onexec */ 807 808 static struct aa_label *handle_onexec(const struct cred *subj_cred, 809 struct aa_label *label, 810 struct aa_label *onexec, bool stack, 811 const struct linux_binprm *bprm, 812 char *buffer, struct path_cond *cond, 813 bool *unsafe) 814 { 815 struct aa_profile *profile; 816 struct aa_label *new; 817 int error; 818 819 AA_BUG(!label); 820 AA_BUG(!onexec); 821 AA_BUG(!bprm); 822 AA_BUG(!buffer); 823 824 if (!stack) { 825 error = fn_for_each_in_ns(label, profile, 826 profile_onexec(subj_cred, profile, onexec, stack, 827 bprm, buffer, cond, unsafe)); 828 if (error) 829 return ERR_PTR(error); 830 new = fn_label_build_in_ns(label, profile, GFP_KERNEL, 831 aa_get_newest_label(onexec), 832 profile_transition(subj_cred, profile, bprm, 833 buffer, 834 cond, unsafe)); 835 836 } else { 837 /* TODO: determine how much we want to loosen this */ 838 error = fn_for_each_in_ns(label, profile, 839 profile_onexec(subj_cred, profile, onexec, stack, bprm, 840 buffer, cond, unsafe)); 841 if (error) 842 return ERR_PTR(error); 843 new = fn_label_build_in_ns(label, profile, GFP_KERNEL, 844 aa_label_merge(&profile->label, onexec, 845 GFP_KERNEL), 846 profile_transition(subj_cred, profile, bprm, 847 buffer, 848 cond, unsafe)); 849 } 850 851 if (new) 852 return new; 853 854 /* TODO: get rid of GLOBAL_ROOT_UID */ 855 error = fn_for_each_in_ns(label, profile, 856 aa_audit_file(subj_cred, profile, &nullperms, 857 OP_CHANGE_ONEXEC, 858 AA_MAY_ONEXEC, bprm->filename, NULL, 859 onexec, GLOBAL_ROOT_UID, 860 "failed to build target label", -ENOMEM)); 861 return ERR_PTR(error); 862 } 863 864 /** 865 * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct 866 * @bprm: binprm for the exec (NOT NULL) 867 * 868 * Returns: %0 or error on failure 869 * 870 * TODO: once the other paths are done see if we can't refactor into a fn 871 */ 872 int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm) 873 { 874 struct aa_task_ctx *ctx; 875 struct aa_label *label, *new = NULL; 876 const struct cred *subj_cred; 877 struct aa_profile *profile; 878 char *buffer = NULL; 879 const char *info = NULL; 880 int error = 0; 881 bool unsafe = false; 882 vfsuid_t vfsuid = i_uid_into_vfsuid(file_mnt_idmap(bprm->file), 883 file_inode(bprm->file)); 884 struct path_cond cond = { 885 vfsuid_into_kuid(vfsuid), 886 file_inode(bprm->file)->i_mode 887 }; 888 889 subj_cred = current_cred(); 890 ctx = task_ctx(current); 891 AA_BUG(!cred_label(bprm->cred)); 892 AA_BUG(!ctx); 893 894 label = aa_get_newest_label(cred_label(bprm->cred)); 895 896 /* 897 * Detect no new privs being set, and store the label it 898 * occurred under. Ideally this would happen when nnp 899 * is set but there isn't a good way to do that yet. 900 * 901 * Testing for unconfined must be done before the subset test 902 */ 903 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) && 904 !ctx->nnp) 905 ctx->nnp = aa_get_label(label); 906 907 /* buffer freed below, name is pointer into buffer */ 908 buffer = aa_get_buffer(false); 909 if (!buffer) { 910 error = -ENOMEM; 911 goto done; 912 } 913 914 /* Test for onexec first as onexec override other x transitions. */ 915 if (ctx->onexec) 916 new = handle_onexec(subj_cred, label, ctx->onexec, ctx->token, 917 bprm, buffer, &cond, &unsafe); 918 else 919 new = fn_label_build(label, profile, GFP_KERNEL, 920 profile_transition(subj_cred, profile, bprm, 921 buffer, 922 &cond, &unsafe)); 923 924 AA_BUG(!new); 925 if (IS_ERR(new)) { 926 error = PTR_ERR(new); 927 goto done; 928 } else if (!new) { 929 error = -ENOMEM; 930 goto done; 931 } 932 933 /* Policy has specified a domain transitions. If no_new_privs and 934 * confined ensure the transition is to confinement that is subset 935 * of the confinement when the task entered no new privs. 936 * 937 * NOTE: Domain transitions from unconfined and to stacked 938 * subsets are allowed even when no_new_privs is set because this 939 * aways results in a further reduction of permissions. 940 */ 941 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && 942 !unconfined(label) && 943 !aa_label_is_unconfined_subset(new, ctx->nnp)) { 944 error = -EPERM; 945 info = "no new privs"; 946 goto audit; 947 } 948 949 if (bprm->unsafe & LSM_UNSAFE_SHARE) { 950 /* FIXME: currently don't mediate shared state */ 951 ; 952 } 953 954 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) { 955 /* TODO: test needs to be profile of label to new */ 956 error = may_change_ptraced_domain(bprm->cred, new, &info); 957 if (error) 958 goto audit; 959 } 960 961 if (unsafe) { 962 if (DEBUG_ON) { 963 dbg_printk("scrubbing environment variables for %s " 964 "label=", bprm->filename); 965 aa_label_printk(new, GFP_KERNEL); 966 dbg_printk("\n"); 967 } 968 bprm->secureexec = 1; 969 } 970 971 if (label->proxy != new->proxy) { 972 /* when transitioning clear unsafe personality bits */ 973 if (DEBUG_ON) { 974 dbg_printk("apparmor: clearing unsafe personality " 975 "bits. %s label=", bprm->filename); 976 aa_label_printk(new, GFP_KERNEL); 977 dbg_printk("\n"); 978 } 979 bprm->per_clear |= PER_CLEAR_ON_SETID; 980 } 981 aa_put_label(cred_label(bprm->cred)); 982 /* transfer reference, released when cred is freed */ 983 set_cred_label(bprm->cred, new); 984 985 done: 986 aa_put_label(label); 987 aa_put_buffer(buffer); 988 989 return error; 990 991 audit: 992 error = fn_for_each(label, profile, 993 aa_audit_file(current_cred(), profile, &nullperms, 994 OP_EXEC, MAY_EXEC, 995 bprm->filename, NULL, new, 996 vfsuid_into_kuid(vfsuid), info, error)); 997 aa_put_label(new); 998 goto done; 999 } 1000 1001 /* 1002 * Functions for self directed profile change 1003 */ 1004 1005 1006 /* helper fn for change_hat 1007 * 1008 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL 1009 */ 1010 static struct aa_label *build_change_hat(const struct cred *subj_cred, 1011 struct aa_profile *profile, 1012 const char *name, bool sibling) 1013 { 1014 struct aa_profile *root, *hat = NULL; 1015 const char *info = NULL; 1016 int error = 0; 1017 1018 if (sibling && PROFILE_IS_HAT(profile)) { 1019 root = aa_get_profile_rcu(&profile->parent); 1020 } else if (!sibling && !PROFILE_IS_HAT(profile)) { 1021 root = aa_get_profile(profile); 1022 } else { 1023 info = "conflicting target types"; 1024 error = -EPERM; 1025 goto audit; 1026 } 1027 1028 hat = aa_find_child(root, name); 1029 if (!hat) { 1030 error = -ENOENT; 1031 if (COMPLAIN_MODE(profile)) { 1032 hat = aa_new_learning_profile(profile, true, name, 1033 GFP_KERNEL); 1034 if (!hat) { 1035 info = "failed null profile create"; 1036 error = -ENOMEM; 1037 } 1038 } 1039 } 1040 aa_put_profile(root); 1041 1042 audit: 1043 aa_audit_file(subj_cred, profile, &nullperms, OP_CHANGE_HAT, 1044 AA_MAY_CHANGEHAT, 1045 name, hat ? hat->base.hname : NULL, 1046 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info, 1047 error); 1048 if (!hat || (error && error != -ENOENT)) 1049 return ERR_PTR(error); 1050 /* if hat && error - complain mode, already audited and we adjust for 1051 * complain mode allow by returning hat->label 1052 */ 1053 return &hat->label; 1054 } 1055 1056 /* helper fn for changing into a hat 1057 * 1058 * Returns: label for hat transition or ERR_PTR. Does not return NULL 1059 */ 1060 static struct aa_label *change_hat(const struct cred *subj_cred, 1061 struct aa_label *label, const char *hats[], 1062 int count, int flags) 1063 { 1064 struct aa_profile *profile, *root, *hat = NULL; 1065 struct aa_label *new; 1066 struct label_it it; 1067 bool sibling = false; 1068 const char *name, *info = NULL; 1069 int i, error; 1070 1071 AA_BUG(!label); 1072 AA_BUG(!hats); 1073 AA_BUG(count < 1); 1074 1075 if (PROFILE_IS_HAT(labels_profile(label))) 1076 sibling = true; 1077 1078 /*find first matching hat */ 1079 for (i = 0; i < count && !hat; i++) { 1080 name = hats[i]; 1081 label_for_each_in_ns(it, labels_ns(label), label, profile) { 1082 if (sibling && PROFILE_IS_HAT(profile)) { 1083 root = aa_get_profile_rcu(&profile->parent); 1084 } else if (!sibling && !PROFILE_IS_HAT(profile)) { 1085 root = aa_get_profile(profile); 1086 } else { /* conflicting change type */ 1087 info = "conflicting targets types"; 1088 error = -EPERM; 1089 goto fail; 1090 } 1091 hat = aa_find_child(root, name); 1092 aa_put_profile(root); 1093 if (!hat) { 1094 if (!COMPLAIN_MODE(profile)) 1095 goto outer_continue; 1096 /* complain mode succeed as if hat */ 1097 } else if (!PROFILE_IS_HAT(hat)) { 1098 info = "target not hat"; 1099 error = -EPERM; 1100 aa_put_profile(hat); 1101 goto fail; 1102 } 1103 aa_put_profile(hat); 1104 } 1105 /* found a hat for all profiles in ns */ 1106 goto build; 1107 outer_continue: 1108 ; 1109 } 1110 /* no hats that match, find appropriate error 1111 * 1112 * In complain mode audit of the failure is based off of the first 1113 * hat supplied. This is done due how userspace interacts with 1114 * change_hat. 1115 */ 1116 name = NULL; 1117 label_for_each_in_ns(it, labels_ns(label), label, profile) { 1118 if (!list_empty(&profile->base.profiles)) { 1119 info = "hat not found"; 1120 error = -ENOENT; 1121 goto fail; 1122 } 1123 } 1124 info = "no hats defined"; 1125 error = -ECHILD; 1126 1127 fail: 1128 label_for_each_in_ns(it, labels_ns(label), label, profile) { 1129 /* 1130 * no target as it has failed to be found or built 1131 * 1132 * change_hat uses probing and should not log failures 1133 * related to missing hats 1134 */ 1135 /* TODO: get rid of GLOBAL_ROOT_UID */ 1136 if (count > 1 || COMPLAIN_MODE(profile)) { 1137 aa_audit_file(subj_cred, profile, &nullperms, 1138 OP_CHANGE_HAT, 1139 AA_MAY_CHANGEHAT, name, NULL, NULL, 1140 GLOBAL_ROOT_UID, info, error); 1141 } 1142 } 1143 return ERR_PTR(error); 1144 1145 build: 1146 new = fn_label_build_in_ns(label, profile, GFP_KERNEL, 1147 build_change_hat(subj_cred, profile, name, 1148 sibling), 1149 aa_get_label(&profile->label)); 1150 if (!new) { 1151 info = "label build failed"; 1152 error = -ENOMEM; 1153 goto fail; 1154 } /* else if (IS_ERR) build_change_hat has logged error so return new */ 1155 1156 return new; 1157 } 1158 1159 /** 1160 * aa_change_hat - change hat to/from subprofile 1161 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0) 1162 * @count: number of hat names in @hats 1163 * @token: magic value to validate the hat change 1164 * @flags: flags affecting behavior of the change 1165 * 1166 * Returns %0 on success, error otherwise. 1167 * 1168 * Change to the first profile specified in @hats that exists, and store 1169 * the @hat_magic in the current task context. If the count == 0 and the 1170 * @token matches that stored in the current task context, return to the 1171 * top level profile. 1172 * 1173 * change_hat only applies to profiles in the current ns, and each profile 1174 * in the ns must make the same transition otherwise change_hat will fail. 1175 */ 1176 int aa_change_hat(const char *hats[], int count, u64 token, int flags) 1177 { 1178 const struct cred *subj_cred; 1179 struct aa_task_ctx *ctx = task_ctx(current); 1180 struct aa_label *label, *previous, *new = NULL, *target = NULL; 1181 struct aa_profile *profile; 1182 struct aa_perms perms = {}; 1183 const char *info = NULL; 1184 int error = 0; 1185 1186 /* released below */ 1187 subj_cred = get_current_cred(); 1188 label = aa_get_newest_cred_label(subj_cred); 1189 previous = aa_get_newest_label(ctx->previous); 1190 1191 /* 1192 * Detect no new privs being set, and store the label it 1193 * occurred under. Ideally this would happen when nnp 1194 * is set but there isn't a good way to do that yet. 1195 * 1196 * Testing for unconfined must be done before the subset test 1197 */ 1198 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp) 1199 ctx->nnp = aa_get_label(label); 1200 1201 if (unconfined(label)) { 1202 info = "unconfined can not change_hat"; 1203 error = -EPERM; 1204 goto fail; 1205 } 1206 1207 if (count) { 1208 new = change_hat(subj_cred, label, hats, count, flags); 1209 AA_BUG(!new); 1210 if (IS_ERR(new)) { 1211 error = PTR_ERR(new); 1212 new = NULL; 1213 /* already audited */ 1214 goto out; 1215 } 1216 1217 /* target cred is the same as current except new label */ 1218 error = may_change_ptraced_domain(subj_cred, new, &info); 1219 if (error) 1220 goto fail; 1221 1222 /* 1223 * no new privs prevents domain transitions that would 1224 * reduce restrictions. 1225 */ 1226 if (task_no_new_privs(current) && !unconfined(label) && 1227 !aa_label_is_unconfined_subset(new, ctx->nnp)) { 1228 /* not an apparmor denial per se, so don't log it */ 1229 AA_DEBUG("no_new_privs - change_hat denied"); 1230 error = -EPERM; 1231 goto out; 1232 } 1233 1234 if (flags & AA_CHANGE_TEST) 1235 goto out; 1236 1237 target = new; 1238 error = aa_set_current_hat(new, token); 1239 if (error == -EACCES) 1240 /* kill task in case of brute force attacks */ 1241 goto kill; 1242 } else if (previous && !(flags & AA_CHANGE_TEST)) { 1243 /* 1244 * no new privs prevents domain transitions that would 1245 * reduce restrictions. 1246 */ 1247 if (task_no_new_privs(current) && !unconfined(label) && 1248 !aa_label_is_unconfined_subset(previous, ctx->nnp)) { 1249 /* not an apparmor denial per se, so don't log it */ 1250 AA_DEBUG("no_new_privs - change_hat denied"); 1251 error = -EPERM; 1252 goto out; 1253 } 1254 1255 /* Return to saved label. Kill task if restore fails 1256 * to avoid brute force attacks 1257 */ 1258 target = previous; 1259 error = aa_restore_previous_label(token); 1260 if (error) { 1261 if (error == -EACCES) 1262 goto kill; 1263 goto fail; 1264 } 1265 } /* else ignore @flags && restores when there is no saved profile */ 1266 1267 out: 1268 aa_put_label(new); 1269 aa_put_label(previous); 1270 aa_put_label(label); 1271 put_cred(subj_cred); 1272 1273 return error; 1274 1275 kill: 1276 info = "failed token match"; 1277 perms.kill = AA_MAY_CHANGEHAT; 1278 1279 fail: 1280 fn_for_each_in_ns(label, profile, 1281 aa_audit_file(subj_cred, profile, &perms, OP_CHANGE_HAT, 1282 AA_MAY_CHANGEHAT, NULL, NULL, target, 1283 GLOBAL_ROOT_UID, info, error)); 1284 1285 goto out; 1286 } 1287 1288 1289 static int change_profile_perms_wrapper(const char *op, const char *name, 1290 const struct cred *subj_cred, 1291 struct aa_profile *profile, 1292 struct aa_label *target, bool stack, 1293 u32 request, struct aa_perms *perms) 1294 { 1295 struct aa_ruleset *rules = list_first_entry(&profile->rules, 1296 typeof(*rules), list); 1297 const char *info = NULL; 1298 int error = 0; 1299 1300 if (!error) 1301 error = change_profile_perms(profile, target, stack, request, 1302 rules->file->start[AA_CLASS_FILE], 1303 perms); 1304 if (error) 1305 error = aa_audit_file(subj_cred, profile, perms, op, request, 1306 name, 1307 NULL, target, GLOBAL_ROOT_UID, info, 1308 error); 1309 1310 return error; 1311 } 1312 1313 static const char *stack_msg = "change_profile unprivileged unconfined converted to stacking"; 1314 1315 /** 1316 * aa_change_profile - perform a one-way profile transition 1317 * @fqname: name of profile may include namespace (NOT NULL) 1318 * @flags: flags affecting change behavior 1319 * 1320 * Change to new profile @name. Unlike with hats, there is no way 1321 * to change back. If @name isn't specified the current profile name is 1322 * used. 1323 * If @onexec then the transition is delayed until 1324 * the next exec. 1325 * 1326 * Returns %0 on success, error otherwise. 1327 */ 1328 int aa_change_profile(const char *fqname, int flags) 1329 { 1330 struct aa_label *label, *new = NULL, *target = NULL; 1331 struct aa_profile *profile; 1332 struct aa_perms perms = {}; 1333 const char *info = NULL; 1334 const char *auditname = fqname; /* retain leading & if stack */ 1335 bool stack = flags & AA_CHANGE_STACK; 1336 struct aa_task_ctx *ctx = task_ctx(current); 1337 const struct cred *subj_cred = get_current_cred(); 1338 int error = 0; 1339 char *op; 1340 u32 request; 1341 1342 label = aa_get_current_label(); 1343 1344 /* 1345 * Detect no new privs being set, and store the label it 1346 * occurred under. Ideally this would happen when nnp 1347 * is set but there isn't a good way to do that yet. 1348 * 1349 * Testing for unconfined must be done before the subset test 1350 */ 1351 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp) 1352 ctx->nnp = aa_get_label(label); 1353 1354 if (!fqname || !*fqname) { 1355 aa_put_label(label); 1356 AA_DEBUG("no profile name"); 1357 return -EINVAL; 1358 } 1359 1360 if (flags & AA_CHANGE_ONEXEC) { 1361 request = AA_MAY_ONEXEC; 1362 if (stack) 1363 op = OP_STACK_ONEXEC; 1364 else 1365 op = OP_CHANGE_ONEXEC; 1366 } else { 1367 request = AA_MAY_CHANGE_PROFILE; 1368 if (stack) 1369 op = OP_STACK; 1370 else 1371 op = OP_CHANGE_PROFILE; 1372 } 1373 1374 /* This should move to a per profile test. Requires pushing build 1375 * into callback 1376 */ 1377 if (!stack && unconfined(label) && 1378 label == &labels_ns(label)->unconfined->label && 1379 aa_unprivileged_unconfined_restricted && 1380 /* TODO: refactor so this check is a fn */ 1381 cap_capable(current_cred(), &init_user_ns, CAP_MAC_OVERRIDE, 1382 CAP_OPT_NOAUDIT)) { 1383 /* regardless of the request in this case apparmor 1384 * stacks against unconfined so admin set policy can't be 1385 * by-passed 1386 */ 1387 stack = true; 1388 perms.audit = request; 1389 (void) fn_for_each_in_ns(label, profile, 1390 aa_audit_file(subj_cred, profile, &perms, op, 1391 request, auditname, NULL, target, 1392 GLOBAL_ROOT_UID, stack_msg, 0)); 1393 perms.audit = 0; 1394 } 1395 1396 if (*fqname == '&') { 1397 stack = true; 1398 /* don't have label_parse() do stacking */ 1399 fqname++; 1400 } 1401 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false); 1402 if (IS_ERR(target)) { 1403 struct aa_profile *tprofile; 1404 1405 info = "label not found"; 1406 error = PTR_ERR(target); 1407 target = NULL; 1408 /* 1409 * TODO: fixme using labels_profile is not right - do profile 1410 * per complain profile 1411 */ 1412 if ((flags & AA_CHANGE_TEST) || 1413 !COMPLAIN_MODE(labels_profile(label))) 1414 goto audit; 1415 /* released below */ 1416 tprofile = aa_new_learning_profile(labels_profile(label), false, 1417 fqname, GFP_KERNEL); 1418 if (!tprofile) { 1419 info = "failed null profile create"; 1420 error = -ENOMEM; 1421 goto audit; 1422 } 1423 target = &tprofile->label; 1424 goto check; 1425 } 1426 1427 /* 1428 * self directed transitions only apply to current policy ns 1429 * TODO: currently requiring perms for stacking and straight change 1430 * stacking doesn't strictly need this. Determine how much 1431 * we want to loosen this restriction for stacking 1432 * 1433 * if (!stack) { 1434 */ 1435 error = fn_for_each_in_ns(label, profile, 1436 change_profile_perms_wrapper(op, auditname, 1437 subj_cred, 1438 profile, target, stack, 1439 request, &perms)); 1440 if (error) 1441 /* auditing done in change_profile_perms_wrapper */ 1442 goto out; 1443 1444 /* } */ 1445 1446 check: 1447 /* check if tracing task is allowed to trace target domain */ 1448 error = may_change_ptraced_domain(subj_cred, target, &info); 1449 if (error && !fn_for_each_in_ns(label, profile, 1450 COMPLAIN_MODE(profile))) 1451 goto audit; 1452 1453 /* TODO: add permission check to allow this 1454 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) { 1455 * info = "not a single threaded task"; 1456 * error = -EACCES; 1457 * goto audit; 1458 * } 1459 */ 1460 if (flags & AA_CHANGE_TEST) 1461 goto out; 1462 1463 /* stacking is always a subset, so only check the nonstack case */ 1464 if (!stack) { 1465 new = fn_label_build_in_ns(label, profile, GFP_KERNEL, 1466 aa_get_label(target), 1467 aa_get_label(&profile->label)); 1468 /* 1469 * no new privs prevents domain transitions that would 1470 * reduce restrictions. 1471 */ 1472 if (task_no_new_privs(current) && !unconfined(label) && 1473 !aa_label_is_unconfined_subset(new, ctx->nnp)) { 1474 /* not an apparmor denial per se, so don't log it */ 1475 AA_DEBUG("no_new_privs - change_hat denied"); 1476 error = -EPERM; 1477 goto out; 1478 } 1479 } 1480 1481 if (!(flags & AA_CHANGE_ONEXEC)) { 1482 /* only transition profiles in the current ns */ 1483 if (stack) 1484 new = aa_label_merge(label, target, GFP_KERNEL); 1485 if (IS_ERR_OR_NULL(new)) { 1486 info = "failed to build target label"; 1487 if (!new) 1488 error = -ENOMEM; 1489 else 1490 error = PTR_ERR(new); 1491 new = NULL; 1492 perms.allow = 0; 1493 goto audit; 1494 } 1495 error = aa_replace_current_label(new); 1496 } else { 1497 if (new) { 1498 aa_put_label(new); 1499 new = NULL; 1500 } 1501 1502 /* full transition will be built in exec path */ 1503 aa_set_current_onexec(target, stack); 1504 } 1505 1506 audit: 1507 error = fn_for_each_in_ns(label, profile, 1508 aa_audit_file(subj_cred, 1509 profile, &perms, op, request, auditname, 1510 NULL, new ? new : target, 1511 GLOBAL_ROOT_UID, info, error)); 1512 1513 out: 1514 aa_put_label(new); 1515 aa_put_label(target); 1516 aa_put_label(label); 1517 put_cred(subj_cred); 1518 1519 return error; 1520 } 1521