Lines Matching +full:test +full:- +full:rules

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
184 return an - bn; in vec_cmp()
209 if (new->size == 1) in accum_label_info()
212 for (i = 0; i < new->size; i++) { in accum_label_info()
213 u |= new->vec[i]->label.flags & (FLAG_DEBUG1 | FLAG_DEBUG2 | in accum_label_info()
215 if (!(u & new->vec[i]->label.flags & FLAG_UNCONFINED)) in accum_label_info()
217 new->mediates |= new->vec[i]->label.mediates; in accum_label_info()
219 new->flags |= u; in accum_label_info()
230 * vec[n - dups]
261 * aa_vec_unique - canonical sort and unique a list of profiles
269 * null terminate vec[n - dups]
290 for (pos = i - 1 - dups; pos >= 0; pos--) { in aa_vec_unique()
301 /* pos is at entry < tmp, or index -1. Set to insert pos */ in aa_vec_unique()
304 for (j = i - dups; j > pos; j--) in aa_vec_unique()
305 vec[j] = vec[j - 1]; in aa_vec_unique()
315 vec[n - dups] = NULL; in aa_vec_unique()
329 aa_put_str(label->hname); in aa_label_destroy()
333 label->vec[i.i] = (struct aa_profile *) in aa_label_destroy()
338 if (label->proxy) { in aa_label_destroy()
339 if (rcu_dereference_protected(label->proxy->label, true) == label) in aa_label_destroy()
340 rcu_assign_pointer(label->proxy->label, NULL); in aa_label_destroy()
341 aa_put_proxy(label->proxy); in aa_label_destroy()
343 aa_free_secid(label->secid); in aa_label_destroy()
345 label->proxy = (struct aa_proxy *) PROXY_POISON + 1; in aa_label_destroy()
359 if (label->flags & FLAG_NS_COUNT) in label_free_switch()
371 if (label->flags & FLAG_IN_TREE) in label_free_rcu()
388 on_list_rcu(&label->vec[0]->base.profiles)); in aa_label_kref()
390 on_list_rcu(&label->vec[0]->base.list)); in aa_label_kref()
393 call_rcu(&label->rcu, label_free_rcu); in aa_label_kref()
413 label->size = size; /* doesn't include null */ in aa_label_init()
414 label->vec[size] = NULL; /* null terminate */ in aa_label_init()
415 kref_init(&label->count); in aa_label_init()
416 RB_CLEAR_NODE(&label->node); in aa_label_init()
422 * aa_label_alloc - allocate a label with a profile vector of @size length
452 new->proxy = proxy; in aa_label_alloc()
464 * label_cmp - label comparison for set ordering
479 return vec_cmp(a->vec, a->size, b->vec, b->size); in label_cmp()
488 for (; i < label->size; i++) { in aa_label_next_confined()
489 if (!profile_unconfined(label->vec[i])) in aa_label_next_confined()
497 * __aa_label_next_not_in_set - return the next profile of @sub not in @set
499 * @set: label to test against
511 AA_BUG(I->i < 0); in __aa_label_next_not_in_set()
512 AA_BUG(I->i > set->size); in __aa_label_next_not_in_set()
514 AA_BUG(I->j < 0); in __aa_label_next_not_in_set()
515 AA_BUG(I->j > sub->size); in __aa_label_next_not_in_set()
517 while (I->j < sub->size && I->i < set->size) { in __aa_label_next_not_in_set()
518 int res = profile_cmp(sub->vec[I->j], set->vec[I->i]); in __aa_label_next_not_in_set()
521 (I->j)++; in __aa_label_next_not_in_set()
522 (I->i)++; in __aa_label_next_not_in_set()
524 (I->i)++; in __aa_label_next_not_in_set()
526 return sub->vec[(I->j)++]; in __aa_label_next_not_in_set()
529 if (I->j < sub->size) in __aa_label_next_not_in_set()
530 return sub->vec[(I->j)++]; in __aa_label_next_not_in_set()
536 * aa_label_is_subset - test if @sub is a subset of @set
537 * @set: label to test against
538 * @sub: label to test if is subset of @set
557 * aa_label_is_unconfined_subset - test if @sub is a subset of @set
558 * @set: label to test against
559 * @sub: label to test if is subset of @set
563 * unconfined in @set then this will not cause the test to fail.
592 * __label_remove - remove @label from the label set
596 * Requires: labels_set(@label)->lock write_lock
605 lockdep_assert_held_write(&ls->lock); in __label_remove()
613 if (label->flags & FLAG_IN_TREE) { in __label_remove()
614 rb_erase(&label->node, &ls->root); in __label_remove()
615 label->flags &= ~FLAG_IN_TREE; in __label_remove()
623 * __label_replace - replace @old with @new in label set
627 * Requires: labels_set(@old)->lock write_lock
642 lockdep_assert_held_write(&ls->lock); in __label_replace()
643 AA_BUG(new->flags & FLAG_IN_TREE); in __label_replace()
648 if (old->flags & FLAG_IN_TREE) { in __label_replace()
649 rb_replace_node(&old->node, &new->node, &ls->root); in __label_replace()
650 old->flags &= ~FLAG_IN_TREE; in __label_replace()
651 new->flags |= FLAG_IN_TREE; in __label_replace()
660 * __label_insert - attempt to insert @l into a label set
665 * Requires: @ls->lock
668 * Returns: @l if successful in inserting @l - with additional refcount
680 lockdep_assert_held_write(&ls->lock); in __label_insert()
681 AA_BUG(label->flags & FLAG_IN_TREE); in __label_insert()
684 new = &ls->root.rb_node; in __label_insert()
704 new = &((*new)->rb_left); in __label_insert()
706 new = &((*new)->rb_right); in __label_insert()
710 rb_link_node(&label->node, parent, new); in __label_insert()
711 rb_insert_color(&label->node, &ls->root); in __label_insert()
712 label->flags |= FLAG_IN_TREE; in __label_insert()
719 * __vec_find - find label that matches @vec in label set
738 node = vec_labelset(vec, n)->root.rb_node; in __vec_find()
741 int result = vec_cmp(this->vec, this->size, vec, n); in __vec_find()
744 node = node->rb_left; in __vec_find()
746 node = node->rb_right; in __vec_find()
755 * __label_find - find label @label in label set
758 * Requires: labels_set(@label)->lock held
769 return __vec_find(label->vec, label->size); in __label_find()
774 * aa_label_remove - remove a label from the labelset
788 write_lock_irqsave(&ls->lock, flags); in aa_label_remove()
790 write_unlock_irqrestore(&ls->lock, flags); in aa_label_remove()
796 * aa_label_replace - replace a label @old with a new version @new
809 write_lock_irqsave(&labels_set(old)->lock, flags); in aa_label_replace()
810 if (old->proxy != new->proxy) in aa_label_replace()
815 write_unlock_irqrestore(&labels_set(old)->lock, flags); in aa_label_replace()
820 write_lock_irqsave(&ls->lock, flags); in aa_label_replace()
823 write_unlock_irqrestore(&ls->lock, flags); in aa_label_replace()
825 write_lock_irqsave(&ls->lock, flags); in aa_label_replace()
829 write_unlock_irqrestore(&ls->lock, flags); in aa_label_replace()
837 * vec_find - find label @l in label set
855 read_lock_irqsave(&ls->lock, flags); in vec_find()
857 read_unlock_irqrestore(&ls->lock, flags); in vec_find()
875 return aa_get_label(&vec[0]->label); in vec_create_and_insert_label()
877 ls = labels_set(&vec[len - 1]->label); in vec_create_and_insert_label()
887 new->vec[i] = aa_get_profile(vec[i]); in vec_create_and_insert_label()
889 write_lock_irqsave(&ls->lock, flags); in vec_create_and_insert_label()
891 write_unlock_irqrestore(&ls->lock, flags); in vec_create_and_insert_label()
910 * aa_label_insert - insert label @label into @ls or return existing label
929 read_lock_irqsave(&ls->lock, flags); in aa_label_insert()
931 read_unlock_irqrestore(&ls->lock, flags); in aa_label_insert()
936 write_lock_irqsave(&ls->lock, flags); in aa_label_insert()
938 write_unlock_irqrestore(&ls->lock, flags); in aa_label_insert()
945 * aa_label_next_in_merge - find the next profile when merging @a and @b
960 AA_BUG(I->i < 0); in aa_label_next_in_merge()
961 AA_BUG(I->i > a->size); in aa_label_next_in_merge()
962 AA_BUG(I->j < 0); in aa_label_next_in_merge()
963 AA_BUG(I->j > b->size); in aa_label_next_in_merge()
965 if (I->i < a->size) { in aa_label_next_in_merge()
966 if (I->j < b->size) { in aa_label_next_in_merge()
967 int res = profile_cmp(a->vec[I->i], b->vec[I->j]); in aa_label_next_in_merge()
970 return b->vec[(I->j)++]; in aa_label_next_in_merge()
972 (I->j)++; in aa_label_next_in_merge()
975 return a->vec[(I->i)++]; in aa_label_next_in_merge()
978 if (I->j < b->size) in aa_label_next_in_merge()
979 return b->vec[(I->j)++]; in aa_label_next_in_merge()
985 * label_merge_cmp - cmp of @a merging with @b against @z for set ordering
1008 k < z->size && (p = aa_label_next_in_merge(&i, a, b)); in label_merge_cmp()
1010 int res = profile_cmp(p, z->vec[k]); in label_merge_cmp()
1018 else if (k < z->size) in label_merge_cmp()
1019 return -1; in label_merge_cmp()
1024 * label_merge_insert - create a new label by merging @a and @b
1053 AA_BUG(a->size < 0); in label_merge_insert()
1055 AA_BUG(b->size < 0); in label_merge_insert()
1057 AA_BUG(new->size < a->size + b->size); in label_merge_insert()
1062 new->vec[k] = aa_get_newest_profile(next); in label_merge_insert()
1063 AA_BUG(!new->vec[k]->label.proxy); in label_merge_insert()
1064 AA_BUG(!new->vec[k]->label.proxy->label); in label_merge_insert()
1065 if (next->label.proxy != new->vec[k]->label.proxy) in label_merge_insert()
1070 new->vec[k++] = aa_get_profile(next); in label_merge_insert()
1073 new->size = k; in label_merge_insert()
1074 new->vec[k] = NULL; in label_merge_insert()
1077 new->size -= aa_vec_unique(&new->vec[0], new->size, in label_merge_insert()
1080 if (new->size == 1) { in label_merge_insert()
1081 label = aa_get_label(&new->vec[0]->label); in label_merge_insert()
1087 * for new->size == a->size == b->size unless a == b in label_merge_insert()
1089 if (k == a->size) in label_merge_insert()
1091 else if (k == b->size) in label_merge_insert()
1095 write_lock_irqsave(&ls->lock, flags); in label_merge_insert()
1097 write_unlock_irqrestore(&ls->lock, flags); in label_merge_insert()
1103 * labelset_of_merge - find which labelset a merged label should be inserted
1116 return &nsa->labels; in labelset_of_merge()
1117 return &nsb->labels; in labelset_of_merge()
1121 * __label_find_merge - find label that is equiv to merge of @a and @b
1126 * Requires: ls->lock read_lock held
1144 node = ls->root.rb_node; in __label_find_merge()
1151 node = node->rb_left; in __label_find_merge()
1153 node = node->rb_right; in __label_find_merge()
1163 * aa_label_find_merge - find label that is equiv to merge of @a and @b
1186 read_lock_irqsave(&ls->lock, flags); in aa_label_find_merge()
1188 read_unlock_irqrestore(&ls->lock, flags); in aa_label_find_merge()
1196 * aa_label_merge - attempt to insert new merged label of @a and @b
1206 * else NULL if could not create label (-ENOMEM)
1234 new = aa_label_alloc(a->size + b->size, NULL, gfp); in aa_label_merge()
1249 * Assumes visibility test has already been done.
1251 * visibility test.
1254 struct aa_ruleset *rules, in match_component() argument
1260 if (profile->ns == tp->ns) in match_component()
1261 return aa_dfa_match(rules->policy->dfa, state, tp->base.hname); in match_component()
1264 ns_name = aa_ns_name(profile->ns, tp->ns, true); in match_component()
1265 state = aa_dfa_match_len(rules->policy->dfa, state, ":", 1); in match_component()
1266 state = aa_dfa_match(rules->policy->dfa, state, ns_name); in match_component()
1267 state = aa_dfa_match_len(rules->policy->dfa, state, ":", 1); in match_component()
1268 return aa_dfa_match(rules->policy->dfa, state, tp->base.hname); in match_component()
1272 * label_compound_match - find perms for full compound label
1274 * @rules: ruleset to search
1288 struct aa_ruleset *rules, in label_compound_match() argument
1298 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_compound_match()
1300 state = match_component(profile, rules, tp, state); in label_compound_match()
1312 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_compound_match()
1314 state = aa_dfa_match(rules->policy->dfa, state, "//&"); in label_compound_match()
1315 state = match_component(profile, rules, tp, state); in label_compound_match()
1319 *perms = *aa_lookup_perms(rules->policy, state); in label_compound_match()
1321 if ((perms->allow & request) != request) in label_compound_match()
1322 return -EACCES; in label_compound_match()
1332 * label_components_match - find perms for all subcomponents of a label
1334 * @rules: ruleset to search
1348 struct aa_ruleset *rules, in label_components_match() argument
1358 /* find first subcomponent to test */ in label_components_match()
1360 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_components_match()
1362 state = match_component(profile, rules, tp, start); in label_components_match()
1368 /* no subcomponents visible - no change in perms */ in label_components_match()
1372 tmp = *aa_lookup_perms(rules->policy, state); in label_components_match()
1376 if (!aa_ns_visible(profile->ns, tp->ns, subns)) in label_components_match()
1378 state = match_component(profile, rules, tp, start); in label_components_match()
1381 tmp = *aa_lookup_perms(rules->policy, state); in label_components_match()
1386 if ((perms->allow & request) != request) in label_components_match()
1387 return -EACCES; in label_components_match()
1393 return -EACCES; in label_components_match()
1397 * aa_label_match - do a multi-component label match
1399 * @rules: ruleset to search
1408 int aa_label_match(struct aa_profile *profile, struct aa_ruleset *rules, in aa_label_match() argument
1412 int error = label_compound_match(profile, rules, label, state, subns, in aa_label_match()
1418 return label_components_match(profile, rules, label, state, subns, in aa_label_match()
1424 * aa_update_label_name - update a label to have a stored name
1444 if (label->hname || labels_ns(label) != ns) in aa_update_label_name()
1451 write_lock_irqsave(&ls->lock, flags); in aa_update_label_name()
1452 if (!label->hname && label->flags & FLAG_IN_TREE) { in aa_update_label_name()
1453 label->hname = name; in aa_update_label_name()
1457 write_unlock_irqrestore(&ls->lock, flags); in aa_update_label_name()
1464 * @label->hname only exists if label is namespace hierarchical
1469 if (label->hname && (!ns || labels_ns(label) == ns) && in use_label_hname()
1484 size -= ulen; \
1489 * aa_profile_snxprint - print a profile name to a buffer
1514 if (view != profile->ns && in aa_profile_snxprint()
1515 (!prev_ns || (*prev_ns != profile->ns))) { in aa_profile_snxprint()
1517 *prev_ns = profile->ns; in aa_profile_snxprint()
1518 ns_name = aa_ns_name(view, profile->ns, in aa_profile_snxprint()
1527 if ((flags & FLAG_SHOW_MODE) && profile != profile->ns->unconfined) { in aa_profile_snxprint()
1528 const char *modestr = aa_profile_mode_names[profile->mode]; in aa_profile_snxprint()
1532 profile->base.hname, modestr); in aa_profile_snxprint()
1533 return snprintf(str, size, "%s (%s)", profile->base.hname, in aa_profile_snxprint()
1539 profile->base.hname); in aa_profile_snxprint()
1540 return snprintf(str, size, "%s", profile->base.hname); in aa_profile_snxprint()
1548 int mode = -1, count = 0; in label_modename()
1551 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) { in label_modename()
1553 if (profile == profile->ns->unconfined) in label_modename()
1559 if (mode == -1) in label_modename()
1560 mode = profile->mode; in label_modename()
1561 else if (mode != profile->mode) in label_modename()
1567 return "-"; in label_modename()
1568 if (mode == -1) in label_modename()
1584 if (aa_ns_visible(ns, profile->ns, in display_mode()
1586 profile != profile->ns->unconfined) in display_mode()
1589 /* only ns->unconfined in set of profiles in ns */ in display_mode()
1597 * aa_label_snxprint - print a label name to a string buffer
1611 * of the label is visible "---" will be used.
1634 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) { in aa_label_snxprint()
1667 * aa_label_asxprint - allocate a string buffer and print label into it
1668 * @strp: Returns - the allocated buffer with the label name. (NOT NULL)
1691 return -ENOMEM; in aa_label_asxprint()
1696 * aa_label_acntsxprint - allocate a __counted string buffer and print label
1720 return -ENOMEM; in aa_label_acntsxprint()
1744 str = (char *) label->hname; in aa_label_xaudit()
1773 seq_printf(f, "%s (%s)", label->hname, in aa_label_seq_xprint()
1776 seq_puts(f, label->hname); in aa_label_seq_xprint()
1796 pr_info("%s (%s)", label->hname, in aa_label_xprintk()
1799 pr_info("%s", label->hname); in aa_label_xprintk()
1818 for (split = aa_label_strn_split(str, end - str); in label_count_strn_entries()
1820 split = aa_label_strn_split(str, end - str)) { in label_count_strn_entries()
1848 * aa_label_strn_parse - parse, validate and convert a text string to a label
1874 base != &root_ns->unconfined->label)) in aa_label_strn_parse()
1875 return ERR_PTR(-EINVAL); in aa_label_strn_parse()
1877 len = label_count_strn_entries(str, end - str); in aa_label_strn_parse()
1880 stack = base->size; in aa_label_strn_parse()
1891 vec[i] = aa_get_profile(base->vec[i]); in aa_label_strn_parse()
1893 for (split = aa_label_strn_split(str, end - str), i = stack; in aa_label_strn_parse()
1895 vec[i] = fqlookupn_profile(base, currbase, str, split - str); in aa_label_strn_parse()
1902 if (vec[i]->ns != labels_ns(currbase)) in aa_label_strn_parse()
1903 currbase = &vec[i]->label; in aa_label_strn_parse()
1905 split = aa_label_strn_split(str, end - str); in aa_label_strn_parse()
1909 vec[i] = fqlookupn_profile(base, currbase, str, end - str); in aa_label_strn_parse()
1915 return &vec[0]->label; in aa_label_strn_parse()
1917 len -= aa_vec_unique(vec, len, VEC_FLAG_TERMINATE); in aa_label_strn_parse()
1920 label = aa_get_label(&vec[0]->label); in aa_label_strn_parse()
1937 label = ERR_PTR(-ENOENT); in aa_label_strn_parse()
1949 * aa_labelset_destroy - remove all labels from the label set
1962 write_lock_irqsave(&ls->lock, flags); in aa_labelset_destroy()
1963 for (node = rb_first(&ls->root); node; node = rb_first(&ls->root)) { in aa_labelset_destroy()
1968 ns_unconfined(labels_ns(this)->parent)); in aa_labelset_destroy()
1972 write_unlock_irqrestore(&ls->lock, flags); in aa_labelset_destroy()
1982 rwlock_init(&ls->lock); in aa_labelset_init()
1983 ls->root = RB_ROOT; in aa_labelset_init()
1994 read_lock_irqsave(&ls->lock, flags); in labelset_next_stale()
1999 vec_is_stale(label->vec, label->size)) && in labelset_next_stale()
2007 read_unlock_irqrestore(&ls->lock, flags); in labelset_next_stale()
2013 * __label_update - insert updated version of @label into labelset
2032 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock)); in __label_update()
2034 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL); in __label_update()
2043 write_lock_irqsave(&ls->lock, flags); in __label_update()
2044 for (i = 0; i < label->size; i++) { in __label_update()
2045 AA_BUG(!label->vec[i]); in __label_update()
2046 new->vec[i] = aa_get_newest_profile(label->vec[i]); in __label_update()
2047 AA_BUG(!new->vec[i]); in __label_update()
2048 AA_BUG(!new->vec[i]->label.proxy); in __label_update()
2049 AA_BUG(!new->vec[i]->label.proxy->label); in __label_update()
2050 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy) in __label_update()
2056 new->size -= aa_vec_unique(&new->vec[0], new->size, in __label_update()
2059 if (new->size == 1) { in __label_update()
2060 tmp = aa_get_label(&new->vec[0]->label); in __label_update()
2065 write_unlock_irqrestore(&ls->lock, flags); in __label_update()
2067 write_lock_irqsave(&ls->lock, flags); in __label_update()
2077 write_unlock_irqrestore(&ls->lock, flags); in __label_update()
2084 * __labelset_update - update labels in @ns
2101 AA_BUG(!mutex_is_locked(&ns->lock)); in __labelset_update()
2104 label = labelset_next_stale(&ns->labels); in __labelset_update()
2115 * __aa_labelset_update_subtree - update all labels with a stale component
2127 AA_BUG(!mutex_is_locked(&ns->lock)); in __aa_labelset_update_subtree()
2131 list_for_each_entry(child, &ns->sub_ns, base.list) { in __aa_labelset_update_subtree()
2132 mutex_lock_nested(&child->lock, child->level); in __aa_labelset_update_subtree()
2134 mutex_unlock(&child->lock); in __aa_labelset_update_subtree()