netfilter: nf_conntrack_pptp: prevent buffer overflows in debug code
[linux/fpc-iii.git] / security / apparmor / label.c
blob2469549842d242a0f64ec7dc1088944bcb9924f8
1 /*
2 * AppArmor security module
4 * This file contains AppArmor label definitions
6 * Copyright 2017 Canonical Ltd.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
11 * License.
14 #include <linux/audit.h>
15 #include <linux/seq_file.h>
16 #include <linux/sort.h>
18 #include "include/apparmor.h"
19 #include "include/cred.h"
20 #include "include/label.h"
21 #include "include/policy.h"
22 #include "include/secid.h"
26 * the aa_label represents the set of profiles confining an object
28 * Labels maintain a reference count to the set of pointers they reference
29 * Labels are ref counted by
30 * tasks and object via the security field/security context off the field
31 * code - will take a ref count on a label if it needs the label
32 * beyond what is possible with an rcu_read_lock.
33 * profiles - each profile is a label
34 * secids - a pinned secid will keep a refcount of the label it is
35 * referencing
36 * objects - inode, files, sockets, ...
38 * Labels are not ref counted by the label set, so they maybe removed and
39 * freed when no longer in use.
43 #define PROXY_POISON 97
44 #define LABEL_POISON 100
46 static void free_proxy(struct aa_proxy *proxy)
48 if (proxy) {
49 /* p->label will not updated any more as p is dead */
50 aa_put_label(rcu_dereference_protected(proxy->label, true));
51 memset(proxy, 0, sizeof(*proxy));
52 RCU_INIT_POINTER(proxy->label, (struct aa_label *)PROXY_POISON);
53 kfree(proxy);
57 void aa_proxy_kref(struct kref *kref)
59 struct aa_proxy *proxy = container_of(kref, struct aa_proxy, count);
61 free_proxy(proxy);
64 struct aa_proxy *aa_alloc_proxy(struct aa_label *label, gfp_t gfp)
66 struct aa_proxy *new;
68 new = kzalloc(sizeof(struct aa_proxy), gfp);
69 if (new) {
70 kref_init(&new->count);
71 rcu_assign_pointer(new->label, aa_get_label(label));
73 return new;
76 /* requires profile list write lock held */
77 void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new)
79 struct aa_label *tmp;
81 AA_BUG(!orig);
82 AA_BUG(!new);
83 lockdep_assert_held_exclusive(&labels_set(orig)->lock);
85 tmp = rcu_dereference_protected(orig->proxy->label,
86 &labels_ns(orig)->lock);
87 rcu_assign_pointer(orig->proxy->label, aa_get_label(new));
88 orig->flags |= FLAG_STALE;
89 aa_put_label(tmp);
92 static void __proxy_share(struct aa_label *old, struct aa_label *new)
94 struct aa_proxy *proxy = new->proxy;
96 new->proxy = aa_get_proxy(old->proxy);
97 __aa_proxy_redirect(old, new);
98 aa_put_proxy(proxy);
103 * ns_cmp - compare ns for label set ordering
104 * @a: ns to compare (NOT NULL)
105 * @b: ns to compare (NOT NULL)
107 * Returns: <0 if a < b
108 * ==0 if a == b
109 * >0 if a > b
111 static int ns_cmp(struct aa_ns *a, struct aa_ns *b)
113 int res;
115 AA_BUG(!a);
116 AA_BUG(!b);
117 AA_BUG(!a->base.hname);
118 AA_BUG(!b->base.hname);
120 if (a == b)
121 return 0;
123 res = a->level - b->level;
124 if (res)
125 return res;
127 return strcmp(a->base.hname, b->base.hname);
131 * profile_cmp - profile comparison for set ordering
132 * @a: profile to compare (NOT NULL)
133 * @b: profile to compare (NOT NULL)
135 * Returns: <0 if a < b
136 * ==0 if a == b
137 * >0 if a > b
139 static int profile_cmp(struct aa_profile *a, struct aa_profile *b)
141 int res;
143 AA_BUG(!a);
144 AA_BUG(!b);
145 AA_BUG(!a->ns);
146 AA_BUG(!b->ns);
147 AA_BUG(!a->base.hname);
148 AA_BUG(!b->base.hname);
150 if (a == b || a->base.hname == b->base.hname)
151 return 0;
152 res = ns_cmp(a->ns, b->ns);
153 if (res)
154 return res;
156 return strcmp(a->base.hname, b->base.hname);
160 * vec_cmp - label comparison for set ordering
161 * @a: label to compare (NOT NULL)
162 * @vec: vector of profiles to compare (NOT NULL)
163 * @n: length of @vec
165 * Returns: <0 if a < vec
166 * ==0 if a == vec
167 * >0 if a > vec
169 static int vec_cmp(struct aa_profile **a, int an, struct aa_profile **b, int bn)
171 int i;
173 AA_BUG(!a);
174 AA_BUG(!*a);
175 AA_BUG(!b);
176 AA_BUG(!*b);
177 AA_BUG(an <= 0);
178 AA_BUG(bn <= 0);
180 for (i = 0; i < an && i < bn; i++) {
181 int res = profile_cmp(a[i], b[i]);
183 if (res != 0)
184 return res;
187 return an - bn;
190 static bool vec_is_stale(struct aa_profile **vec, int n)
192 int i;
194 AA_BUG(!vec);
196 for (i = 0; i < n; i++) {
197 if (profile_is_stale(vec[i]))
198 return true;
201 return false;
204 static bool vec_unconfined(struct aa_profile **vec, int n)
206 int i;
208 AA_BUG(!vec);
210 for (i = 0; i < n; i++) {
211 if (!profile_unconfined(vec[i]))
212 return false;
215 return true;
218 static int sort_cmp(const void *a, const void *b)
220 return profile_cmp(*(struct aa_profile **)a, *(struct aa_profile **)b);
224 * assumes vec is sorted
225 * Assumes @vec has null terminator at vec[n], and will null terminate
226 * vec[n - dups]
228 static inline int unique(struct aa_profile **vec, int n)
230 int i, pos, dups = 0;
232 AA_BUG(n < 1);
233 AA_BUG(!vec);
235 pos = 0;
236 for (i = 1; i < n; i++) {
237 int res = profile_cmp(vec[pos], vec[i]);
239 AA_BUG(res > 0, "vec not sorted");
240 if (res == 0) {
241 /* drop duplicate */
242 aa_put_profile(vec[i]);
243 dups++;
244 continue;
246 pos++;
247 if (dups)
248 vec[pos] = vec[i];
251 AA_BUG(dups < 0);
253 return dups;
257 * aa_vec_unique - canonical sort and unique a list of profiles
258 * @n: number of refcounted profiles in the list (@n > 0)
259 * @vec: list of profiles to sort and merge
261 * Returns: the number of duplicates eliminated == references put
263 * If @flags & VEC_FLAG_TERMINATE @vec has null terminator at vec[n], and will
264 * null terminate vec[n - dups]
266 int aa_vec_unique(struct aa_profile **vec, int n, int flags)
268 int i, dups = 0;
270 AA_BUG(n < 1);
271 AA_BUG(!vec);
273 /* vecs are usually small and inorder, have a fallback for larger */
274 if (n > 8) {
275 sort(vec, n, sizeof(struct aa_profile *), sort_cmp, NULL);
276 dups = unique(vec, n);
277 goto out;
280 /* insertion sort + unique in one */
281 for (i = 1; i < n; i++) {
282 struct aa_profile *tmp = vec[i];
283 int pos, j;
285 for (pos = i - 1 - dups; pos >= 0; pos--) {
286 int res = profile_cmp(vec[pos], tmp);
288 if (res == 0) {
289 /* drop duplicate entry */
290 aa_put_profile(tmp);
291 dups++;
292 goto continue_outer;
293 } else if (res < 0)
294 break;
296 /* pos is at entry < tmp, or index -1. Set to insert pos */
297 pos++;
299 for (j = i - dups; j > pos; j--)
300 vec[j] = vec[j - 1];
301 vec[pos] = tmp;
302 continue_outer:
306 AA_BUG(dups < 0);
308 out:
309 if (flags & VEC_FLAG_TERMINATE)
310 vec[n - dups] = NULL;
312 return dups;
316 static void label_destroy(struct aa_label *label)
318 struct aa_label *tmp;
320 AA_BUG(!label);
322 if (!label_isprofile(label)) {
323 struct aa_profile *profile;
324 struct label_it i;
326 aa_put_str(label->hname);
328 label_for_each(i, label, profile) {
329 aa_put_profile(profile);
330 label->vec[i.i] = (struct aa_profile *)
331 (LABEL_POISON + (long) i.i);
335 if (rcu_dereference_protected(label->proxy->label, true) == label)
336 rcu_assign_pointer(label->proxy->label, NULL);
338 aa_free_secid(label->secid);
340 tmp = rcu_dereference_protected(label->proxy->label, true);
341 if (tmp == label)
342 rcu_assign_pointer(label->proxy->label, NULL);
344 aa_put_proxy(label->proxy);
345 label->proxy = (struct aa_proxy *) PROXY_POISON + 1;
348 void aa_label_free(struct aa_label *label)
350 if (!label)
351 return;
353 label_destroy(label);
354 kfree(label);
357 static void label_free_switch(struct aa_label *label)
359 if (label->flags & FLAG_NS_COUNT)
360 aa_free_ns(labels_ns(label));
361 else if (label_isprofile(label))
362 aa_free_profile(labels_profile(label));
363 else
364 aa_label_free(label);
367 static void label_free_rcu(struct rcu_head *head)
369 struct aa_label *label = container_of(head, struct aa_label, rcu);
371 if (label->flags & FLAG_IN_TREE)
372 (void) aa_label_remove(label);
373 label_free_switch(label);
376 void aa_label_kref(struct kref *kref)
378 struct aa_label *label = container_of(kref, struct aa_label, count);
379 struct aa_ns *ns = labels_ns(label);
381 if (!ns) {
382 /* never live, no rcu callback needed, just using the fn */
383 label_free_switch(label);
384 return;
386 /* TODO: update labels_profile macro so it works here */
387 AA_BUG(label_isprofile(label) &&
388 on_list_rcu(&label->vec[0]->base.profiles));
389 AA_BUG(label_isprofile(label) &&
390 on_list_rcu(&label->vec[0]->base.list));
392 /* TODO: if compound label and not stale add to reclaim cache */
393 call_rcu(&label->rcu, label_free_rcu);
396 static void label_free_or_put_new(struct aa_label *label, struct aa_label *new)
398 if (label != new)
399 /* need to free directly to break circular ref with proxy */
400 aa_label_free(new);
401 else
402 aa_put_label(new);
405 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp)
407 AA_BUG(!label);
408 AA_BUG(size < 1);
410 if (aa_alloc_secid(label, gfp) < 0)
411 return false;
413 label->size = size; /* doesn't include null */
414 label->vec[size] = NULL; /* null terminate */
415 kref_init(&label->count);
416 RB_CLEAR_NODE(&label->node);
418 return true;
422 * aa_label_alloc - allocate a label with a profile vector of @size length
423 * @size: size of profile vector in the label
424 * @proxy: proxy to use OR null if to allocate a new one
425 * @gfp: memory allocation type
427 * Returns: new label
428 * else NULL if failed
430 struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp)
432 struct aa_label *new;
434 AA_BUG(size < 1);
436 /* + 1 for null terminator entry on vec */
437 new = kzalloc(sizeof(*new) + sizeof(struct aa_profile *) * (size + 1),
438 gfp);
439 AA_DEBUG("%s (%p)\n", __func__, new);
440 if (!new)
441 goto fail;
443 if (!aa_label_init(new, size, gfp))
444 goto fail;
446 if (!proxy) {
447 proxy = aa_alloc_proxy(new, gfp);
448 if (!proxy)
449 goto fail;
450 } else
451 aa_get_proxy(proxy);
452 /* just set new's proxy, don't redirect proxy here if it was passed in*/
453 new->proxy = proxy;
455 return new;
457 fail:
458 kfree(new);
460 return NULL;
465 * label_cmp - label comparison for set ordering
466 * @a: label to compare (NOT NULL)
467 * @b: label to compare (NOT NULL)
469 * Returns: <0 if a < b
470 * ==0 if a == b
471 * >0 if a > b
473 static int label_cmp(struct aa_label *a, struct aa_label *b)
475 AA_BUG(!b);
477 if (a == b)
478 return 0;
480 return vec_cmp(a->vec, a->size, b->vec, b->size);
483 /* helper fn for label_for_each_confined */
484 int aa_label_next_confined(struct aa_label *label, int i)
486 AA_BUG(!label);
487 AA_BUG(i < 0);
489 for (; i < label->size; i++) {
490 if (!profile_unconfined(label->vec[i]))
491 return i;
494 return i;
498 * aa_label_next_not_in_set - return the next profile of @sub not in @set
499 * @I: label iterator
500 * @set: label to test against
501 * @sub: label to if is subset of @set
503 * Returns: profile in @sub that is not in @set, with iterator set pos after
504 * else NULL if @sub is a subset of @set
506 struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
507 struct aa_label *set,
508 struct aa_label *sub)
510 AA_BUG(!set);
511 AA_BUG(!I);
512 AA_BUG(I->i < 0);
513 AA_BUG(I->i > set->size);
514 AA_BUG(!sub);
515 AA_BUG(I->j < 0);
516 AA_BUG(I->j > sub->size);
518 while (I->j < sub->size && I->i < set->size) {
519 int res = profile_cmp(sub->vec[I->j], set->vec[I->i]);
521 if (res == 0) {
522 (I->j)++;
523 (I->i)++;
524 } else if (res > 0)
525 (I->i)++;
526 else
527 return sub->vec[(I->j)++];
530 if (I->j < sub->size)
531 return sub->vec[(I->j)++];
533 return NULL;
537 * aa_label_is_subset - test if @sub is a subset of @set
538 * @set: label to test against
539 * @sub: label to test if is subset of @set
541 * Returns: true if @sub is subset of @set
542 * else false
544 bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub)
546 struct label_it i = { };
548 AA_BUG(!set);
549 AA_BUG(!sub);
551 if (sub == set)
552 return true;
554 return __aa_label_next_not_in_set(&i, set, sub) == NULL;
560 * __label_remove - remove @label from the label set
561 * @l: label to remove
562 * @new: label to redirect to
564 * Requires: labels_set(@label)->lock write_lock
565 * Returns: true if the label was in the tree and removed
567 static bool __label_remove(struct aa_label *label, struct aa_label *new)
569 struct aa_labelset *ls = labels_set(label);
571 AA_BUG(!ls);
572 AA_BUG(!label);
573 lockdep_assert_held_exclusive(&ls->lock);
575 if (new)
576 __aa_proxy_redirect(label, new);
578 if (!label_is_stale(label))
579 __label_make_stale(label);
581 if (label->flags & FLAG_IN_TREE) {
582 rb_erase(&label->node, &ls->root);
583 label->flags &= ~FLAG_IN_TREE;
584 return true;
587 return false;
591 * __label_replace - replace @old with @new in label set
592 * @old: label to remove from label set
593 * @new: label to replace @old with
595 * Requires: labels_set(@old)->lock write_lock
596 * valid ref count be held on @new
597 * Returns: true if @old was in set and replaced by @new
599 * Note: current implementation requires label set be order in such a way
600 * that @new directly replaces @old position in the set (ie.
601 * using pointer comparison of the label address would not work)
603 static bool __label_replace(struct aa_label *old, struct aa_label *new)
605 struct aa_labelset *ls = labels_set(old);
607 AA_BUG(!ls);
608 AA_BUG(!old);
609 AA_BUG(!new);
610 lockdep_assert_held_exclusive(&ls->lock);
611 AA_BUG(new->flags & FLAG_IN_TREE);
613 if (!label_is_stale(old))
614 __label_make_stale(old);
616 if (old->flags & FLAG_IN_TREE) {
617 rb_replace_node(&old->node, &new->node, &ls->root);
618 old->flags &= ~FLAG_IN_TREE;
619 new->flags |= FLAG_IN_TREE;
620 return true;
623 return false;
627 * __label_insert - attempt to insert @l into a label set
628 * @ls: set of labels to insert @l into (NOT NULL)
629 * @label: new label to insert (NOT NULL)
630 * @replace: whether insertion should replace existing entry that is not stale
632 * Requires: @ls->lock
633 * caller to hold a valid ref on l
634 * if @replace is true l has a preallocated proxy associated
635 * Returns: @l if successful in inserting @l - with additional refcount
636 * else ref counted equivalent label that is already in the set,
637 * the else condition only happens if @replace is false
639 static struct aa_label *__label_insert(struct aa_labelset *ls,
640 struct aa_label *label, bool replace)
642 struct rb_node **new, *parent = NULL;
644 AA_BUG(!ls);
645 AA_BUG(!label);
646 AA_BUG(labels_set(label) != ls);
647 lockdep_assert_held_exclusive(&ls->lock);
648 AA_BUG(label->flags & FLAG_IN_TREE);
650 /* Figure out where to put new node */
651 new = &ls->root.rb_node;
652 while (*new) {
653 struct aa_label *this = rb_entry(*new, struct aa_label, node);
654 int result = label_cmp(label, this);
656 parent = *new;
657 if (result == 0) {
658 /* !__aa_get_label means queued for destruction,
659 * so replace in place, however the label has
660 * died before the replacement so do not share
661 * the proxy
663 if (!replace && !label_is_stale(this)) {
664 if (__aa_get_label(this))
665 return this;
666 } else
667 __proxy_share(this, label);
668 AA_BUG(!__label_replace(this, label));
669 return aa_get_label(label);
670 } else if (result < 0)
671 new = &((*new)->rb_left);
672 else /* (result > 0) */
673 new = &((*new)->rb_right);
676 /* Add new node and rebalance tree. */
677 rb_link_node(&label->node, parent, new);
678 rb_insert_color(&label->node, &ls->root);
679 label->flags |= FLAG_IN_TREE;
681 return aa_get_label(label);
685 * __vec_find - find label that matches @vec in label set
686 * @vec: vec of profiles to find matching label for (NOT NULL)
687 * @n: length of @vec
689 * Requires: @vec_labelset(vec) lock held
690 * caller to hold a valid ref on l
692 * Returns: ref counted @label if matching label is in tree
693 * ref counted label that is equiv to @l in tree
694 * else NULL if @vec equiv is not in tree
696 static struct aa_label *__vec_find(struct aa_profile **vec, int n)
698 struct rb_node *node;
700 AA_BUG(!vec);
701 AA_BUG(!*vec);
702 AA_BUG(n <= 0);
704 node = vec_labelset(vec, n)->root.rb_node;
705 while (node) {
706 struct aa_label *this = rb_entry(node, struct aa_label, node);
707 int result = vec_cmp(this->vec, this->size, vec, n);
709 if (result > 0)
710 node = node->rb_left;
711 else if (result < 0)
712 node = node->rb_right;
713 else
714 return __aa_get_label(this);
717 return NULL;
721 * __label_find - find label @label in label set
722 * @label: label to find (NOT NULL)
724 * Requires: labels_set(@label)->lock held
725 * caller to hold a valid ref on l
727 * Returns: ref counted @label if @label is in tree OR
728 * ref counted label that is equiv to @label in tree
729 * else NULL if @label or equiv is not in tree
731 static struct aa_label *__label_find(struct aa_label *label)
733 AA_BUG(!label);
735 return __vec_find(label->vec, label->size);
740 * aa_label_remove - remove a label from the labelset
741 * @label: label to remove
743 * Returns: true if @label was removed from the tree
744 * else @label was not in tree so it could not be removed
746 bool aa_label_remove(struct aa_label *label)
748 struct aa_labelset *ls = labels_set(label);
749 unsigned long flags;
750 bool res;
752 AA_BUG(!ls);
754 write_lock_irqsave(&ls->lock, flags);
755 res = __label_remove(label, ns_unconfined(labels_ns(label)));
756 write_unlock_irqrestore(&ls->lock, flags);
758 return res;
762 * aa_label_replace - replace a label @old with a new version @new
763 * @old: label to replace
764 * @new: label replacing @old
766 * Returns: true if @old was in tree and replaced
767 * else @old was not in tree, and @new was not inserted
769 bool aa_label_replace(struct aa_label *old, struct aa_label *new)
771 unsigned long flags;
772 bool res;
774 if (name_is_shared(old, new) && labels_ns(old) == labels_ns(new)) {
775 write_lock_irqsave(&labels_set(old)->lock, flags);
776 if (old->proxy != new->proxy)
777 __proxy_share(old, new);
778 else
779 __aa_proxy_redirect(old, new);
780 res = __label_replace(old, new);
781 write_unlock_irqrestore(&labels_set(old)->lock, flags);
782 } else {
783 struct aa_label *l;
784 struct aa_labelset *ls = labels_set(old);
786 write_lock_irqsave(&ls->lock, flags);
787 res = __label_remove(old, new);
788 if (labels_ns(old) != labels_ns(new)) {
789 write_unlock_irqrestore(&ls->lock, flags);
790 ls = labels_set(new);
791 write_lock_irqsave(&ls->lock, flags);
793 l = __label_insert(ls, new, true);
794 res = (l == new);
795 write_unlock_irqrestore(&ls->lock, flags);
796 aa_put_label(l);
799 return res;
803 * vec_find - find label @l in label set
804 * @vec: array of profiles to find equiv label for (NOT NULL)
805 * @n: length of @vec
807 * Returns: refcounted label if @vec equiv is in tree
808 * else NULL if @vec equiv is not in tree
810 static struct aa_label *vec_find(struct aa_profile **vec, int n)
812 struct aa_labelset *ls;
813 struct aa_label *label;
814 unsigned long flags;
816 AA_BUG(!vec);
817 AA_BUG(!*vec);
818 AA_BUG(n <= 0);
820 ls = vec_labelset(vec, n);
821 read_lock_irqsave(&ls->lock, flags);
822 label = __vec_find(vec, n);
823 read_unlock_irqrestore(&ls->lock, flags);
825 return label;
828 /* requires sort and merge done first */
829 static struct aa_label *vec_create_and_insert_label(struct aa_profile **vec,
830 int len, gfp_t gfp)
832 struct aa_label *label = NULL;
833 struct aa_labelset *ls;
834 unsigned long flags;
835 struct aa_label *new;
836 int i;
838 AA_BUG(!vec);
840 if (len == 1)
841 return aa_get_label(&vec[0]->label);
843 ls = labels_set(&vec[len - 1]->label);
845 /* TODO: enable when read side is lockless
846 * check if label exists before taking locks
848 new = aa_label_alloc(len, NULL, gfp);
849 if (!new)
850 return NULL;
852 for (i = 0; i < len; i++)
853 new->vec[i] = aa_get_profile(vec[i]);
855 write_lock_irqsave(&ls->lock, flags);
856 label = __label_insert(ls, new, false);
857 write_unlock_irqrestore(&ls->lock, flags);
858 label_free_or_put_new(label, new);
860 return label;
863 struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
864 gfp_t gfp)
866 struct aa_label *label = vec_find(vec, len);
868 if (label)
869 return label;
871 return vec_create_and_insert_label(vec, len, gfp);
875 * aa_label_find - find label @label in label set
876 * @label: label to find (NOT NULL)
878 * Requires: caller to hold a valid ref on l
880 * Returns: refcounted @label if @label is in tree
881 * refcounted label that is equiv to @label in tree
882 * else NULL if @label or equiv is not in tree
884 struct aa_label *aa_label_find(struct aa_label *label)
886 AA_BUG(!label);
888 return vec_find(label->vec, label->size);
893 * aa_label_insert - insert label @label into @ls or return existing label
894 * @ls - labelset to insert @label into
895 * @label - label to insert
897 * Requires: caller to hold a valid ref on @label
899 * Returns: ref counted @label if successful in inserting @label
900 * else ref counted equivalent label that is already in the set
902 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *label)
904 struct aa_label *l;
905 unsigned long flags;
907 AA_BUG(!ls);
908 AA_BUG(!label);
910 /* check if label exists before taking lock */
911 if (!label_is_stale(label)) {
912 read_lock_irqsave(&ls->lock, flags);
913 l = __label_find(label);
914 read_unlock_irqrestore(&ls->lock, flags);
915 if (l)
916 return l;
919 write_lock_irqsave(&ls->lock, flags);
920 l = __label_insert(ls, label, false);
921 write_unlock_irqrestore(&ls->lock, flags);
923 return l;
928 * aa_label_next_in_merge - find the next profile when merging @a and @b
929 * @I: label iterator
930 * @a: label to merge
931 * @b: label to merge
933 * Returns: next profile
934 * else null if no more profiles
936 struct aa_profile *aa_label_next_in_merge(struct label_it *I,
937 struct aa_label *a,
938 struct aa_label *b)
940 AA_BUG(!a);
941 AA_BUG(!b);
942 AA_BUG(!I);
943 AA_BUG(I->i < 0);
944 AA_BUG(I->i > a->size);
945 AA_BUG(I->j < 0);
946 AA_BUG(I->j > b->size);
948 if (I->i < a->size) {
949 if (I->j < b->size) {
950 int res = profile_cmp(a->vec[I->i], b->vec[I->j]);
952 if (res > 0)
953 return b->vec[(I->j)++];
954 if (res == 0)
955 (I->j)++;
958 return a->vec[(I->i)++];
961 if (I->j < b->size)
962 return b->vec[(I->j)++];
964 return NULL;
968 * label_merge_cmp - cmp of @a merging with @b against @z for set ordering
969 * @a: label to merge then compare (NOT NULL)
970 * @b: label to merge then compare (NOT NULL)
971 * @z: label to compare merge against (NOT NULL)
973 * Assumes: using the most recent versions of @a, @b, and @z
975 * Returns: <0 if a < b
976 * ==0 if a == b
977 * >0 if a > b
979 static int label_merge_cmp(struct aa_label *a, struct aa_label *b,
980 struct aa_label *z)
982 struct aa_profile *p = NULL;
983 struct label_it i = { };
984 int k;
986 AA_BUG(!a);
987 AA_BUG(!b);
988 AA_BUG(!z);
990 for (k = 0;
991 k < z->size && (p = aa_label_next_in_merge(&i, a, b));
992 k++) {
993 int res = profile_cmp(p, z->vec[k]);
995 if (res != 0)
996 return res;
999 if (p)
1000 return 1;
1001 else if (k < z->size)
1002 return -1;
1003 return 0;
1007 * label_merge_insert - create a new label by merging @a and @b
1008 * @new: preallocated label to merge into (NOT NULL)
1009 * @a: label to merge with @b (NOT NULL)
1010 * @b: label to merge with @a (NOT NULL)
1012 * Requires: preallocated proxy
1014 * Returns: ref counted label either @new if merge is unique
1015 * @a if @b is a subset of @a
1016 * @b if @a is a subset of @b
1018 * NOTE: will not use @new if the merge results in @new == @a or @b
1020 * Must be used within labelset write lock to avoid racing with
1021 * setting labels stale.
1023 static struct aa_label *label_merge_insert(struct aa_label *new,
1024 struct aa_label *a,
1025 struct aa_label *b)
1027 struct aa_label *label;
1028 struct aa_labelset *ls;
1029 struct aa_profile *next;
1030 struct label_it i;
1031 unsigned long flags;
1032 int k = 0, invcount = 0;
1033 bool stale = false;
1035 AA_BUG(!a);
1036 AA_BUG(a->size < 0);
1037 AA_BUG(!b);
1038 AA_BUG(b->size < 0);
1039 AA_BUG(!new);
1040 AA_BUG(new->size < a->size + b->size);
1042 label_for_each_in_merge(i, a, b, next) {
1043 AA_BUG(!next);
1044 if (profile_is_stale(next)) {
1045 new->vec[k] = aa_get_newest_profile(next);
1046 AA_BUG(!new->vec[k]->label.proxy);
1047 AA_BUG(!new->vec[k]->label.proxy->label);
1048 if (next->label.proxy != new->vec[k]->label.proxy)
1049 invcount++;
1050 k++;
1051 stale = true;
1052 } else
1053 new->vec[k++] = aa_get_profile(next);
1055 /* set to actual size which is <= allocated len */
1056 new->size = k;
1057 new->vec[k] = NULL;
1059 if (invcount) {
1060 new->size -= aa_vec_unique(&new->vec[0], new->size,
1061 VEC_FLAG_TERMINATE);
1062 /* TODO: deal with reference labels */
1063 if (new->size == 1) {
1064 label = aa_get_label(&new->vec[0]->label);
1065 return label;
1067 } else if (!stale) {
1069 * merge could be same as a || b, note: it is not possible
1070 * for new->size == a->size == b->size unless a == b
1072 if (k == a->size)
1073 return aa_get_label(a);
1074 else if (k == b->size)
1075 return aa_get_label(b);
1077 if (vec_unconfined(new->vec, new->size))
1078 new->flags |= FLAG_UNCONFINED;
1079 ls = labels_set(new);
1080 write_lock_irqsave(&ls->lock, flags);
1081 label = __label_insert(labels_set(new), new, false);
1082 write_unlock_irqrestore(&ls->lock, flags);
1084 return label;
1088 * labelset_of_merge - find which labelset a merged label should be inserted
1089 * @a: label to merge and insert
1090 * @b: label to merge and insert
1092 * Returns: labelset that the merged label should be inserted into
1094 static struct aa_labelset *labelset_of_merge(struct aa_label *a,
1095 struct aa_label *b)
1097 struct aa_ns *nsa = labels_ns(a);
1098 struct aa_ns *nsb = labels_ns(b);
1100 if (ns_cmp(nsa, nsb) <= 0)
1101 return &nsa->labels;
1102 return &nsb->labels;
1106 * __label_find_merge - find label that is equiv to merge of @a and @b
1107 * @ls: set of labels to search (NOT NULL)
1108 * @a: label to merge with @b (NOT NULL)
1109 * @b: label to merge with @a (NOT NULL)
1111 * Requires: ls->lock read_lock held
1113 * Returns: ref counted label that is equiv to merge of @a and @b
1114 * else NULL if merge of @a and @b is not in set
1116 static struct aa_label *__label_find_merge(struct aa_labelset *ls,
1117 struct aa_label *a,
1118 struct aa_label *b)
1120 struct rb_node *node;
1122 AA_BUG(!ls);
1123 AA_BUG(!a);
1124 AA_BUG(!b);
1126 if (a == b)
1127 return __label_find(a);
1129 node = ls->root.rb_node;
1130 while (node) {
1131 struct aa_label *this = container_of(node, struct aa_label,
1132 node);
1133 int result = label_merge_cmp(a, b, this);
1135 if (result < 0)
1136 node = node->rb_left;
1137 else if (result > 0)
1138 node = node->rb_right;
1139 else
1140 return __aa_get_label(this);
1143 return NULL;
1148 * aa_label_find_merge - find label that is equiv to merge of @a and @b
1149 * @a: label to merge with @b (NOT NULL)
1150 * @b: label to merge with @a (NOT NULL)
1152 * Requires: labels be fully constructed with a valid ns
1154 * Returns: ref counted label that is equiv to merge of @a and @b
1155 * else NULL if merge of @a and @b is not in set
1157 struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b)
1159 struct aa_labelset *ls;
1160 struct aa_label *label, *ar = NULL, *br = NULL;
1161 unsigned long flags;
1163 AA_BUG(!a);
1164 AA_BUG(!b);
1166 if (label_is_stale(a))
1167 a = ar = aa_get_newest_label(a);
1168 if (label_is_stale(b))
1169 b = br = aa_get_newest_label(b);
1170 ls = labelset_of_merge(a, b);
1171 read_lock_irqsave(&ls->lock, flags);
1172 label = __label_find_merge(ls, a, b);
1173 read_unlock_irqrestore(&ls->lock, flags);
1174 aa_put_label(ar);
1175 aa_put_label(br);
1177 return label;
1181 * aa_label_merge - attempt to insert new merged label of @a and @b
1182 * @ls: set of labels to insert label into (NOT NULL)
1183 * @a: label to merge with @b (NOT NULL)
1184 * @b: label to merge with @a (NOT NULL)
1185 * @gfp: memory allocation type
1187 * Requires: caller to hold valid refs on @a and @b
1188 * labels be fully constructed with a valid ns
1190 * Returns: ref counted new label if successful in inserting merge of a & b
1191 * else ref counted equivalent label that is already in the set.
1192 * else NULL if could not create label (-ENOMEM)
1194 struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
1195 gfp_t gfp)
1197 struct aa_label *label = NULL;
1199 AA_BUG(!a);
1200 AA_BUG(!b);
1202 if (a == b)
1203 return aa_get_newest_label(a);
1205 /* TODO: enable when read side is lockless
1206 * check if label exists before taking locks
1207 if (!label_is_stale(a) && !label_is_stale(b))
1208 label = aa_label_find_merge(a, b);
1211 if (!label) {
1212 struct aa_label *new;
1214 a = aa_get_newest_label(a);
1215 b = aa_get_newest_label(b);
1217 /* could use label_merge_len(a, b), but requires double
1218 * comparison for small savings
1220 new = aa_label_alloc(a->size + b->size, NULL, gfp);
1221 if (!new)
1222 goto out;
1224 label = label_merge_insert(new, a, b);
1225 label_free_or_put_new(label, new);
1226 out:
1227 aa_put_label(a);
1228 aa_put_label(b);
1231 return label;
1234 static inline bool label_is_visible(struct aa_profile *profile,
1235 struct aa_label *label)
1237 return aa_ns_visible(profile->ns, labels_ns(label), true);
1240 /* match a profile and its associated ns component if needed
1241 * Assumes visibility test has already been done.
1242 * If a subns profile is not to be matched should be prescreened with
1243 * visibility test.
1245 static inline unsigned int match_component(struct aa_profile *profile,
1246 struct aa_profile *tp,
1247 unsigned int state)
1249 const char *ns_name;
1251 if (profile->ns == tp->ns)
1252 return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
1254 /* try matching with namespace name and then profile */
1255 ns_name = aa_ns_name(profile->ns, tp->ns, true);
1256 state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
1257 state = aa_dfa_match(profile->policy.dfa, state, ns_name);
1258 state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
1259 return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
1263 * label_compound_match - find perms for full compound label
1264 * @profile: profile to find perms for
1265 * @label: label to check access permissions for
1266 * @start: state to start match in
1267 * @subns: whether to do permission checks on components in a subns
1268 * @request: permissions to request
1269 * @perms: perms struct to set
1271 * Returns: 0 on success else ERROR
1273 * For the label A//&B//&C this does the perm match for A//&B//&C
1274 * @perms should be preinitialized with allperms OR a previous permission
1275 * check to be stacked.
1277 static int label_compound_match(struct aa_profile *profile,
1278 struct aa_label *label,
1279 unsigned int state, bool subns, u32 request,
1280 struct aa_perms *perms)
1282 struct aa_profile *tp;
1283 struct label_it i;
1285 /* find first subcomponent that is visible */
1286 label_for_each(i, label, tp) {
1287 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1288 continue;
1289 state = match_component(profile, tp, state);
1290 if (!state)
1291 goto fail;
1292 goto next;
1295 /* no component visible */
1296 *perms = allperms;
1297 return 0;
1299 next:
1300 label_for_each_cont(i, label, tp) {
1301 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1302 continue;
1303 state = aa_dfa_match(profile->policy.dfa, state, "//&");
1304 state = match_component(profile, tp, state);
1305 if (!state)
1306 goto fail;
1308 aa_compute_perms(profile->policy.dfa, state, perms);
1309 aa_apply_modes_to_perms(profile, perms);
1310 if ((perms->allow & request) != request)
1311 return -EACCES;
1313 return 0;
1315 fail:
1316 *perms = nullperms;
1317 return state;
1321 * label_components_match - find perms for all subcomponents of a label
1322 * @profile: profile to find perms for
1323 * @label: label to check access permissions for
1324 * @start: state to start match in
1325 * @subns: whether to do permission checks on components in a subns
1326 * @request: permissions to request
1327 * @perms: an initialized perms struct to add accumulation to
1329 * Returns: 0 on success else ERROR
1331 * For the label A//&B//&C this does the perm match for each of A and B and C
1332 * @perms should be preinitialized with allperms OR a previous permission
1333 * check to be stacked.
1335 static int label_components_match(struct aa_profile *profile,
1336 struct aa_label *label, unsigned int start,
1337 bool subns, u32 request,
1338 struct aa_perms *perms)
1340 struct aa_profile *tp;
1341 struct label_it i;
1342 struct aa_perms tmp;
1343 unsigned int state = 0;
1345 /* find first subcomponent to test */
1346 label_for_each(i, label, tp) {
1347 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1348 continue;
1349 state = match_component(profile, tp, start);
1350 if (!state)
1351 goto fail;
1352 goto next;
1355 /* no subcomponents visible - no change in perms */
1356 return 0;
1358 next:
1359 aa_compute_perms(profile->policy.dfa, state, &tmp);
1360 aa_apply_modes_to_perms(profile, &tmp);
1361 aa_perms_accum(perms, &tmp);
1362 label_for_each_cont(i, label, tp) {
1363 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1364 continue;
1365 state = match_component(profile, tp, start);
1366 if (!state)
1367 goto fail;
1368 aa_compute_perms(profile->policy.dfa, state, &tmp);
1369 aa_apply_modes_to_perms(profile, &tmp);
1370 aa_perms_accum(perms, &tmp);
1373 if ((perms->allow & request) != request)
1374 return -EACCES;
1376 return 0;
1378 fail:
1379 *perms = nullperms;
1380 return -EACCES;
1384 * aa_label_match - do a multi-component label match
1385 * @profile: profile to match against (NOT NULL)
1386 * @label: label to match (NOT NULL)
1387 * @state: state to start in
1388 * @subns: whether to match subns components
1389 * @request: permission request
1390 * @perms: Returns computed perms (NOT NULL)
1392 * Returns: the state the match finished in, may be the none matching state
1394 int aa_label_match(struct aa_profile *profile, struct aa_label *label,
1395 unsigned int state, bool subns, u32 request,
1396 struct aa_perms *perms)
1398 int error = label_compound_match(profile, label, state, subns, request,
1399 perms);
1400 if (!error)
1401 return error;
1403 *perms = allperms;
1404 return label_components_match(profile, label, state, subns, request,
1405 perms);
1410 * aa_update_label_name - update a label to have a stored name
1411 * @ns: ns being viewed from (NOT NULL)
1412 * @label: label to update (NOT NULL)
1413 * @gfp: type of memory allocation
1415 * Requires: labels_set(label) not locked in caller
1417 * note: only updates the label name if it does not have a name already
1418 * and if it is in the labelset
1420 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp)
1422 struct aa_labelset *ls;
1423 unsigned long flags;
1424 char __counted *name;
1425 bool res = false;
1427 AA_BUG(!ns);
1428 AA_BUG(!label);
1430 if (label->hname || labels_ns(label) != ns)
1431 return res;
1433 if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) == -1)
1434 return res;
1436 ls = labels_set(label);
1437 write_lock_irqsave(&ls->lock, flags);
1438 if (!label->hname && label->flags & FLAG_IN_TREE) {
1439 label->hname = name;
1440 res = true;
1441 } else
1442 aa_put_str(name);
1443 write_unlock_irqrestore(&ls->lock, flags);
1445 return res;
1449 * cached label name is present and visible
1450 * @label->hname only exists if label is namespace hierachical
1452 static inline bool use_label_hname(struct aa_ns *ns, struct aa_label *label,
1453 int flags)
1455 if (label->hname && (!ns || labels_ns(label) == ns) &&
1456 !(flags & ~FLAG_SHOW_MODE))
1457 return true;
1459 return false;
1462 /* helper macro for snprint routines */
1463 #define update_for_len(total, len, size, str) \
1464 do { \
1465 size_t ulen = len; \
1467 AA_BUG(len < 0); \
1468 total += ulen; \
1469 ulen = min(ulen, size); \
1470 size -= ulen; \
1471 str += ulen; \
1472 } while (0)
1475 * aa_profile_snxprint - print a profile name to a buffer
1476 * @str: buffer to write to. (MAY BE NULL if @size == 0)
1477 * @size: size of buffer
1478 * @view: namespace profile is being viewed from
1479 * @profile: profile to view (NOT NULL)
1480 * @flags: whether to include the mode string
1481 * @prev_ns: last ns printed when used in compound print
1483 * Returns: size of name written or would be written if larger than
1484 * available buffer
1486 * Note: will not print anything if the profile is not visible
1488 static int aa_profile_snxprint(char *str, size_t size, struct aa_ns *view,
1489 struct aa_profile *profile, int flags,
1490 struct aa_ns **prev_ns)
1492 const char *ns_name = NULL;
1494 AA_BUG(!str && size != 0);
1495 AA_BUG(!profile);
1497 if (!view)
1498 view = profiles_ns(profile);
1500 if (view != profile->ns &&
1501 (!prev_ns || (*prev_ns != profile->ns))) {
1502 if (prev_ns)
1503 *prev_ns = profile->ns;
1504 ns_name = aa_ns_name(view, profile->ns,
1505 flags & FLAG_VIEW_SUBNS);
1506 if (ns_name == aa_hidden_ns_name) {
1507 if (flags & FLAG_HIDDEN_UNCONFINED)
1508 return snprintf(str, size, "%s", "unconfined");
1509 return snprintf(str, size, "%s", ns_name);
1513 if ((flags & FLAG_SHOW_MODE) && profile != profile->ns->unconfined) {
1514 const char *modestr = aa_profile_mode_names[profile->mode];
1516 if (ns_name)
1517 return snprintf(str, size, ":%s:%s (%s)", ns_name,
1518 profile->base.hname, modestr);
1519 return snprintf(str, size, "%s (%s)", profile->base.hname,
1520 modestr);
1523 if (ns_name)
1524 return snprintf(str, size, ":%s:%s", ns_name,
1525 profile->base.hname);
1526 return snprintf(str, size, "%s", profile->base.hname);
1529 static const char *label_modename(struct aa_ns *ns, struct aa_label *label,
1530 int flags)
1532 struct aa_profile *profile;
1533 struct label_it i;
1534 int mode = -1, count = 0;
1536 label_for_each(i, label, profile) {
1537 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
1538 if (profile->mode == APPARMOR_UNCONFINED)
1539 /* special case unconfined so stacks with
1540 * unconfined don't report as mixed. ie.
1541 * profile_foo//&:ns1:unconfined (mixed)
1543 continue;
1544 count++;
1545 if (mode == -1)
1546 mode = profile->mode;
1547 else if (mode != profile->mode)
1548 return "mixed";
1552 if (count == 0)
1553 return "-";
1554 if (mode == -1)
1555 /* everything was unconfined */
1556 mode = APPARMOR_UNCONFINED;
1558 return aa_profile_mode_names[mode];
1561 /* if any visible label is not unconfined the display_mode returns true */
1562 static inline bool display_mode(struct aa_ns *ns, struct aa_label *label,
1563 int flags)
1565 if ((flags & FLAG_SHOW_MODE)) {
1566 struct aa_profile *profile;
1567 struct label_it i;
1569 label_for_each(i, label, profile) {
1570 if (aa_ns_visible(ns, profile->ns,
1571 flags & FLAG_VIEW_SUBNS) &&
1572 profile != profile->ns->unconfined)
1573 return true;
1575 /* only ns->unconfined in set of profiles in ns */
1576 return false;
1579 return false;
1583 * aa_label_snxprint - print a label name to a string buffer
1584 * @str: buffer to write to. (MAY BE NULL if @size == 0)
1585 * @size: size of buffer
1586 * @ns: namespace profile is being viewed from
1587 * @label: label to view (NOT NULL)
1588 * @flags: whether to include the mode string
1590 * Returns: size of name written or would be written if larger than
1591 * available buffer
1593 * Note: labels do not have to be strictly hierarchical to the ns as
1594 * objects may be shared across different namespaces and thus
1595 * pickup labeling from each ns. If a particular part of the
1596 * label is not visible it will just be excluded. And if none
1597 * of the label is visible "---" will be used.
1599 int aa_label_snxprint(char *str, size_t size, struct aa_ns *ns,
1600 struct aa_label *label, int flags)
1602 struct aa_profile *profile;
1603 struct aa_ns *prev_ns = NULL;
1604 struct label_it i;
1605 int count = 0, total = 0;
1606 ssize_t len;
1608 AA_BUG(!str && size != 0);
1609 AA_BUG(!label);
1611 if (flags & FLAG_ABS_ROOT) {
1612 ns = root_ns;
1613 len = snprintf(str, size, "=");
1614 update_for_len(total, len, size, str);
1615 } else if (!ns) {
1616 ns = labels_ns(label);
1619 label_for_each(i, label, profile) {
1620 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
1621 if (count > 0) {
1622 len = snprintf(str, size, "//&");
1623 update_for_len(total, len, size, str);
1625 len = aa_profile_snxprint(str, size, ns, profile,
1626 flags & FLAG_VIEW_SUBNS,
1627 &prev_ns);
1628 update_for_len(total, len, size, str);
1629 count++;
1633 if (count == 0) {
1634 if (flags & FLAG_HIDDEN_UNCONFINED)
1635 return snprintf(str, size, "%s", "unconfined");
1636 return snprintf(str, size, "%s", aa_hidden_ns_name);
1639 /* count == 1 && ... is for backwards compat where the mode
1640 * is not displayed for 'unconfined' in the current ns
1642 if (display_mode(ns, label, flags)) {
1643 len = snprintf(str, size, " (%s)",
1644 label_modename(ns, label, flags));
1645 update_for_len(total, len, size, str);
1648 return total;
1650 #undef update_for_len
1653 * aa_label_asxprint - allocate a string buffer and print label into it
1654 * @strp: Returns - the allocated buffer with the label name. (NOT NULL)
1655 * @ns: namespace profile is being viewed from
1656 * @label: label to view (NOT NULL)
1657 * @flags: flags controlling what label info is printed
1658 * @gfp: kernel memory allocation type
1660 * Returns: size of name written or would be written if larger than
1661 * available buffer
1663 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
1664 int flags, gfp_t gfp)
1666 int size;
1668 AA_BUG(!strp);
1669 AA_BUG(!label);
1671 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1672 if (size < 0)
1673 return size;
1675 *strp = kmalloc(size + 1, gfp);
1676 if (!*strp)
1677 return -ENOMEM;
1678 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1682 * aa_label_acntsxprint - allocate a __counted string buffer and print label
1683 * @strp: buffer to write to. (MAY BE NULL if @size == 0)
1684 * @ns: namespace profile is being viewed from
1685 * @label: label to view (NOT NULL)
1686 * @flags: flags controlling what label info is printed
1687 * @gfp: kernel memory allocation type
1689 * Returns: size of name written or would be written if larger than
1690 * available buffer
1692 int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
1693 struct aa_label *label, int flags, gfp_t gfp)
1695 int size;
1697 AA_BUG(!strp);
1698 AA_BUG(!label);
1700 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1701 if (size < 0)
1702 return size;
1704 *strp = aa_str_alloc(size + 1, gfp);
1705 if (!*strp)
1706 return -ENOMEM;
1707 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1711 void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
1712 struct aa_label *label, int flags, gfp_t gfp)
1714 const char *str;
1715 char *name = NULL;
1716 int len;
1718 AA_BUG(!ab);
1719 AA_BUG(!label);
1721 if (!use_label_hname(ns, label, flags) ||
1722 display_mode(ns, label, flags)) {
1723 len = aa_label_asxprint(&name, ns, label, flags, gfp);
1724 if (len == -1) {
1725 AA_DEBUG("label print error");
1726 return;
1728 str = name;
1729 } else {
1730 str = (char *) label->hname;
1731 len = strlen(str);
1733 if (audit_string_contains_control(str, len))
1734 audit_log_n_hex(ab, str, len);
1735 else
1736 audit_log_n_string(ab, str, len);
1738 kfree(name);
1741 void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
1742 struct aa_label *label, int flags, gfp_t gfp)
1744 AA_BUG(!f);
1745 AA_BUG(!label);
1747 if (!use_label_hname(ns, label, flags)) {
1748 char *str;
1749 int len;
1751 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1752 if (len == -1) {
1753 AA_DEBUG("label print error");
1754 return;
1756 seq_printf(f, "%s", str);
1757 kfree(str);
1758 } else if (display_mode(ns, label, flags))
1759 seq_printf(f, "%s (%s)", label->hname,
1760 label_modename(ns, label, flags));
1761 else
1762 seq_printf(f, "%s", label->hname);
1765 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
1766 gfp_t gfp)
1768 AA_BUG(!label);
1770 if (!use_label_hname(ns, label, flags)) {
1771 char *str;
1772 int len;
1774 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1775 if (len == -1) {
1776 AA_DEBUG("label print error");
1777 return;
1779 pr_info("%s", str);
1780 kfree(str);
1781 } else if (display_mode(ns, label, flags))
1782 pr_info("%s (%s)", label->hname,
1783 label_modename(ns, label, flags));
1784 else
1785 pr_info("%s", label->hname);
1788 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp)
1790 struct aa_ns *ns = aa_get_current_ns();
1792 aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp);
1793 aa_put_ns(ns);
1796 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp)
1798 struct aa_ns *ns = aa_get_current_ns();
1800 aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp);
1801 aa_put_ns(ns);
1804 void aa_label_printk(struct aa_label *label, gfp_t gfp)
1806 struct aa_ns *ns = aa_get_current_ns();
1808 aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp);
1809 aa_put_ns(ns);
1812 static int label_count_strn_entries(const char *str, size_t n)
1814 const char *end = str + n;
1815 const char *split;
1816 int count = 1;
1818 AA_BUG(!str);
1820 for (split = aa_label_strn_split(str, end - str);
1821 split;
1822 split = aa_label_strn_split(str, end - str)) {
1823 count++;
1824 str = split + 3;
1827 return count;
1831 * ensure stacks with components like
1832 * :ns:A//&B
1833 * have :ns: applied to both 'A' and 'B' by making the lookup relative
1834 * to the base if the lookup specifies an ns, else making the stacked lookup
1835 * relative to the last embedded ns in the string.
1837 static struct aa_profile *fqlookupn_profile(struct aa_label *base,
1838 struct aa_label *currentbase,
1839 const char *str, size_t n)
1841 const char *first = skipn_spaces(str, n);
1843 if (first && *first == ':')
1844 return aa_fqlookupn_profile(base, str, n);
1846 return aa_fqlookupn_profile(currentbase, str, n);
1850 * aa_label_strn_parse - parse, validate and convert a text string to a label
1851 * @base: base label to use for lookups (NOT NULL)
1852 * @str: null terminated text string (NOT NULL)
1853 * @n: length of str to parse, will stop at \0 if encountered before n
1854 * @gfp: allocation type
1855 * @create: true if should create compound labels if they don't exist
1856 * @force_stack: true if should stack even if no leading &
1858 * Returns: the matching refcounted label if present
1859 * else ERRPTR
1861 struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
1862 size_t n, gfp_t gfp, bool create,
1863 bool force_stack)
1865 DEFINE_VEC(profile, vec);
1866 struct aa_label *label, *currbase = base;
1867 int i, len, stack = 0, error;
1868 const char *end = str + n;
1869 const char *split;
1871 AA_BUG(!base);
1872 AA_BUG(!str);
1874 str = skipn_spaces(str, n);
1875 if (str == NULL || (*str == '=' && base != &root_ns->unconfined->label))
1876 return ERR_PTR(-EINVAL);
1878 len = label_count_strn_entries(str, end - str);
1879 if (*str == '&' || force_stack) {
1880 /* stack on top of base */
1881 stack = base->size;
1882 len += stack;
1883 if (*str == '&')
1884 str++;
1887 error = vec_setup(profile, vec, len, gfp);
1888 if (error)
1889 return ERR_PTR(error);
1891 for (i = 0; i < stack; i++)
1892 vec[i] = aa_get_profile(base->vec[i]);
1894 for (split = aa_label_strn_split(str, end - str), i = stack;
1895 split && i < len; i++) {
1896 vec[i] = fqlookupn_profile(base, currbase, str, split - str);
1897 if (!vec[i])
1898 goto fail;
1900 * if component specified a new ns it becomes the new base
1901 * so that subsequent lookups are relative to it
1903 if (vec[i]->ns != labels_ns(currbase))
1904 currbase = &vec[i]->label;
1905 str = split + 3;
1906 split = aa_label_strn_split(str, end - str);
1908 /* last element doesn't have a split */
1909 if (i < len) {
1910 vec[i] = fqlookupn_profile(base, currbase, str, end - str);
1911 if (!vec[i])
1912 goto fail;
1914 if (len == 1)
1915 /* no need to free vec as len < LOCAL_VEC_ENTRIES */
1916 return &vec[0]->label;
1918 len -= aa_vec_unique(vec, len, VEC_FLAG_TERMINATE);
1919 /* TODO: deal with reference labels */
1920 if (len == 1) {
1921 label = aa_get_label(&vec[0]->label);
1922 goto out;
1925 if (create)
1926 label = aa_vec_find_or_create_label(vec, len, gfp);
1927 else
1928 label = vec_find(vec, len);
1929 if (!label)
1930 goto fail;
1932 out:
1933 /* use adjusted len from after vec_unique, not original */
1934 vec_cleanup(profile, vec, len);
1935 return label;
1937 fail:
1938 label = ERR_PTR(-ENOENT);
1939 goto out;
1942 struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
1943 gfp_t gfp, bool create, bool force_stack)
1945 return aa_label_strn_parse(base, str, strlen(str), gfp, create,
1946 force_stack);
1950 * aa_labelset_destroy - remove all labels from the label set
1951 * @ls: label set to cleanup (NOT NULL)
1953 * Labels that are removed from the set may still exist beyond the set
1954 * being destroyed depending on their reference counting
1956 void aa_labelset_destroy(struct aa_labelset *ls)
1958 struct rb_node *node;
1959 unsigned long flags;
1961 AA_BUG(!ls);
1963 write_lock_irqsave(&ls->lock, flags);
1964 for (node = rb_first(&ls->root); node; node = rb_first(&ls->root)) {
1965 struct aa_label *this = rb_entry(node, struct aa_label, node);
1967 if (labels_ns(this) != root_ns)
1968 __label_remove(this,
1969 ns_unconfined(labels_ns(this)->parent));
1970 else
1971 __label_remove(this, NULL);
1973 write_unlock_irqrestore(&ls->lock, flags);
1977 * @ls: labelset to init (NOT NULL)
1979 void aa_labelset_init(struct aa_labelset *ls)
1981 AA_BUG(!ls);
1983 rwlock_init(&ls->lock);
1984 ls->root = RB_ROOT;
1987 static struct aa_label *labelset_next_stale(struct aa_labelset *ls)
1989 struct aa_label *label;
1990 struct rb_node *node;
1991 unsigned long flags;
1993 AA_BUG(!ls);
1995 read_lock_irqsave(&ls->lock, flags);
1997 __labelset_for_each(ls, node) {
1998 label = rb_entry(node, struct aa_label, node);
1999 if ((label_is_stale(label) ||
2000 vec_is_stale(label->vec, label->size)) &&
2001 __aa_get_label(label))
2002 goto out;
2005 label = NULL;
2007 out:
2008 read_unlock_irqrestore(&ls->lock, flags);
2010 return label;
2014 * __label_update - insert updated version of @label into labelset
2015 * @label - the label to update/replace
2017 * Returns: new label that is up to date
2018 * else NULL on failure
2020 * Requires: @ns lock be held
2022 * Note: worst case is the stale @label does not get updated and has
2023 * to be updated at a later time.
2025 static struct aa_label *__label_update(struct aa_label *label)
2027 struct aa_label *new, *tmp;
2028 struct aa_labelset *ls;
2029 unsigned long flags;
2030 int i, invcount = 0;
2032 AA_BUG(!label);
2033 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock));
2035 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL);
2036 if (!new)
2037 return NULL;
2040 * while holding the ns_lock will stop profile replacement, removal,
2041 * and label updates, label merging and removal can be occurring
2043 ls = labels_set(label);
2044 write_lock_irqsave(&ls->lock, flags);
2045 for (i = 0; i < label->size; i++) {
2046 AA_BUG(!label->vec[i]);
2047 new->vec[i] = aa_get_newest_profile(label->vec[i]);
2048 AA_BUG(!new->vec[i]);
2049 AA_BUG(!new->vec[i]->label.proxy);
2050 AA_BUG(!new->vec[i]->label.proxy->label);
2051 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy)
2052 invcount++;
2055 /* updated stale label by being removed/renamed from labelset */
2056 if (invcount) {
2057 new->size -= aa_vec_unique(&new->vec[0], new->size,
2058 VEC_FLAG_TERMINATE);
2059 /* TODO: deal with reference labels */
2060 if (new->size == 1) {
2061 tmp = aa_get_label(&new->vec[0]->label);
2062 AA_BUG(tmp == label);
2063 goto remove;
2065 if (labels_set(label) != labels_set(new)) {
2066 write_unlock_irqrestore(&ls->lock, flags);
2067 tmp = aa_label_insert(labels_set(new), new);
2068 write_lock_irqsave(&ls->lock, flags);
2069 goto remove;
2071 } else
2072 AA_BUG(labels_ns(label) != labels_ns(new));
2074 tmp = __label_insert(labels_set(label), new, true);
2075 remove:
2076 /* ensure label is removed, and redirected correctly */
2077 __label_remove(label, tmp);
2078 write_unlock_irqrestore(&ls->lock, flags);
2079 label_free_or_put_new(tmp, new);
2081 return tmp;
2085 * __labelset_update - update labels in @ns
2086 * @ns: namespace to update labels in (NOT NULL)
2088 * Requires: @ns lock be held
2090 * Walk the labelset ensuring that all labels are up to date and valid
2091 * Any label that has a stale component is marked stale and replaced and
2092 * by an updated version.
2094 * If failures happen due to memory pressures then stale labels will
2095 * be left in place until the next pass.
2097 static void __labelset_update(struct aa_ns *ns)
2099 struct aa_label *label;
2101 AA_BUG(!ns);
2102 AA_BUG(!mutex_is_locked(&ns->lock));
2104 do {
2105 label = labelset_next_stale(&ns->labels);
2106 if (label) {
2107 struct aa_label *l = __label_update(label);
2109 aa_put_label(l);
2110 aa_put_label(label);
2112 } while (label);
2116 * __aa_labelset_udate_subtree - update all labels with a stale component
2117 * @ns: ns to start update at (NOT NULL)
2119 * Requires: @ns lock be held
2121 * Invalidates labels based on @p in @ns and any children namespaces.
2123 void __aa_labelset_update_subtree(struct aa_ns *ns)
2125 struct aa_ns *child;
2127 AA_BUG(!ns);
2128 AA_BUG(!mutex_is_locked(&ns->lock));
2130 __labelset_update(ns);
2132 list_for_each_entry(child, &ns->sub_ns, base.list) {
2133 mutex_lock_nested(&child->lock, child->level);
2134 __aa_labelset_update_subtree(child);
2135 mutex_unlock(&child->lock);