1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor label definitions
7 * Copyright 2017 Canonical Ltd.
10 #include <linux/audit.h>
11 #include <linux/seq_file.h>
12 #include <linux/sort.h>
14 #include "include/apparmor.h"
15 #include "include/cred.h"
16 #include "include/label.h"
17 #include "include/policy.h"
18 #include "include/secid.h"
22 * the aa_label represents the set of profiles confining an object
24 * Labels maintain a reference count to the set of pointers they reference
25 * Labels are ref counted by
26 * tasks and object via the security field/security context off the field
27 * code - will take a ref count on a label if it needs the label
28 * beyond what is possible with an rcu_read_lock.
29 * profiles - each profile is a label
30 * secids - a pinned secid will keep a refcount of the label it is
32 * objects - inode, files, sockets, ...
34 * Labels are not ref counted by the label set, so they maybe removed and
35 * freed when no longer in use.
39 #define PROXY_POISON 97
40 #define LABEL_POISON 100
42 static void free_proxy(struct aa_proxy *proxy)
45 /* p->label will not updated any more as p is dead */
46 aa_put_label(rcu_dereference_protected(proxy->label, true));
47 memset(proxy, 0, sizeof(*proxy));
48 RCU_INIT_POINTER(proxy->label, (struct aa_label *)PROXY_POISON);
53 void aa_proxy_kref(struct kref *kref)
55 struct aa_proxy *proxy = container_of(kref, struct aa_proxy, count);
60 struct aa_proxy *aa_alloc_proxy(struct aa_label *label, gfp_t gfp)
64 new = kzalloc(sizeof(struct aa_proxy), gfp);
66 kref_init(&new->count);
67 rcu_assign_pointer(new->label, aa_get_label(label));
72 /* requires profile list write lock held */
73 void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new)
79 lockdep_assert_held_write(&labels_set(orig)->lock);
81 tmp = rcu_dereference_protected(orig->proxy->label,
82 &labels_ns(orig)->lock);
83 rcu_assign_pointer(orig->proxy->label, aa_get_label(new));
84 orig->flags |= FLAG_STALE;
88 static void __proxy_share(struct aa_label *old, struct aa_label *new)
90 struct aa_proxy *proxy = new->proxy;
92 new->proxy = aa_get_proxy(old->proxy);
93 __aa_proxy_redirect(old, new);
99 * ns_cmp - compare ns for label set ordering
100 * @a: ns to compare (NOT NULL)
101 * @b: ns to compare (NOT NULL)
103 * Returns: <0 if a < b
107 static int ns_cmp(struct aa_ns *a, struct aa_ns *b)
113 AA_BUG(!a->base.hname);
114 AA_BUG(!b->base.hname);
119 res = a->level - b->level;
123 return strcmp(a->base.hname, b->base.hname);
127 * profile_cmp - profile comparison for set ordering
128 * @a: profile to compare (NOT NULL)
129 * @b: profile to compare (NOT NULL)
131 * Returns: <0 if a < b
135 static int profile_cmp(struct aa_profile *a, struct aa_profile *b)
143 AA_BUG(!a->base.hname);
144 AA_BUG(!b->base.hname);
146 if (a == b || a->base.hname == b->base.hname)
148 res = ns_cmp(a->ns, b->ns);
152 return strcmp(a->base.hname, b->base.hname);
156 * vec_cmp - label comparison for set ordering
157 * @a: label to compare (NOT NULL)
158 * @vec: vector of profiles to compare (NOT NULL)
161 * Returns: <0 if a < vec
165 static int vec_cmp(struct aa_profile **a, int an, struct aa_profile **b, int bn)
176 for (i = 0; i < an && i < bn; i++) {
177 int res = profile_cmp(a[i], b[i]);
186 static bool vec_is_stale(struct aa_profile **vec, int n)
192 for (i = 0; i < n; i++) {
193 if (profile_is_stale(vec[i]))
200 static long union_vec_flags(struct aa_profile **vec, int n, long mask)
207 for (i = 0; i < n; i++) {
208 u |= vec[i]->label.flags & mask;
214 static int sort_cmp(const void *a, const void *b)
216 return profile_cmp(*(struct aa_profile **)a, *(struct aa_profile **)b);
220 * assumes vec is sorted
221 * Assumes @vec has null terminator at vec[n], and will null terminate
224 static inline int unique(struct aa_profile **vec, int n)
226 int i, pos, dups = 0;
232 for (i = 1; i < n; i++) {
233 int res = profile_cmp(vec[pos], vec[i]);
235 AA_BUG(res > 0, "vec not sorted");
238 aa_put_profile(vec[i]);
253 * aa_vec_unique - canonical sort and unique a list of profiles
254 * @n: number of refcounted profiles in the list (@n > 0)
255 * @vec: list of profiles to sort and merge
257 * Returns: the number of duplicates eliminated == references put
259 * If @flags & VEC_FLAG_TERMINATE @vec has null terminator at vec[n], and will
260 * null terminate vec[n - dups]
262 int aa_vec_unique(struct aa_profile **vec, int n, int flags)
269 /* vecs are usually small and inorder, have a fallback for larger */
271 sort(vec, n, sizeof(struct aa_profile *), sort_cmp, NULL);
272 dups = unique(vec, n);
276 /* insertion sort + unique in one */
277 for (i = 1; i < n; i++) {
278 struct aa_profile *tmp = vec[i];
281 for (pos = i - 1 - dups; pos >= 0; pos--) {
282 int res = profile_cmp(vec[pos], tmp);
285 /* drop duplicate entry */
292 /* pos is at entry < tmp, or index -1. Set to insert pos */
295 for (j = i - dups; j > pos; j--)
305 if (flags & VEC_FLAG_TERMINATE)
306 vec[n - dups] = NULL;
312 void aa_label_destroy(struct aa_label *label)
316 if (!label_isprofile(label)) {
317 struct aa_profile *profile;
320 aa_put_str(label->hname);
322 label_for_each(i, label, profile) {
323 aa_put_profile(profile);
324 label->vec[i.i] = (struct aa_profile *)
325 (LABEL_POISON + (long) i.i);
330 if (rcu_dereference_protected(label->proxy->label, true) == label)
331 rcu_assign_pointer(label->proxy->label, NULL);
332 aa_put_proxy(label->proxy);
334 aa_free_secid(label->secid);
336 label->proxy = (struct aa_proxy *) PROXY_POISON + 1;
339 void aa_label_free(struct aa_label *label)
344 aa_label_destroy(label);
348 static void label_free_switch(struct aa_label *label)
350 if (label->flags & FLAG_NS_COUNT)
351 aa_free_ns(labels_ns(label));
352 else if (label_isprofile(label))
353 aa_free_profile(labels_profile(label));
355 aa_label_free(label);
358 static void label_free_rcu(struct rcu_head *head)
360 struct aa_label *label = container_of(head, struct aa_label, rcu);
362 if (label->flags & FLAG_IN_TREE)
363 (void) aa_label_remove(label);
364 label_free_switch(label);
367 void aa_label_kref(struct kref *kref)
369 struct aa_label *label = container_of(kref, struct aa_label, count);
370 struct aa_ns *ns = labels_ns(label);
373 /* never live, no rcu callback needed, just using the fn */
374 label_free_switch(label);
377 /* TODO: update labels_profile macro so it works here */
378 AA_BUG(label_isprofile(label) &&
379 on_list_rcu(&label->vec[0]->base.profiles));
380 AA_BUG(label_isprofile(label) &&
381 on_list_rcu(&label->vec[0]->base.list));
383 /* TODO: if compound label and not stale add to reclaim cache */
384 call_rcu(&label->rcu, label_free_rcu);
387 static void label_free_or_put_new(struct aa_label *label, struct aa_label *new)
390 /* need to free directly to break circular ref with proxy */
396 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp)
401 if (aa_alloc_secid(label, gfp) < 0)
404 label->size = size; /* doesn't include null */
405 label->vec[size] = NULL; /* null terminate */
406 kref_init(&label->count);
407 RB_CLEAR_NODE(&label->node);
413 * aa_label_alloc - allocate a label with a profile vector of @size length
414 * @size: size of profile vector in the label
415 * @proxy: proxy to use OR null if to allocate a new one
416 * @gfp: memory allocation type
419 * else NULL if failed
421 struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp)
423 struct aa_label *new;
427 /* + 1 for null terminator entry on vec */
428 new = kzalloc(struct_size(new, vec, size + 1), gfp);
429 AA_DEBUG("%s (%p)\n", __func__, new);
433 if (!aa_label_init(new, size, gfp))
437 proxy = aa_alloc_proxy(new, gfp);
442 /* just set new's proxy, don't redirect proxy here if it was passed in*/
455 * label_cmp - label comparison for set ordering
456 * @a: label to compare (NOT NULL)
457 * @b: label to compare (NOT NULL)
459 * Returns: <0 if a < b
463 static int label_cmp(struct aa_label *a, struct aa_label *b)
470 return vec_cmp(a->vec, a->size, b->vec, b->size);
473 /* helper fn for label_for_each_confined */
474 int aa_label_next_confined(struct aa_label *label, int i)
479 for (; i < label->size; i++) {
480 if (!profile_unconfined(label->vec[i]))
488 * __aa_label_next_not_in_set - return the next profile of @sub not in @set
490 * @set: label to test against
491 * @sub: label to if is subset of @set
493 * Returns: profile in @sub that is not in @set, with iterator set pos after
494 * else NULL if @sub is a subset of @set
496 struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
497 struct aa_label *set,
498 struct aa_label *sub)
503 AA_BUG(I->i > set->size);
506 AA_BUG(I->j > sub->size);
508 while (I->j < sub->size && I->i < set->size) {
509 int res = profile_cmp(sub->vec[I->j], set->vec[I->i]);
517 return sub->vec[(I->j)++];
520 if (I->j < sub->size)
521 return sub->vec[(I->j)++];
527 * aa_label_is_subset - test if @sub is a subset of @set
528 * @set: label to test against
529 * @sub: label to test if is subset of @set
531 * Returns: true if @sub is subset of @set
534 bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub)
536 struct label_it i = { };
544 return __aa_label_next_not_in_set(&i, set, sub) == NULL;
548 * aa_label_is_unconfined_subset - test if @sub is a subset of @set
549 * @set: label to test against
550 * @sub: label to test if is subset of @set
552 * This checks for subset but taking into account unconfined. IF
553 * @sub contains an unconfined profile that does not have a matching
554 * unconfined in @set then this will not cause the test to fail.
555 * Conversely we don't care about an unconfined in @set that is not in
558 * Returns: true if @sub is special_subset of @set
561 bool aa_label_is_unconfined_subset(struct aa_label *set, struct aa_label *sub)
563 struct label_it i = { };
564 struct aa_profile *p;
573 p = __aa_label_next_not_in_set(&i, set, sub);
574 if (p && !profile_unconfined(p))
583 * __label_remove - remove @label from the label set
584 * @l: label to remove
585 * @new: label to redirect to
587 * Requires: labels_set(@label)->lock write_lock
588 * Returns: true if the label was in the tree and removed
590 static bool __label_remove(struct aa_label *label, struct aa_label *new)
592 struct aa_labelset *ls = labels_set(label);
596 lockdep_assert_held_write(&ls->lock);
599 __aa_proxy_redirect(label, new);
601 if (!label_is_stale(label))
602 __label_make_stale(label);
604 if (label->flags & FLAG_IN_TREE) {
605 rb_erase(&label->node, &ls->root);
606 label->flags &= ~FLAG_IN_TREE;
614 * __label_replace - replace @old with @new in label set
615 * @old: label to remove from label set
616 * @new: label to replace @old with
618 * Requires: labels_set(@old)->lock write_lock
619 * valid ref count be held on @new
620 * Returns: true if @old was in set and replaced by @new
622 * Note: current implementation requires label set be order in such a way
623 * that @new directly replaces @old position in the set (ie.
624 * using pointer comparison of the label address would not work)
626 static bool __label_replace(struct aa_label *old, struct aa_label *new)
628 struct aa_labelset *ls = labels_set(old);
633 lockdep_assert_held_write(&ls->lock);
634 AA_BUG(new->flags & FLAG_IN_TREE);
636 if (!label_is_stale(old))
637 __label_make_stale(old);
639 if (old->flags & FLAG_IN_TREE) {
640 rb_replace_node(&old->node, &new->node, &ls->root);
641 old->flags &= ~FLAG_IN_TREE;
642 new->flags |= FLAG_IN_TREE;
650 * __label_insert - attempt to insert @l into a label set
651 * @ls: set of labels to insert @l into (NOT NULL)
652 * @label: new label to insert (NOT NULL)
653 * @replace: whether insertion should replace existing entry that is not stale
655 * Requires: @ls->lock
656 * caller to hold a valid ref on l
657 * if @replace is true l has a preallocated proxy associated
658 * Returns: @l if successful in inserting @l - with additional refcount
659 * else ref counted equivalent label that is already in the set,
660 * the else condition only happens if @replace is false
662 static struct aa_label *__label_insert(struct aa_labelset *ls,
663 struct aa_label *label, bool replace)
665 struct rb_node **new, *parent = NULL;
669 AA_BUG(labels_set(label) != ls);
670 lockdep_assert_held_write(&ls->lock);
671 AA_BUG(label->flags & FLAG_IN_TREE);
673 /* Figure out where to put new node */
674 new = &ls->root.rb_node;
676 struct aa_label *this = rb_entry(*new, struct aa_label, node);
677 int result = label_cmp(label, this);
681 /* !__aa_get_label means queued for destruction,
682 * so replace in place, however the label has
683 * died before the replacement so do not share
686 if (!replace && !label_is_stale(this)) {
687 if (__aa_get_label(this))
690 __proxy_share(this, label);
691 AA_BUG(!__label_replace(this, label));
692 return aa_get_label(label);
693 } else if (result < 0)
694 new = &((*new)->rb_left);
695 else /* (result > 0) */
696 new = &((*new)->rb_right);
699 /* Add new node and rebalance tree. */
700 rb_link_node(&label->node, parent, new);
701 rb_insert_color(&label->node, &ls->root);
702 label->flags |= FLAG_IN_TREE;
704 return aa_get_label(label);
708 * __vec_find - find label that matches @vec in label set
709 * @vec: vec of profiles to find matching label for (NOT NULL)
712 * Requires: @vec_labelset(vec) lock held
713 * caller to hold a valid ref on l
715 * Returns: ref counted @label if matching label is in tree
716 * ref counted label that is equiv to @l in tree
717 * else NULL if @vec equiv is not in tree
719 static struct aa_label *__vec_find(struct aa_profile **vec, int n)
721 struct rb_node *node;
727 node = vec_labelset(vec, n)->root.rb_node;
729 struct aa_label *this = rb_entry(node, struct aa_label, node);
730 int result = vec_cmp(this->vec, this->size, vec, n);
733 node = node->rb_left;
735 node = node->rb_right;
737 return __aa_get_label(this);
744 * __label_find - find label @label in label set
745 * @label: label to find (NOT NULL)
747 * Requires: labels_set(@label)->lock held
748 * caller to hold a valid ref on l
750 * Returns: ref counted @label if @label is in tree OR
751 * ref counted label that is equiv to @label in tree
752 * else NULL if @label or equiv is not in tree
754 static struct aa_label *__label_find(struct aa_label *label)
758 return __vec_find(label->vec, label->size);
763 * aa_label_remove - remove a label from the labelset
764 * @label: label to remove
766 * Returns: true if @label was removed from the tree
767 * else @label was not in tree so it could not be removed
769 bool aa_label_remove(struct aa_label *label)
771 struct aa_labelset *ls = labels_set(label);
777 write_lock_irqsave(&ls->lock, flags);
778 res = __label_remove(label, ns_unconfined(labels_ns(label)));
779 write_unlock_irqrestore(&ls->lock, flags);
785 * aa_label_replace - replace a label @old with a new version @new
786 * @old: label to replace
787 * @new: label replacing @old
789 * Returns: true if @old was in tree and replaced
790 * else @old was not in tree, and @new was not inserted
792 bool aa_label_replace(struct aa_label *old, struct aa_label *new)
797 if (name_is_shared(old, new) && labels_ns(old) == labels_ns(new)) {
798 write_lock_irqsave(&labels_set(old)->lock, flags);
799 if (old->proxy != new->proxy)
800 __proxy_share(old, new);
802 __aa_proxy_redirect(old, new);
803 res = __label_replace(old, new);
804 write_unlock_irqrestore(&labels_set(old)->lock, flags);
807 struct aa_labelset *ls = labels_set(old);
809 write_lock_irqsave(&ls->lock, flags);
810 res = __label_remove(old, new);
811 if (labels_ns(old) != labels_ns(new)) {
812 write_unlock_irqrestore(&ls->lock, flags);
813 ls = labels_set(new);
814 write_lock_irqsave(&ls->lock, flags);
816 l = __label_insert(ls, new, true);
818 write_unlock_irqrestore(&ls->lock, flags);
826 * vec_find - find label @l in label set
827 * @vec: array of profiles to find equiv label for (NOT NULL)
830 * Returns: refcounted label if @vec equiv is in tree
831 * else NULL if @vec equiv is not in tree
833 static struct aa_label *vec_find(struct aa_profile **vec, int n)
835 struct aa_labelset *ls;
836 struct aa_label *label;
843 ls = vec_labelset(vec, n);
844 read_lock_irqsave(&ls->lock, flags);
845 label = __vec_find(vec, n);
846 read_unlock_irqrestore(&ls->lock, flags);
851 /* requires sort and merge done first */
852 static struct aa_label *vec_create_and_insert_label(struct aa_profile **vec,
855 struct aa_label *label = NULL;
856 struct aa_labelset *ls;
858 struct aa_label *new;
864 return aa_get_label(&vec[0]->label);
866 ls = labels_set(&vec[len - 1]->label);
868 /* TODO: enable when read side is lockless
869 * check if label exists before taking locks
871 new = aa_label_alloc(len, NULL, gfp);
875 for (i = 0; i < len; i++)
876 new->vec[i] = aa_get_profile(vec[i]);
878 write_lock_irqsave(&ls->lock, flags);
879 label = __label_insert(ls, new, false);
880 write_unlock_irqrestore(&ls->lock, flags);
881 label_free_or_put_new(label, new);
886 struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
889 struct aa_label *label = vec_find(vec, len);
894 return vec_create_and_insert_label(vec, len, gfp);
898 * aa_label_find - find label @label in label set
899 * @label: label to find (NOT NULL)
901 * Requires: caller to hold a valid ref on l
903 * Returns: refcounted @label if @label is in tree
904 * refcounted label that is equiv to @label in tree
905 * else NULL if @label or equiv is not in tree
907 struct aa_label *aa_label_find(struct aa_label *label)
911 return vec_find(label->vec, label->size);
916 * aa_label_insert - insert label @label into @ls or return existing label
917 * @ls - labelset to insert @label into
918 * @label - label to insert
920 * Requires: caller to hold a valid ref on @label
922 * Returns: ref counted @label if successful in inserting @label
923 * else ref counted equivalent label that is already in the set
925 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *label)
933 /* check if label exists before taking lock */
934 if (!label_is_stale(label)) {
935 read_lock_irqsave(&ls->lock, flags);
936 l = __label_find(label);
937 read_unlock_irqrestore(&ls->lock, flags);
942 write_lock_irqsave(&ls->lock, flags);
943 l = __label_insert(ls, label, false);
944 write_unlock_irqrestore(&ls->lock, flags);
951 * aa_label_next_in_merge - find the next profile when merging @a and @b
956 * Returns: next profile
957 * else null if no more profiles
959 struct aa_profile *aa_label_next_in_merge(struct label_it *I,
967 AA_BUG(I->i > a->size);
969 AA_BUG(I->j > b->size);
971 if (I->i < a->size) {
972 if (I->j < b->size) {
973 int res = profile_cmp(a->vec[I->i], b->vec[I->j]);
976 return b->vec[(I->j)++];
981 return a->vec[(I->i)++];
985 return b->vec[(I->j)++];
991 * label_merge_cmp - cmp of @a merging with @b against @z for set ordering
992 * @a: label to merge then compare (NOT NULL)
993 * @b: label to merge then compare (NOT NULL)
994 * @z: label to compare merge against (NOT NULL)
996 * Assumes: using the most recent versions of @a, @b, and @z
998 * Returns: <0 if a < b
1002 static int label_merge_cmp(struct aa_label *a, struct aa_label *b,
1005 struct aa_profile *p = NULL;
1006 struct label_it i = { };
1014 k < z->size && (p = aa_label_next_in_merge(&i, a, b));
1016 int res = profile_cmp(p, z->vec[k]);
1024 else if (k < z->size)
1030 * label_merge_insert - create a new label by merging @a and @b
1031 * @new: preallocated label to merge into (NOT NULL)
1032 * @a: label to merge with @b (NOT NULL)
1033 * @b: label to merge with @a (NOT NULL)
1035 * Requires: preallocated proxy
1037 * Returns: ref counted label either @new if merge is unique
1038 * @a if @b is a subset of @a
1039 * @b if @a is a subset of @b
1041 * NOTE: will not use @new if the merge results in @new == @a or @b
1043 * Must be used within labelset write lock to avoid racing with
1044 * setting labels stale.
1046 static struct aa_label *label_merge_insert(struct aa_label *new,
1050 struct aa_label *label;
1051 struct aa_labelset *ls;
1052 struct aa_profile *next;
1054 unsigned long flags;
1055 int k = 0, invcount = 0;
1059 AA_BUG(a->size < 0);
1061 AA_BUG(b->size < 0);
1063 AA_BUG(new->size < a->size + b->size);
1065 label_for_each_in_merge(i, a, b, next) {
1067 if (profile_is_stale(next)) {
1068 new->vec[k] = aa_get_newest_profile(next);
1069 AA_BUG(!new->vec[k]->label.proxy);
1070 AA_BUG(!new->vec[k]->label.proxy->label);
1071 if (next->label.proxy != new->vec[k]->label.proxy)
1076 new->vec[k++] = aa_get_profile(next);
1078 /* set to actual size which is <= allocated len */
1083 new->size -= aa_vec_unique(&new->vec[0], new->size,
1084 VEC_FLAG_TERMINATE);
1085 /* TODO: deal with reference labels */
1086 if (new->size == 1) {
1087 label = aa_get_label(&new->vec[0]->label);
1090 } else if (!stale) {
1092 * merge could be same as a || b, note: it is not possible
1093 * for new->size == a->size == b->size unless a == b
1096 return aa_get_label(a);
1097 else if (k == b->size)
1098 return aa_get_label(b);
1100 new->flags |= union_vec_flags(new->vec, new->size, FLAG_UNCONFINED |
1101 FLAG_DEBUG1 | FLAG_DEBUG2);
1102 ls = labels_set(new);
1103 write_lock_irqsave(&ls->lock, flags);
1104 label = __label_insert(labels_set(new), new, false);
1105 write_unlock_irqrestore(&ls->lock, flags);
1111 * labelset_of_merge - find which labelset a merged label should be inserted
1112 * @a: label to merge and insert
1113 * @b: label to merge and insert
1115 * Returns: labelset that the merged label should be inserted into
1117 static struct aa_labelset *labelset_of_merge(struct aa_label *a,
1120 struct aa_ns *nsa = labels_ns(a);
1121 struct aa_ns *nsb = labels_ns(b);
1123 if (ns_cmp(nsa, nsb) <= 0)
1124 return &nsa->labels;
1125 return &nsb->labels;
1129 * __label_find_merge - find label that is equiv to merge of @a and @b
1130 * @ls: set of labels to search (NOT NULL)
1131 * @a: label to merge with @b (NOT NULL)
1132 * @b: label to merge with @a (NOT NULL)
1134 * Requires: ls->lock read_lock held
1136 * Returns: ref counted label that is equiv to merge of @a and @b
1137 * else NULL if merge of @a and @b is not in set
1139 static struct aa_label *__label_find_merge(struct aa_labelset *ls,
1143 struct rb_node *node;
1150 return __label_find(a);
1152 node = ls->root.rb_node;
1154 struct aa_label *this = container_of(node, struct aa_label,
1156 int result = label_merge_cmp(a, b, this);
1159 node = node->rb_left;
1160 else if (result > 0)
1161 node = node->rb_right;
1163 return __aa_get_label(this);
1171 * aa_label_find_merge - find label that is equiv to merge of @a and @b
1172 * @a: label to merge with @b (NOT NULL)
1173 * @b: label to merge with @a (NOT NULL)
1175 * Requires: labels be fully constructed with a valid ns
1177 * Returns: ref counted label that is equiv to merge of @a and @b
1178 * else NULL if merge of @a and @b is not in set
1180 struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b)
1182 struct aa_labelset *ls;
1183 struct aa_label *label, *ar = NULL, *br = NULL;
1184 unsigned long flags;
1189 if (label_is_stale(a))
1190 a = ar = aa_get_newest_label(a);
1191 if (label_is_stale(b))
1192 b = br = aa_get_newest_label(b);
1193 ls = labelset_of_merge(a, b);
1194 read_lock_irqsave(&ls->lock, flags);
1195 label = __label_find_merge(ls, a, b);
1196 read_unlock_irqrestore(&ls->lock, flags);
1204 * aa_label_merge - attempt to insert new merged label of @a and @b
1205 * @ls: set of labels to insert label into (NOT NULL)
1206 * @a: label to merge with @b (NOT NULL)
1207 * @b: label to merge with @a (NOT NULL)
1208 * @gfp: memory allocation type
1210 * Requires: caller to hold valid refs on @a and @b
1211 * labels be fully constructed with a valid ns
1213 * Returns: ref counted new label if successful in inserting merge of a & b
1214 * else ref counted equivalent label that is already in the set.
1215 * else NULL if could not create label (-ENOMEM)
1217 struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
1220 struct aa_label *label = NULL;
1226 return aa_get_newest_label(a);
1228 /* TODO: enable when read side is lockless
1229 * check if label exists before taking locks
1230 if (!label_is_stale(a) && !label_is_stale(b))
1231 label = aa_label_find_merge(a, b);
1235 struct aa_label *new;
1237 a = aa_get_newest_label(a);
1238 b = aa_get_newest_label(b);
1240 /* could use label_merge_len(a, b), but requires double
1241 * comparison for small savings
1243 new = aa_label_alloc(a->size + b->size, NULL, gfp);
1247 label = label_merge_insert(new, a, b);
1248 label_free_or_put_new(label, new);
1257 static inline bool label_is_visible(struct aa_profile *profile,
1258 struct aa_label *label)
1260 return aa_ns_visible(profile->ns, labels_ns(label), true);
1263 /* match a profile and its associated ns component if needed
1264 * Assumes visibility test has already been done.
1265 * If a subns profile is not to be matched should be prescreened with
1268 static inline unsigned int match_component(struct aa_profile *profile,
1269 struct aa_profile *tp,
1272 const char *ns_name;
1274 if (profile->ns == tp->ns)
1275 return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
1277 /* try matching with namespace name and then profile */
1278 ns_name = aa_ns_name(profile->ns, tp->ns, true);
1279 state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
1280 state = aa_dfa_match(profile->policy.dfa, state, ns_name);
1281 state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
1282 return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
1286 * label_compound_match - find perms for full compound label
1287 * @profile: profile to find perms for
1288 * @label: label to check access permissions for
1289 * @start: state to start match in
1290 * @subns: whether to do permission checks on components in a subns
1291 * @request: permissions to request
1292 * @perms: perms struct to set
1294 * Returns: 0 on success else ERROR
1296 * For the label A//&B//&C this does the perm match for A//&B//&C
1297 * @perms should be preinitialized with allperms OR a previous permission
1298 * check to be stacked.
1300 static int label_compound_match(struct aa_profile *profile,
1301 struct aa_label *label,
1302 unsigned int state, bool subns, u32 request,
1303 struct aa_perms *perms)
1305 struct aa_profile *tp;
1308 /* find first subcomponent that is visible */
1309 label_for_each(i, label, tp) {
1310 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1312 state = match_component(profile, tp, state);
1318 /* no component visible */
1323 label_for_each_cont(i, label, tp) {
1324 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1326 state = aa_dfa_match(profile->policy.dfa, state, "//&");
1327 state = match_component(profile, tp, state);
1331 aa_compute_perms(profile->policy.dfa, state, perms);
1332 aa_apply_modes_to_perms(profile, perms);
1333 if ((perms->allow & request) != request)
1344 * label_components_match - find perms for all subcomponents of a label
1345 * @profile: profile to find perms for
1346 * @label: label to check access permissions for
1347 * @start: state to start match in
1348 * @subns: whether to do permission checks on components in a subns
1349 * @request: permissions to request
1350 * @perms: an initialized perms struct to add accumulation to
1352 * Returns: 0 on success else ERROR
1354 * For the label A//&B//&C this does the perm match for each of A and B and C
1355 * @perms should be preinitialized with allperms OR a previous permission
1356 * check to be stacked.
1358 static int label_components_match(struct aa_profile *profile,
1359 struct aa_label *label, unsigned int start,
1360 bool subns, u32 request,
1361 struct aa_perms *perms)
1363 struct aa_profile *tp;
1365 struct aa_perms tmp;
1366 unsigned int state = 0;
1368 /* find first subcomponent to test */
1369 label_for_each(i, label, tp) {
1370 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1372 state = match_component(profile, tp, start);
1378 /* no subcomponents visible - no change in perms */
1382 aa_compute_perms(profile->policy.dfa, state, &tmp);
1383 aa_apply_modes_to_perms(profile, &tmp);
1384 aa_perms_accum(perms, &tmp);
1385 label_for_each_cont(i, label, tp) {
1386 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1388 state = match_component(profile, tp, start);
1391 aa_compute_perms(profile->policy.dfa, state, &tmp);
1392 aa_apply_modes_to_perms(profile, &tmp);
1393 aa_perms_accum(perms, &tmp);
1396 if ((perms->allow & request) != request)
1407 * aa_label_match - do a multi-component label match
1408 * @profile: profile to match against (NOT NULL)
1409 * @label: label to match (NOT NULL)
1410 * @state: state to start in
1411 * @subns: whether to match subns components
1412 * @request: permission request
1413 * @perms: Returns computed perms (NOT NULL)
1415 * Returns: the state the match finished in, may be the none matching state
1417 int aa_label_match(struct aa_profile *profile, struct aa_label *label,
1418 unsigned int state, bool subns, u32 request,
1419 struct aa_perms *perms)
1421 int error = label_compound_match(profile, label, state, subns, request,
1427 return label_components_match(profile, label, state, subns, request,
1433 * aa_update_label_name - update a label to have a stored name
1434 * @ns: ns being viewed from (NOT NULL)
1435 * @label: label to update (NOT NULL)
1436 * @gfp: type of memory allocation
1438 * Requires: labels_set(label) not locked in caller
1440 * note: only updates the label name if it does not have a name already
1441 * and if it is in the labelset
1443 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp)
1445 struct aa_labelset *ls;
1446 unsigned long flags;
1447 char __counted *name;
1453 if (label->hname || labels_ns(label) != ns)
1456 if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) < 0)
1459 ls = labels_set(label);
1460 write_lock_irqsave(&ls->lock, flags);
1461 if (!label->hname && label->flags & FLAG_IN_TREE) {
1462 label->hname = name;
1466 write_unlock_irqrestore(&ls->lock, flags);
1472 * cached label name is present and visible
1473 * @label->hname only exists if label is namespace hierachical
1475 static inline bool use_label_hname(struct aa_ns *ns, struct aa_label *label,
1478 if (label->hname && (!ns || labels_ns(label) == ns) &&
1479 !(flags & ~FLAG_SHOW_MODE))
1485 /* helper macro for snprint routines */
1486 #define update_for_len(total, len, size, str) \
1488 size_t ulen = len; \
1492 ulen = min(ulen, size); \
1498 * aa_profile_snxprint - print a profile name to a buffer
1499 * @str: buffer to write to. (MAY BE NULL if @size == 0)
1500 * @size: size of buffer
1501 * @view: namespace profile is being viewed from
1502 * @profile: profile to view (NOT NULL)
1503 * @flags: whether to include the mode string
1504 * @prev_ns: last ns printed when used in compound print
1506 * Returns: size of name written or would be written if larger than
1509 * Note: will not print anything if the profile is not visible
1511 static int aa_profile_snxprint(char *str, size_t size, struct aa_ns *view,
1512 struct aa_profile *profile, int flags,
1513 struct aa_ns **prev_ns)
1515 const char *ns_name = NULL;
1517 AA_BUG(!str && size != 0);
1521 view = profiles_ns(profile);
1523 if (view != profile->ns &&
1524 (!prev_ns || (*prev_ns != profile->ns))) {
1526 *prev_ns = profile->ns;
1527 ns_name = aa_ns_name(view, profile->ns,
1528 flags & FLAG_VIEW_SUBNS);
1529 if (ns_name == aa_hidden_ns_name) {
1530 if (flags & FLAG_HIDDEN_UNCONFINED)
1531 return snprintf(str, size, "%s", "unconfined");
1532 return snprintf(str, size, "%s", ns_name);
1536 if ((flags & FLAG_SHOW_MODE) && profile != profile->ns->unconfined) {
1537 const char *modestr = aa_profile_mode_names[profile->mode];
1540 return snprintf(str, size, ":%s:%s (%s)", ns_name,
1541 profile->base.hname, modestr);
1542 return snprintf(str, size, "%s (%s)", profile->base.hname,
1547 return snprintf(str, size, ":%s:%s", ns_name,
1548 profile->base.hname);
1549 return snprintf(str, size, "%s", profile->base.hname);
1552 static const char *label_modename(struct aa_ns *ns, struct aa_label *label,
1555 struct aa_profile *profile;
1557 int mode = -1, count = 0;
1559 label_for_each(i, label, profile) {
1560 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
1562 if (profile == profile->ns->unconfined)
1563 /* special case unconfined so stacks with
1564 * unconfined don't report as mixed. ie.
1565 * profile_foo//&:ns1:unconfined (mixed)
1569 mode = profile->mode;
1570 else if (mode != profile->mode)
1578 /* everything was unconfined */
1579 mode = APPARMOR_UNCONFINED;
1581 return aa_profile_mode_names[mode];
1584 /* if any visible label is not unconfined the display_mode returns true */
1585 static inline bool display_mode(struct aa_ns *ns, struct aa_label *label,
1588 if ((flags & FLAG_SHOW_MODE)) {
1589 struct aa_profile *profile;
1592 label_for_each(i, label, profile) {
1593 if (aa_ns_visible(ns, profile->ns,
1594 flags & FLAG_VIEW_SUBNS) &&
1595 profile != profile->ns->unconfined)
1598 /* only ns->unconfined in set of profiles in ns */
1606 * aa_label_snxprint - print a label name to a string buffer
1607 * @str: buffer to write to. (MAY BE NULL if @size == 0)
1608 * @size: size of buffer
1609 * @ns: namespace profile is being viewed from
1610 * @label: label to view (NOT NULL)
1611 * @flags: whether to include the mode string
1613 * Returns: size of name written or would be written if larger than
1616 * Note: labels do not have to be strictly hierarchical to the ns as
1617 * objects may be shared across different namespaces and thus
1618 * pickup labeling from each ns. If a particular part of the
1619 * label is not visible it will just be excluded. And if none
1620 * of the label is visible "---" will be used.
1622 int aa_label_snxprint(char *str, size_t size, struct aa_ns *ns,
1623 struct aa_label *label, int flags)
1625 struct aa_profile *profile;
1626 struct aa_ns *prev_ns = NULL;
1628 int count = 0, total = 0;
1631 AA_BUG(!str && size != 0);
1634 if (AA_DEBUG_LABEL && (flags & FLAG_ABS_ROOT)) {
1636 len = snprintf(str, size, "_");
1637 update_for_len(total, len, size, str);
1639 ns = labels_ns(label);
1642 label_for_each(i, label, profile) {
1643 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
1645 len = snprintf(str, size, "//&");
1646 update_for_len(total, len, size, str);
1648 len = aa_profile_snxprint(str, size, ns, profile,
1649 flags & FLAG_VIEW_SUBNS,
1651 update_for_len(total, len, size, str);
1657 if (flags & FLAG_HIDDEN_UNCONFINED)
1658 return snprintf(str, size, "%s", "unconfined");
1659 return snprintf(str, size, "%s", aa_hidden_ns_name);
1662 /* count == 1 && ... is for backwards compat where the mode
1663 * is not displayed for 'unconfined' in the current ns
1665 if (display_mode(ns, label, flags)) {
1666 len = snprintf(str, size, " (%s)",
1667 label_modename(ns, label, flags));
1668 update_for_len(total, len, size, str);
1673 #undef update_for_len
1676 * aa_label_asxprint - allocate a string buffer and print label into it
1677 * @strp: Returns - the allocated buffer with the label name. (NOT NULL)
1678 * @ns: namespace profile is being viewed from
1679 * @label: label to view (NOT NULL)
1680 * @flags: flags controlling what label info is printed
1681 * @gfp: kernel memory allocation type
1683 * Returns: size of name written or would be written if larger than
1686 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
1687 int flags, gfp_t gfp)
1694 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1698 *strp = kmalloc(size + 1, gfp);
1701 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1705 * aa_label_acntsxprint - allocate a __counted string buffer and print label
1706 * @strp: buffer to write to.
1707 * @ns: namespace profile is being viewed from
1708 * @label: label to view (NOT NULL)
1709 * @flags: flags controlling what label info is printed
1710 * @gfp: kernel memory allocation type
1712 * Returns: size of name written or would be written if larger than
1715 int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
1716 struct aa_label *label, int flags, gfp_t gfp)
1723 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1727 *strp = aa_str_alloc(size + 1, gfp);
1730 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1734 void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
1735 struct aa_label *label, int flags, gfp_t gfp)
1744 if (!use_label_hname(ns, label, flags) ||
1745 display_mode(ns, label, flags)) {
1746 len = aa_label_asxprint(&name, ns, label, flags, gfp);
1748 AA_DEBUG("label print error");
1753 str = (char *) label->hname;
1756 if (audit_string_contains_control(str, len))
1757 audit_log_n_hex(ab, str, len);
1759 audit_log_n_string(ab, str, len);
1764 void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
1765 struct aa_label *label, int flags, gfp_t gfp)
1770 if (!use_label_hname(ns, label, flags)) {
1774 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1776 AA_DEBUG("label print error");
1781 } else if (display_mode(ns, label, flags))
1782 seq_printf(f, "%s (%s)", label->hname,
1783 label_modename(ns, label, flags));
1785 seq_puts(f, label->hname);
1788 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
1793 if (!use_label_hname(ns, label, flags)) {
1797 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1799 AA_DEBUG("label print error");
1804 } else if (display_mode(ns, label, flags))
1805 pr_info("%s (%s)", label->hname,
1806 label_modename(ns, label, flags));
1808 pr_info("%s", label->hname);
1811 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp)
1813 struct aa_ns *ns = aa_get_current_ns();
1815 aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp);
1819 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp)
1821 struct aa_ns *ns = aa_get_current_ns();
1823 aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp);
1827 void aa_label_printk(struct aa_label *label, gfp_t gfp)
1829 struct aa_ns *ns = aa_get_current_ns();
1831 aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp);
1835 static int label_count_strn_entries(const char *str, size_t n)
1837 const char *end = str + n;
1843 for (split = aa_label_strn_split(str, end - str);
1845 split = aa_label_strn_split(str, end - str)) {
1854 * ensure stacks with components like
1856 * have :ns: applied to both 'A' and 'B' by making the lookup relative
1857 * to the base if the lookup specifies an ns, else making the stacked lookup
1858 * relative to the last embedded ns in the string.
1860 static struct aa_profile *fqlookupn_profile(struct aa_label *base,
1861 struct aa_label *currentbase,
1862 const char *str, size_t n)
1864 const char *first = skipn_spaces(str, n);
1866 if (first && *first == ':')
1867 return aa_fqlookupn_profile(base, str, n);
1869 return aa_fqlookupn_profile(currentbase, str, n);
1873 * aa_label_strn_parse - parse, validate and convert a text string to a label
1874 * @base: base label to use for lookups (NOT NULL)
1875 * @str: null terminated text string (NOT NULL)
1876 * @n: length of str to parse, will stop at \0 if encountered before n
1877 * @gfp: allocation type
1878 * @create: true if should create compound labels if they don't exist
1879 * @force_stack: true if should stack even if no leading &
1881 * Returns: the matching refcounted label if present
1884 struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
1885 size_t n, gfp_t gfp, bool create,
1888 DEFINE_VEC(profile, vec);
1889 struct aa_label *label, *currbase = base;
1890 int i, len, stack = 0, error;
1891 const char *end = str + n;
1897 str = skipn_spaces(str, n);
1898 if (str == NULL || (AA_DEBUG_LABEL && *str == '_' &&
1899 base != &root_ns->unconfined->label))
1900 return ERR_PTR(-EINVAL);
1902 len = label_count_strn_entries(str, end - str);
1903 if (*str == '&' || force_stack) {
1904 /* stack on top of base */
1911 error = vec_setup(profile, vec, len, gfp);
1913 return ERR_PTR(error);
1915 for (i = 0; i < stack; i++)
1916 vec[i] = aa_get_profile(base->vec[i]);
1918 for (split = aa_label_strn_split(str, end - str), i = stack;
1919 split && i < len; i++) {
1920 vec[i] = fqlookupn_profile(base, currbase, str, split - str);
1924 * if component specified a new ns it becomes the new base
1925 * so that subsequent lookups are relative to it
1927 if (vec[i]->ns != labels_ns(currbase))
1928 currbase = &vec[i]->label;
1930 split = aa_label_strn_split(str, end - str);
1932 /* last element doesn't have a split */
1934 vec[i] = fqlookupn_profile(base, currbase, str, end - str);
1939 /* no need to free vec as len < LOCAL_VEC_ENTRIES */
1940 return &vec[0]->label;
1942 len -= aa_vec_unique(vec, len, VEC_FLAG_TERMINATE);
1943 /* TODO: deal with reference labels */
1945 label = aa_get_label(&vec[0]->label);
1950 label = aa_vec_find_or_create_label(vec, len, gfp);
1952 label = vec_find(vec, len);
1957 /* use adjusted len from after vec_unique, not original */
1958 vec_cleanup(profile, vec, len);
1962 label = ERR_PTR(-ENOENT);
1966 struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
1967 gfp_t gfp, bool create, bool force_stack)
1969 return aa_label_strn_parse(base, str, strlen(str), gfp, create,
1974 * aa_labelset_destroy - remove all labels from the label set
1975 * @ls: label set to cleanup (NOT NULL)
1977 * Labels that are removed from the set may still exist beyond the set
1978 * being destroyed depending on their reference counting
1980 void aa_labelset_destroy(struct aa_labelset *ls)
1982 struct rb_node *node;
1983 unsigned long flags;
1987 write_lock_irqsave(&ls->lock, flags);
1988 for (node = rb_first(&ls->root); node; node = rb_first(&ls->root)) {
1989 struct aa_label *this = rb_entry(node, struct aa_label, node);
1991 if (labels_ns(this) != root_ns)
1992 __label_remove(this,
1993 ns_unconfined(labels_ns(this)->parent));
1995 __label_remove(this, NULL);
1997 write_unlock_irqrestore(&ls->lock, flags);
2001 * @ls: labelset to init (NOT NULL)
2003 void aa_labelset_init(struct aa_labelset *ls)
2007 rwlock_init(&ls->lock);
2011 static struct aa_label *labelset_next_stale(struct aa_labelset *ls)
2013 struct aa_label *label;
2014 struct rb_node *node;
2015 unsigned long flags;
2019 read_lock_irqsave(&ls->lock, flags);
2021 __labelset_for_each(ls, node) {
2022 label = rb_entry(node, struct aa_label, node);
2023 if ((label_is_stale(label) ||
2024 vec_is_stale(label->vec, label->size)) &&
2025 __aa_get_label(label))
2032 read_unlock_irqrestore(&ls->lock, flags);
2038 * __label_update - insert updated version of @label into labelset
2039 * @label - the label to update/replace
2041 * Returns: new label that is up to date
2042 * else NULL on failure
2044 * Requires: @ns lock be held
2046 * Note: worst case is the stale @label does not get updated and has
2047 * to be updated at a later time.
2049 static struct aa_label *__label_update(struct aa_label *label)
2051 struct aa_label *new, *tmp;
2052 struct aa_labelset *ls;
2053 unsigned long flags;
2054 int i, invcount = 0;
2057 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock));
2059 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL);
2064 * while holding the ns_lock will stop profile replacement, removal,
2065 * and label updates, label merging and removal can be occurring
2067 ls = labels_set(label);
2068 write_lock_irqsave(&ls->lock, flags);
2069 for (i = 0; i < label->size; i++) {
2070 AA_BUG(!label->vec[i]);
2071 new->vec[i] = aa_get_newest_profile(label->vec[i]);
2072 AA_BUG(!new->vec[i]);
2073 AA_BUG(!new->vec[i]->label.proxy);
2074 AA_BUG(!new->vec[i]->label.proxy->label);
2075 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy)
2079 /* updated stale label by being removed/renamed from labelset */
2081 new->size -= aa_vec_unique(&new->vec[0], new->size,
2082 VEC_FLAG_TERMINATE);
2083 /* TODO: deal with reference labels */
2084 if (new->size == 1) {
2085 tmp = aa_get_label(&new->vec[0]->label);
2086 AA_BUG(tmp == label);
2089 if (labels_set(label) != labels_set(new)) {
2090 write_unlock_irqrestore(&ls->lock, flags);
2091 tmp = aa_label_insert(labels_set(new), new);
2092 write_lock_irqsave(&ls->lock, flags);
2096 AA_BUG(labels_ns(label) != labels_ns(new));
2098 tmp = __label_insert(labels_set(label), new, true);
2100 /* ensure label is removed, and redirected correctly */
2101 __label_remove(label, tmp);
2102 write_unlock_irqrestore(&ls->lock, flags);
2103 label_free_or_put_new(tmp, new);
2109 * __labelset_update - update labels in @ns
2110 * @ns: namespace to update labels in (NOT NULL)
2112 * Requires: @ns lock be held
2114 * Walk the labelset ensuring that all labels are up to date and valid
2115 * Any label that has a stale component is marked stale and replaced and
2116 * by an updated version.
2118 * If failures happen due to memory pressures then stale labels will
2119 * be left in place until the next pass.
2121 static void __labelset_update(struct aa_ns *ns)
2123 struct aa_label *label;
2126 AA_BUG(!mutex_is_locked(&ns->lock));
2129 label = labelset_next_stale(&ns->labels);
2131 struct aa_label *l = __label_update(label);
2134 aa_put_label(label);
2140 * __aa_labelset_update_subtree - update all labels with a stale component
2141 * @ns: ns to start update at (NOT NULL)
2143 * Requires: @ns lock be held
2145 * Invalidates labels based on @p in @ns and any children namespaces.
2147 void __aa_labelset_update_subtree(struct aa_ns *ns)
2149 struct aa_ns *child;
2152 AA_BUG(!mutex_is_locked(&ns->lock));
2154 __labelset_update(ns);
2156 list_for_each_entry(child, &ns->sub_ns, base.list) {
2157 mutex_lock_nested(&child->lock, child->level);
2158 __aa_labelset_update_subtree(child);
2159 mutex_unlock(&child->lock);