1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor policy manipulation functions 6 * 7 * Copyright (C) 1998-2008 Novell/SUSE 8 * Copyright 2009-2010 Canonical Ltd. 9 * 10 * AppArmor policy is based around profiles, which contain the rules a 11 * task is confined by. Every task in the system has a profile attached 12 * to it determined either by matching "unconfined" tasks against the 13 * visible set of profiles or by following a profiles attachment rules. 14 * 15 * Each profile exists in a profile namespace which is a container of 16 * visible profiles. Each namespace contains a special "unconfined" profile, 17 * which doesn't enforce any confinement on a task beyond DAC. 18 * 19 * Namespace and profile names can be written together in either 20 * of two syntaxes. 21 * :namespace:profile - used by kernel interfaces for easy detection 22 * namespace://profile - used by policy 23 * 24 * Profile names can not start with : or @ or ^ and may not contain \0 25 * 26 * Reserved profile names 27 * unconfined - special automatically generated unconfined profile 28 * inherit - special name to indicate profile inheritance 29 * null-XXXX-YYYY - special automatically generated learning profiles 30 * 31 * Namespace names may not start with / or @ and may not contain \0 or : 32 * Reserved namespace names 33 * user-XXXX - user defined profiles 34 * 35 * a // in a profile or namespace name indicates a hierarchical name with the 36 * name before the // being the parent and the name after the child. 37 * 38 * Profile and namespace hierarchies serve two different but similar purposes. 39 * The namespace contains the set of visible profiles that are considered 40 * for attachment. The hierarchy of namespaces allows for virtualizing 41 * the namespace so that for example a chroot can have its own set of profiles 42 * which may define some local user namespaces. 43 * The profile hierarchy severs two distinct purposes, 44 * - it allows for sub profiles or hats, which allows an application to run 45 * subprograms under its own profile with different restriction than it 46 * self, and not have it use the system profile. 47 * eg. if a mail program starts an editor, the policy might make the 48 * restrictions tighter on the editor tighter than the mail program, 49 * and definitely different than general editor restrictions 50 * - it allows for binary hierarchy of profiles, so that execution history 51 * is preserved. This feature isn't exploited by AppArmor reference policy 52 * but is allowed. NOTE: this is currently suboptimal because profile 53 * aliasing is not currently implemented so that a profile for each 54 * level must be defined. 55 * eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started 56 * from /bin/bash 57 * 58 * A profile or namespace name that can contain one or more // separators 59 * is referred to as an hname (hierarchical). 60 * eg. /bin/bash//bin/ls 61 * 62 * An fqname is a name that may contain both namespace and profile hnames. 63 * eg. :ns:/bin/bash//bin/ls 64 * 65 * NOTES: 66 * - locking of profile lists is currently fairly coarse. All profile 67 * lists within a namespace use the namespace lock. 68 * FIXME: move profile lists to using rcu_lists 69 */ 70 71 #include <linux/slab.h> 72 #include <linux/spinlock.h> 73 #include <linux/string.h> 74 #include <linux/cred.h> 75 #include <linux/rculist.h> 76 #include <linux/user_namespace.h> 77 78 #include "include/apparmor.h" 79 #include "include/capability.h" 80 #include "include/cred.h" 81 #include "include/file.h" 82 #include "include/ipc.h" 83 #include "include/match.h" 84 #include "include/path.h" 85 #include "include/policy.h" 86 #include "include/policy_ns.h" 87 #include "include/policy_unpack.h" 88 #include "include/resource.h" 89 90 int unprivileged_userns_apparmor_policy = 1; 91 int aa_unprivileged_unconfined_restricted; 92 93 const char *const aa_profile_mode_names[] = { 94 "enforce", 95 "complain", 96 "kill", 97 "unconfined", 98 "user", 99 }; 100 101 void aa_destroy_tags(struct aa_tags_struct *tags) 102 { 103 kfree_sensitive(tags->hdrs.table); 104 kfree_sensitive(tags->sets.table); 105 aa_destroy_str_table(&tags->strs); 106 memset(tags, 0, sizeof(*tags)); 107 } 108 109 static void aa_free_pdb(struct aa_policydb *pdb) 110 { 111 if (pdb) { 112 aa_put_dfa(pdb->dfa); 113 kvfree(pdb->perms); 114 aa_destroy_str_table(&pdb->trans); 115 aa_destroy_tags(&pdb->tags); 116 kfree(pdb); 117 } 118 } 119 120 /** 121 * aa_pdb_free_kref - free aa_policydb by kref (called by aa_put_pdb) 122 * @kref: kref callback for freeing of a dfa (NOT NULL) 123 */ 124 void aa_pdb_free_kref(struct kref *kref) 125 { 126 struct aa_policydb *pdb = container_of(kref, struct aa_policydb, count); 127 128 aa_free_pdb(pdb); 129 } 130 131 132 struct aa_policydb *aa_alloc_pdb(gfp_t gfp) 133 { 134 struct aa_policydb *pdb = kzalloc_obj(struct aa_policydb, gfp); 135 136 if (!pdb) 137 return NULL; 138 139 kref_init(&pdb->count); 140 141 return pdb; 142 } 143 144 145 /** 146 * __add_profile - add a profiles to list and label tree 147 * @list: list to add it to (NOT NULL) 148 * @profile: the profile to add (NOT NULL) 149 * 150 * refcount @profile, should be put by __list_remove_profile 151 * 152 * Requires: namespace lock be held, or list not be shared 153 */ 154 static void __add_profile(struct list_head *list, struct aa_profile *profile) 155 { 156 struct aa_label *l; 157 158 AA_BUG(!list); 159 AA_BUG(!profile); 160 AA_BUG(!profile->ns); 161 AA_BUG(!mutex_is_locked(&profile->ns->lock)); 162 163 list_add_rcu(&profile->base.list, list); 164 /* get list reference */ 165 aa_get_profile(profile); 166 l = aa_label_insert(&profile->ns->labels, &profile->label); 167 AA_BUG(l != &profile->label); 168 aa_put_label(l); 169 } 170 171 /** 172 * __list_remove_profile - remove a profile from the list it is on 173 * @profile: the profile to remove (NOT NULL) 174 * 175 * remove a profile from the list, warning generally removal should 176 * be done with __replace_profile as most profile removals are 177 * replacements to the unconfined profile. 178 * 179 * put @profile list refcount 180 * 181 * Requires: namespace lock be held, or list not have been live 182 */ 183 static void __list_remove_profile(struct aa_profile *profile) 184 { 185 AA_BUG(!profile); 186 AA_BUG(!profile->ns); 187 AA_BUG(!mutex_is_locked(&profile->ns->lock)); 188 189 list_del_rcu(&profile->base.list); 190 aa_put_profile(profile); 191 } 192 193 /** 194 * __remove_profile - remove old profile, and children 195 * @profile: profile to be replaced (NOT NULL) 196 * 197 * Requires: namespace list lock be held, or list not be shared 198 */ 199 static void __remove_profile(struct aa_profile *profile) 200 { 201 AA_BUG(!profile); 202 AA_BUG(!profile->ns); 203 AA_BUG(!mutex_is_locked(&profile->ns->lock)); 204 205 /* release any children lists first */ 206 __aa_profile_list_release(&profile->base.profiles); 207 /* released by free_profile */ 208 aa_label_remove(&profile->label); 209 __aafs_profile_rmdir(profile); 210 __list_remove_profile(profile); 211 } 212 213 /** 214 * __aa_profile_list_release - remove all profiles on the list and put refs 215 * @head: list of profiles (NOT NULL) 216 * 217 * Requires: namespace lock be held 218 */ 219 void __aa_profile_list_release(struct list_head *head) 220 { 221 struct aa_profile *profile, *tmp; 222 list_for_each_entry_safe(profile, tmp, head, base.list) 223 __remove_profile(profile); 224 } 225 226 /** 227 * aa_free_data - free a data blob 228 * @ptr: data to free 229 * @arg: unused 230 */ 231 static void aa_free_data(void *ptr, void *arg) 232 { 233 struct aa_data *data = ptr; 234 235 if (!ptr) 236 return; 237 238 kvfree_sensitive(data->data, data->size); 239 kfree_sensitive(data->key); 240 kfree_sensitive(data); 241 } 242 243 static void free_attachment(struct aa_attachment *attach) 244 { 245 int i; 246 247 if (!attach) 248 return; 249 250 for (i = 0; i < attach->xattr_count; i++) 251 kfree_sensitive(attach->xattrs[i]); 252 kfree_sensitive(attach->xattrs); 253 aa_put_pdb(attach->xmatch); 254 } 255 256 static void free_ruleset(struct aa_ruleset *rules) 257 { 258 int i; 259 260 if (!rules) 261 return; 262 263 aa_put_pdb(rules->file); 264 aa_put_pdb(rules->policy); 265 aa_free_cap_rules(&rules->caps); 266 aa_free_rlimit_rules(&rules->rlimits); 267 268 for (i = 0; i < rules->secmark_count; i++) 269 kfree_sensitive(rules->secmark[i].label); 270 kfree_sensitive(rules->secmark); 271 kfree_sensitive(rules); 272 } 273 274 struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp) 275 { 276 struct aa_ruleset *rules; 277 278 rules = kzalloc_obj(*rules, gfp); 279 280 return rules; 281 } 282 283 /** 284 * aa_free_profile - free a profile 285 * @profile: the profile to free (MAYBE NULL) 286 * 287 * Free a profile, its hats and null_profile. All references to the profile, 288 * its hats and null_profile must have been put. 289 * 290 * If the profile was referenced from a task context, free_profile() will 291 * be called from an rcu callback routine, so we must not sleep here. 292 */ 293 void aa_free_profile(struct aa_profile *profile) 294 { 295 struct rhashtable *rht; 296 297 AA_DEBUG(DEBUG_POLICY, "%s(%p)\n", __func__, profile); 298 299 if (!profile) 300 return; 301 302 /* free children profiles */ 303 aa_policy_destroy(&profile->base); 304 aa_put_profile(rcu_access_pointer(profile->parent)); 305 306 aa_put_ns(profile->ns); 307 kfree_sensitive(profile->rename); 308 kfree_sensitive(profile->disconnected); 309 310 free_attachment(&profile->attach); 311 312 /* 313 * at this point there are no tasks that can have a reference 314 * to rules 315 */ 316 for (int i = 0; i < profile->n_rules; i++) 317 free_ruleset(profile->label.rules[i]); 318 319 kfree_sensitive(profile->dirname); 320 321 if (profile->data) { 322 rht = profile->data; 323 profile->data = NULL; 324 rhashtable_free_and_destroy(rht, aa_free_data, NULL); 325 kfree_sensitive(rht); 326 } 327 328 kfree_sensitive(profile->hash); 329 aa_put_loaddata(profile->rawdata); 330 aa_label_destroy(&profile->label); 331 332 kfree_sensitive(profile); 333 } 334 335 /** 336 * aa_alloc_profile - allocate, initialize and return a new profile 337 * @hname: name of the profile (NOT NULL) 338 * @proxy: proxy to use OR null if to allocate a new one 339 * @gfp: allocation type 340 * 341 * Returns: refcount profile or NULL on failure 342 */ 343 struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy, 344 gfp_t gfp) 345 { 346 struct aa_profile *profile; 347 348 /* freed by free_profile - usually through aa_put_profile 349 * this adds space for a single ruleset in the rules section of the 350 * label 351 */ 352 profile = kzalloc_flex(*profile, label.rules, 1, gfp); 353 if (!profile) 354 return NULL; 355 356 if (!aa_policy_init(&profile->base, NULL, hname, gfp)) 357 goto fail; 358 if (!aa_label_init(&profile->label, 1, gfp)) 359 goto fail; 360 361 /* allocate the first ruleset, but leave it empty */ 362 profile->label.rules[0] = aa_alloc_ruleset(gfp); 363 if (!profile->label.rules[0]) 364 goto fail; 365 profile->n_rules = 1; 366 367 /* update being set needed by fs interface */ 368 if (!proxy) { 369 proxy = aa_alloc_proxy(&profile->label, gfp); 370 if (!proxy) 371 goto fail; 372 } else 373 aa_get_proxy(proxy); 374 profile->label.proxy = proxy; 375 376 profile->label.hname = profile->base.hname; 377 profile->label.flags |= FLAG_PROFILE; 378 profile->label.vec[0] = profile; 379 380 profile->signal = SIGKILL; 381 /* refcount released by caller */ 382 return profile; 383 384 fail: 385 aa_free_profile(profile); 386 387 return NULL; 388 } 389 390 static inline bool ANY_RULE_MEDIATES(struct aa_profile *profile, 391 unsigned char class) 392 { 393 int i; 394 395 for (i = 0; i < profile->n_rules; i++) { 396 if (RULE_MEDIATES(profile->label.rules[i], class)) 397 return true; 398 } 399 return false; 400 } 401 402 /* set of rules that are mediated by unconfined */ 403 static int unconfined_mediates[] = { AA_CLASS_NS, AA_CLASS_IO_URING, 0 }; 404 405 /* must be called after profile rulesets and start information is setup */ 406 void aa_compute_profile_mediates(struct aa_profile *profile) 407 { 408 int c; 409 410 if (profile_unconfined(profile)) { 411 int *pos; 412 413 for (pos = unconfined_mediates; *pos; pos++) { 414 if (ANY_RULE_MEDIATES(profile, *pos)) 415 profile->label.mediates |= ((u64) 1) << AA_CLASS_NS; 416 } 417 return; 418 } 419 for (c = 0; c <= AA_CLASS_LAST; c++) { 420 if (ANY_RULE_MEDIATES(profile, c)) 421 profile->label.mediates |= ((u64) 1) << c; 422 } 423 } 424 425 /* TODO: profile accounting - setup in remove */ 426 427 /** 428 * __strn_find_child - find a profile on @head list using substring of @name 429 * @head: list to search (NOT NULL) 430 * @name: name of profile (NOT NULL) 431 * @len: length of @name substring to match 432 * 433 * Requires: rcu_read_lock be held 434 * 435 * Returns: unrefcounted profile ptr, or NULL if not found 436 */ 437 static struct aa_profile *__strn_find_child(struct list_head *head, 438 const char *name, int len) 439 { 440 return (struct aa_profile *)__policy_strn_find(head, name, len); 441 } 442 443 /** 444 * __find_child - find a profile on @head list with a name matching @name 445 * @head: list to search (NOT NULL) 446 * @name: name of profile (NOT NULL) 447 * 448 * Requires: rcu_read_lock be held 449 * 450 * Returns: unrefcounted profile ptr, or NULL if not found 451 */ 452 static struct aa_profile *__find_child(struct list_head *head, const char *name) 453 { 454 return __strn_find_child(head, name, strlen(name)); 455 } 456 457 /** 458 * aa_find_child - find a profile by @name in @parent 459 * @parent: profile to search (NOT NULL) 460 * @name: profile name to search for (NOT NULL) 461 * 462 * Returns: a refcounted profile or NULL if not found 463 */ 464 struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name) 465 { 466 struct aa_profile *profile; 467 468 rcu_read_lock(); 469 do { 470 profile = __find_child(&parent->base.profiles, name); 471 } while (profile && !aa_get_profile_not0(profile)); 472 rcu_read_unlock(); 473 474 /* refcount released by caller */ 475 return profile; 476 } 477 478 /** 479 * __lookup_parent - lookup the parent of a profile of name @hname 480 * @ns: namespace to lookup profile in (NOT NULL) 481 * @hname: hierarchical profile name to find parent of (NOT NULL) 482 * 483 * Lookups up the parent of a fully qualified profile name, the profile 484 * that matches hname does not need to exist, in general this 485 * is used to load a new profile. 486 * 487 * Requires: rcu_read_lock be held 488 * 489 * Returns: unrefcounted policy or NULL if not found 490 */ 491 static struct aa_policy *__lookup_parent(struct aa_ns *ns, 492 const char *hname) 493 { 494 struct aa_policy *policy; 495 struct aa_profile *profile = NULL; 496 char *split; 497 498 policy = &ns->base; 499 500 for (split = strstr(hname, "//"); split;) { 501 profile = __strn_find_child(&policy->profiles, hname, 502 split - hname); 503 if (!profile) 504 return NULL; 505 policy = &profile->base; 506 hname = split + 2; 507 split = strstr(hname, "//"); 508 } 509 if (!profile) 510 return &ns->base; 511 return &profile->base; 512 } 513 514 /** 515 * __create_missing_ancestors - create place holders for missing ancestors 516 * @ns: namespace to lookup profile in (NOT NULL) 517 * @hname: hierarchical profile name to find parent of (NOT NULL) 518 * @gfp: type of allocation. 519 * 520 * Requires: ns mutex lock held 521 * 522 * Return: unrefcounted parent policy on success or %NULL if error creating 523 * place holder profiles. 524 */ 525 static struct aa_policy *__create_missing_ancestors(struct aa_ns *ns, 526 const char *hname, 527 gfp_t gfp) 528 { 529 struct aa_policy *policy; 530 struct aa_profile *parent, *profile = NULL; 531 char *split; 532 533 AA_BUG(!ns); 534 AA_BUG(!hname); 535 536 policy = &ns->base; 537 538 for (split = strstr(hname, "//"); split;) { 539 parent = profile; 540 profile = __strn_find_child(&policy->profiles, hname, 541 split - hname); 542 if (!profile) { 543 const char *name = kstrndup(hname, split - hname, 544 gfp); 545 if (!name) 546 return NULL; 547 profile = aa_alloc_null(parent, name, gfp); 548 kfree(name); 549 if (!profile) 550 return NULL; 551 if (!parent) 552 profile->ns = aa_get_ns(ns); 553 } 554 policy = &profile->base; 555 hname = split + 2; 556 split = strstr(hname, "//"); 557 } 558 if (!profile) 559 return &ns->base; 560 return &profile->base; 561 } 562 563 /** 564 * __lookupn_profile - lookup the profile matching @hname 565 * @base: base list to start looking up profile name from (NOT NULL) 566 * @hname: hierarchical profile name (NOT NULL) 567 * @n: length of @hname 568 * 569 * Requires: rcu_read_lock be held 570 * 571 * Returns: unrefcounted profile pointer or NULL if not found 572 * 573 * Do a relative name lookup, recursing through profile tree. 574 */ 575 static struct aa_profile *__lookupn_profile(struct aa_policy *base, 576 const char *hname, size_t n) 577 { 578 struct aa_profile *profile = NULL; 579 const char *split; 580 581 for (split = strnstr(hname, "//", n); split; 582 split = strnstr(hname, "//", n)) { 583 profile = __strn_find_child(&base->profiles, hname, 584 split - hname); 585 if (!profile) 586 return NULL; 587 588 base = &profile->base; 589 n -= split + 2 - hname; 590 hname = split + 2; 591 } 592 593 if (n) 594 return __strn_find_child(&base->profiles, hname, n); 595 return NULL; 596 } 597 598 static struct aa_profile *__lookup_profile(struct aa_policy *base, 599 const char *hname) 600 { 601 return __lookupn_profile(base, hname, strlen(hname)); 602 } 603 604 /** 605 * aa_lookupn_profile - find a profile by its full or partial name 606 * @ns: the namespace to start from (NOT NULL) 607 * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL) 608 * @n: size of @hname 609 * 610 * Returns: refcounted profile or NULL if not found 611 */ 612 struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname, 613 size_t n) 614 { 615 struct aa_profile *profile; 616 617 rcu_read_lock(); 618 do { 619 profile = __lookupn_profile(&ns->base, hname, n); 620 } while (profile && !aa_get_profile_not0(profile)); 621 rcu_read_unlock(); 622 623 /* the unconfined profile is not in the regular profile list */ 624 if (!profile && strncmp(hname, "unconfined", n) == 0) 625 profile = aa_get_newest_profile(ns->unconfined); 626 627 /* refcount released by caller */ 628 return profile; 629 } 630 631 struct aa_profile *aa_fqlookupn_profile(struct aa_label *base, 632 const char *fqname, size_t n) 633 { 634 struct aa_profile *profile; 635 struct aa_ns *ns; 636 const char *name, *ns_name; 637 size_t ns_len; 638 639 name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len); 640 if (ns_name) { 641 ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len); 642 if (!ns) 643 return NULL; 644 } else 645 ns = aa_get_ns(labels_ns(base)); 646 647 if (name) 648 profile = aa_lookupn_profile(ns, name, n - (name - fqname)); 649 else if (ns) 650 /* default profile for ns, currently unconfined */ 651 profile = aa_get_newest_profile(ns->unconfined); 652 else 653 profile = NULL; 654 aa_put_ns(ns); 655 656 return profile; 657 } 658 659 660 struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name, 661 gfp_t gfp) 662 { 663 struct aa_profile *profile; 664 struct aa_ruleset *rules; 665 666 profile = aa_alloc_profile(name, NULL, gfp); 667 if (!profile) 668 return NULL; 669 670 /* TODO: ideally we should inherit abi from parent */ 671 profile->label.flags |= FLAG_NULL; 672 profile->attach.xmatch = aa_get_pdb(nullpdb); 673 rules = profile->label.rules[0]; 674 rules->file = aa_get_pdb(nullpdb); 675 rules->policy = aa_get_pdb(nullpdb); 676 aa_compute_profile_mediates(profile); 677 678 if (parent) { 679 profile->path_flags = parent->path_flags; 680 /* override/inherit what is mediated from parent */ 681 profile->label.mediates = parent->label.mediates; 682 /* released on free_profile */ 683 rcu_assign_pointer(profile->parent, aa_get_profile(parent)); 684 profile->ns = aa_get_ns(parent->ns); 685 } 686 687 return profile; 688 } 689 690 /** 691 * aa_new_learning_profile - create or find a null-X learning profile 692 * @parent: profile that caused this profile to be created (NOT NULL) 693 * @hat: true if the null- learning profile is a hat 694 * @base: name to base the null profile off of 695 * @gfp: type of allocation 696 * 697 * Find/Create a null- complain mode profile used in learning mode. The 698 * name of the profile is unique and follows the format of parent//null-XXX. 699 * where XXX is based on the @name or if that fails or is not supplied 700 * a unique number 701 * 702 * null profiles are added to the profile list but the list does not 703 * hold a count on them so that they are automatically released when 704 * not in use. 705 * 706 * Returns: new refcounted profile else NULL on failure 707 */ 708 struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat, 709 const char *base, gfp_t gfp) 710 { 711 struct aa_profile *p, *profile; 712 const char *bname; 713 char *name = NULL; 714 size_t name_sz; 715 716 AA_BUG(!parent); 717 718 if (base) { 719 name_sz = strlen(parent->base.hname) + 8 + strlen(base); 720 name = kmalloc(name_sz, gfp); 721 if (name) { 722 snprintf(name, name_sz, "%s//null-%s", 723 parent->base.hname, base); 724 goto name; 725 } 726 /* fall through to try shorter uniq */ 727 } 728 729 name_sz = strlen(parent->base.hname) + 2 + 7 + 8; 730 name = kmalloc(name_sz, gfp); 731 if (!name) 732 return NULL; 733 snprintf(name, name_sz, "%s//null-%x", parent->base.hname, 734 atomic_inc_return(&parent->ns->uniq_null)); 735 736 name: 737 /* lookup to see if this is a dup creation */ 738 bname = basename(name); 739 profile = aa_find_child(parent, bname); 740 if (profile) 741 goto out; 742 743 profile = aa_alloc_null(parent, name, gfp); 744 if (!profile) 745 goto fail; 746 profile->mode = APPARMOR_COMPLAIN; 747 if (hat) 748 profile->label.flags |= FLAG_HAT; 749 750 mutex_lock_nested(&profile->ns->lock, profile->ns->level); 751 p = __find_child(&parent->base.profiles, bname); 752 if (p) { 753 aa_free_profile(profile); 754 profile = aa_get_profile(p); 755 } else { 756 __add_profile(&parent->base.profiles, profile); 757 } 758 mutex_unlock(&profile->ns->lock); 759 760 /* refcount released by caller */ 761 out: 762 kfree(name); 763 764 return profile; 765 766 fail: 767 kfree(name); 768 aa_free_profile(profile); 769 return NULL; 770 } 771 772 /** 773 * replacement_allowed - test to see if replacement is allowed 774 * @profile: profile to test if it can be replaced (MAYBE NULL) 775 * @noreplace: true if replacement shouldn't be allowed but addition is okay 776 * @info: Returns - info about why replacement failed (NOT NULL) 777 * 778 * Returns: %0 if replacement allowed else error code 779 */ 780 static int replacement_allowed(struct aa_profile *profile, int noreplace, 781 const char **info) 782 { 783 if (profile) { 784 if (profile->label.flags & FLAG_IMMUTIBLE) { 785 *info = "cannot replace immutable profile"; 786 return -EPERM; 787 } else if (noreplace) { 788 *info = "profile already exists"; 789 return -EEXIST; 790 } 791 } 792 return 0; 793 } 794 795 /* audit callback for net specific fields */ 796 static void audit_cb(struct audit_buffer *ab, void *va) 797 { 798 struct common_audit_data *sa = va; 799 struct apparmor_audit_data *ad = aad(sa); 800 801 if (ad->iface.ns) { 802 audit_log_format(ab, " ns="); 803 audit_log_untrustedstring(ab, ad->iface.ns); 804 } 805 } 806 807 /** 808 * audit_policy - Do auditing of policy changes 809 * @subj_label: label to check if it can manage policy 810 * @op: policy operation being performed 811 * @ns_name: name of namespace being manipulated 812 * @name: name of profile being manipulated (NOT NULL) 813 * @info: any extra information to be audited (MAYBE NULL) 814 * @error: error code 815 * 816 * Returns: the error to be returned after audit is done 817 */ 818 static int audit_policy(struct aa_label *subj_label, const char *op, 819 const char *ns_name, const char *name, 820 const char *info, int error) 821 { 822 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, op); 823 824 ad.iface.ns = ns_name; 825 ad.name = name; 826 ad.info = info; 827 ad.error = error; 828 ad.subj_label = subj_label; 829 830 aa_audit_msg(AUDIT_APPARMOR_STATUS, &ad, audit_cb); 831 832 return error; 833 } 834 835 /* don't call out to other LSMs in the stack for apparmor policy admin 836 * permissions 837 */ 838 static int policy_ns_capable(const struct cred *subj_cred, 839 struct aa_label *label, 840 struct user_namespace *userns, int cap) 841 { 842 int err; 843 844 /* check for MAC_ADMIN cap in cred */ 845 err = cap_capable(subj_cred, userns, cap, CAP_OPT_NONE); 846 if (!err) 847 err = aa_capable(subj_cred, label, cap, CAP_OPT_NONE); 848 849 return err; 850 } 851 852 /** 853 * aa_policy_view_capable - check if viewing policy in at @ns is allowed 854 * @subj_cred: cred of subject 855 * @label: label that is trying to view policy in ns 856 * @ns: namespace being viewed by @label (may be NULL if @label's ns) 857 * 858 * Returns: true if viewing policy is allowed 859 * 860 * If @ns is NULL then the namespace being viewed is assumed to be the 861 * tasks current namespace. 862 */ 863 bool aa_policy_view_capable(const struct cred *subj_cred, 864 struct aa_label *label, struct aa_ns *ns) 865 { 866 struct user_namespace *user_ns = subj_cred->user_ns; 867 struct aa_ns *view_ns = labels_view(label); 868 bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) || 869 in_egroup_p(make_kgid(user_ns, 0)); 870 bool response = false; 871 if (!ns) 872 ns = view_ns; 873 874 if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) && 875 (user_ns == &init_user_ns || 876 (unprivileged_userns_apparmor_policy != 0 && 877 user_ns->level == view_ns->level))) 878 response = true; 879 880 return response; 881 } 882 883 bool aa_policy_admin_capable(const struct cred *subj_cred, 884 struct aa_label *label, struct aa_ns *ns) 885 { 886 struct user_namespace *user_ns = subj_cred->user_ns; 887 bool capable = policy_ns_capable(subj_cred, label, user_ns, 888 CAP_MAC_ADMIN) == 0; 889 890 AA_DEBUG(DEBUG_POLICY, "cap_mac_admin? %d\n", capable); 891 AA_DEBUG(DEBUG_POLICY, "policy locked? %d\n", aa_g_lock_policy); 892 893 return aa_policy_view_capable(subj_cred, label, ns) && capable && 894 !aa_g_lock_policy; 895 } 896 897 bool aa_current_policy_view_capable(struct aa_ns *ns) 898 { 899 struct aa_label *label; 900 bool needput, res; 901 902 label = __begin_current_label_crit_section(&needput); 903 res = aa_policy_view_capable(current_cred(), label, ns); 904 __end_current_label_crit_section(label, needput); 905 906 return res; 907 } 908 909 bool aa_current_policy_admin_capable(struct aa_ns *ns) 910 { 911 struct aa_label *label; 912 bool needput, res; 913 914 label = __begin_current_label_crit_section(&needput); 915 res = aa_policy_admin_capable(current_cred(), label, ns); 916 __end_current_label_crit_section(label, needput); 917 918 return res; 919 } 920 921 /** 922 * aa_may_manage_policy - can the current task manage policy 923 * @subj_cred: subjects cred 924 * @label: label to check if it can manage policy 925 * @ns: namespace being managed by @label (may be NULL if @label's ns) 926 * @mask: contains the policy manipulation operation being done 927 * 928 * Returns: 0 if the task is allowed to manipulate policy else error 929 */ 930 int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label, 931 struct aa_ns *ns, u32 mask) 932 { 933 const char *op; 934 935 if (mask & AA_MAY_REMOVE_POLICY) 936 op = OP_PROF_RM; 937 else if (mask & AA_MAY_REPLACE_POLICY) 938 op = OP_PROF_REPL; 939 else 940 op = OP_PROF_LOAD; 941 942 /* check if loading policy is locked out */ 943 if (aa_g_lock_policy) 944 return audit_policy(label, op, NULL, NULL, "policy_locked", 945 -EACCES); 946 947 if (!aa_policy_admin_capable(subj_cred, label, ns)) 948 return audit_policy(label, op, NULL, NULL, "not policy admin", 949 -EACCES); 950 951 /* TODO: add fine grained mediation of policy loads */ 952 return 0; 953 } 954 955 static struct aa_profile *__list_lookup_parent(struct list_head *lh, 956 struct aa_profile *profile) 957 { 958 const char *base = basename(profile->base.hname); 959 long len = base - profile->base.hname; 960 struct aa_load_ent *ent; 961 962 /* parent won't have trailing // so remove from len */ 963 if (len <= 2) 964 return NULL; 965 len -= 2; 966 967 list_for_each_entry(ent, lh, list) { 968 if (ent->new == profile) 969 continue; 970 if (strncmp(ent->new->base.hname, profile->base.hname, len) == 971 0 && ent->new->base.hname[len] == 0) 972 return ent->new; 973 } 974 975 return NULL; 976 } 977 978 /** 979 * __replace_profile - replace @old with @new on a list 980 * @old: profile to be replaced (NOT NULL) 981 * @new: profile to replace @old with (NOT NULL) 982 * 983 * Will duplicate and refcount elements that @new inherits from @old 984 * and will inherit @old children. 985 * 986 * refcount @new for list, put @old list refcount 987 * 988 * Requires: namespace list lock be held, or list not be shared 989 */ 990 static void __replace_profile(struct aa_profile *old, struct aa_profile *new) 991 { 992 struct aa_profile *child, *tmp; 993 994 if (!list_empty(&old->base.profiles)) { 995 LIST_HEAD(lh); 996 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu); 997 998 list_for_each_entry_safe(child, tmp, &lh, base.list) { 999 struct aa_profile *p; 1000 1001 list_del_init(&child->base.list); 1002 p = __find_child(&new->base.profiles, child->base.name); 1003 if (p) { 1004 /* @p replaces @child */ 1005 __replace_profile(child, p); 1006 continue; 1007 } 1008 1009 /* inherit @child and its children */ 1010 /* TODO: update hname of inherited children */ 1011 /* list refcount transferred to @new */ 1012 p = aa_deref_parent(child); 1013 rcu_assign_pointer(child->parent, aa_get_profile(new)); 1014 list_add_rcu(&child->base.list, &new->base.profiles); 1015 aa_put_profile(p); 1016 } 1017 } 1018 1019 if (!rcu_access_pointer(new->parent)) { 1020 struct aa_profile *parent = aa_deref_parent(old); 1021 rcu_assign_pointer(new->parent, aa_get_profile(parent)); 1022 } 1023 aa_label_replace(&old->label, &new->label); 1024 /* migrate dents must come after label replacement b/c update */ 1025 __aafs_profile_migrate_dents(old, new); 1026 1027 if (list_empty(&new->base.list)) { 1028 /* new is not on a list already */ 1029 list_replace_rcu(&old->base.list, &new->base.list); 1030 aa_get_profile(new); 1031 aa_put_profile(old); 1032 } else 1033 __list_remove_profile(old); 1034 } 1035 1036 /** 1037 * __lookup_replace - lookup replacement information for a profile 1038 * @ns: namespace the lookup occurs in 1039 * @hname: name of profile to lookup 1040 * @noreplace: true if not replacing an existing profile 1041 * @p: Returns - profile to be replaced 1042 * @info: Returns - info string on why lookup failed 1043 * 1044 * Returns: profile to replace (no ref) on success else ptr error 1045 */ 1046 static int __lookup_replace(struct aa_ns *ns, const char *hname, 1047 bool noreplace, struct aa_profile **p, 1048 const char **info) 1049 { 1050 *p = aa_get_profile(__lookup_profile(&ns->base, hname)); 1051 if (*p) { 1052 int error = replacement_allowed(*p, noreplace, info); 1053 if (error) { 1054 *info = "profile can not be replaced"; 1055 return error; 1056 } 1057 } 1058 1059 return 0; 1060 } 1061 1062 static void share_name(struct aa_profile *old, struct aa_profile *new) 1063 { 1064 aa_put_str(new->base.hname); 1065 aa_get_str(old->base.hname); 1066 new->base.hname = old->base.hname; 1067 new->base.name = old->base.name; 1068 new->label.hname = old->label.hname; 1069 } 1070 1071 /* Update to newest version of parent after previous replacements 1072 * Returns: unrefcount newest version of parent 1073 */ 1074 static struct aa_profile *update_to_newest_parent(struct aa_profile *new) 1075 { 1076 struct aa_profile *parent, *newest; 1077 1078 parent = rcu_dereference_protected(new->parent, 1079 mutex_is_locked(&new->ns->lock)); 1080 newest = aa_get_newest_profile(parent); 1081 1082 /* parent replaced in this atomic set? */ 1083 if (newest != parent) { 1084 aa_put_profile(parent); 1085 rcu_assign_pointer(new->parent, newest); 1086 } else 1087 aa_put_profile(newest); 1088 1089 return newest; 1090 } 1091 1092 /** 1093 * aa_replace_profiles - replace profile(s) on the profile list 1094 * @policy_ns: namespace load is occurring on 1095 * @label: label that is attempting to load/replace policy 1096 * @mask: permission mask 1097 * @udata: serialized data stream (NOT NULL) 1098 * 1099 * unpack and replace a profile on the profile list and uses of that profile 1100 * by any task creds via invalidating the old version of the profile, which 1101 * tasks will notice to update their own cred. If the profile does not exist 1102 * on the profile list it is added. 1103 * 1104 * Returns: size of data consumed else error code on failure. 1105 */ 1106 ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label, 1107 u32 mask, struct aa_loaddata *udata) 1108 { 1109 const char *ns_name = NULL, *info = NULL; 1110 struct aa_ns *ns = NULL; 1111 struct aa_load_ent *ent, *tmp; 1112 struct aa_loaddata *rawdata_ent; 1113 const char *op; 1114 ssize_t count, error; 1115 LIST_HEAD(lh); 1116 1117 op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD; 1118 aa_get_loaddata(udata); 1119 /* released below */ 1120 error = aa_unpack(udata, &lh, &ns_name); 1121 if (error) 1122 goto out; 1123 1124 /* ensure that profiles are all for the same ns 1125 * TODO: update locking to remove this constraint. All profiles in 1126 * the load set must succeed as a set or the load will 1127 * fail. Sort ent list and take ns locks in hierarchy order 1128 */ 1129 count = 0; 1130 list_for_each_entry(ent, &lh, list) { 1131 if (ns_name) { 1132 if (ent->ns_name && 1133 strcmp(ent->ns_name, ns_name) != 0) { 1134 info = "policy load has mixed namespaces"; 1135 error = -EACCES; 1136 goto fail; 1137 } 1138 } else if (ent->ns_name) { 1139 if (count) { 1140 info = "policy load has mixed namespaces"; 1141 error = -EACCES; 1142 goto fail; 1143 } 1144 ns_name = ent->ns_name; 1145 } else 1146 count++; 1147 } 1148 if (ns_name) { 1149 ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label), 1150 ns_name); 1151 if (IS_ERR(ns)) { 1152 op = OP_PROF_LOAD; 1153 info = "failed to prepare namespace"; 1154 error = PTR_ERR(ns); 1155 ns = NULL; 1156 ent = NULL; 1157 goto fail; 1158 } 1159 } else 1160 ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label)); 1161 1162 mutex_lock_nested(&ns->lock, ns->level); 1163 /* check for duplicate rawdata blobs: space and file dedup */ 1164 if (!list_empty(&ns->rawdata_list)) { 1165 list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) { 1166 if (aa_rawdata_eq(rawdata_ent, udata)) { 1167 struct aa_loaddata *tmp; 1168 1169 tmp = __aa_get_loaddata(rawdata_ent); 1170 /* check we didn't fail the race */ 1171 if (tmp) { 1172 aa_put_loaddata(udata); 1173 udata = tmp; 1174 break; 1175 } 1176 } 1177 } 1178 } 1179 /* setup parent and ns info */ 1180 list_for_each_entry(ent, &lh, list) { 1181 struct aa_policy *policy; 1182 struct aa_profile *p; 1183 1184 if (aa_g_export_binary) 1185 ent->new->rawdata = aa_get_loaddata(udata); 1186 error = __lookup_replace(ns, ent->new->base.hname, 1187 !(mask & AA_MAY_REPLACE_POLICY), 1188 &ent->old, &info); 1189 if (error) 1190 goto fail_lock; 1191 1192 if (ent->new->rename) { 1193 error = __lookup_replace(ns, ent->new->rename, 1194 !(mask & AA_MAY_REPLACE_POLICY), 1195 &ent->rename, &info); 1196 if (error) 1197 goto fail_lock; 1198 } 1199 1200 /* released when @new is freed */ 1201 ent->new->ns = aa_get_ns(ns); 1202 1203 if (ent->old || ent->rename) 1204 continue; 1205 1206 /* no ref on policy only use inside lock */ 1207 p = NULL; 1208 policy = __lookup_parent(ns, ent->new->base.hname); 1209 if (!policy) { 1210 /* first check for parent in the load set */ 1211 p = __list_lookup_parent(&lh, ent->new); 1212 if (!p) { 1213 /* 1214 * fill in missing parent with null 1215 * profile that doesn't have 1216 * permissions. This allows for 1217 * individual profile loading where 1218 * the child is loaded before the 1219 * parent, and outside of the current 1220 * atomic set. This unfortunately can 1221 * happen with some userspaces. The 1222 * null profile will be replaced once 1223 * the parent is loaded. 1224 */ 1225 policy = __create_missing_ancestors(ns, 1226 ent->new->base.hname, 1227 GFP_KERNEL); 1228 if (!policy) { 1229 error = -ENOENT; 1230 info = "parent does not exist"; 1231 goto fail_lock; 1232 } 1233 } 1234 } 1235 if (!p && policy != &ns->base) 1236 /* released on profile replacement or free_profile */ 1237 p = (struct aa_profile *) policy; 1238 rcu_assign_pointer(ent->new->parent, aa_get_profile(p)); 1239 } 1240 1241 /* create new fs entries for introspection if needed */ 1242 if (!udata->dents[AAFS_LOADDATA_DIR] && aa_g_export_binary) { 1243 error = __aa_fs_create_rawdata(ns, udata); 1244 if (error) { 1245 info = "failed to create raw_data dir and files"; 1246 ent = NULL; 1247 goto fail_lock; 1248 } 1249 } 1250 list_for_each_entry(ent, &lh, list) { 1251 if (!ent->old) { 1252 struct dentry *parent; 1253 if (rcu_access_pointer(ent->new->parent)) { 1254 struct aa_profile *p; 1255 p = aa_deref_parent(ent->new); 1256 parent = prof_child_dir(p); 1257 } else 1258 parent = ns_subprofs_dir(ent->new->ns); 1259 error = __aafs_profile_mkdir(ent->new, parent); 1260 } 1261 1262 if (error) { 1263 info = "failed to create"; 1264 goto fail_lock; 1265 } 1266 } 1267 1268 /* Done with checks that may fail - do actual replacement */ 1269 __aa_bump_ns_revision(ns); 1270 if (aa_g_export_binary) 1271 __aa_loaddata_update(udata, ns->revision); 1272 list_for_each_entry_safe(ent, tmp, &lh, list) { 1273 list_del_init(&ent->list); 1274 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL; 1275 1276 if (ent->old && ent->old->rawdata == ent->new->rawdata && 1277 ent->new->rawdata) { 1278 /* dedup actual profile replacement */ 1279 audit_policy(label, op, ns_name, ent->new->base.hname, 1280 "same as current profile, skipping", 1281 error); 1282 /* break refcount cycle with proxy. */ 1283 aa_put_proxy(ent->new->label.proxy); 1284 ent->new->label.proxy = NULL; 1285 goto skip; 1286 } 1287 1288 /* 1289 * TODO: finer dedup based on profile range in data. Load set 1290 * can differ but profile may remain unchanged 1291 */ 1292 audit_policy(label, op, ns_name, ent->new->base.hname, NULL, 1293 error); 1294 1295 if (ent->old) { 1296 share_name(ent->old, ent->new); 1297 __replace_profile(ent->old, ent->new); 1298 } else { 1299 struct list_head *lh; 1300 1301 if (rcu_access_pointer(ent->new->parent)) { 1302 struct aa_profile *parent; 1303 1304 parent = update_to_newest_parent(ent->new); 1305 lh = &parent->base.profiles; 1306 } else 1307 lh = &ns->base.profiles; 1308 __add_profile(lh, ent->new); 1309 } 1310 skip: 1311 aa_load_ent_free(ent); 1312 } 1313 __aa_labelset_update_subtree(ns); 1314 mutex_unlock(&ns->lock); 1315 1316 out: 1317 aa_put_ns(ns); 1318 aa_put_loaddata(udata); 1319 kfree(ns_name); 1320 1321 if (error) 1322 return error; 1323 return udata->size; 1324 1325 fail_lock: 1326 mutex_unlock(&ns->lock); 1327 1328 /* audit cause of failure */ 1329 op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL; 1330 fail: 1331 audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL, 1332 info, error); 1333 /* audit status that rest of profiles in the atomic set failed too */ 1334 info = "valid profile in failed atomic policy load"; 1335 list_for_each_entry(tmp, &lh, list) { 1336 if (tmp == ent) { 1337 info = "unchecked profile in failed atomic policy load"; 1338 /* skip entry that caused failure */ 1339 continue; 1340 } 1341 op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL; 1342 audit_policy(label, op, ns_name, tmp->new->base.hname, info, 1343 error); 1344 } 1345 list_for_each_entry_safe(ent, tmp, &lh, list) { 1346 list_del_init(&ent->list); 1347 aa_load_ent_free(ent); 1348 } 1349 1350 goto out; 1351 } 1352 1353 /** 1354 * aa_remove_profiles - remove profile(s) from the system 1355 * @policy_ns: namespace the remove is being done from 1356 * @subj: label attempting to remove policy 1357 * @fqname: name of the profile or namespace to remove (NOT NULL) 1358 * @size: size of the name 1359 * 1360 * Remove a profile or sub namespace from the current namespace, so that 1361 * they can not be found anymore and mark them as replaced by unconfined 1362 * 1363 * NOTE: removing confinement does not restore rlimits to preconfinement values 1364 * 1365 * Returns: size of data consume else error code if fails 1366 */ 1367 ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj, 1368 char *fqname, size_t size) 1369 { 1370 struct aa_ns *ns = NULL; 1371 struct aa_profile *profile = NULL; 1372 const char *name = fqname, *info = NULL; 1373 const char *ns_name = NULL; 1374 ssize_t error = 0; 1375 1376 if (*fqname == 0) { 1377 info = "no profile specified"; 1378 error = -ENOENT; 1379 goto fail; 1380 } 1381 1382 if (fqname[0] == ':') { 1383 size_t ns_len; 1384 1385 name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len); 1386 /* released below */ 1387 ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj), 1388 ns_name, ns_len); 1389 if (!ns) { 1390 info = "namespace does not exist"; 1391 error = -ENOENT; 1392 goto fail; 1393 } 1394 } else 1395 /* released below */ 1396 ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj)); 1397 1398 if (!name) { 1399 /* remove namespace - can only happen if fqname[0] == ':' */ 1400 mutex_lock_nested(&ns->parent->lock, ns->parent->level); 1401 __aa_bump_ns_revision(ns); 1402 __aa_remove_ns(ns); 1403 mutex_unlock(&ns->parent->lock); 1404 } else { 1405 /* remove profile */ 1406 mutex_lock_nested(&ns->lock, ns->level); 1407 profile = aa_get_profile(__lookup_profile(&ns->base, name)); 1408 if (!profile) { 1409 error = -ENOENT; 1410 info = "profile does not exist"; 1411 goto fail_ns_lock; 1412 } 1413 name = profile->base.hname; 1414 __aa_bump_ns_revision(ns); 1415 __remove_profile(profile); 1416 __aa_labelset_update_subtree(ns); 1417 mutex_unlock(&ns->lock); 1418 } 1419 1420 /* don't fail removal if audit fails */ 1421 (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info, 1422 error); 1423 aa_put_ns(ns); 1424 aa_put_profile(profile); 1425 return size; 1426 1427 fail_ns_lock: 1428 mutex_unlock(&ns->lock); 1429 aa_put_ns(ns); 1430 1431 fail: 1432 (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info, 1433 error); 1434 return error; 1435 } 1436