aboutsummaryrefslogtreecommitdiffstats
path: root/security/apparmor/policy.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/apparmor/policy.c')
-rw-r--r--security/apparmor/policy.c611
1 files changed, 355 insertions, 256 deletions
diff --git a/security/apparmor/policy.c b/security/apparmor/policy.c
index 0f345c4dee5f..705c2879d3a9 100644
--- a/security/apparmor/policy.c
+++ b/security/apparmor/policy.c
@@ -92,10 +92,11 @@
92/* root profile namespace */ 92/* root profile namespace */
93struct aa_namespace *root_ns; 93struct aa_namespace *root_ns;
94 94
95const char *const profile_mode_names[] = { 95const char *const aa_profile_mode_names[] = {
96 "enforce", 96 "enforce",
97 "complain", 97 "complain",
98 "kill", 98 "kill",
99 "unconfined",
99}; 100};
100 101
101/** 102/**
@@ -141,7 +142,6 @@ static bool policy_init(struct aa_policy *policy, const char *prefix,
141 policy->name = (char *)hname_tail(policy->hname); 142 policy->name = (char *)hname_tail(policy->hname);
142 INIT_LIST_HEAD(&policy->list); 143 INIT_LIST_HEAD(&policy->list);
143 INIT_LIST_HEAD(&policy->profiles); 144 INIT_LIST_HEAD(&policy->profiles);
144 kref_init(&policy->count);
145 145
146 return 1; 146 return 1;
147} 147}
@@ -153,13 +153,13 @@ static bool policy_init(struct aa_policy *policy, const char *prefix,
153static void policy_destroy(struct aa_policy *policy) 153static void policy_destroy(struct aa_policy *policy)
154{ 154{
155 /* still contains profiles -- invalid */ 155 /* still contains profiles -- invalid */
156 if (!list_empty(&policy->profiles)) { 156 if (on_list_rcu(&policy->profiles)) {
157 AA_ERROR("%s: internal error, " 157 AA_ERROR("%s: internal error, "
158 "policy '%s' still contains profiles\n", 158 "policy '%s' still contains profiles\n",
159 __func__, policy->name); 159 __func__, policy->name);
160 BUG(); 160 BUG();
161 } 161 }
162 if (!list_empty(&policy->list)) { 162 if (on_list_rcu(&policy->list)) {
163 AA_ERROR("%s: internal error, policy '%s' still on list\n", 163 AA_ERROR("%s: internal error, policy '%s' still on list\n",
164 __func__, policy->name); 164 __func__, policy->name);
165 BUG(); 165 BUG();
@@ -174,7 +174,7 @@ static void policy_destroy(struct aa_policy *policy)
174 * @head: list to search (NOT NULL) 174 * @head: list to search (NOT NULL)
175 * @name: name to search for (NOT NULL) 175 * @name: name to search for (NOT NULL)
176 * 176 *
177 * Requires: correct locks for the @head list be held 177 * Requires: rcu_read_lock be held
178 * 178 *
179 * Returns: unrefcounted policy that match @name or NULL if not found 179 * Returns: unrefcounted policy that match @name or NULL if not found
180 */ 180 */
@@ -182,7 +182,7 @@ static struct aa_policy *__policy_find(struct list_head *head, const char *name)
182{ 182{
183 struct aa_policy *policy; 183 struct aa_policy *policy;
184 184
185 list_for_each_entry(policy, head, list) { 185 list_for_each_entry_rcu(policy, head, list) {
186 if (!strcmp(policy->name, name)) 186 if (!strcmp(policy->name, name))
187 return policy; 187 return policy;
188 } 188 }
@@ -195,7 +195,7 @@ static struct aa_policy *__policy_find(struct list_head *head, const char *name)
195 * @str: string to search for (NOT NULL) 195 * @str: string to search for (NOT NULL)
196 * @len: length of match required 196 * @len: length of match required
197 * 197 *
198 * Requires: correct locks for the @head list be held 198 * Requires: rcu_read_lock be held
199 * 199 *
200 * Returns: unrefcounted policy that match @str or NULL if not found 200 * Returns: unrefcounted policy that match @str or NULL if not found
201 * 201 *
@@ -207,7 +207,7 @@ static struct aa_policy *__policy_strn_find(struct list_head *head,
207{ 207{
208 struct aa_policy *policy; 208 struct aa_policy *policy;
209 209
210 list_for_each_entry(policy, head, list) { 210 list_for_each_entry_rcu(policy, head, list) {
211 if (aa_strneq(policy->name, str, len)) 211 if (aa_strneq(policy->name, str, len))
212 return policy; 212 return policy;
213 } 213 }
@@ -284,22 +284,19 @@ static struct aa_namespace *alloc_namespace(const char *prefix,
284 goto fail_ns; 284 goto fail_ns;
285 285
286 INIT_LIST_HEAD(&ns->sub_ns); 286 INIT_LIST_HEAD(&ns->sub_ns);
287 rwlock_init(&ns->lock); 287 mutex_init(&ns->lock);
288 288
289 /* released by free_namespace */ 289 /* released by free_namespace */
290 ns->unconfined = aa_alloc_profile("unconfined"); 290 ns->unconfined = aa_alloc_profile("unconfined");
291 if (!ns->unconfined) 291 if (!ns->unconfined)
292 goto fail_unconfined; 292 goto fail_unconfined;
293 293
294 ns->unconfined->flags = PFLAG_UNCONFINED | PFLAG_IX_ON_NAME_ERROR | 294 ns->unconfined->flags = PFLAG_IX_ON_NAME_ERROR |
295 PFLAG_IMMUTABLE; 295 PFLAG_IMMUTABLE | PFLAG_NS_COUNT;
296 ns->unconfined->mode = APPARMOR_UNCONFINED;
296 297
297 /* 298 /* ns and ns->unconfined share ns->unconfined refcount */
298 * released by free_namespace, however __remove_namespace breaks 299 ns->unconfined->ns = ns;
299 * the cyclic references (ns->unconfined, and unconfined->ns) and
300 * replaces with refs to parent namespace unconfined
301 */
302 ns->unconfined->ns = aa_get_namespace(ns);
303 300
304 atomic_set(&ns->uniq_null, 0); 301 atomic_set(&ns->uniq_null, 0);
305 302
@@ -327,30 +324,19 @@ static void free_namespace(struct aa_namespace *ns)
327 policy_destroy(&ns->base); 324 policy_destroy(&ns->base);
328 aa_put_namespace(ns->parent); 325 aa_put_namespace(ns->parent);
329 326
330 if (ns->unconfined && ns->unconfined->ns == ns) 327 ns->unconfined->ns = NULL;
331 ns->unconfined->ns = NULL; 328 aa_free_profile(ns->unconfined);
332
333 aa_put_profile(ns->unconfined);
334 kzfree(ns); 329 kzfree(ns);
335} 330}
336 331
337/** 332/**
338 * aa_free_namespace_kref - free aa_namespace by kref (see aa_put_namespace)
339 * @kr: kref callback for freeing of a namespace (NOT NULL)
340 */
341void aa_free_namespace_kref(struct kref *kref)
342{
343 free_namespace(container_of(kref, struct aa_namespace, base.count));
344}
345
346/**
347 * __aa_find_namespace - find a namespace on a list by @name 333 * __aa_find_namespace - find a namespace on a list by @name
348 * @head: list to search for namespace on (NOT NULL) 334 * @head: list to search for namespace on (NOT NULL)
349 * @name: name of namespace to look for (NOT NULL) 335 * @name: name of namespace to look for (NOT NULL)
350 * 336 *
351 * Returns: unrefcounted namespace 337 * Returns: unrefcounted namespace
352 * 338 *
353 * Requires: ns lock be held 339 * Requires: rcu_read_lock be held
354 */ 340 */
355static struct aa_namespace *__aa_find_namespace(struct list_head *head, 341static struct aa_namespace *__aa_find_namespace(struct list_head *head,
356 const char *name) 342 const char *name)
@@ -373,9 +359,9 @@ struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
373{ 359{
374 struct aa_namespace *ns = NULL; 360 struct aa_namespace *ns = NULL;
375 361
376 read_lock(&root->lock); 362 rcu_read_lock();
377 ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name)); 363 ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name));
378 read_unlock(&root->lock); 364 rcu_read_unlock();
379 365
380 return ns; 366 return ns;
381} 367}
@@ -392,7 +378,7 @@ static struct aa_namespace *aa_prepare_namespace(const char *name)
392 378
393 root = aa_current_profile()->ns; 379 root = aa_current_profile()->ns;
394 380
395 write_lock(&root->lock); 381 mutex_lock(&root->lock);
396 382
397 /* if name isn't specified the profile is loaded to the current ns */ 383 /* if name isn't specified the profile is loaded to the current ns */
398 if (!name) { 384 if (!name) {
@@ -405,31 +391,23 @@ static struct aa_namespace *aa_prepare_namespace(const char *name)
405 /* released by caller */ 391 /* released by caller */
406 ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name)); 392 ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name));
407 if (!ns) { 393 if (!ns) {
408 /* namespace not found */ 394 ns = alloc_namespace(root->base.hname, name);
409 struct aa_namespace *new_ns; 395 if (!ns)
410 write_unlock(&root->lock); 396 goto out;
411 new_ns = alloc_namespace(root->base.hname, name); 397 if (__aa_fs_namespace_mkdir(ns, ns_subns_dir(root), name)) {
412 if (!new_ns) 398 AA_ERROR("Failed to create interface for ns %s\n",
413 return NULL; 399 ns->base.name);
414 write_lock(&root->lock); 400 free_namespace(ns);
415 /* test for race when new_ns was allocated */ 401 ns = NULL;
416 ns = __aa_find_namespace(&root->sub_ns, name); 402 goto out;
417 if (!ns) {
418 /* add parent ref */
419 new_ns->parent = aa_get_namespace(root);
420
421 list_add(&new_ns->base.list, &root->sub_ns);
422 /* add list ref */
423 ns = aa_get_namespace(new_ns);
424 } else {
425 /* raced so free the new one */
426 free_namespace(new_ns);
427 /* get reference on namespace */
428 aa_get_namespace(ns);
429 } 403 }
404 ns->parent = aa_get_namespace(root);
405 list_add_rcu(&ns->base.list, &root->sub_ns);
406 /* add list ref */
407 aa_get_namespace(ns);
430 } 408 }
431out: 409out:
432 write_unlock(&root->lock); 410 mutex_unlock(&root->lock);
433 411
434 /* return ref */ 412 /* return ref */
435 return ns; 413 return ns;
@@ -447,7 +425,7 @@ out:
447static void __list_add_profile(struct list_head *list, 425static void __list_add_profile(struct list_head *list,
448 struct aa_profile *profile) 426 struct aa_profile *profile)
449{ 427{
450 list_add(&profile->base.list, list); 428 list_add_rcu(&profile->base.list, list);
451 /* get list reference */ 429 /* get list reference */
452 aa_get_profile(profile); 430 aa_get_profile(profile);
453} 431}
@@ -466,49 +444,8 @@ static void __list_add_profile(struct list_head *list,
466 */ 444 */
467static void __list_remove_profile(struct aa_profile *profile) 445static void __list_remove_profile(struct aa_profile *profile)
468{ 446{
469 list_del_init(&profile->base.list); 447 list_del_rcu(&profile->base.list);
470 if (!(profile->flags & PFLAG_NO_LIST_REF)) 448 aa_put_profile(profile);
471 /* release list reference */
472 aa_put_profile(profile);
473}
474
475/**
476 * __replace_profile - replace @old with @new on a list
477 * @old: profile to be replaced (NOT NULL)
478 * @new: profile to replace @old with (NOT NULL)
479 *
480 * Will duplicate and refcount elements that @new inherits from @old
481 * and will inherit @old children.
482 *
483 * refcount @new for list, put @old list refcount
484 *
485 * Requires: namespace list lock be held, or list not be shared
486 */
487static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
488{
489 struct aa_policy *policy;
490 struct aa_profile *child, *tmp;
491
492 if (old->parent)
493 policy = &old->parent->base;
494 else
495 policy = &old->ns->base;
496
497 /* released when @new is freed */
498 new->parent = aa_get_profile(old->parent);
499 new->ns = aa_get_namespace(old->ns);
500 __list_add_profile(&policy->profiles, new);
501 /* inherit children */
502 list_for_each_entry_safe(child, tmp, &old->base.profiles, base.list) {
503 aa_put_profile(child->parent);
504 child->parent = aa_get_profile(new);
505 /* list refcount transferred to @new*/
506 list_move(&child->base.list, &new->base.profiles);
507 }
508
509 /* released by free_profile */
510 old->replacedby = aa_get_profile(new);
511 __list_remove_profile(old);
512} 449}
513 450
514static void __profile_list_release(struct list_head *head); 451static void __profile_list_release(struct list_head *head);
@@ -524,7 +461,8 @@ static void __remove_profile(struct aa_profile *profile)
524 /* release any children lists first */ 461 /* release any children lists first */
525 __profile_list_release(&profile->base.profiles); 462 __profile_list_release(&profile->base.profiles);
526 /* released by free_profile */ 463 /* released by free_profile */
527 profile->replacedby = aa_get_profile(profile->ns->unconfined); 464 __aa_update_replacedby(profile, profile->ns->unconfined);
465 __aa_fs_profile_rmdir(profile);
528 __list_remove_profile(profile); 466 __list_remove_profile(profile);
529} 467}
530 468
@@ -552,14 +490,17 @@ static void destroy_namespace(struct aa_namespace *ns)
552 if (!ns) 490 if (!ns)
553 return; 491 return;
554 492
555 write_lock(&ns->lock); 493 mutex_lock(&ns->lock);
556 /* release all profiles in this namespace */ 494 /* release all profiles in this namespace */
557 __profile_list_release(&ns->base.profiles); 495 __profile_list_release(&ns->base.profiles);
558 496
559 /* release all sub namespaces */ 497 /* release all sub namespaces */
560 __ns_list_release(&ns->sub_ns); 498 __ns_list_release(&ns->sub_ns);
561 499
562 write_unlock(&ns->lock); 500 if (ns->parent)
501 __aa_update_replacedby(ns->unconfined, ns->parent->unconfined);
502 __aa_fs_namespace_rmdir(ns);
503 mutex_unlock(&ns->lock);
563} 504}
564 505
565/** 506/**
@@ -570,25 +511,9 @@ static void destroy_namespace(struct aa_namespace *ns)
570 */ 511 */
571static void __remove_namespace(struct aa_namespace *ns) 512static void __remove_namespace(struct aa_namespace *ns)
572{ 513{
573 struct aa_profile *unconfined = ns->unconfined;
574
575 /* remove ns from namespace list */ 514 /* remove ns from namespace list */
576 list_del_init(&ns->base.list); 515 list_del_rcu(&ns->base.list);
577
578 /*
579 * break the ns, unconfined profile cyclic reference and forward
580 * all new unconfined profiles requests to the parent namespace
581 * This will result in all confined tasks that have a profile
582 * being removed, inheriting the parent->unconfined profile.
583 */
584 if (ns->parent)
585 ns->unconfined = aa_get_profile(ns->parent->unconfined);
586
587 destroy_namespace(ns); 516 destroy_namespace(ns);
588
589 /* release original ns->unconfined ref */
590 aa_put_profile(unconfined);
591 /* release ns->base.list ref, from removal above */
592 aa_put_namespace(ns); 517 aa_put_namespace(ns);
593} 518}
594 519
@@ -634,8 +559,26 @@ void __init aa_free_root_ns(void)
634 aa_put_namespace(ns); 559 aa_put_namespace(ns);
635} 560}
636 561
562
563static void free_replacedby(struct aa_replacedby *r)
564{
565 if (r) {
566 /* r->profile will not be updated any more as r is dead */
567 aa_put_profile(rcu_dereference_protected(r->profile, true));
568 kzfree(r);
569 }
570}
571
572
573void aa_free_replacedby_kref(struct kref *kref)
574{
575 struct aa_replacedby *r = container_of(kref, struct aa_replacedby,
576 count);
577 free_replacedby(r);
578}
579
637/** 580/**
638 * free_profile - free a profile 581 * aa_free_profile - free a profile
639 * @profile: the profile to free (MAYBE NULL) 582 * @profile: the profile to free (MAYBE NULL)
640 * 583 *
641 * Free a profile, its hats and null_profile. All references to the profile, 584 * Free a profile, its hats and null_profile. All references to the profile,
@@ -644,25 +587,16 @@ void __init aa_free_root_ns(void)
644 * If the profile was referenced from a task context, free_profile() will 587 * If the profile was referenced from a task context, free_profile() will
645 * be called from an rcu callback routine, so we must not sleep here. 588 * be called from an rcu callback routine, so we must not sleep here.
646 */ 589 */
647static void free_profile(struct aa_profile *profile) 590void aa_free_profile(struct aa_profile *profile)
648{ 591{
649 struct aa_profile *p;
650
651 AA_DEBUG("%s(%p)\n", __func__, profile); 592 AA_DEBUG("%s(%p)\n", __func__, profile);
652 593
653 if (!profile) 594 if (!profile)
654 return; 595 return;
655 596
656 if (!list_empty(&profile->base.list)) {
657 AA_ERROR("%s: internal error, "
658 "profile '%s' still on ns list\n",
659 __func__, profile->base.name);
660 BUG();
661 }
662
663 /* free children profiles */ 597 /* free children profiles */
664 policy_destroy(&profile->base); 598 policy_destroy(&profile->base);
665 aa_put_profile(profile->parent); 599 aa_put_profile(rcu_access_pointer(profile->parent));
666 600
667 aa_put_namespace(profile->ns); 601 aa_put_namespace(profile->ns);
668 kzfree(profile->rename); 602 kzfree(profile->rename);
@@ -671,44 +605,36 @@ static void free_profile(struct aa_profile *profile)
671 aa_free_cap_rules(&profile->caps); 605 aa_free_cap_rules(&profile->caps);
672 aa_free_rlimit_rules(&profile->rlimits); 606 aa_free_rlimit_rules(&profile->rlimits);
673 607
608 kzfree(profile->dirname);
674 aa_put_dfa(profile->xmatch); 609 aa_put_dfa(profile->xmatch);
675 aa_put_dfa(profile->policy.dfa); 610 aa_put_dfa(profile->policy.dfa);
611 aa_put_replacedby(profile->replacedby);
676 612
677 /* put the profile reference for replacedby, but not via 613 kzfree(profile->hash);
678 * put_profile(kref_put).
679 * replacedby can form a long chain that can result in cascading
680 * frees that blows the stack because kref_put makes a nested fn
681 * call (it looks like recursion, with free_profile calling
682 * free_profile) for each profile in the chain lp#1056078.
683 */
684 for (p = profile->replacedby; p; ) {
685 if (atomic_dec_and_test(&p->base.count.refcount)) {
686 /* no more refs on p, grab its replacedby */
687 struct aa_profile *next = p->replacedby;
688 /* break the chain */
689 p->replacedby = NULL;
690 /* now free p, chain is broken */
691 free_profile(p);
692
693 /* follow up with next profile in the chain */
694 p = next;
695 } else
696 break;
697 }
698
699 kzfree(profile); 614 kzfree(profile);
700} 615}
701 616
702/** 617/**
618 * aa_free_profile_rcu - free aa_profile by rcu (called by aa_free_profile_kref)
619 * @head: rcu_head callback for freeing of a profile (NOT NULL)
620 */
621static void aa_free_profile_rcu(struct rcu_head *head)
622{
623 struct aa_profile *p = container_of(head, struct aa_profile, rcu);
624 if (p->flags & PFLAG_NS_COUNT)
625 free_namespace(p->ns);
626 else
627 aa_free_profile(p);
628}
629
630/**
703 * aa_free_profile_kref - free aa_profile by kref (called by aa_put_profile) 631 * aa_free_profile_kref - free aa_profile by kref (called by aa_put_profile)
704 * @kr: kref callback for freeing of a profile (NOT NULL) 632 * @kr: kref callback for freeing of a profile (NOT NULL)
705 */ 633 */
706void aa_free_profile_kref(struct kref *kref) 634void aa_free_profile_kref(struct kref *kref)
707{ 635{
708 struct aa_profile *p = container_of(kref, struct aa_profile, 636 struct aa_profile *p = container_of(kref, struct aa_profile, count);
709 base.count); 637 call_rcu(&p->rcu, aa_free_profile_rcu);
710
711 free_profile(p);
712} 638}
713 639
714/** 640/**
@@ -726,13 +652,23 @@ struct aa_profile *aa_alloc_profile(const char *hname)
726 if (!profile) 652 if (!profile)
727 return NULL; 653 return NULL;
728 654
729 if (!policy_init(&profile->base, NULL, hname)) { 655 profile->replacedby = kzalloc(sizeof(struct aa_replacedby), GFP_KERNEL);
730 kzfree(profile); 656 if (!profile->replacedby)
731 return NULL; 657 goto fail;
732 } 658 kref_init(&profile->replacedby->count);
659
660 if (!policy_init(&profile->base, NULL, hname))
661 goto fail;
662 kref_init(&profile->count);
733 663
734 /* refcount released by caller */ 664 /* refcount released by caller */
735 return profile; 665 return profile;
666
667fail:
668 kzfree(profile->replacedby);
669 kzfree(profile);
670
671 return NULL;
736} 672}
737 673
738/** 674/**
@@ -772,12 +708,12 @@ struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat)
772 profile->flags |= PFLAG_HAT; 708 profile->flags |= PFLAG_HAT;
773 709
774 /* released on free_profile */ 710 /* released on free_profile */
775 profile->parent = aa_get_profile(parent); 711 rcu_assign_pointer(profile->parent, aa_get_profile(parent));
776 profile->ns = aa_get_namespace(parent->ns); 712 profile->ns = aa_get_namespace(parent->ns);
777 713
778 write_lock(&profile->ns->lock); 714 mutex_lock(&profile->ns->lock);
779 __list_add_profile(&parent->base.profiles, profile); 715 __list_add_profile(&parent->base.profiles, profile);
780 write_unlock(&profile->ns->lock); 716 mutex_unlock(&profile->ns->lock);
781 717
782 /* refcount released by caller */ 718 /* refcount released by caller */
783 return profile; 719 return profile;
@@ -793,7 +729,7 @@ fail:
793 * @head: list to search (NOT NULL) 729 * @head: list to search (NOT NULL)
794 * @name: name of profile (NOT NULL) 730 * @name: name of profile (NOT NULL)
795 * 731 *
796 * Requires: ns lock protecting list be held 732 * Requires: rcu_read_lock be held
797 * 733 *
798 * Returns: unrefcounted profile ptr, or NULL if not found 734 * Returns: unrefcounted profile ptr, or NULL if not found
799 */ 735 */
@@ -808,7 +744,7 @@ static struct aa_profile *__find_child(struct list_head *head, const char *name)
808 * @name: name of profile (NOT NULL) 744 * @name: name of profile (NOT NULL)
809 * @len: length of @name substring to match 745 * @len: length of @name substring to match
810 * 746 *
811 * Requires: ns lock protecting list be held 747 * Requires: rcu_read_lock be held
812 * 748 *
813 * Returns: unrefcounted profile ptr, or NULL if not found 749 * Returns: unrefcounted profile ptr, or NULL if not found
814 */ 750 */
@@ -829,9 +765,9 @@ struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
829{ 765{
830 struct aa_profile *profile; 766 struct aa_profile *profile;
831 767
832 read_lock(&parent->ns->lock); 768 rcu_read_lock();
833 profile = aa_get_profile(__find_child(&parent->base.profiles, name)); 769 profile = aa_get_profile(__find_child(&parent->base.profiles, name));
834 read_unlock(&parent->ns->lock); 770 rcu_read_unlock();
835 771
836 /* refcount released by caller */ 772 /* refcount released by caller */
837 return profile; 773 return profile;
@@ -846,7 +782,7 @@ struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
846 * that matches hname does not need to exist, in general this 782 * that matches hname does not need to exist, in general this
847 * is used to load a new profile. 783 * is used to load a new profile.
848 * 784 *
849 * Requires: ns->lock be held 785 * Requires: rcu_read_lock be held
850 * 786 *
851 * Returns: unrefcounted policy or NULL if not found 787 * Returns: unrefcounted policy or NULL if not found
852 */ 788 */
@@ -878,7 +814,7 @@ static struct aa_policy *__lookup_parent(struct aa_namespace *ns,
878 * @base: base list to start looking up profile name from (NOT NULL) 814 * @base: base list to start looking up profile name from (NOT NULL)
879 * @hname: hierarchical profile name (NOT NULL) 815 * @hname: hierarchical profile name (NOT NULL)
880 * 816 *
881 * Requires: ns->lock be held 817 * Requires: rcu_read_lock be held
882 * 818 *
883 * Returns: unrefcounted profile pointer or NULL if not found 819 * Returns: unrefcounted profile pointer or NULL if not found
884 * 820 *
@@ -917,13 +853,15 @@ struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *hname)
917{ 853{
918 struct aa_profile *profile; 854 struct aa_profile *profile;
919 855
920 read_lock(&ns->lock); 856 rcu_read_lock();
921 profile = aa_get_profile(__lookup_profile(&ns->base, hname)); 857 do {
922 read_unlock(&ns->lock); 858 profile = __lookup_profile(&ns->base, hname);
859 } while (profile && !aa_get_profile_not0(profile));
860 rcu_read_unlock();
923 861
924 /* the unconfined profile is not in the regular profile list */ 862 /* the unconfined profile is not in the regular profile list */
925 if (!profile && strcmp(hname, "unconfined") == 0) 863 if (!profile && strcmp(hname, "unconfined") == 0)
926 profile = aa_get_profile(ns->unconfined); 864 profile = aa_get_newest_profile(ns->unconfined);
927 865
928 /* refcount released by caller */ 866 /* refcount released by caller */
929 return profile; 867 return profile;
@@ -953,25 +891,6 @@ static int replacement_allowed(struct aa_profile *profile, int noreplace,
953} 891}
954 892
955/** 893/**
956 * __add_new_profile - simple wrapper around __list_add_profile
957 * @ns: namespace that profile is being added to (NOT NULL)
958 * @policy: the policy container to add the profile to (NOT NULL)
959 * @profile: profile to add (NOT NULL)
960 *
961 * add a profile to a list and do other required basic allocations
962 */
963static void __add_new_profile(struct aa_namespace *ns, struct aa_policy *policy,
964 struct aa_profile *profile)
965{
966 if (policy != &ns->base)
967 /* released on profile replacement or free_profile */
968 profile->parent = aa_get_profile((struct aa_profile *) policy);
969 __list_add_profile(&policy->profiles, profile);
970 /* released on free_profile */
971 profile->ns = aa_get_namespace(ns);
972}
973
974/**
975 * aa_audit_policy - Do auditing of policy changes 894 * aa_audit_policy - Do auditing of policy changes
976 * @op: policy operation being performed 895 * @op: policy operation being performed
977 * @gfp: memory allocation flags 896 * @gfp: memory allocation flags
@@ -1019,6 +938,121 @@ bool aa_may_manage_policy(int op)
1019 return 1; 938 return 1;
1020} 939}
1021 940
941static struct aa_profile *__list_lookup_parent(struct list_head *lh,
942 struct aa_profile *profile)
943{
944 const char *base = hname_tail(profile->base.hname);
945 long len = base - profile->base.hname;
946 struct aa_load_ent *ent;
947
948 /* parent won't have trailing // so remove from len */
949 if (len <= 2)
950 return NULL;
951 len -= 2;
952
953 list_for_each_entry(ent, lh, list) {
954 if (ent->new == profile)
955 continue;
956 if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
957 0 && ent->new->base.hname[len] == 0)
958 return ent->new;
959 }
960
961 return NULL;
962}
963
964/**
965 * __replace_profile - replace @old with @new on a list
966 * @old: profile to be replaced (NOT NULL)
967 * @new: profile to replace @old with (NOT NULL)
968 * @share_replacedby: transfer @old->replacedby to @new
969 *
970 * Will duplicate and refcount elements that @new inherits from @old
971 * and will inherit @old children.
972 *
973 * refcount @new for list, put @old list refcount
974 *
975 * Requires: namespace list lock be held, or list not be shared
976 */
977static void __replace_profile(struct aa_profile *old, struct aa_profile *new,
978 bool share_replacedby)
979{
980 struct aa_profile *child, *tmp;
981
982 if (!list_empty(&old->base.profiles)) {
983 LIST_HEAD(lh);
984 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
985
986 list_for_each_entry_safe(child, tmp, &lh, base.list) {
987 struct aa_profile *p;
988
989 list_del_init(&child->base.list);
990 p = __find_child(&new->base.profiles, child->base.name);
991 if (p) {
992 /* @p replaces @child */
993 __replace_profile(child, p, share_replacedby);
994 continue;
995 }
996
997 /* inherit @child and its children */
998 /* TODO: update hname of inherited children */
999 /* list refcount transferred to @new */
1000 p = aa_deref_parent(child);
1001 rcu_assign_pointer(child->parent, aa_get_profile(new));
1002 list_add_rcu(&child->base.list, &new->base.profiles);
1003 aa_put_profile(p);
1004 }
1005 }
1006
1007 if (!rcu_access_pointer(new->parent)) {
1008 struct aa_profile *parent = aa_deref_parent(old);
1009 rcu_assign_pointer(new->parent, aa_get_profile(parent));
1010 }
1011 __aa_update_replacedby(old, new);
1012 if (share_replacedby) {
1013 aa_put_replacedby(new->replacedby);
1014 new->replacedby = aa_get_replacedby(old->replacedby);
1015 } else if (!rcu_access_pointer(new->replacedby->profile))
1016 /* aafs interface uses replacedby */
1017 rcu_assign_pointer(new->replacedby->profile,
1018 aa_get_profile(new));
1019 __aa_fs_profile_migrate_dents(old, new);
1020
1021 if (list_empty(&new->base.list)) {
1022 /* new is not on a list already */
1023 list_replace_rcu(&old->base.list, &new->base.list);
1024 aa_get_profile(new);
1025 aa_put_profile(old);
1026 } else
1027 __list_remove_profile(old);
1028}
1029
1030/**
1031 * __lookup_replace - lookup replacement information for a profile
1032 * @ns - namespace the lookup occurs in
1033 * @hname - name of profile to lookup
1034 * @noreplace - true if not replacing an existing profile
1035 * @p - Returns: profile to be replaced
1036 * @info - Returns: info string on why lookup failed
1037 *
1038 * Returns: profile to replace (no ref) on success else ptr error
1039 */
1040static int __lookup_replace(struct aa_namespace *ns, const char *hname,
1041 bool noreplace, struct aa_profile **p,
1042 const char **info)
1043{
1044 *p = aa_get_profile(__lookup_profile(&ns->base, hname));
1045 if (*p) {
1046 int error = replacement_allowed(*p, noreplace, info);
1047 if (error) {
1048 *info = "profile can not be replaced";
1049 return error;
1050 }
1051 }
1052
1053 return 0;
1054}
1055
1022/** 1056/**
1023 * aa_replace_profiles - replace profile(s) on the profile list 1057 * aa_replace_profiles - replace profile(s) on the profile list
1024 * @udata: serialized data stream (NOT NULL) 1058 * @udata: serialized data stream (NOT NULL)
@@ -1033,21 +1067,17 @@ bool aa_may_manage_policy(int op)
1033 */ 1067 */
1034ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace) 1068ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace)
1035{ 1069{
1036 struct aa_policy *policy;
1037 struct aa_profile *old_profile = NULL, *new_profile = NULL;
1038 struct aa_profile *rename_profile = NULL;
1039 struct aa_namespace *ns = NULL;
1040 const char *ns_name, *name = NULL, *info = NULL; 1070 const char *ns_name, *name = NULL, *info = NULL;
1071 struct aa_namespace *ns = NULL;
1072 struct aa_load_ent *ent, *tmp;
1041 int op = OP_PROF_REPL; 1073 int op = OP_PROF_REPL;
1042 ssize_t error; 1074 ssize_t error;
1075 LIST_HEAD(lh);
1043 1076
1044 /* released below */ 1077 /* released below */
1045 new_profile = aa_unpack(udata, size, &ns_name); 1078 error = aa_unpack(udata, size, &lh, &ns_name);
1046 if (IS_ERR(new_profile)) { 1079 if (error)
1047 error = PTR_ERR(new_profile); 1080 goto out;
1048 new_profile = NULL;
1049 goto fail;
1050 }
1051 1081
1052 /* released below */ 1082 /* released below */
1053 ns = aa_prepare_namespace(ns_name); 1083 ns = aa_prepare_namespace(ns_name);
@@ -1058,71 +1088,140 @@ ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace)
1058 goto fail; 1088 goto fail;
1059 } 1089 }
1060 1090
1061 name = new_profile->base.hname; 1091 mutex_lock(&ns->lock);
1062 1092 /* setup parent and ns info */
1063 write_lock(&ns->lock); 1093 list_for_each_entry(ent, &lh, list) {
1064 /* no ref on policy only use inside lock */ 1094 struct aa_policy *policy;
1065 policy = __lookup_parent(ns, new_profile->base.hname); 1095
1096 name = ent->new->base.hname;
1097 error = __lookup_replace(ns, ent->new->base.hname, noreplace,
1098 &ent->old, &info);
1099 if (error)
1100 goto fail_lock;
1101
1102 if (ent->new->rename) {
1103 error = __lookup_replace(ns, ent->new->rename,
1104 noreplace, &ent->rename,
1105 &info);
1106 if (error)
1107 goto fail_lock;
1108 }
1066 1109
1067 if (!policy) { 1110 /* released when @new is freed */
1068 info = "parent does not exist"; 1111 ent->new->ns = aa_get_namespace(ns);
1069 error = -ENOENT; 1112
1070 goto audit; 1113 if (ent->old || ent->rename)
1114 continue;
1115
1116 /* no ref on policy only use inside lock */
1117 policy = __lookup_parent(ns, ent->new->base.hname);
1118 if (!policy) {
1119 struct aa_profile *p;
1120 p = __list_lookup_parent(&lh, ent->new);
1121 if (!p) {
1122 error = -ENOENT;
1123 info = "parent does not exist";
1124 name = ent->new->base.hname;
1125 goto fail_lock;
1126 }
1127 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1128 } else if (policy != &ns->base) {
1129 /* released on profile replacement or free_profile */
1130 struct aa_profile *p = (struct aa_profile *) policy;
1131 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1132 }
1071 } 1133 }
1072 1134
1073 old_profile = __find_child(&policy->profiles, new_profile->base.name); 1135 /* create new fs entries for introspection if needed */
1074 /* released below */ 1136 list_for_each_entry(ent, &lh, list) {
1075 aa_get_profile(old_profile); 1137 if (ent->old) {
1138 /* inherit old interface files */
1076 1139
1077 if (new_profile->rename) { 1140 /* if (ent->rename)
1078 rename_profile = __lookup_profile(&ns->base, 1141 TODO: support rename */
1079 new_profile->rename); 1142 /* } else if (ent->rename) {
1080 /* released below */ 1143 TODO: support rename */
1081 aa_get_profile(rename_profile); 1144 } else {
1145 struct dentry *parent;
1146 if (rcu_access_pointer(ent->new->parent)) {
1147 struct aa_profile *p;
1148 p = aa_deref_parent(ent->new);
1149 parent = prof_child_dir(p);
1150 } else
1151 parent = ns_subprofs_dir(ent->new->ns);
1152 error = __aa_fs_profile_mkdir(ent->new, parent);
1153 }
1082 1154
1083 if (!rename_profile) { 1155 if (error) {
1084 info = "profile to rename does not exist"; 1156 info = "failed to create ";
1085 name = new_profile->rename; 1157 goto fail_lock;
1086 error = -ENOENT;
1087 goto audit;
1088 } 1158 }
1089 } 1159 }
1090 1160
1091 error = replacement_allowed(old_profile, noreplace, &info); 1161 /* Done with checks that may fail - do actual replacement */
1092 if (error) 1162 list_for_each_entry_safe(ent, tmp, &lh, list) {
1093 goto audit; 1163 list_del_init(&ent->list);
1094 1164 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
1095 error = replacement_allowed(rename_profile, noreplace, &info); 1165
1096 if (error) 1166 audit_policy(op, GFP_ATOMIC, ent->new->base.name, NULL, error);
1097 goto audit; 1167
1098 1168 if (ent->old) {
1099audit: 1169 __replace_profile(ent->old, ent->new, 1);
1100 if (!old_profile && !rename_profile) 1170 if (ent->rename) {
1101 op = OP_PROF_LOAD; 1171 /* aafs interface uses replacedby */
1102 1172 struct aa_replacedby *r = ent->new->replacedby;
1103 error = audit_policy(op, GFP_ATOMIC, name, info, error); 1173 rcu_assign_pointer(r->profile,
1104 1174 aa_get_profile(ent->new));
1105 if (!error) { 1175 __replace_profile(ent->rename, ent->new, 0);
1106 if (rename_profile) 1176 }
1107 __replace_profile(rename_profile, new_profile); 1177 } else if (ent->rename) {
1108 if (old_profile) 1178 /* aafs interface uses replacedby */
1109 __replace_profile(old_profile, new_profile); 1179 rcu_assign_pointer(ent->new->replacedby->profile,
1110 if (!(old_profile || rename_profile)) 1180 aa_get_profile(ent->new));
1111 __add_new_profile(ns, policy, new_profile); 1181 __replace_profile(ent->rename, ent->new, 0);
1182 } else if (ent->new->parent) {
1183 struct aa_profile *parent, *newest;
1184 parent = aa_deref_parent(ent->new);
1185 newest = aa_get_newest_profile(parent);
1186
1187 /* parent replaced in this atomic set? */
1188 if (newest != parent) {
1189 aa_get_profile(newest);
1190 aa_put_profile(parent);
1191 rcu_assign_pointer(ent->new->parent, newest);
1192 } else
1193 aa_put_profile(newest);
1194 /* aafs interface uses replacedby */
1195 rcu_assign_pointer(ent->new->replacedby->profile,
1196 aa_get_profile(ent->new));
1197 __list_add_profile(&parent->base.profiles, ent->new);
1198 } else {
1199 /* aafs interface uses replacedby */
1200 rcu_assign_pointer(ent->new->replacedby->profile,
1201 aa_get_profile(ent->new));
1202 __list_add_profile(&ns->base.profiles, ent->new);
1203 }
1204 aa_load_ent_free(ent);
1112 } 1205 }
1113 write_unlock(&ns->lock); 1206 mutex_unlock(&ns->lock);
1114 1207
1115out: 1208out:
1116 aa_put_namespace(ns); 1209 aa_put_namespace(ns);
1117 aa_put_profile(rename_profile); 1210
1118 aa_put_profile(old_profile);
1119 aa_put_profile(new_profile);
1120 if (error) 1211 if (error)
1121 return error; 1212 return error;
1122 return size; 1213 return size;
1123 1214
1215fail_lock:
1216 mutex_unlock(&ns->lock);
1124fail: 1217fail:
1125 error = audit_policy(op, GFP_KERNEL, name, info, error); 1218 error = audit_policy(op, GFP_KERNEL, name, info, error);
1219
1220 list_for_each_entry_safe(ent, tmp, &lh, list) {
1221 list_del_init(&ent->list);
1222 aa_load_ent_free(ent);
1223 }
1224
1126 goto out; 1225 goto out;
1127} 1226}
1128 1227
@@ -1169,12 +1268,12 @@ ssize_t aa_remove_profiles(char *fqname, size_t size)
1169 1268
1170 if (!name) { 1269 if (!name) {
1171 /* remove namespace - can only happen if fqname[0] == ':' */ 1270 /* remove namespace - can only happen if fqname[0] == ':' */
1172 write_lock(&ns->parent->lock); 1271 mutex_lock(&ns->parent->lock);
1173 __remove_namespace(ns); 1272 __remove_namespace(ns);
1174 write_unlock(&ns->parent->lock); 1273 mutex_unlock(&ns->parent->lock);
1175 } else { 1274 } else {
1176 /* remove profile */ 1275 /* remove profile */
1177 write_lock(&ns->lock); 1276 mutex_lock(&ns->lock);
1178 profile = aa_get_profile(__lookup_profile(&ns->base, name)); 1277 profile = aa_get_profile(__lookup_profile(&ns->base, name));
1179 if (!profile) { 1278 if (!profile) {
1180 error = -ENOENT; 1279 error = -ENOENT;
@@ -1183,7 +1282,7 @@ ssize_t aa_remove_profiles(char *fqname, size_t size)
1183 } 1282 }
1184 name = profile->base.hname; 1283 name = profile->base.hname;
1185 __remove_profile(profile); 1284 __remove_profile(profile);
1186 write_unlock(&ns->lock); 1285 mutex_unlock(&ns->lock);
1187 } 1286 }
1188 1287
1189 /* don't fail removal if audit fails */ 1288 /* don't fail removal if audit fails */
@@ -1193,7 +1292,7 @@ ssize_t aa_remove_profiles(char *fqname, size_t size)
1193 return size; 1292 return size;
1194 1293
1195fail_ns_lock: 1294fail_ns_lock:
1196 write_unlock(&ns->lock); 1295 mutex_unlock(&ns->lock);
1197 aa_put_namespace(ns); 1296 aa_put_namespace(ns);
1198 1297
1199fail: 1298fail: