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
209 u |= vec[i]->label.flags & (FLAG_DEBUG1 | FLAG_DEBUG2 | in accum_vec_flags()
211 if (!(u & vec[i]->label.flags & FLAG_UNCONFINED)) in accum_vec_flags()
317 void aa_label_destroy(struct aa_label *label) in aa_label_destroy() argument
319 AA_BUG(!label); in aa_label_destroy()
321 if (!label_isprofile(label)) { in aa_label_destroy()
325 aa_put_str(label->hname); in aa_label_destroy()
327 label_for_each(i, label, profile) { in aa_label_destroy()
329 label->vec[i.i] = (struct aa_profile *) in aa_label_destroy()
334 if (label->proxy) { in aa_label_destroy()
335 if (rcu_dereference_protected(label->proxy->label, true) == label) in aa_label_destroy()
336 rcu_assign_pointer(label->proxy->label, NULL); in aa_label_destroy()
337 aa_put_proxy(label->proxy); in aa_label_destroy()
339 aa_free_secid(label->secid); in aa_label_destroy()
341 label->proxy = (struct aa_proxy *) PROXY_POISON + 1; in aa_label_destroy()
344 void aa_label_free(struct aa_label *label) in aa_label_free() argument
346 if (!label) in aa_label_free()
349 aa_label_destroy(label); in aa_label_free()
350 kfree(label); in aa_label_free()
353 static void label_free_switch(struct aa_label *label) in label_free_switch() argument
355 if (label->flags & FLAG_NS_COUNT) in label_free_switch()
356 aa_free_ns(labels_ns(label)); in label_free_switch()
357 else if (label_isprofile(label)) in label_free_switch()
358 aa_free_profile(labels_profile(label)); in label_free_switch()
360 aa_label_free(label); in label_free_switch()
365 struct aa_label *label = container_of(head, struct aa_label, rcu); in label_free_rcu() local
367 if (label->flags & FLAG_IN_TREE) in label_free_rcu()
368 (void) aa_label_remove(label); in label_free_rcu()
369 label_free_switch(label); in label_free_rcu()
374 struct aa_label *label = container_of(kref, struct aa_label, count); in aa_label_kref() local
375 struct aa_ns *ns = labels_ns(label); in aa_label_kref()
379 label_free_switch(label); in aa_label_kref()
383 AA_BUG(label_isprofile(label) && in aa_label_kref()
384 on_list_rcu(&label->vec[0]->base.profiles)); in aa_label_kref()
385 AA_BUG(label_isprofile(label) && in aa_label_kref()
386 on_list_rcu(&label->vec[0]->base.list)); in aa_label_kref()
388 /* TODO: if compound label and not stale add to reclaim cache */ in aa_label_kref()
389 call_rcu(&label->rcu, label_free_rcu); in aa_label_kref()
392 static void label_free_or_put_new(struct aa_label *label, struct aa_label *new) in label_free_or_put_new() argument
394 if (label != new) in label_free_or_put_new()
401 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp) in aa_label_init() argument
403 AA_BUG(!label); in aa_label_init()
406 if (aa_alloc_secid(label, gfp) < 0) in aa_label_init()
409 label->size = size; /* doesn't include null */ in aa_label_init()
410 label->vec[size] = NULL; /* null terminate */ in aa_label_init()
411 kref_init(&label->count); in aa_label_init()
412 RB_CLEAR_NODE(&label->node); in aa_label_init()
418 * aa_label_alloc - allocate a label with a profile vector of @size length
419 * @size: size of profile vector in the label
423 * Returns: new label
460 * label_cmp - label comparison for set ordering
461 * @a: label to compare (NOT NULL)
462 * @b: label to compare (NOT NULL)
479 int aa_label_next_confined(struct aa_label *label, int i) in aa_label_next_confined() argument
481 AA_BUG(!label); in aa_label_next_confined()
484 for (; i < label->size; i++) { in aa_label_next_confined()
485 if (!profile_unconfined(label->vec[i])) in aa_label_next_confined()
494 * @I: label iterator
495 * @set: label to test against
496 * @sub: label to if is subset of @set
533 * @set: label to test against
534 * @sub: label to test if is subset of @set
554 * @set: label to test against
555 * @sub: label to test if is subset of @set
588 * __label_remove - remove @label from the label set
589 * @label: label to remove
590 * @new: label to redirect to
592 * Requires: labels_set(@label)->lock write_lock
593 * Returns: true if the label was in the tree and removed
595 static bool __label_remove(struct aa_label *label, struct aa_label *new) in __label_remove() argument
597 struct aa_labelset *ls = labels_set(label); in __label_remove()
600 AA_BUG(!label); in __label_remove()
604 __aa_proxy_redirect(label, new); in __label_remove()
606 if (!label_is_stale(label)) in __label_remove()
607 __label_make_stale(label); in __label_remove()
609 if (label->flags & FLAG_IN_TREE) { in __label_remove()
610 rb_erase(&label->node, &ls->root); in __label_remove()
611 label->flags &= ~FLAG_IN_TREE; in __label_remove()
619 * __label_replace - replace @old with @new in label set
620 * @old: label to remove from label set
621 * @new: label to replace @old with
627 * Note: current implementation requires label set be order in such a way
629 * using pointer comparison of the label address would not work)
655 * __label_insert - attempt to insert @l into a label set
657 * @label: new label to insert (NOT NULL)
664 * else ref counted equivalent label that is already in the set,
668 struct aa_label *label, bool replace) in __label_insert() argument
673 AA_BUG(!label); in __label_insert()
674 AA_BUG(labels_set(label) != ls); in __label_insert()
676 AA_BUG(label->flags & FLAG_IN_TREE); in __label_insert()
682 int result = label_cmp(label, this); in __label_insert()
687 * so replace in place, however the label has in __label_insert()
695 __proxy_share(this, label); in __label_insert()
696 AA_BUG(!__label_replace(this, label)); in __label_insert()
697 return aa_get_label(label); in __label_insert()
705 rb_link_node(&label->node, parent, new); in __label_insert()
706 rb_insert_color(&label->node, &ls->root); in __label_insert()
707 label->flags |= FLAG_IN_TREE; in __label_insert()
709 return aa_get_label(label); in __label_insert()
713 * __vec_find - find label that matches @vec in label set
714 * @vec: vec of profiles to find matching label for (NOT NULL)
720 * Returns: ref counted @label if matching label is in tree
721 * ref counted label that is equiv to @l in tree
749 * __label_find - find label @label in label set
750 * @label: label to find (NOT NULL)
752 * Requires: labels_set(@label)->lock held
755 * Returns: ref counted @label if @label is in tree OR
756 * ref counted label that is equiv to @label in tree
757 * else NULL if @label or equiv is not in tree
759 static struct aa_label *__label_find(struct aa_label *label) in __label_find() argument
761 AA_BUG(!label); in __label_find()
763 return __vec_find(label->vec, label->size); in __label_find()
768 * aa_label_remove - remove a label from the labelset
769 * @label: label to remove
771 * Returns: true if @label was removed from the tree
772 * else @label was not in tree so it could not be removed
774 bool aa_label_remove(struct aa_label *label) in aa_label_remove() argument
776 struct aa_labelset *ls = labels_set(label); in aa_label_remove()
783 res = __label_remove(label, ns_unconfined(labels_ns(label))); in aa_label_remove()
790 * aa_label_replace - replace a label @old with a new version @new
791 * @old: label to replace
792 * @new: label replacing @old
831 * vec_find - find label @l in label set
832 * @vec: array of profiles to find equiv label for (NOT NULL)
835 * Returns: refcounted label if @vec equiv is in tree
841 struct aa_label *label; in vec_find() local
850 label = __vec_find(vec, n); in vec_find()
853 return label; in vec_find()
860 struct aa_label *label = NULL; in vec_create_and_insert_label() local
869 return aa_get_label(&vec[0]->label); in vec_create_and_insert_label()
871 ls = labels_set(&vec[len - 1]->label); in vec_create_and_insert_label()
874 * check if label exists before taking locks in vec_create_and_insert_label()
884 label = __label_insert(ls, new, false); in vec_create_and_insert_label()
886 label_free_or_put_new(label, new); in vec_create_and_insert_label()
888 return label; in vec_create_and_insert_label()
894 struct aa_label *label = vec_find(vec, len); in aa_vec_find_or_create_label() local
896 if (label) in aa_vec_find_or_create_label()
897 return label; in aa_vec_find_or_create_label()
903 * aa_label_find - find label @label in label set
904 * @label: label to find (NOT NULL)
908 * Returns: refcounted @label if @label is in tree
909 * refcounted label that is equiv to @label in tree
910 * else NULL if @label or equiv is not in tree
912 struct aa_label *aa_label_find(struct aa_label *label) in aa_label_find() argument
914 AA_BUG(!label); in aa_label_find()
916 return vec_find(label->vec, label->size); in aa_label_find()
921 * aa_label_insert - insert label @label into @ls or return existing label
922 * @ls: labelset to insert @label into
923 * @label: label to insert
925 * Requires: caller to hold a valid ref on @label
927 * Returns: ref counted @label if successful in inserting @label
928 * else ref counted equivalent label that is already in the set
930 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *label) in aa_label_insert() argument
936 AA_BUG(!label); in aa_label_insert()
938 /* check if label exists before taking lock */ in aa_label_insert()
939 if (!label_is_stale(label)) { in aa_label_insert()
941 l = __label_find(label); in aa_label_insert()
948 l = __label_insert(ls, label, false); in aa_label_insert()
957 * @I: label iterator
958 * @a: label to merge
959 * @b: label to merge
997 * @a: label to merge then compare (NOT NULL)
998 * @b: label to merge then compare (NOT NULL)
999 * @z: label to compare merge against (NOT NULL)
1035 * label_merge_insert - create a new label by merging @a and @b
1036 * @new: preallocated label to merge into (NOT NULL)
1037 * @a: label to merge with @b (NOT NULL)
1038 * @b: label to merge with @a (NOT NULL)
1042 * Returns: ref counted label either @new if merge is unique
1055 struct aa_label *label; in label_merge_insert() local
1074 AA_BUG(!new->vec[k]->label.proxy); in label_merge_insert()
1075 AA_BUG(!new->vec[k]->label.proxy->label); in label_merge_insert()
1076 if (next->label.proxy != new->vec[k]->label.proxy) in label_merge_insert()
1092 label = aa_get_label(&new->vec[0]->label); in label_merge_insert()
1093 return label; in label_merge_insert()
1108 label = __label_insert(labels_set(new), new, false); in label_merge_insert()
1111 return label; in label_merge_insert()
1115 * labelset_of_merge - find which labelset a merged label should be inserted
1116 * @a: label to merge and insert
1117 * @b: label to merge and insert
1119 * Returns: labelset that the merged label should be inserted into
1133 * __label_find_merge - find label that is equiv to merge of @a and @b
1135 * @a: label to merge with @b (NOT NULL)
1136 * @b: label to merge with @a (NOT NULL)
1140 * Returns: ref counted label that is equiv to merge of @a and @b
1175 * aa_label_find_merge - find label that is equiv to merge of @a and @b
1176 * @a: label to merge with @b (NOT NULL)
1177 * @b: label to merge with @a (NOT NULL)
1181 * Returns: ref counted label that is equiv to merge of @a and @b
1187 struct aa_label *label, *ar = NULL, *br = NULL; in aa_label_find_merge() local
1199 label = __label_find_merge(ls, a, b); in aa_label_find_merge()
1204 return label; in aa_label_find_merge()
1208 * aa_label_merge - attempt to insert new merged label of @a and @b
1209 * @a: label to merge with @b (NOT NULL)
1210 * @b: label to merge with @a (NOT NULL)
1216 * Returns: ref counted new label if successful in inserting merge of a & b
1217 * else ref counted equivalent label that is already in the set.
1218 * else NULL if could not create label (-ENOMEM)
1223 struct aa_label *label = NULL; in aa_label_merge() local
1232 * check if label exists before taking locks in aa_label_merge()
1234 label = aa_label_find_merge(a, b); in aa_label_merge()
1237 if (!label) { in aa_label_merge()
1250 label = label_merge_insert(new, a, b); in aa_label_merge()
1251 label_free_or_put_new(label, new); in aa_label_merge()
1257 return label; in aa_label_merge()
1284 * label_compound_match - find perms for full compound label
1287 * @label: label to check access permissions for
1295 * 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
1347 * @label: label to check access permissions for
1355 * For the label A//&B//&C this does the perm match for each of A and B and C
1361 struct aa_label *label, aa_state_t start, in label_components_match() argument
1371 label_for_each(i, label, tp) { in label_components_match()
1387 label_for_each_cont(i, label, tp) { in label_components_match()
1409 * aa_label_match - do a multi-component label match
1412 * @label: label to match (NOT NULL)
1421 struct aa_label *label, aa_state_t state, bool subns, in aa_label_match() argument
1424 int error = label_compound_match(profile, rules, label, state, subns, in aa_label_match()
1430 return label_components_match(profile, rules, label, state, subns, in aa_label_match()
1436 * aa_update_label_name - update a label to have a stored name
1438 * @label: label to update (NOT NULL)
1441 * Requires: labels_set(label) not locked in caller
1443 * note: only updates the label name if it does not have a name already
1446 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp) in aa_update_label_name() argument
1454 AA_BUG(!label); in aa_update_label_name()
1456 if (label->hname || labels_ns(label) != ns) in aa_update_label_name()
1459 if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) < 0) in aa_update_label_name()
1462 ls = labels_set(label); in aa_update_label_name()
1464 if (!label->hname && label->flags & FLAG_IN_TREE) { in aa_update_label_name()
1465 label->hname = name; in aa_update_label_name()
1475 * cached label name is present and visible
1476 * @label->hname only exists if label is namespace hierachical
1478 static inline bool use_label_hname(struct aa_ns *ns, struct aa_label *label, in use_label_hname() argument
1481 if (label->hname && (!ns || labels_ns(label) == ns) && in use_label_hname()
1555 static const char *label_modename(struct aa_ns *ns, struct aa_label *label, in label_modename() argument
1562 label_for_each(i, label, profile) { in label_modename()
1587 /* if any visible label is not unconfined the display_mode returns true */
1588 static inline bool display_mode(struct aa_ns *ns, struct aa_label *label, in display_mode() argument
1595 label_for_each(i, label, profile) { in display_mode()
1609 * aa_label_snxprint - print a label name to a string buffer
1613 * @label: label to view (NOT NULL)
1622 * label is not visible it will just be excluded. And if none
1623 * of the label is visible "---" will be used.
1626 struct aa_label *label, int flags) in aa_label_snxprint() argument
1635 AA_BUG(!label); in aa_label_snxprint()
1642 ns = labels_ns(label); in aa_label_snxprint()
1645 label_for_each(i, label, profile) { in aa_label_snxprint()
1668 if (display_mode(ns, label, flags)) { in aa_label_snxprint()
1670 label_modename(ns, label, flags)); in aa_label_snxprint()
1679 * aa_label_asxprint - allocate a string buffer and print label into it
1680 * @strp: Returns - the allocated buffer with the label name. (NOT NULL)
1682 * @label: label to view (NOT NULL)
1683 * @flags: flags controlling what label info is printed
1689 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label, in aa_label_asxprint() argument
1695 AA_BUG(!label); in aa_label_asxprint()
1697 size = aa_label_snxprint(NULL, 0, ns, label, flags); in aa_label_asxprint()
1704 return aa_label_snxprint(*strp, size + 1, ns, label, flags); in aa_label_asxprint()
1708 * aa_label_acntsxprint - allocate a __counted string buffer and print label
1711 * @label: label to view (NOT NULL)
1712 * @flags: flags controlling what label info is printed
1719 struct aa_label *label, int flags, gfp_t gfp) in aa_label_acntsxprint() argument
1724 AA_BUG(!label); in aa_label_acntsxprint()
1726 size = aa_label_snxprint(NULL, 0, ns, label, flags); in aa_label_acntsxprint()
1733 return aa_label_snxprint(*strp, size + 1, ns, label, flags); in aa_label_acntsxprint()
1738 struct aa_label *label, int flags, gfp_t gfp) in aa_label_xaudit() argument
1745 AA_BUG(!label); in aa_label_xaudit()
1747 if (!use_label_hname(ns, label, flags) || in aa_label_xaudit()
1748 display_mode(ns, label, flags)) { in aa_label_xaudit()
1749 len = aa_label_asxprint(&name, ns, label, flags, gfp); in aa_label_xaudit()
1751 AA_DEBUG("label print error"); in aa_label_xaudit()
1756 str = (char *) label->hname; in aa_label_xaudit()
1768 struct aa_label *label, int flags, gfp_t gfp) in aa_label_seq_xprint() argument
1771 AA_BUG(!label); in aa_label_seq_xprint()
1773 if (!use_label_hname(ns, label, flags)) { in aa_label_seq_xprint()
1777 len = aa_label_asxprint(&str, ns, label, flags, gfp); in aa_label_seq_xprint()
1779 AA_DEBUG("label print error"); in aa_label_seq_xprint()
1784 } else if (display_mode(ns, label, flags)) in aa_label_seq_xprint()
1785 seq_printf(f, "%s (%s)", label->hname, in aa_label_seq_xprint()
1786 label_modename(ns, label, flags)); in aa_label_seq_xprint()
1788 seq_puts(f, label->hname); in aa_label_seq_xprint()
1791 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags, in aa_label_xprintk() argument
1794 AA_BUG(!label); in aa_label_xprintk()
1796 if (!use_label_hname(ns, label, flags)) { in aa_label_xprintk()
1800 len = aa_label_asxprint(&str, ns, label, flags, gfp); in aa_label_xprintk()
1802 AA_DEBUG("label print error"); in aa_label_xprintk()
1807 } else if (display_mode(ns, label, flags)) in aa_label_xprintk()
1808 pr_info("%s (%s)", label->hname, in aa_label_xprintk()
1809 label_modename(ns, label, flags)); in aa_label_xprintk()
1811 pr_info("%s", label->hname); in aa_label_xprintk()
1814 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp) in aa_label_audit() argument
1818 aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp); in aa_label_audit()
1822 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp) in aa_label_seq_print() argument
1826 aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp); in aa_label_seq_print()
1830 void aa_label_printk(struct aa_label *label, gfp_t gfp) in aa_label_printk() argument
1834 aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp); in aa_label_printk()
1876 * aa_label_strn_parse - parse, validate and convert a text string to a label
1877 * @base: base label to use for lookups (NOT NULL)
1884 * Returns: the matching refcounted label if present
1892 struct aa_label *label, *currbase = base; in aa_label_strn_parse() local
1902 base != &root_ns->unconfined->label)) in aa_label_strn_parse()
1931 currbase = &vec[i]->label; in aa_label_strn_parse()
1943 return &vec[0]->label; in aa_label_strn_parse()
1948 label = aa_get_label(&vec[0]->label); in aa_label_strn_parse()
1953 label = aa_vec_find_or_create_label(vec, len, gfp); in aa_label_strn_parse()
1955 label = vec_find(vec, len); in aa_label_strn_parse()
1956 if (!label) in aa_label_strn_parse()
1962 return label; in aa_label_strn_parse()
1965 label = ERR_PTR(-ENOENT); in aa_label_strn_parse()
1977 * aa_labelset_destroy - remove all labels from the label set
1978 * @ls: label set to cleanup (NOT NULL)
2016 struct aa_label *label; in labelset_next_stale() local
2025 label = rb_entry(node, struct aa_label, node); in labelset_next_stale()
2026 if ((label_is_stale(label) || in labelset_next_stale()
2027 vec_is_stale(label->vec, label->size)) && in labelset_next_stale()
2028 __aa_get_label(label)) in labelset_next_stale()
2032 label = NULL; in labelset_next_stale()
2037 return label; in labelset_next_stale()
2041 * __label_update - insert updated version of @label into labelset
2042 * @label: the label to update/replace
2044 * Returns: new label that is up to date
2049 * Note: worst case is the stale @label does not get updated and has
2052 static struct aa_label *__label_update(struct aa_label *label) in __label_update() argument
2059 AA_BUG(!label); in __label_update()
2060 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock)); in __label_update()
2062 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL); in __label_update()
2068 * and label updates, label merging and removal can be occurring in __label_update()
2070 ls = labels_set(label); in __label_update()
2072 for (i = 0; i < label->size; i++) { in __label_update()
2073 AA_BUG(!label->vec[i]); in __label_update()
2074 new->vec[i] = aa_get_newest_profile(label->vec[i]); in __label_update()
2076 AA_BUG(!new->vec[i]->label.proxy); in __label_update()
2077 AA_BUG(!new->vec[i]->label.proxy->label); in __label_update()
2078 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy) in __label_update()
2082 /* updated stale label by being removed/renamed from labelset */ in __label_update()
2088 tmp = aa_get_label(&new->vec[0]->label); in __label_update()
2089 AA_BUG(tmp == label); in __label_update()
2092 if (labels_set(label) != labels_set(new)) { in __label_update()
2099 AA_BUG(labels_ns(label) != labels_ns(new)); in __label_update()
2101 tmp = __label_insert(labels_set(label), new, true); in __label_update()
2103 /* ensure label is removed, and redirected correctly */ in __label_update()
2104 __label_remove(label, tmp); in __label_update()
2118 * Any label that has a stale component is marked stale and replaced and
2126 struct aa_label *label; in __labelset_update() local
2132 label = labelset_next_stale(&ns->labels); in __labelset_update()
2133 if (label) { in __labelset_update()
2134 struct aa_label *l = __label_update(label); in __labelset_update()
2137 aa_put_label(label); in __labelset_update()
2139 } while (label); in __labelset_update()