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
157 * @a: label to compare (NOT NULL)
312 void aa_label_destroy(struct aa_label *label)
314 AA_BUG(!label);
316 if (!label_isprofile(label)) {
320 aa_put_str(label->hname);
322 label_for_each(i, label, profile) {
324 label->vec[i.i] = (struct aa_profile *)
329 if (label->proxy) {
330 if (rcu_dereference_protected(label->proxy->label, true) == label)
331 rcu_assign_pointer(label->proxy->label, NULL);
332 aa_put_proxy(label->proxy);
334 aa_free_secid(label->secid);
336 label->proxy = (struct aa_proxy *) PROXY_POISON + 1;
339 void aa_label_free(struct aa_label *label)
341 if (!label)
344 aa_label_destroy(label);
345 kfree(label);
348 static void label_free_switch(struct aa_label *label)
350 if (label->flags & FLAG_NS_COUNT)
351 aa_free_ns(labels_ns(label));
352 else if (label_isprofile(label))
353 aa_free_profile(labels_profile(label));
355 aa_label_free(label);
360 struct aa_label *label = container_of(head, struct aa_label, rcu);
362 if (label->flags & FLAG_IN_TREE)
363 (void) aa_label_remove(label);
364 label_free_switch(label);
369 struct aa_label *label = container_of(kref, struct aa_label, count);
370 struct aa_ns *ns = labels_ns(label);
374 label_free_switch(label);
378 AA_BUG(label_isprofile(label) &&
379 on_list_rcu(&label->vec[0]->base.profiles));
380 AA_BUG(label_isprofile(label) &&
381 on_list_rcu(&label->vec[0]->base.list));
383 /* TODO: if compound label and not stale add to reclaim cache */
384 call_rcu(&label->rcu, label_free_rcu);
387 static void label_free_or_put_new(struct aa_label *label, struct aa_label *new)
389 if (label != new)
396 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp)
398 AA_BUG(!label);
401 if (aa_alloc_secid(label, gfp) < 0)
404 label->size = size; /* doesn't include null */
405 label->vec[size] = NULL; /* null terminate */
406 kref_init(&label->count);
407 RB_CLEAR_NODE(&label->node);
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
456 * label_cmp - label comparison for set ordering
457 * @a: label to compare (NOT NULL)
458 * @b: label to compare (NOT NULL)
475 int aa_label_next_confined(struct aa_label *label, int i)
477 AA_BUG(!label);
480 for (; i < label->size; i++) {
481 if (!profile_unconfined(label->vec[i]))
490 * @I: label iterator
491 * @set: label to test against
492 * @sub: label to if is subset of @set
529 * @set: label to test against
530 * @sub: label to test if is subset of @set
550 * @set: label to test against
551 * @sub: label to test if is subset of @set
584 * __label_remove - remove @label from the label set
585 * @l: label to remove
586 * @new: label to redirect to
588 * Requires: labels_set(@label)->lock write_lock
589 * Returns: true if the label was in the tree and removed
591 static bool __label_remove(struct aa_label *label, struct aa_label *new)
593 struct aa_labelset *ls = labels_set(label);
596 AA_BUG(!label);
600 __aa_proxy_redirect(label, new);
602 if (!label_is_stale(label))
603 __label_make_stale(label);
605 if (label->flags & FLAG_IN_TREE) {
606 rb_erase(&label->node, &ls->root);
607 label->flags &= ~FLAG_IN_TREE;
615 * __label_replace - replace @old with @new in label set
616 * @old: label to remove from label set
617 * @new: label to replace @old with
623 * Note: current implementation requires label set be order in such a way
625 * using pointer comparison of the label address would not work)
651 * __label_insert - attempt to insert @l into a label set
653 * @label: new label to insert (NOT NULL)
660 * else ref counted equivalent label that is already in the set,
664 struct aa_label *label, bool replace)
669 AA_BUG(!label);
670 AA_BUG(labels_set(label) != ls);
672 AA_BUG(label->flags & FLAG_IN_TREE);
678 int result = label_cmp(label, this);
683 * so replace in place, however the label has
691 __proxy_share(this, label);
692 AA_BUG(!__label_replace(this, label));
693 return aa_get_label(label);
701 rb_link_node(&label->node, parent, new);
702 rb_insert_color(&label->node, &ls->root);
703 label->flags |= FLAG_IN_TREE;
705 return aa_get_label(label);
709 * __vec_find - find label that matches @vec in label set
710 * @vec: vec of profiles to find matching label for (NOT NULL)
716 * Returns: ref counted @label if matching label is in tree
717 * ref counted label that is equiv to @l in tree
745 * __label_find - find label @label in label set
746 * @label: label to find (NOT NULL)
748 * Requires: labels_set(@label)->lock held
751 * Returns: ref counted @label if @label is in tree OR
752 * ref counted label that is equiv to @label in tree
753 * else NULL if @label or equiv is not in tree
755 static struct aa_label *__label_find(struct aa_label *label)
757 AA_BUG(!label);
759 return __vec_find(label->vec, label->size);
764 * aa_label_remove - remove a label from the labelset
765 * @label: label to remove
767 * Returns: true if @label was removed from the tree
768 * else @label was not in tree so it could not be removed
770 bool aa_label_remove(struct aa_label *label)
772 struct aa_labelset *ls = labels_set(label);
779 res = __label_remove(label, ns_unconfined(labels_ns(label)));
786 * aa_label_replace - replace a label @old with a new version @new
787 * @old: label to replace
788 * @new: label replacing @old
827 * vec_find - find label @l in label set
828 * @vec: array of profiles to find equiv label for (NOT NULL)
831 * Returns: refcounted label if @vec equiv is in tree
837 struct aa_label *label;
846 label = __vec_find(vec, n);
849 return label;
856 struct aa_label *label = NULL;
865 return aa_get_label(&vec[0]->label);
867 ls = labels_set(&vec[len - 1]->label);
870 * check if label exists before taking locks
880 label = __label_insert(ls, new, false);
882 label_free_or_put_new(label, new);
884 return label;
890 struct aa_label *label = vec_find(vec, len);
892 if (label)
893 return label;
899 * aa_label_find - find label @label in label set
900 * @label: label to find (NOT NULL)
904 * Returns: refcounted @label if @label is in tree
905 * refcounted label that is equiv to @label in tree
906 * else NULL if @label or equiv is not in tree
908 struct aa_label *aa_label_find(struct aa_label *label)
910 AA_BUG(!label);
912 return vec_find(label->vec, label->size);
917 * aa_label_insert - insert label @label into @ls or return existing label
918 * @ls - labelset to insert @label into
919 * @label - label to insert
921 * Requires: caller to hold a valid ref on @label
923 * Returns: ref counted @label if successful in inserting @label
924 * else ref counted equivalent label that is already in the set
926 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *label)
932 AA_BUG(!label);
934 /* check if label exists before taking lock */
935 if (!label_is_stale(label)) {
937 l = __label_find(label);
944 l = __label_insert(ls, label, false);
953 * @I: label iterator
954 * @a: label to merge
955 * @b: label to merge
993 * @a: label to merge then compare (NOT NULL)
994 * @b: label to merge then compare (NOT NULL)
995 * @z: label to compare merge against (NOT NULL)
1031 * label_merge_insert - create a new label by merging @a and @b
1032 * @new: preallocated label to merge into (NOT NULL)
1033 * @a: label to merge with @b (NOT NULL)
1034 * @b: label to merge with @a (NOT NULL)
1038 * Returns: ref counted label either @new if merge is unique
1051 struct aa_label *label;
1070 AA_BUG(!new->vec[k]->label.proxy);
1071 AA_BUG(!new->vec[k]->label.proxy->label);
1072 if (next->label.proxy != new->vec[k]->label.proxy)
1088 label = aa_get_label(&new->vec[0]->label);
1089 return label;
1105 label = __label_insert(labels_set(new), new, false);
1108 return label;
1112 * labelset_of_merge - find which labelset a merged label should be inserted
1113 * @a: label to merge and insert
1114 * @b: label to merge and insert
1116 * Returns: labelset that the merged label should be inserted into
1130 * __label_find_merge - find label that is equiv to merge of @a and @b
1132 * @a: label to merge with @b (NOT NULL)
1133 * @b: label to merge with @a (NOT NULL)
1137 * Returns: ref counted label that is equiv to merge of @a and @b
1172 * aa_label_find_merge - find label that is equiv to merge of @a and @b
1173 * @a: label to merge with @b (NOT NULL)
1174 * @b: label to merge with @a (NOT NULL)
1178 * Returns: ref counted label that is equiv to merge of @a and @b
1184 struct aa_label *label, *ar = NULL, *br = NULL;
1196 label = __label_find_merge(ls, a, b);
1201 return label;
1205 * aa_label_merge - attempt to insert new merged label of @a and @b
1206 * @ls: set of labels to insert label into (NOT NULL)
1207 * @a: label to merge with @b (NOT NULL)
1208 * @b: label to merge with @a (NOT NULL)
1214 * Returns: ref counted new label if successful in inserting merge of a & b
1215 * else ref counted equivalent label that is already in the set.
1216 * else NULL if could not create label (-ENOMEM)
1221 struct aa_label *label = NULL;
1230 * check if label exists before taking locks
1232 label = aa_label_find_merge(a, b);
1235 if (!label) {
1248 label = label_merge_insert(new, a, b);
1249 label_free_or_put_new(label, new);
1255 return label;
1259 struct aa_label *label)
1261 return aa_ns_visible(profile->ns, labels_ns(label), true);
1287 * label_compound_match - find perms for full compound label
1289 * @label: label to check access permissions for
1297 * For the label A//&B//&C this does the perm match for A//&B//&C
1302 struct aa_label *label,
1310 label_for_each(i, label, tp) {
1324 label_for_each_cont(i, label, tp) {
1345 * 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
1360 struct aa_label *label, unsigned int start,
1370 label_for_each(i, label, tp) {
1386 label_for_each_cont(i, label, tp) {
1408 * aa_label_match - do a multi-component label match
1410 * @label: label to match (NOT NULL)
1418 int aa_label_match(struct aa_profile *profile, struct aa_label *label,
1422 int error = label_compound_match(profile, label, state, subns, request,
1428 return label_components_match(profile, label, state, subns, request,
1434 * aa_update_label_name - update a label to have a stored name
1436 * @label: label to update (NOT NULL)
1439 * Requires: labels_set(label) not locked in caller
1441 * note: only updates the label name if it does not have a name already
1444 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp)
1452 AA_BUG(!label);
1454 if (label->hname || labels_ns(label) != ns)
1457 if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) < 0)
1460 ls = labels_set(label);
1462 if (!label->hname && label->flags & FLAG_IN_TREE) {
1463 label->hname = name;
1473 * cached label name is present and visible
1474 * @label->hname only exists if label is namespace hierachical
1476 static inline bool use_label_hname(struct aa_ns *ns, struct aa_label *label,
1479 if (label->hname && (!ns || labels_ns(label) == ns) &&
1553 static const char *label_modename(struct aa_ns *ns, struct aa_label *label,
1560 label_for_each(i, label, profile) {
1585 /* if any visible label is not unconfined the display_mode returns true */
1586 static inline bool display_mode(struct aa_ns *ns, struct aa_label *label,
1593 label_for_each(i, label, profile) {
1607 * aa_label_snxprint - print a label name to a string buffer
1611 * @label: label to view (NOT NULL)
1620 * label is not visible it will just be excluded. And if none
1621 * of the label is visible "---" will be used.
1624 struct aa_label *label, int flags)
1633 AA_BUG(!label);
1640 ns = labels_ns(label);
1643 label_for_each(i, label, profile) {
1666 if (display_mode(ns, label, flags)) {
1668 label_modename(ns, label, flags));
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)
1681 * @flags: flags controlling what label info is printed
1687 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
1693 AA_BUG(!label);
1695 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1702 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1706 * aa_label_acntsxprint - allocate a __counted string buffer and print label
1709 * @label: label to view (NOT NULL)
1710 * @flags: flags controlling what label info is printed
1717 struct aa_label *label, int flags, gfp_t gfp)
1722 AA_BUG(!label);
1724 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1731 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1736 struct aa_label *label, int flags, gfp_t gfp)
1743 AA_BUG(!label);
1745 if (!use_label_hname(ns, label, flags) ||
1746 display_mode(ns, label, flags)) {
1747 len = aa_label_asxprint(&name, ns, label, flags, gfp);
1749 AA_DEBUG("label print error");
1754 str = (char *) label->hname;
1766 struct aa_label *label, int flags, gfp_t gfp)
1769 AA_BUG(!label);
1771 if (!use_label_hname(ns, label, flags)) {
1775 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1777 AA_DEBUG("label print error");
1782 } else if (display_mode(ns, label, flags))
1783 seq_printf(f, "%s (%s)", label->hname,
1784 label_modename(ns, label, flags));
1786 seq_puts(f, label->hname);
1789 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
1792 AA_BUG(!label);
1794 if (!use_label_hname(ns, label, flags)) {
1798 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1800 AA_DEBUG("label print error");
1805 } else if (display_mode(ns, label, flags))
1806 pr_info("%s (%s)", label->hname,
1807 label_modename(ns, label, flags));
1809 pr_info("%s", label->hname);
1812 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp)
1816 aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp);
1820 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp)
1824 aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp);
1828 void aa_label_printk(struct aa_label *label, gfp_t gfp)
1832 aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp);
1874 * aa_label_strn_parse - parse, validate and convert a text string to a label
1875 * @base: base label to use for lookups (NOT NULL)
1882 * Returns: the matching refcounted label if present
1890 struct aa_label *label, *currbase = base;
1900 base != &root_ns->unconfined->label))
1929 currbase = &vec[i]->label;
1941 return &vec[0]->label;
1946 label = aa_get_label(&vec[0]->label);
1951 label = aa_vec_find_or_create_label(vec, len, gfp);
1953 label = vec_find(vec, len);
1954 if (!label)
1960 return label;
1963 label = ERR_PTR(-ENOENT);
1975 * aa_labelset_destroy - remove all labels from the label set
1976 * @ls: label set to cleanup (NOT NULL)
2014 struct aa_label *label;
2023 label = rb_entry(node, struct aa_label, node);
2024 if ((label_is_stale(label) ||
2025 vec_is_stale(label->vec, label->size)) &&
2026 __aa_get_label(label))
2030 label = NULL;
2035 return label;
2039 * __label_update - insert updated version of @label into labelset
2040 * @label - the label to update/replace
2042 * Returns: new label that is up to date
2047 * Note: worst case is the stale @label does not get updated and has
2050 static struct aa_label *__label_update(struct aa_label *label)
2057 AA_BUG(!label);
2058 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock));
2060 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL);
2066 * and label updates, label merging and removal can be occurring
2068 ls = labels_set(label);
2070 for (i = 0; i < label->size; i++) {
2071 AA_BUG(!label->vec[i]);
2072 new->vec[i] = aa_get_newest_profile(label->vec[i]);
2074 AA_BUG(!new->vec[i]->label.proxy);
2075 AA_BUG(!new->vec[i]->label.proxy->label);
2076 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy)
2080 /* updated stale label by being removed/renamed from labelset */
2086 tmp = aa_get_label(&new->vec[0]->label);
2087 AA_BUG(tmp == label);
2090 if (labels_set(label) != labels_set(new)) {
2097 AA_BUG(labels_ns(label) != labels_ns(new));
2099 tmp = __label_insert(labels_set(label), new, true);
2101 /* ensure label is removed, and redirected correctly */
2102 __label_remove(label, tmp);
2116 * Any label that has a stale component is marked stale and replaced and
2124 struct aa_label *label;
2130 label = labelset_next_stale(&ns->labels);
2131 if (label) {
2132 struct aa_label *l = __label_update(label);
2135 aa_put_label(label);
2137 } while (label);