Lines Matching full:label

5  * This file contains AppArmor label definitions
16 #include "include/label.h"
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
34 * Labels are not ref counted by the label set, so they maybe removed and
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()
60 struct aa_proxy *aa_alloc_proxy(struct aa_label *label, gfp_t gfp) in aa_alloc_proxy() argument
67 rcu_assign_pointer(new->label, aa_get_label(label)); in aa_alloc_proxy()
81 tmp = rcu_dereference_protected(orig->proxy->label, in __aa_proxy_redirect()
83 rcu_assign_pointer(orig->proxy->label, aa_get_label(new)); in __aa_proxy_redirect()
99 * ns_cmp - compare ns for label set ordering
156 * vec_cmp - label comparison for set ordering
157 * @a: label to compare (NOT NULL)
208 u |= vec[i]->label.flags & mask; in union_vec_flags()
312 void aa_label_destroy(struct aa_label *label) in aa_label_destroy() argument
314 AA_BUG(!label); in aa_label_destroy()
316 if (!label_isprofile(label)) { in aa_label_destroy()
320 aa_put_str(label->hname); in aa_label_destroy()
322 label_for_each(i, label, profile) { in aa_label_destroy()
324 label->vec[i.i] = (struct aa_profile *) in aa_label_destroy()
329 if (label->proxy) { in aa_label_destroy()
330 if (rcu_dereference_protected(label->proxy->label, true) == label) in aa_label_destroy()
331 rcu_assign_pointer(label->proxy->label, NULL); in aa_label_destroy()
332 aa_put_proxy(label->proxy); in aa_label_destroy()
334 aa_free_secid(label->secid); in aa_label_destroy()
336 label->proxy = (struct aa_proxy *) PROXY_POISON + 1; in aa_label_destroy()
339 void aa_label_free(struct aa_label *label) in aa_label_free() argument
341 if (!label) in aa_label_free()
344 aa_label_destroy(label); in aa_label_free()
345 kfree(label); in aa_label_free()
348 static void label_free_switch(struct aa_label *label) in label_free_switch() argument
350 if (label->flags & FLAG_NS_COUNT) in label_free_switch()
351 aa_free_ns(labels_ns(label)); in label_free_switch()
352 else if (label_isprofile(label)) in label_free_switch()
353 aa_free_profile(labels_profile(label)); in label_free_switch()
355 aa_label_free(label); in label_free_switch()
360 struct aa_label *label = container_of(head, struct aa_label, rcu); in label_free_rcu() local
362 if (label->flags & FLAG_IN_TREE) in label_free_rcu()
363 (void) aa_label_remove(label); in label_free_rcu()
364 label_free_switch(label); in label_free_rcu()
369 struct aa_label *label = container_of(kref, struct aa_label, count); in aa_label_kref() local
370 struct aa_ns *ns = labels_ns(label); in aa_label_kref()
374 label_free_switch(label); in aa_label_kref()
378 AA_BUG(label_isprofile(label) && in aa_label_kref()
379 on_list_rcu(&label->vec[0]->base.profiles)); in aa_label_kref()
380 AA_BUG(label_isprofile(label) && in aa_label_kref()
381 on_list_rcu(&label->vec[0]->base.list)); in aa_label_kref()
383 /* TODO: if compound label and not stale add to reclaim cache */ in aa_label_kref()
384 call_rcu(&label->rcu, label_free_rcu); in aa_label_kref()
387 static void label_free_or_put_new(struct aa_label *label, struct aa_label *new) in label_free_or_put_new() argument
389 if (label != new) in label_free_or_put_new()
396 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp) in aa_label_init() argument
398 AA_BUG(!label); in aa_label_init()
401 if (aa_alloc_secid(label, gfp) < 0) in aa_label_init()
404 label->size = size; /* doesn't include null */ in aa_label_init()
405 label->vec[size] = NULL; /* null terminate */ in aa_label_init()
406 kref_init(&label->count); in aa_label_init()
407 RB_CLEAR_NODE(&label->node); in aa_label_init()
413 * aa_label_alloc - allocate a label with a profile vector of @size length
414 * @size: size of profile vector in the label
418 * Returns: new label
455 * label_cmp - label comparison for set ordering
456 * @a: label to compare (NOT NULL)
457 * @b: label to compare (NOT NULL)
474 int aa_label_next_confined(struct aa_label *label, int i) in aa_label_next_confined() argument
476 AA_BUG(!label); in aa_label_next_confined()
479 for (; i < label->size; i++) { in aa_label_next_confined()
480 if (!profile_unconfined(label->vec[i])) in aa_label_next_confined()
489 * @I: label iterator
490 * @set: label to test against
491 * @sub: label to if is subset of @set
528 * @set: label to test against
529 * @sub: label to test if is subset of @set
549 * @set: label to test against
550 * @sub: label to test if is subset of @set
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) in __label_remove() argument
592 struct aa_labelset *ls = labels_set(label); in __label_remove()
595 AA_BUG(!label); in __label_remove()
599 __aa_proxy_redirect(label, new); in __label_remove()
601 if (!label_is_stale(label)) in __label_remove()
602 __label_make_stale(label); in __label_remove()
604 if (label->flags & FLAG_IN_TREE) { in __label_remove()
605 rb_erase(&label->node, &ls->root); in __label_remove()
606 label->flags &= ~FLAG_IN_TREE; in __label_remove()
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
622 * Note: current implementation requires label set be order in such a way
624 * using pointer comparison of the label address would not work)
650 * __label_insert - attempt to insert @l into a label set
652 * @label: new label to insert (NOT NULL)
659 * else ref counted equivalent label that is already in the set,
663 struct aa_label *label, bool replace) in __label_insert() argument
668 AA_BUG(!label); in __label_insert()
669 AA_BUG(labels_set(label) != ls); in __label_insert()
671 AA_BUG(label->flags & FLAG_IN_TREE); in __label_insert()
677 int result = label_cmp(label, this); in __label_insert()
682 * so replace in place, however the label has in __label_insert()
690 __proxy_share(this, label); in __label_insert()
691 AA_BUG(!__label_replace(this, label)); in __label_insert()
692 return aa_get_label(label); in __label_insert()
700 rb_link_node(&label->node, parent, new); in __label_insert()
701 rb_insert_color(&label->node, &ls->root); in __label_insert()
702 label->flags |= FLAG_IN_TREE; in __label_insert()
704 return aa_get_label(label); in __label_insert()
708 * __vec_find - find label that matches @vec in label set
709 * @vec: vec of profiles to find matching label for (NOT NULL)
715 * Returns: ref counted @label if matching label is in tree
716 * ref counted label that is equiv to @l in tree
744 * __label_find - find label @label in label set
745 * @label: label to find (NOT NULL)
747 * Requires: labels_set(@label)->lock held
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) in __label_find() argument
756 AA_BUG(!label); in __label_find()
758 return __vec_find(label->vec, label->size); in __label_find()
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) in aa_label_remove() argument
771 struct aa_labelset *ls = labels_set(label); in aa_label_remove()
778 res = __label_remove(label, ns_unconfined(labels_ns(label))); in aa_label_remove()
785 * aa_label_replace - replace a label @old with a new version @new
786 * @old: label to replace
787 * @new: label replacing @old
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
836 struct aa_label *label; in vec_find() local
845 label = __vec_find(vec, n); in vec_find()
848 return label; in vec_find()
855 struct aa_label *label = NULL; in vec_create_and_insert_label() local
864 return aa_get_label(&vec[0]->label); in vec_create_and_insert_label()
866 ls = labels_set(&vec[len - 1]->label); in vec_create_and_insert_label()
869 * check if label exists before taking locks in vec_create_and_insert_label()
879 label = __label_insert(ls, new, false); in vec_create_and_insert_label()
881 label_free_or_put_new(label, new); in vec_create_and_insert_label()
883 return label; in vec_create_and_insert_label()
889 struct aa_label *label = vec_find(vec, len); in aa_vec_find_or_create_label() local
891 if (label) in aa_vec_find_or_create_label()
892 return label; in aa_vec_find_or_create_label()
898 * aa_label_find - find label @label in label set
899 * @label: label to find (NOT NULL)
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) in aa_label_find() argument
909 AA_BUG(!label); in aa_label_find()
911 return vec_find(label->vec, label->size); in aa_label_find()
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) in aa_label_insert() argument
931 AA_BUG(!label); in aa_label_insert()
933 /* check if label exists before taking lock */ in aa_label_insert()
934 if (!label_is_stale(label)) { in aa_label_insert()
936 l = __label_find(label); in aa_label_insert()
943 l = __label_insert(ls, label, false); in aa_label_insert()
952 * @I: label iterator
953 * @a: label to merge
954 * @b: label to merge
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)
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)
1037 * Returns: ref counted label either @new if merge is unique
1050 struct aa_label *label; in label_merge_insert() local
1069 AA_BUG(!new->vec[k]->label.proxy); in label_merge_insert()
1070 AA_BUG(!new->vec[k]->label.proxy->label); in label_merge_insert()
1071 if (next->label.proxy != new->vec[k]->label.proxy) in label_merge_insert()
1087 label = aa_get_label(&new->vec[0]->label); in label_merge_insert()
1088 return label; in label_merge_insert()
1104 label = __label_insert(labels_set(new), new, false); in label_merge_insert()
1107 return label; in label_merge_insert()
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
1129 * __label_find_merge - find label that is equiv to merge of @a and @b
1131 * @a: label to merge with @b (NOT NULL)
1132 * @b: label to merge with @a (NOT NULL)
1136 * Returns: ref counted label that is equiv to merge of @a and @b
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)
1177 * Returns: ref counted label that is equiv to merge of @a and @b
1183 struct aa_label *label, *ar = NULL, *br = NULL; in aa_label_find_merge() local
1195 label = __label_find_merge(ls, a, b); in aa_label_find_merge()
1200 return label; in aa_label_find_merge()
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)
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)
1220 struct aa_label *label = NULL; in aa_label_merge() local
1229 * check if label exists before taking locks in aa_label_merge()
1231 label = aa_label_find_merge(a, b); in aa_label_merge()
1234 if (!label) { in aa_label_merge()
1247 label = label_merge_insert(new, a, b); in aa_label_merge()
1248 label_free_or_put_new(label, new); in aa_label_merge()
1254 return label; in aa_label_merge()
1258 struct aa_label *label) in label_is_visible() argument
1260 return aa_ns_visible(profile->ns, labels_ns(label), true); in label_is_visible()
1286 * label_compound_match - find perms for full compound label
1288 * @label: label to check access permissions for
1296 * For the label A//&B//&C this does the perm match for A//&B//&C
1301 struct aa_label *label, in label_compound_match() argument
1309 label_for_each(i, label, tp) { in label_compound_match()
1323 label_for_each_cont(i, label, tp) { in label_compound_match()
1344 * label_components_match - find perms for all subcomponents of a label
1346 * @label: label to check access permissions for
1354 * For the label A//&B//&C this does the perm match for each of A and B and C
1359 struct aa_label *label, unsigned int start, in label_components_match() argument
1369 label_for_each(i, label, tp) { in label_components_match()
1385 label_for_each_cont(i, label, tp) { in label_components_match()
1407 * aa_label_match - do a multi-component label match
1409 * @label: label to match (NOT NULL)
1417 int aa_label_match(struct aa_profile *profile, struct aa_label *label, in aa_label_match() argument
1421 int error = label_compound_match(profile, label, state, subns, request, in aa_label_match()
1427 return label_components_match(profile, label, state, subns, request, in aa_label_match()
1433 * aa_update_label_name - update a label to have a stored name
1435 * @label: label to update (NOT NULL)
1438 * Requires: labels_set(label) not locked in caller
1440 * note: only updates the label name if it does not have a name already
1443 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp) in aa_update_label_name() argument
1451 AA_BUG(!label); in aa_update_label_name()
1453 if (label->hname || labels_ns(label) != ns) in aa_update_label_name()
1456 if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) < 0) in aa_update_label_name()
1459 ls = labels_set(label); in aa_update_label_name()
1461 if (!label->hname && label->flags & FLAG_IN_TREE) { in aa_update_label_name()
1462 label->hname = name; in aa_update_label_name()
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, in use_label_hname() argument
1478 if (label->hname && (!ns || labels_ns(label) == ns) && in use_label_hname()
1552 static const char *label_modename(struct aa_ns *ns, struct aa_label *label, in label_modename() argument
1559 label_for_each(i, label, profile) { in label_modename()
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, in display_mode() argument
1592 label_for_each(i, label, profile) { in display_mode()
1606 * aa_label_snxprint - print a label name to a string buffer
1610 * @label: label to view (NOT NULL)
1619 * label is not visible it will just be excluded. And if none
1620 * of the label is visible "---" will be used.
1623 struct aa_label *label, int flags) in aa_label_snxprint() argument
1632 AA_BUG(!label); in aa_label_snxprint()
1639 ns = labels_ns(label); in aa_label_snxprint()
1642 label_for_each(i, label, profile) { in aa_label_snxprint()
1665 if (display_mode(ns, label, flags)) { in aa_label_snxprint()
1667 label_modename(ns, label, flags)); in aa_label_snxprint()
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)
1679 * @label: label to view (NOT NULL)
1680 * @flags: flags controlling what label info is printed
1686 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label, in aa_label_asxprint() argument
1692 AA_BUG(!label); in aa_label_asxprint()
1694 size = aa_label_snxprint(NULL, 0, ns, label, flags); in aa_label_asxprint()
1701 return aa_label_snxprint(*strp, size + 1, ns, label, flags); in aa_label_asxprint()
1705 * aa_label_acntsxprint - allocate a __counted string buffer and print label
1708 * @label: label to view (NOT NULL)
1709 * @flags: flags controlling what label info is printed
1716 struct aa_label *label, int flags, gfp_t gfp) in aa_label_acntsxprint() argument
1721 AA_BUG(!label); in aa_label_acntsxprint()
1723 size = aa_label_snxprint(NULL, 0, ns, label, flags); in aa_label_acntsxprint()
1730 return aa_label_snxprint(*strp, size + 1, ns, label, flags); in aa_label_acntsxprint()
1735 struct aa_label *label, int flags, gfp_t gfp) in aa_label_xaudit() argument
1742 AA_BUG(!label); in aa_label_xaudit()
1744 if (!use_label_hname(ns, label, flags) || in aa_label_xaudit()
1745 display_mode(ns, label, flags)) { in aa_label_xaudit()
1746 len = aa_label_asxprint(&name, ns, label, flags, gfp); in aa_label_xaudit()
1748 AA_DEBUG("label print error"); in aa_label_xaudit()
1753 str = (char *) label->hname; in aa_label_xaudit()
1765 struct aa_label *label, int flags, gfp_t gfp) in aa_label_seq_xprint() argument
1768 AA_BUG(!label); in aa_label_seq_xprint()
1770 if (!use_label_hname(ns, label, flags)) { in aa_label_seq_xprint()
1774 len = aa_label_asxprint(&str, ns, label, flags, gfp); in aa_label_seq_xprint()
1776 AA_DEBUG("label print error"); in aa_label_seq_xprint()
1781 } else if (display_mode(ns, label, flags)) in aa_label_seq_xprint()
1782 seq_printf(f, "%s (%s)", label->hname, in aa_label_seq_xprint()
1783 label_modename(ns, label, flags)); in aa_label_seq_xprint()
1785 seq_puts(f, label->hname); in aa_label_seq_xprint()
1788 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags, in aa_label_xprintk() argument
1791 AA_BUG(!label); in aa_label_xprintk()
1793 if (!use_label_hname(ns, label, flags)) { in aa_label_xprintk()
1797 len = aa_label_asxprint(&str, ns, label, flags, gfp); in aa_label_xprintk()
1799 AA_DEBUG("label print error"); in aa_label_xprintk()
1804 } else if (display_mode(ns, label, flags)) in aa_label_xprintk()
1805 pr_info("%s (%s)", label->hname, in aa_label_xprintk()
1806 label_modename(ns, label, flags)); in aa_label_xprintk()
1808 pr_info("%s", label->hname); in aa_label_xprintk()
1811 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp) in aa_label_audit() argument
1815 aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp); in aa_label_audit()
1819 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp) in aa_label_seq_print() argument
1823 aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp); in aa_label_seq_print()
1827 void aa_label_printk(struct aa_label *label, gfp_t gfp) in aa_label_printk() argument
1831 aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp); in aa_label_printk()
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)
1881 * Returns: the matching refcounted label if present
1889 struct aa_label *label, *currbase = base; in aa_label_strn_parse() local
1899 base != &root_ns->unconfined->label)) in aa_label_strn_parse()
1928 currbase = &vec[i]->label; in aa_label_strn_parse()
1940 return &vec[0]->label; in aa_label_strn_parse()
1945 label = aa_get_label(&vec[0]->label); in aa_label_strn_parse()
1950 label = aa_vec_find_or_create_label(vec, len, gfp); in aa_label_strn_parse()
1952 label = vec_find(vec, len); in aa_label_strn_parse()
1953 if (!label) in aa_label_strn_parse()
1959 return label; in aa_label_strn_parse()
1962 label = ERR_PTR(-ENOENT); in aa_label_strn_parse()
1974 * aa_labelset_destroy - remove all labels from the label set
1975 * @ls: label set to cleanup (NOT NULL)
2013 struct aa_label *label; in labelset_next_stale() local
2022 label = rb_entry(node, struct aa_label, node); in labelset_next_stale()
2023 if ((label_is_stale(label) || in labelset_next_stale()
2024 vec_is_stale(label->vec, label->size)) && in labelset_next_stale()
2025 __aa_get_label(label)) in labelset_next_stale()
2029 label = NULL; in labelset_next_stale()
2034 return label; in labelset_next_stale()
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
2046 * Note: worst case is the stale @label does not get updated and has
2049 static struct aa_label *__label_update(struct aa_label *label) in __label_update() argument
2056 AA_BUG(!label); in __label_update()
2057 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock)); in __label_update()
2059 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL); in __label_update()
2065 * and label updates, label merging and removal can be occurring in __label_update()
2067 ls = labels_set(label); in __label_update()
2069 for (i = 0; i < label->size; i++) { in __label_update()
2070 AA_BUG(!label->vec[i]); in __label_update()
2071 new->vec[i] = aa_get_newest_profile(label->vec[i]); in __label_update()
2073 AA_BUG(!new->vec[i]->label.proxy); in __label_update()
2074 AA_BUG(!new->vec[i]->label.proxy->label); in __label_update()
2075 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy) in __label_update()
2079 /* updated stale label by being removed/renamed from labelset */ in __label_update()
2085 tmp = aa_get_label(&new->vec[0]->label); in __label_update()
2086 AA_BUG(tmp == label); in __label_update()
2089 if (labels_set(label) != labels_set(new)) { in __label_update()
2096 AA_BUG(labels_ns(label) != labels_ns(new)); in __label_update()
2098 tmp = __label_insert(labels_set(label), new, true); in __label_update()
2100 /* ensure label is removed, and redirected correctly */ in __label_update()
2101 __label_remove(label, tmp); in __label_update()
2115 * Any label that has a stale component is marked stale and replaced and
2123 struct aa_label *label; in __labelset_update() local
2129 label = labelset_next_stale(&ns->labels); in __labelset_update()
2130 if (label) { in __labelset_update()
2131 struct aa_label *l = __label_update(label); in __labelset_update()
2134 aa_put_label(label); in __labelset_update()
2136 } while (label); in __labelset_update()