Lines Matching +full:- +full:- +full:tree +full:- +full:view

1 // SPDX-License-Identifier: GPL-2.0-only
27 * code - will take a ref count on a label if it needs the label
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, ...
45 /* p->label will not updated any more as p is dead */ in free_proxy()
46 aa_put_label(rcu_dereference_protected(proxy->label, true)); in free_proxy()
48 RCU_INIT_POINTER(proxy->label, (struct aa_label *)PROXY_POISON); in free_proxy()
66 kref_init(&new->count); in aa_alloc_proxy()
67 rcu_assign_pointer(new->label, aa_get_label(label)); in aa_alloc_proxy()
79 lockdep_assert_held_write(&labels_set(orig)->lock); in __aa_proxy_redirect()
81 tmp = rcu_dereference_protected(orig->proxy->label, in __aa_proxy_redirect()
82 &labels_ns(orig)->lock); in __aa_proxy_redirect()
83 rcu_assign_pointer(orig->proxy->label, aa_get_label(new)); in __aa_proxy_redirect()
84 orig->flags |= FLAG_STALE; in __aa_proxy_redirect()
90 struct aa_proxy *proxy = new->proxy; in __proxy_share()
92 new->proxy = aa_get_proxy(old->proxy); in __proxy_share()
99 * ns_cmp - compare ns for label set ordering
113 AA_BUG(!a->base.hname); in ns_cmp()
114 AA_BUG(!b->base.hname); in ns_cmp()
119 res = a->level - b->level; in ns_cmp()
123 return strcmp(a->base.hname, b->base.hname); in ns_cmp()
127 * profile_cmp - profile comparison for set ordering
141 AA_BUG(!a->ns); in profile_cmp()
142 AA_BUG(!b->ns); in profile_cmp()
143 AA_BUG(!a->base.hname); in profile_cmp()
144 AA_BUG(!b->base.hname); in profile_cmp()
146 if (a == b || a->base.hname == b->base.hname) in profile_cmp()
148 res = ns_cmp(a->ns, b->ns); in profile_cmp()
152 return strcmp(a->base.hname, b->base.hname); in profile_cmp()
156 * vec_cmp - label comparison for set ordering
183 return an - bn; in vec_cmp()
208 u |= vec[i]->label.flags & (FLAG_DEBUG1 | FLAG_DEBUG2 | in accum_vec_flags()
210 if (!(u & vec[i]->label.flags & FLAG_UNCONFINED)) in accum_vec_flags()
225 * vec[n - dups]
256 * aa_vec_unique - canonical sort and unique a list of profiles
263 * null terminate vec[n - dups]
284 for (pos = i - 1 - dups; pos >= 0; pos--) { in aa_vec_unique()
295 /* pos is at entry < tmp, or index -1. Set to insert pos */ in aa_vec_unique()
298 for (j = i - dups; j > pos; j--) in aa_vec_unique()
299 vec[j] = vec[j - 1]; in aa_vec_unique()
309 vec[n - dups] = NULL; in aa_vec_unique()
323 aa_put_str(label->hname); in aa_label_destroy()
327 label->vec[i.i] = (struct aa_profile *) in aa_label_destroy()
332 if (label->proxy) { in aa_label_destroy()
333 if (rcu_dereference_protected(label->proxy->label, true) == label) in aa_label_destroy()
334 rcu_assign_pointer(label->proxy->label, NULL); in aa_label_destroy()
335 aa_put_proxy(label->proxy); in aa_label_destroy()
337 aa_free_secid(label->secid); in aa_label_destroy()
339 label->proxy = (struct aa_proxy *) PROXY_POISON + 1; in aa_label_destroy()
353 if (label->flags & FLAG_NS_COUNT) in label_free_switch()
365 if (label->flags & FLAG_IN_TREE) in label_free_rcu()
382 on_list_rcu(&label->vec[0]->base.profiles)); in aa_label_kref()
384 on_list_rcu(&label->vec[0]->base.list)); in aa_label_kref()
387 call_rcu(&label->rcu, label_free_rcu); in aa_label_kref()
407 label->size = size; /* doesn't include null */ in aa_label_init()
408 label->vec[size] = NULL; /* null terminate */ in aa_label_init()
409 kref_init(&label->count); in aa_label_init()
410 RB_CLEAR_NODE(&label->node); in aa_label_init()
416 * aa_label_alloc - allocate a label with a profile vector of @size length
446 new->proxy = proxy; in aa_label_alloc()
458 * label_cmp - label comparison for set ordering
473 return vec_cmp(a->vec, a->size, b->vec, b->size); in label_cmp()
482 for (; i < label->size; i++) { in aa_label_next_confined()
483 if (!profile_unconfined(label->vec[i])) in aa_label_next_confined()
491 * __aa_label_next_not_in_set - return the next profile of @sub not in @set
505 AA_BUG(I->i < 0); in __aa_label_next_not_in_set()
506 AA_BUG(I->i > set->size); in __aa_label_next_not_in_set()
508 AA_BUG(I->j < 0); in __aa_label_next_not_in_set()
509 AA_BUG(I->j > sub->size); in __aa_label_next_not_in_set()
511 while (I->j < sub->size && I->i < set->size) { in __aa_label_next_not_in_set()
512 int res = profile_cmp(sub->vec[I->j], set->vec[I->i]); in __aa_label_next_not_in_set()
515 (I->j)++; in __aa_label_next_not_in_set()
516 (I->i)++; in __aa_label_next_not_in_set()
518 (I->i)++; in __aa_label_next_not_in_set()
520 return sub->vec[(I->j)++]; in __aa_label_next_not_in_set()
523 if (I->j < sub->size) in __aa_label_next_not_in_set()
524 return sub->vec[(I->j)++]; in __aa_label_next_not_in_set()
530 * aa_label_is_subset - test if @sub is a subset of @set
551 * aa_label_is_unconfined_subset - test if @sub is a subset of @set
586 * __label_remove - remove @label from the label set
590 * Requires: labels_set(@label)->lock write_lock
591 * Returns: true if the label was in the tree and removed
599 lockdep_assert_held_write(&ls->lock); in __label_remove()
607 if (label->flags & FLAG_IN_TREE) { in __label_remove()
608 rb_erase(&label->node, &ls->root); in __label_remove()
609 label->flags &= ~FLAG_IN_TREE; in __label_remove()
617 * __label_replace - replace @old with @new in label set
621 * Requires: labels_set(@old)->lock write_lock
636 lockdep_assert_held_write(&ls->lock); in __label_replace()
637 AA_BUG(new->flags & FLAG_IN_TREE); in __label_replace()
642 if (old->flags & FLAG_IN_TREE) { in __label_replace()
643 rb_replace_node(&old->node, &new->node, &ls->root); in __label_replace()
644 old->flags &= ~FLAG_IN_TREE; in __label_replace()
645 new->flags |= FLAG_IN_TREE; in __label_replace()
653 * __label_insert - attempt to insert @l into a label set
658 * Requires: @ls->lock
661 * Returns: @l if successful in inserting @l - with additional refcount
673 lockdep_assert_held_write(&ls->lock); in __label_insert()
674 AA_BUG(label->flags & FLAG_IN_TREE); in __label_insert()
677 new = &ls->root.rb_node; in __label_insert()
697 new = &((*new)->rb_left); in __label_insert()
699 new = &((*new)->rb_right); in __label_insert()
702 /* Add new node and rebalance tree. */ in __label_insert()
703 rb_link_node(&label->node, parent, new); in __label_insert()
704 rb_insert_color(&label->node, &ls->root); in __label_insert()
705 label->flags |= FLAG_IN_TREE; in __label_insert()
711 * __vec_find - find label that matches @vec in label set
718 * Returns: ref counted @label if matching label is in tree
719 * ref counted label that is equiv to @l in tree
720 * else NULL if @vec equiv is not in tree
730 node = vec_labelset(vec, n)->root.rb_node; in __vec_find()
733 int result = vec_cmp(this->vec, this->size, vec, n); in __vec_find()
736 node = node->rb_left; in __vec_find()
738 node = node->rb_right; in __vec_find()
747 * __label_find - find label @label in label set
750 * Requires: labels_set(@label)->lock held
753 * Returns: ref counted @label if @label is in tree OR
754 * ref counted label that is equiv to @label in tree
755 * else NULL if @label or equiv is not in tree
761 return __vec_find(label->vec, label->size); in __label_find()
766 * aa_label_remove - remove a label from the labelset
769 * Returns: true if @label was removed from the tree
770 * else @label was not in tree so it could not be removed
780 write_lock_irqsave(&ls->lock, flags); in aa_label_remove()
782 write_unlock_irqrestore(&ls->lock, flags); in aa_label_remove()
788 * aa_label_replace - replace a label @old with a new version @new
792 * Returns: true if @old was in tree and replaced
793 * else @old was not in tree, and @new was not inserted
801 write_lock_irqsave(&labels_set(old)->lock, flags); in aa_label_replace()
802 if (old->proxy != new->proxy) in aa_label_replace()
807 write_unlock_irqrestore(&labels_set(old)->lock, flags); in aa_label_replace()
812 write_lock_irqsave(&ls->lock, flags); in aa_label_replace()
815 write_unlock_irqrestore(&ls->lock, flags); in aa_label_replace()
817 write_lock_irqsave(&ls->lock, flags); in aa_label_replace()
821 write_unlock_irqrestore(&ls->lock, flags); in aa_label_replace()
829 * vec_find - find label @l in label set
833 * Returns: refcounted label if @vec equiv is in tree
834 * else NULL if @vec equiv is not in tree
847 read_lock_irqsave(&ls->lock, flags); in vec_find()
849 read_unlock_irqrestore(&ls->lock, flags); in vec_find()
867 return aa_get_label(&vec[0]->label); in vec_create_and_insert_label()
869 ls = labels_set(&vec[len - 1]->label); in vec_create_and_insert_label()
879 new->vec[i] = aa_get_profile(vec[i]); in vec_create_and_insert_label()
881 write_lock_irqsave(&ls->lock, flags); in vec_create_and_insert_label()
883 write_unlock_irqrestore(&ls->lock, flags); in vec_create_and_insert_label()
901 * aa_label_find - find label @label in label set
906 * Returns: refcounted @label if @label is in tree
907 * refcounted label that is equiv to @label in tree
908 * else NULL if @label or equiv is not in tree
914 return vec_find(label->vec, label->size); in aa_label_find()
919 * aa_label_insert - insert label @label into @ls or return existing label
920 * @ls - labelset to insert @label into
921 * @label - label to insert
938 read_lock_irqsave(&ls->lock, flags); in aa_label_insert()
940 read_unlock_irqrestore(&ls->lock, flags); in aa_label_insert()
945 write_lock_irqsave(&ls->lock, flags); in aa_label_insert()
947 write_unlock_irqrestore(&ls->lock, flags); in aa_label_insert()
954 * aa_label_next_in_merge - find the next profile when merging @a and @b
969 AA_BUG(I->i < 0); in aa_label_next_in_merge()
970 AA_BUG(I->i > a->size); in aa_label_next_in_merge()
971 AA_BUG(I->j < 0); in aa_label_next_in_merge()
972 AA_BUG(I->j > b->size); in aa_label_next_in_merge()
974 if (I->i < a->size) { in aa_label_next_in_merge()
975 if (I->j < b->size) { in aa_label_next_in_merge()
976 int res = profile_cmp(a->vec[I->i], b->vec[I->j]); in aa_label_next_in_merge()
979 return b->vec[(I->j)++]; in aa_label_next_in_merge()
981 (I->j)++; in aa_label_next_in_merge()
984 return a->vec[(I->i)++]; in aa_label_next_in_merge()
987 if (I->j < b->size) in aa_label_next_in_merge()
988 return b->vec[(I->j)++]; in aa_label_next_in_merge()
994 * label_merge_cmp - cmp of @a merging with @b against @z for set ordering
1017 k < z->size && (p = aa_label_next_in_merge(&i, a, b)); in label_merge_cmp()
1019 int res = profile_cmp(p, z->vec[k]); in label_merge_cmp()
1027 else if (k < z->size) in label_merge_cmp()
1028 return -1; in label_merge_cmp()
1033 * label_merge_insert - create a new label by merging @a and @b
1062 AA_BUG(a->size < 0); in label_merge_insert()
1064 AA_BUG(b->size < 0); in label_merge_insert()
1066 AA_BUG(new->size < a->size + b->size); in label_merge_insert()
1071 new->vec[k] = aa_get_newest_profile(next); in label_merge_insert()
1072 AA_BUG(!new->vec[k]->label.proxy); in label_merge_insert()
1073 AA_BUG(!new->vec[k]->label.proxy->label); in label_merge_insert()
1074 if (next->label.proxy != new->vec[k]->label.proxy) in label_merge_insert()
1079 new->vec[k++] = aa_get_profile(next); in label_merge_insert()
1082 new->size = k; in label_merge_insert()
1083 new->vec[k] = NULL; in label_merge_insert()
1086 new->size -= aa_vec_unique(&new->vec[0], new->size, in label_merge_insert()
1089 if (new->size == 1) { in label_merge_insert()
1090 label = aa_get_label(&new->vec[0]->label); in label_merge_insert()
1096 * for new->size == a->size == b->size unless a == b in label_merge_insert()
1098 if (k == a->size) in label_merge_insert()
1100 else if (k == b->size) in label_merge_insert()
1103 new->flags |= accum_vec_flags(new->vec, new->size); in label_merge_insert()
1105 write_lock_irqsave(&ls->lock, flags); in label_merge_insert()
1107 write_unlock_irqrestore(&ls->lock, flags); in label_merge_insert()
1113 * labelset_of_merge - find which labelset a merged label should be inserted
1126 return &nsa->labels; in labelset_of_merge()
1127 return &nsb->labels; in labelset_of_merge()
1131 * __label_find_merge - find label that is equiv to merge of @a and @b
1136 * Requires: ls->lock read_lock held
1154 node = ls->root.rb_node; in __label_find_merge()
1161 node = node->rb_left; in __label_find_merge()
1163 node = node->rb_right; in __label_find_merge()
1173 * aa_label_find_merge - find label that is equiv to merge of @a and @b
1196 read_lock_irqsave(&ls->lock, flags); in aa_label_find_merge()
1198 read_unlock_irqrestore(&ls->lock, flags); in aa_label_find_merge()
1206 * aa_label_merge - attempt to insert new merged label of @a and @b
1217 * else NULL if could not create label (-ENOMEM)
1245 new = aa_label_alloc(a->size + b->size, NULL, gfp); in aa_label_merge()
1271 if (profile->ns == tp->ns) in match_component()
1272 return aa_dfa_match(rules->policy.dfa, state, tp->base.hname); in match_component()
1275 ns_name = aa_ns_name(profile->ns, tp->ns, true); in match_component()
1276 state = aa_dfa_match_len(rules->policy.dfa, state, ":", 1); in match_component()
1277 state = aa_dfa_match(rules->policy.dfa, state, ns_name); in match_component()
1278 state = aa_dfa_match_len(rules->policy.dfa, state, ":", 1); in match_component()
1279 return aa_dfa_match(rules->policy.dfa, state, tp->base.hname); in match_component()
1283 * label_compound_match - find perms for full compound label
1308 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_compound_match()
1322 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_compound_match()
1324 state = aa_dfa_match(rules->policy.dfa, state, "//&"); in label_compound_match()
1329 *perms = *aa_lookup_perms(&rules->policy, state); in label_compound_match()
1331 if ((perms->allow & request) != request) in label_compound_match()
1332 return -EACCES; in label_compound_match()
1342 * label_components_match - find perms for all subcomponents of a label
1370 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_components_match()
1378 /* no subcomponents visible - no change in perms */ in label_components_match()
1382 tmp = *aa_lookup_perms(&rules->policy, state); in label_components_match()
1386 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_components_match()
1391 tmp = *aa_lookup_perms(&rules->policy, state); in label_components_match()
1396 if ((perms->allow & request) != request) in label_components_match()
1397 return -EACCES; in label_components_match()
1403 return -EACCES; in label_components_match()
1407 * aa_label_match - do a multi-component label match
1434 * aa_update_label_name - update a label to have a stored name
1454 if (label->hname || labels_ns(label) != ns) in aa_update_label_name()
1461 write_lock_irqsave(&ls->lock, flags); in aa_update_label_name()
1462 if (!label->hname && label->flags & FLAG_IN_TREE) { in aa_update_label_name()
1463 label->hname = name; in aa_update_label_name()
1467 write_unlock_irqrestore(&ls->lock, flags); in aa_update_label_name()
1474 * @label->hname only exists if label is namespace hierachical
1479 if (label->hname && (!ns || labels_ns(label) == ns) && in use_label_hname()
1494 size -= ulen; \
1499 * aa_profile_snxprint - print a profile name to a buffer
1502 * @view: namespace profile is being viewed from
1503 * @profile: profile to view (NOT NULL)
1512 static int aa_profile_snxprint(char *str, size_t size, struct aa_ns *view, in aa_profile_snxprint() argument
1521 if (!view) in aa_profile_snxprint()
1522 view = profiles_ns(profile); in aa_profile_snxprint()
1524 if (view != profile->ns && in aa_profile_snxprint()
1525 (!prev_ns || (*prev_ns != profile->ns))) { in aa_profile_snxprint()
1527 *prev_ns = profile->ns; in aa_profile_snxprint()
1528 ns_name = aa_ns_name(view, profile->ns, in aa_profile_snxprint()
1537 if ((flags & FLAG_SHOW_MODE) && profile != profile->ns->unconfined) { in aa_profile_snxprint()
1538 const char *modestr = aa_profile_mode_names[profile->mode]; in aa_profile_snxprint()
1542 profile->base.hname, modestr); in aa_profile_snxprint()
1543 return snprintf(str, size, "%s (%s)", profile->base.hname, in aa_profile_snxprint()
1549 profile->base.hname); in aa_profile_snxprint()
1550 return snprintf(str, size, "%s", profile->base.hname); in aa_profile_snxprint()
1558 int mode = -1, count = 0; in label_modename()
1561 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) { in label_modename()
1563 if (profile == profile->ns->unconfined) in label_modename()
1569 if (mode == -1) in label_modename()
1570 mode = profile->mode; in label_modename()
1571 else if (mode != profile->mode) in label_modename()
1577 return "-"; in label_modename()
1578 if (mode == -1) in label_modename()
1594 if (aa_ns_visible(ns, profile->ns, in display_mode()
1596 profile != profile->ns->unconfined) in display_mode()
1599 /* only ns->unconfined in set of profiles in ns */ in display_mode()
1607 * aa_label_snxprint - print a label name to a string buffer
1611 * @label: label to view (NOT NULL)
1621 * of the label is visible "---" will be used.
1644 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) { in aa_label_snxprint()
1677 * aa_label_asxprint - allocate a string buffer and print label into it
1678 * @strp: Returns - the allocated buffer with the label name. (NOT NULL)
1680 * @label: label to view (NOT NULL)
1701 return -ENOMEM; in aa_label_asxprint()
1706 * aa_label_acntsxprint - allocate a __counted string buffer and print label
1709 * @label: label to view (NOT NULL)
1730 return -ENOMEM; in aa_label_acntsxprint()
1754 str = (char *) label->hname; in aa_label_xaudit()
1783 seq_printf(f, "%s (%s)", label->hname, in aa_label_seq_xprint()
1786 seq_puts(f, label->hname); in aa_label_seq_xprint()
1806 pr_info("%s (%s)", label->hname, in aa_label_xprintk()
1809 pr_info("%s", label->hname); in aa_label_xprintk()
1844 for (split = aa_label_strn_split(str, end - str); in label_count_strn_entries()
1846 split = aa_label_strn_split(str, end - str)) { in label_count_strn_entries()
1874 * aa_label_strn_parse - parse, validate and convert a text string to a label
1900 base != &root_ns->unconfined->label)) in aa_label_strn_parse()
1901 return ERR_PTR(-EINVAL); in aa_label_strn_parse()
1903 len = label_count_strn_entries(str, end - str); in aa_label_strn_parse()
1906 stack = base->size; in aa_label_strn_parse()
1917 vec[i] = aa_get_profile(base->vec[i]); in aa_label_strn_parse()
1919 for (split = aa_label_strn_split(str, end - str), i = stack; in aa_label_strn_parse()
1921 vec[i] = fqlookupn_profile(base, currbase, str, split - str); in aa_label_strn_parse()
1928 if (vec[i]->ns != labels_ns(currbase)) in aa_label_strn_parse()
1929 currbase = &vec[i]->label; in aa_label_strn_parse()
1931 split = aa_label_strn_split(str, end - str); in aa_label_strn_parse()
1935 vec[i] = fqlookupn_profile(base, currbase, str, end - str); in aa_label_strn_parse()
1941 return &vec[0]->label; in aa_label_strn_parse()
1943 len -= aa_vec_unique(vec, len, VEC_FLAG_TERMINATE); in aa_label_strn_parse()
1946 label = aa_get_label(&vec[0]->label); in aa_label_strn_parse()
1963 label = ERR_PTR(-ENOENT); in aa_label_strn_parse()
1975 * aa_labelset_destroy - remove all labels from the label set
1988 write_lock_irqsave(&ls->lock, flags); in aa_labelset_destroy()
1989 for (node = rb_first(&ls->root); node; node = rb_first(&ls->root)) { in aa_labelset_destroy()
1994 ns_unconfined(labels_ns(this)->parent)); in aa_labelset_destroy()
1998 write_unlock_irqrestore(&ls->lock, flags); in aa_labelset_destroy()
2008 rwlock_init(&ls->lock); in aa_labelset_init()
2009 ls->root = RB_ROOT; in aa_labelset_init()
2020 read_lock_irqsave(&ls->lock, flags); in labelset_next_stale()
2025 vec_is_stale(label->vec, label->size)) && in labelset_next_stale()
2033 read_unlock_irqrestore(&ls->lock, flags); in labelset_next_stale()
2039 * __label_update - insert updated version of @label into labelset
2040 * @label - the label to update/replace
2058 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock)); in __label_update()
2060 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL); in __label_update()
2069 write_lock_irqsave(&ls->lock, flags); in __label_update()
2070 for (i = 0; i < label->size; i++) { in __label_update()
2071 AA_BUG(!label->vec[i]); in __label_update()
2072 new->vec[i] = aa_get_newest_profile(label->vec[i]); in __label_update()
2073 AA_BUG(!new->vec[i]); in __label_update()
2074 AA_BUG(!new->vec[i]->label.proxy); in __label_update()
2075 AA_BUG(!new->vec[i]->label.proxy->label); in __label_update()
2076 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy) in __label_update()
2082 new->size -= aa_vec_unique(&new->vec[0], new->size, in __label_update()
2085 if (new->size == 1) { in __label_update()
2086 tmp = aa_get_label(&new->vec[0]->label); in __label_update()
2091 write_unlock_irqrestore(&ls->lock, flags); in __label_update()
2093 write_lock_irqsave(&ls->lock, flags); in __label_update()
2103 write_unlock_irqrestore(&ls->lock, flags); in __label_update()
2110 * __labelset_update - update labels in @ns
2127 AA_BUG(!mutex_is_locked(&ns->lock)); in __labelset_update()
2130 label = labelset_next_stale(&ns->labels); in __labelset_update()
2141 * __aa_labelset_update_subtree - update all labels with a stale component
2153 AA_BUG(!mutex_is_locked(&ns->lock)); in __aa_labelset_update_subtree()
2157 list_for_each_entry(child, &ns->sub_ns, base.list) { in __aa_labelset_update_subtree()
2158 mutex_lock_nested(&child->lock, child->level); in __aa_labelset_update_subtree()
2160 mutex_unlock(&child->lock); in __aa_labelset_update_subtree()