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