Lines Matching refs: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 */
46 aa_put_label(rcu_dereference_protected(proxy->label, true));
48 RCU_INIT_POINTER(proxy->label, (struct aa_label *)PROXY_POISON);
60 struct aa_proxy *aa_alloc_proxy(struct aa_label *label, gfp_t gfp)
67 rcu_assign_pointer(new->label, aa_get_label(label));
81 tmp = rcu_dereference_protected(orig->proxy->label,
83 rcu_assign_pointer(orig->proxy->label, aa_get_label(new));
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 |
211 if (!(u & vec[i]->label.flags & FLAG_UNCONFINED))
317 void aa_label_destroy(struct aa_label *label)
319 AA_BUG(!label);
321 if (!label_isprofile(label)) {
325 aa_put_str(label->hname);
327 label_for_each(i, label, profile) {
329 label->vec[i.i] = (struct aa_profile *)
334 if (label->proxy) {
335 if (rcu_dereference_protected(label->proxy->label, true) == label)
336 rcu_assign_pointer(label->proxy->label, NULL);
337 aa_put_proxy(label->proxy);
339 aa_free_secid(label->secid);
341 label->proxy = (struct aa_proxy *) PROXY_POISON + 1;
344 void aa_label_free(struct aa_label *label)
346 if (!label)
349 aa_label_destroy(label);
350 kfree(label);
353 static void label_free_switch(struct aa_label *label)
355 if (label->flags & FLAG_NS_COUNT)
356 aa_free_ns(labels_ns(label));
357 else if (label_isprofile(label))
358 aa_free_profile(labels_profile(label));
360 aa_label_free(label);
365 struct aa_label *label = container_of(head, struct aa_label, rcu);
367 if (label->flags & FLAG_IN_TREE)
368 (void) aa_label_remove(label);
369 label_free_switch(label);
374 struct aa_label *label = container_of(kref, struct aa_label, count);
375 struct aa_ns *ns = labels_ns(label);
379 label_free_switch(label);
383 AA_BUG(label_isprofile(label) &&
384 on_list_rcu(&label->vec[0]->base.profiles));
385 AA_BUG(label_isprofile(label) &&
386 on_list_rcu(&label->vec[0]->base.list));
388 /* TODO: if compound label and not stale add to reclaim cache */
389 call_rcu(&label->rcu, label_free_rcu);
392 static void label_free_or_put_new(struct aa_label *label, struct aa_label *new)
394 if (label != new)
401 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp)
403 AA_BUG(!label);
406 if (aa_alloc_secid(label, gfp) < 0)
409 label->size = size; /* doesn't include null */
410 label->vec[size] = NULL; /* null terminate */
411 kref_init(&label->count);
412 RB_CLEAR_NODE(&label->node);
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)
481 AA_BUG(!label);
484 for (; i < label->size; i++) {
485 if (!profile_unconfined(label->vec[i]))
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)
597 struct aa_labelset *ls = labels_set(label);
600 AA_BUG(!label);
604 __aa_proxy_redirect(label, new);
606 if (!label_is_stale(label))
607 __label_make_stale(label);
609 if (label->flags & FLAG_IN_TREE) {
610 rb_erase(&label->node, &ls->root);
611 label->flags &= ~FLAG_IN_TREE;
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)
673 AA_BUG(!label);
674 AA_BUG(labels_set(label) != ls);
676 AA_BUG(label->flags & FLAG_IN_TREE);
682 int result = label_cmp(label, this);
687 * so replace in place, however the label has
695 __proxy_share(this, label);
696 AA_BUG(!__label_replace(this, label));
697 return aa_get_label(label);
705 rb_link_node(&label->node, parent, new);
706 rb_insert_color(&label->node, &ls->root);
707 label->flags |= FLAG_IN_TREE;
709 return aa_get_label(label);
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)
761 AA_BUG(!label);
763 return __vec_find(label->vec, label->size);
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)
776 struct aa_labelset *ls = labels_set(label);
783 res = __label_remove(label, ns_unconfined(labels_ns(label)));
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;
850 label = __vec_find(vec, n);
853 return label;
860 struct aa_label *label = NULL;
869 return aa_get_label(&vec[0]->label);
871 ls = labels_set(&vec[len - 1]->label);
874 * check if label exists before taking locks
884 label = __label_insert(ls, new, false);
886 label_free_or_put_new(label, new);
888 return label;
894 struct aa_label *label = vec_find(vec, len);
896 if (label)
897 return 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)
914 AA_BUG(!label);
916 return vec_find(label->vec, label->size);
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)
936 AA_BUG(!label);
938 /* check if label exists before taking lock */
939 if (!label_is_stale(label)) {
941 l = __label_find(label);
948 l = __label_insert(ls, label, false);
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;
1074 AA_BUG(!new->vec[k]->label.proxy);
1075 AA_BUG(!new->vec[k]->label.proxy->label);
1076 if (next->label.proxy != new->vec[k]->label.proxy)
1092 label = aa_get_label(&new->vec[0]->label);
1093 return label;
1108 label = __label_insert(labels_set(new), new, false);
1111 return label;
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;
1199 label = __label_find_merge(ls, a, b);
1204 return label;
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;
1232 * check if label exists before taking locks
1234 label = aa_label_find_merge(a, b);
1237 if (!label) {
1250 label = label_merge_insert(new, a, b);
1251 label_free_or_put_new(label, new);
1257 return label;
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,
1309 label_for_each(i, label, tp) {
1323 label_for_each_cont(i, label, tp) {
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,
1371 label_for_each(i, label, tp) {
1387 label_for_each_cont(i, label, tp) {
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,
1424 int error = label_compound_match(profile, rules, label, state, subns,
1430 return label_components_match(profile, rules, label, state, subns,
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)
1454 AA_BUG(!label);
1456 if (label->hname || labels_ns(label) != ns)
1459 if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) < 0)
1462 ls = labels_set(label);
1464 if (!label->hname && label->flags & FLAG_IN_TREE) {
1465 label->hname = 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,
1481 if (label->hname && (!ns || labels_ns(label) == ns) &&
1555 static const char *label_modename(struct aa_ns *ns, struct aa_label *label,
1562 label_for_each(i, label, profile) {
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,
1595 label_for_each(i, label, profile) {
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)
1635 AA_BUG(!label);
1642 ns = labels_ns(label);
1645 label_for_each(i, label, profile) {
1668 if (display_mode(ns, label, flags)) {
1670 label_modename(ns, label, flags));
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,
1695 AA_BUG(!label);
1697 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1704 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
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)
1724 AA_BUG(!label);
1726 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1733 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1738 struct aa_label *label, int flags, gfp_t gfp)
1745 AA_BUG(!label);
1747 if (!use_label_hname(ns, label, flags) ||
1748 display_mode(ns, label, flags)) {
1749 len = aa_label_asxprint(&name, ns, label, flags, gfp);
1751 AA_DEBUG("label print error");
1756 str = (char *) label->hname;
1768 struct aa_label *label, int flags, gfp_t gfp)
1771 AA_BUG(!label);
1773 if (!use_label_hname(ns, label, flags)) {
1777 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1779 AA_DEBUG("label print error");
1784 } else if (display_mode(ns, label, flags))
1785 seq_printf(f, "%s (%s)", label->hname,
1786 label_modename(ns, label, flags));
1788 seq_puts(f, label->hname);
1791 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
1794 AA_BUG(!label);
1796 if (!use_label_hname(ns, label, flags)) {
1800 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1802 AA_DEBUG("label print error");
1807 } else if (display_mode(ns, label, flags))
1808 pr_info("%s (%s)", label->hname,
1809 label_modename(ns, label, flags));
1811 pr_info("%s", label->hname);
1814 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp)
1818 aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp);
1822 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp)
1826 aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp);
1830 void aa_label_printk(struct aa_label *label, gfp_t gfp)
1834 aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp);
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;
1902 base != &root_ns->unconfined->label))
1931 currbase = &vec[i]->label;
1943 return &vec[0]->label;
1948 label = aa_get_label(&vec[0]->label);
1953 label = aa_vec_find_or_create_label(vec, len, gfp);
1955 label = vec_find(vec, len);
1956 if (!label)
1962 return label;
1965 label = ERR_PTR(-ENOENT);
1977 * aa_labelset_destroy - remove all labels from the label set
1978 * @ls: label set to cleanup (NOT NULL)
2016 struct aa_label *label;
2025 label = rb_entry(node, struct aa_label, node);
2026 if ((label_is_stale(label) ||
2027 vec_is_stale(label->vec, label->size)) &&
2028 __aa_get_label(label))
2032 label = NULL;
2037 return label;
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)
2059 AA_BUG(!label);
2060 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock));
2062 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL);
2068 * and label updates, label merging and removal can be occurring
2070 ls = labels_set(label);
2072 for (i = 0; i < label->size; i++) {
2073 AA_BUG(!label->vec[i]);
2074 new->vec[i] = aa_get_newest_profile(label->vec[i]);
2076 AA_BUG(!new->vec[i]->label.proxy);
2077 AA_BUG(!new->vec[i]->label.proxy->label);
2078 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy)
2082 /* updated stale label by being removed/renamed from labelset */
2088 tmp = aa_get_label(&new->vec[0]->label);
2089 AA_BUG(tmp == label);
2092 if (labels_set(label) != labels_set(new)) {
2099 AA_BUG(labels_ns(label) != labels_ns(new));
2101 tmp = __label_insert(labels_set(label), new, true);
2103 /* ensure label is removed, and redirected correctly */
2104 __label_remove(label, tmp);
2118 * Any label that has a stale component is marked stale and replaced and
2126 struct aa_label *label;
2132 label = labelset_next_stale(&ns->labels);
2133 if (label) {
2134 struct aa_label *l = __label_update(label);
2137 aa_put_label(label);
2139 } while (label);