]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - security/apparmor/label.c
apparmor: add more assertions for updates/merges to help catch errors
[mirror_ubuntu-zesty-kernel.git] / security / apparmor / label.c
1 /*
2 * AppArmor security module
3 *
4 * This file contains AppArmor label definitions
5 *
6 * Copyright 2013 Canonical Ltd.
7 *
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.
12 */
13
14 #include <linux/audit.h>
15 #include <linux/seq_file.h>
16 #include <linux/sort.h>
17
18 #include "include/apparmor.h"
19 #include "include/context.h"
20 #include "include/label.h"
21 #include "include/policy.h"
22 #include "include/sid.h"
23
24
25 /*
26 * the aa_label represents the set of profiles confining an object
27 *
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 * sids - a pinned sid will keep a refcount of the label it is
35 * referencing
36 * objects - inode, files, sockets, ...
37 *
38 * Labels are not ref counted by the label set, so they maybe removed and
39 * freed when no longer in use.
40 *
41 */
42
43 #define PROXY_POISON 97
44 #define LABEL_POISON 100
45
46 static void free_proxy(struct aa_proxy *proxy)
47 {
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 proxy->label = (struct aa_label *) PROXY_POISON;
53 kfree(proxy);
54 }
55 }
56
57 void aa_proxy_kref(struct kref *kref)
58 {
59 struct aa_proxy *proxy = container_of(kref, struct aa_proxy, count);
60 free_proxy(proxy);
61 }
62
63 struct aa_proxy *aa_alloc_proxy(struct aa_label *label, gfp_t gfp)
64 {
65 struct aa_proxy *new;
66
67 new = kzalloc(sizeof(struct aa_proxy), gfp);
68 if (new) {
69 kref_init(&new->count);
70 rcu_assign_pointer(new->label, aa_get_label(label));
71 }
72 return new;
73 }
74
75 /* requires profile list write lock held */
76 void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new)
77 {
78 struct aa_label *tmp;
79
80 AA_BUG(!orig);
81 AA_BUG(!new);
82 AA_BUG(!write_is_locked(&labels_set(orig)->lock));
83
84 tmp = rcu_dereference_protected(orig->proxy->label,
85 &labels_ns(orig)->lock);
86 rcu_assign_pointer(orig->proxy->label, aa_get_label(new));
87 orig->flags |= FLAG_STALE;
88 aa_put_label(tmp);
89 }
90
91 static void __proxy_share(struct aa_label *old, struct aa_label *new)
92 {
93 struct aa_proxy *proxy = new->proxy;
94 new->proxy = aa_get_proxy(old->proxy);
95 __aa_proxy_redirect(old, new);
96 aa_put_proxy(proxy);
97 }
98
99
100 /**
101 * ns_cmp - compare ns for label set ordering
102 * @a: ns to compare (NOT NULL)
103 * @b: ns to compare (NOT NULL)
104 *
105 * Returns: <0 if a < b
106 * ==0 if a == b
107 * >0 if a > b
108 */
109 static int ns_cmp(struct aa_ns *a, struct aa_ns *b)
110 {
111 int res;
112
113 AA_BUG(!a);
114 AA_BUG(!b);
115 AA_BUG(!a->base.hname);
116 AA_BUG(!b->base.hname);
117
118 if (a == b)
119 return 0;
120
121 res = a->level - b->level;
122 if (res)
123 return res;
124
125 return strcmp(a->base.hname, b->base.hname);
126 }
127
128 /**
129 * profile_cmp - profile comparision for set ordering
130 * @a: profile to compare (NOT NULL)
131 * @b: profile to compare (NOT NULL)
132 *
133 * Returns: <0 if a < b
134 * ==0 if a == b
135 * >0 if a > b
136 */
137 static int profile_cmp(struct aa_profile *a, struct aa_profile *b)
138 {
139 int res;
140
141 AA_BUG(!a);
142 AA_BUG(!b);
143 AA_BUG(!a->ns);
144 AA_BUG(!b->ns);
145 AA_BUG(!a->base.hname);
146 AA_BUG(!b->base.hname);
147
148 if (a == b || a->base.hname == b->base.hname)
149 return 0;
150 res = ns_cmp(a->ns, b->ns);
151 if (res)
152 return res;
153
154 return strcmp(a->base.hname, b->base.hname);
155 }
156
157 /**
158 * vec_cmp - label comparision for set ordering
159 * @a: label to compare (NOT NULL)
160 * @vec: vector of profiles to compare (NOT NULL)
161 * @n: length of @vec
162 *
163 * Returns: <0 if a < vec
164 * ==0 if a == vec
165 * >0 if a > vec
166 */
167 static int vec_cmp(struct aa_profile **a, int an, struct aa_profile **b, int bn)
168 {
169 int i;
170
171 AA_BUG(!a);
172 AA_BUG(!*a);
173 AA_BUG(!b);
174 AA_BUG(!*b);
175 AA_BUG(an <= 0);
176 AA_BUG(bn <= 0);
177
178 for (i = 0; i < an && i < bn; i++) {
179 int res = profile_cmp(a[i], b[i]);
180 if (res != 0)
181 return res;
182 }
183
184 return an - bn;
185 }
186
187 static bool vec_is_stale(struct aa_profile **vec, int n)
188 {
189 int i;
190
191 AA_BUG(!vec);
192
193 for (i = 0; i < n; i++) {
194 if (profile_is_stale(vec[i]))
195 return true;
196 }
197
198 return false;
199 }
200
201 static bool vec_unconfined(struct aa_profile **vec, int n)
202 {
203 int i;
204
205 AA_BUG(!vec);
206
207 for (i = 0; i < n; i++) {
208 if (!profile_unconfined(vec[i]))
209 return false;
210 }
211
212 return true;
213 }
214
215 static int sort_cmp(const void *a, const void *b)
216 {
217 return profile_cmp(*(struct aa_profile **)a, *(struct aa_profile **)b);
218 }
219
220 /* assumes vec is sorted
221 * Assumes @vec has null terminator at vec[n], and will null terminate
222 * vec[n - dups]
223 */
224 static inline int unique(struct aa_profile **vec, int n)
225 {
226 int i, pos, dups = 0;
227
228 AA_BUG(n < 1);
229 AA_BUG(!vec);
230
231 pos = 0;
232 for (i = 1; i < n; i++) {
233 int res = profile_cmp(vec[pos], vec[i]);
234 AA_BUG(res > 0, "vec not sorted");
235 if (res == 0) {
236 /* drop duplicate */
237 aa_put_profile(vec[i]);
238 dups++;
239 continue;
240 }
241 pos++;
242 if (dups)
243 vec[pos] = vec[i];
244 }
245
246 AA_BUG(dups < 0);
247
248 return dups;
249 }
250
251 /**
252 * vec_unique - canonical sort and unique a list of profiles
253 * @n: number of refcounted profiles in the list (@n > 0)
254 * @vec: list of profiles to sort and merge
255 *
256 * Returns: the number of duplicates eliminated == references put
257 *
258 * If @flags & VEC_FLAG_TERMINATE @vec has null terminator at vec[n], and will
259 * null terminate vec[n - dups]
260 */
261 int aa_vec_unique(struct aa_profile **vec, int n, int flags)
262 {
263 int i, dups = 0;
264
265 AA_BUG(n < 1);
266 AA_BUG(!vec);
267
268 /* vecs are usually small and inorder, have a fallback for larger */
269 if (n > 8) {
270 sort(vec, n, sizeof(struct aa_profile *), sort_cmp, NULL);
271 dups = unique(vec, n);
272 goto out;
273 }
274
275 /* insertion sort + unique in one */
276 for (i = 1; i < n; i++) {
277 struct aa_profile *tmp = vec[i];
278 int pos, j;
279
280 for (pos = i - 1 - dups; pos >= 0; pos--) {
281 int res = profile_cmp(vec[pos], tmp);
282 if (res == 0) {
283 /* drop duplicate entry */
284 aa_put_profile(tmp);
285 dups++;
286 goto continue_outer;
287 } else if (res < 0)
288 break;
289 }
290 /* pos is at entry < tmp, or index -1. Set to insert pos */
291 pos++;
292
293 for (j = i - dups; j > pos; j--)
294 vec[j] = vec[j - 1];
295 vec[pos] = tmp;
296 continue_outer: ;
297 }
298
299 AA_BUG(dups < 0);
300
301 out:
302 if (flags & VEC_FLAG_TERMINATE)
303 vec[n - dups] = NULL;
304
305 return dups;
306 }
307
308
309 static void label_destroy(struct aa_label *label)
310 {
311 AA_BUG(!label);
312
313 if (label_is_stale(label))
314 labelsetstats_dec(labels_set(label), stale);
315
316 if (!label_isprofile(label)) {
317 struct aa_profile *profile;
318 struct label_it i;
319
320 aa_put_str(label->hname);
321
322 label_for_each(i, label, profile) {
323 aa_put_profile(profile);
324 label->vec[i.i] = (struct aa_profile *) (LABEL_POISON + (long) i.i);
325 }
326 }
327
328 if (rcu_dereference_protected(label->proxy->label, true) == label)
329 rcu_assign_pointer(label->proxy->label, NULL);
330
331 aa_free_sid(label->sid);
332 aa_put_proxy(label->proxy);
333 label->proxy = (struct aa_proxy *) PROXY_POISON + 1;
334 }
335
336 void aa_label_free(struct aa_label *label)
337 {
338 if (!label)
339 return;
340
341 label_destroy(label);
342 labelstats_inc(freed);
343 kfree(label);
344 }
345
346 static void label_free_switch(struct aa_label *label)
347 {
348 if (label->flags & FLAG_NS_COUNT)
349 aa_free_ns(labels_ns(label));
350 else if (label_isprofile(label))
351 aa_free_profile(labels_profile(label));
352 else
353 aa_label_free(label);
354 }
355
356 static void label_free_rcu(struct rcu_head *head)
357 {
358 struct aa_label *label = container_of(head, struct aa_label, rcu);
359
360 if (label->flags & FLAG_IN_TREE)
361 (void) aa_label_remove(label);
362 label_free_switch(label);
363 }
364
365 void aa_label_kref(struct kref *kref)
366 {
367 struct aa_label *label = container_of(kref, struct aa_label, count);
368 struct aa_ns *ns = labels_ns(label);
369
370 if (!ns) {
371 /* never live, no rcu callback needed, just using the fn */
372 label_free_switch(label);
373 return;
374 }
375 /* TODO: update labels_profile macro so it works here */
376 AA_BUG(label_isprofile(label) && on_list_rcu(&label->vec[0]->base.profiles));
377 AA_BUG(label_isprofile(label) && on_list_rcu(&label->vec[0]->base.list));
378
379 /* TODO: if compound label and not stale add to reclaim cache */
380 call_rcu(&label->rcu, label_free_rcu);
381 }
382
383 bool aa_label_init(struct aa_label *label, int size)
384 {
385 AA_BUG(!label);
386 AA_BUG(size < 1);
387
388 label->sid = aa_alloc_sid();
389 if (label->sid == AA_SID_INVALID)
390 return false;
391
392 label->size = size; /* doesn't include null */
393 label->vec[size] = NULL; /* null terminate */
394 kref_init(&label->count);
395 RB_CLEAR_NODE(&label->node);
396
397 return true;
398 }
399
400 /**
401 * aa_label_alloc - allocate a label with a profile vector of @size length
402 * @size: size of profile vector in the label
403 * @proxy: proxy to use OR null if to allocate a new one
404 * @gfp: memory allocation type
405 *
406 * Returns: new label
407 * else NULL if failed
408 */
409 struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp)
410 {
411 struct aa_label *new;
412
413 AA_BUG(size < 1);
414
415 /* + 1 for null terminator entry on vec */
416 new = kzalloc(sizeof(*new) + sizeof(struct aa_profile *) * (size + 1),
417 gfp);
418 AA_DEBUG("%s (%p)\n", __func__, new);
419 if (!new)
420 goto fail;
421
422 if (!aa_label_init(new, size))
423 goto fail;
424
425 if (!proxy) {
426 proxy = aa_alloc_proxy(new, gfp);
427 if (!proxy)
428 goto fail;
429 } else
430 aa_get_proxy(proxy);
431 /* just set new's proxy, don't redirect proxy here if it was passed in*/
432 new->proxy = proxy;
433
434 labelstats_inc(allocated);
435
436 return new;
437
438 fail:
439 kfree(new);
440 labelstats_inc(failed);
441
442 return NULL;
443 }
444
445
446 /**
447 * label_cmp - label comparision for set ordering
448 * @a: label to compare (NOT NULL)
449 * @b: label to compare (NOT NULL)
450 *
451 * Returns: <0 if a < b
452 * ==0 if a == b
453 * >0 if a > b
454 */
455 static int label_cmp(struct aa_label *a, struct aa_label *b)
456 {
457 AA_BUG(!b);
458
459 if (a == b)
460 return 0;
461
462 return vec_cmp(a->vec, a->size, b->vec, b->size);
463 }
464
465 /* helper fn for label_for_each_confined */
466 int aa_label_next_confined(struct aa_label *label, int i)
467 {
468 AA_BUG(!label);
469 AA_BUG(i < 0);
470
471 for (; i < label->size; i++) {
472 if (!profile_unconfined(label->vec[i]))
473 return i;
474 }
475
476 return i;
477 }
478
479 /**
480 * aa_label_next_not_in_set - return the next profile of @sub not in @set
481 * @I: label iterator
482 * @set: label to test against
483 * @sub: label to if is subset of @set
484 *
485 * Returns: profile in @sub that is not in @set, with iterator set pos after
486 * else NULL if @sub is a subset of @set
487 */
488 struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
489 struct aa_label *set,
490 struct aa_label *sub)
491 {
492 AA_BUG(!set);
493 AA_BUG(!I);
494 AA_BUG(I->i < 0);
495 AA_BUG(I->i > set->size);
496 AA_BUG(!sub);
497 AA_BUG(I->j < 0);
498 AA_BUG(I->j > sub->size);
499
500 while (I->j < sub->size && I->i < set->size) {
501 int res = profile_cmp(sub->vec[I->j], set->vec[I->i]);
502 if (res == 0) {
503 (I->j)++;
504 (I->i)++;
505 } else if (res > 0)
506 (I->i)++;
507 else
508 return sub->vec[(I->j)++];
509 }
510
511 if (I->j < sub->size)
512 return sub->vec[(I->j)++];
513
514 return NULL;
515 }
516
517 /**
518 * aa_label_is_subset - test if @sub is a subset of @set
519 * @set: label to test against
520 * @sub: label to test if is subset of @set
521 *
522 * Returns: true if @sub is subset of @set
523 * else false
524 */
525 bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub)
526 {
527 struct label_it i = { };
528
529 AA_BUG(!set);
530 AA_BUG(!sub);
531
532 if (sub == set)
533 return true;
534
535 return __aa_label_next_not_in_set(&i, set, sub) == NULL;
536 }
537
538
539
540 /**
541 * __label_remove - remove @label from the label set
542 * @l: label to remove
543 * @new: label to redirect to
544 *
545 * Requires: labels_set(@label)->lock write_lock
546 * Returns: true if the label was in the tree and removed
547 */
548 static bool __label_remove(struct aa_label *label, struct aa_label *new)
549 {
550 struct aa_labelset *ls = labels_set(label);
551 AA_BUG(!ls);
552 AA_BUG(!label);
553 AA_BUG(!write_is_locked(&ls->lock));
554
555 if (new)
556 __aa_proxy_redirect(label, new);
557
558 if (label_is_stale(label))
559 labelstats_dec(stale_intree);
560 else
561 __label_make_stale(label);
562
563 if (label->flags & FLAG_IN_TREE) {
564 labelsetstats_dec(ls, intree);
565 rb_erase(&label->node, &ls->root);
566 label->flags &= ~FLAG_IN_TREE;
567 return true;
568 }
569
570 return false;
571 }
572
573 /**
574 * __label_replace - replace @old with @new in label set
575 * @old: label to remove from label set
576 * @new: label to replace @old with
577 *
578 * Requires: labels_set(@old)->lock write_lock
579 * valid ref count be held on @new
580 * Returns: true if @old was in set and replaced by @new
581 *
582 * Note: current implementation requires label set be order in such a way
583 * that @new directly replaces @old position in the set (ie.
584 * using pointer comparison of the label address would not work)
585 */
586 static bool __label_replace(struct aa_label *old, struct aa_label *new)
587 {
588 struct aa_labelset *ls = labels_set(old);
589 AA_BUG(!ls);
590 AA_BUG(!old);
591 AA_BUG(!new);
592 AA_BUG(!write_is_locked(&ls->lock));
593 AA_BUG(new->flags & FLAG_IN_TREE);
594
595 if (label_is_stale(old))
596 labelstats_dec(stale_intree);
597 else
598 __label_make_stale(old);
599
600 if (old->flags & FLAG_IN_TREE) {
601 rb_replace_node(&old->node, &new->node, &ls->root);
602 old->flags &= ~FLAG_IN_TREE;
603 new->flags |= FLAG_IN_TREE;
604 return true;
605 }
606
607 return false;
608 }
609
610 /**
611 * __label_insert - attempt to insert @l into a label set
612 * @ls: set of labels to insert @l into (NOT NULL)
613 * @label: new label to insert (NOT NULL)
614 * @replace: whether insertion should replace existing entry that is not stale
615 *
616 * Requires: @ls->lock
617 * caller to hold a valid ref on l
618 * if @replace is true l has a preallocated proxy associated
619 * Returns: @l if successful in inserting @l - with additional refcount
620 * else ref counted equivalent label that is already in the set,
621 the else condition only happens if @replace is false
622 */
623 static struct aa_label *__label_insert(struct aa_labelset *ls,
624 struct aa_label *label, bool replace)
625 {
626 struct rb_node **new, *parent = NULL;
627
628 AA_BUG(!ls);
629 AA_BUG(!label);
630 AA_BUG(labels_set(label) != ls);
631 AA_BUG(!write_is_locked(&ls->lock));
632 AA_BUG(label->flags & FLAG_IN_TREE);
633
634 /* Figure out where to put new node */
635 new = &ls->root.rb_node;
636 while (*new) {
637 struct aa_label *this = rb_entry(*new, struct aa_label, node);
638 int result = label_cmp(label, this);
639
640 parent = *new;
641 if (result == 0) {
642 labelsetstats_inc(ls, existing);
643 /* !aa_get_label_not0 means queued for destruction,
644 * so replace in place, however the label has
645 * died before the replacement so do not share
646 * the proxy
647 */
648 if (!replace && !label_is_stale(this)) {
649 if (aa_get_label_not0(this))
650 return this;
651 } else
652 __proxy_share(this, label);
653 AA_BUG(!__label_replace(this, label));
654 return aa_get_label(label);
655 } else if (result < 0)
656 new = &((*new)->rb_left);
657 else /* (result > 0) */
658 new = &((*new)->rb_right);
659 }
660
661 /* Add new node and rebalance tree. */
662 rb_link_node(&label->node, parent, new);
663 rb_insert_color(&label->node, &ls->root);
664 label->flags |= FLAG_IN_TREE;
665 labelsetstats_inc(ls, insert);
666 labelsetstats_inc(ls, intree);
667
668 return aa_get_label(label);
669 }
670
671 /**
672 * __vec_find - find label that matches @vec in label set
673 * @vec: vec of profiles to find matching label for (NOT NULL)
674 * @n: length of @vec
675 *
676 * Requires: @vec_labelset(vec) lock held
677 * caller to hold a valid ref on l
678 *
679 * Returns: ref counted @label if matching label is in tree
680 * ref counted label that is equiv to @l in tree
681 * else NULL if @vec equiv is not in tree
682 */
683 static struct aa_label *__vec_find(struct aa_profile **vec, int n)
684 {
685 struct rb_node *node;
686
687 AA_BUG(!vec);
688 AA_BUG(!*vec);
689 AA_BUG(n <= 0);
690
691 node = vec_labelset(vec, n)->root.rb_node;
692 while (node) {
693 struct aa_label *this = rb_entry(node, struct aa_label, node);
694 int result = vec_cmp(this->vec, this->size, vec, n);
695
696 if (result > 0)
697 node = node->rb_left;
698 else if (result < 0)
699 node = node->rb_right;
700 else
701 return aa_get_label_not0(this);
702 }
703
704 return NULL;
705 }
706
707 /**
708 * __label_find - find label @label in label set
709 * @label: label to find (NOT NULL)
710 *
711 * Requires: labels_set(@label)->lock held
712 * caller to hold a valid ref on l
713 *
714 * Returns: ref counted @label if @label is in tree OR
715 * ref counted label that is equiv to @label in tree
716 * else NULL if @label or equiv is not in tree
717 */
718 static struct aa_label *__label_find(struct aa_label *label)
719 {
720 AA_BUG(!label);
721
722 return __vec_find(label->vec, label->size);
723 }
724
725
726 /**
727 * aa_label_remove - remove a label from the labelset
728 * @label: label to remove
729 *
730 * Returns: true if @label was removed from the tree
731 * else @label was not in tree so it could not be removed
732 */
733 bool aa_label_remove(struct aa_label *label)
734 {
735 struct aa_labelset *ls = labels_set(label);
736 unsigned long flags;
737 bool res;
738
739 AA_BUG(!ls);
740
741 write_lock_irqsave(&ls->lock, flags);
742 res = __label_remove(label, ns_unconfined(labels_ns(label)));
743 write_unlock_irqrestore(&ls->lock, flags);
744
745 return res;
746 }
747
748 /**
749 * aa_label_replace - replace a label @old with a new version @new
750 * @old: label to replace
751 * @new: label replacing @old
752 *
753 * Returns: true if @old was in tree and replaced
754 * else @old was not in tree, and @new was not inserted
755 */
756 bool aa_label_replace(struct aa_label *old, struct aa_label *new)
757 {
758 unsigned long flags;
759 bool res;
760
761 if (name_is_shared(old, new) && labels_ns(old) == labels_ns(new)) {
762 write_lock_irqsave(&labels_set(old)->lock, flags);
763 if (old->proxy != new->proxy) {
764 __proxy_share(old, new);
765 } else
766 __aa_proxy_redirect(old, new);
767 res = __label_replace(old, new);
768 write_unlock_irqrestore(&labels_set(old)->lock, flags);
769 } else {
770 struct aa_label *l;
771 struct aa_labelset *ls = labels_set(old);
772 write_lock_irqsave(&ls->lock, flags);
773 res = __label_remove(old, new);
774 if (labels_ns(old) != labels_ns(new)) {
775 write_unlock_irqrestore(&ls->lock, flags);
776 ls = labels_set(new);
777 write_lock_irqsave(&ls->lock, flags);
778 }
779 l = __label_insert(ls, new, true);
780 res = (l == new);
781 write_unlock_irqrestore(&ls->lock, flags);
782 aa_put_label(l);
783 }
784
785 return res;
786 }
787
788 /**
789 * vec_find - find label @l in label set
790 * @vec: array of profiles to find equiv label for (NOT NULL)
791 * @n: length of @vec
792 *
793 * Returns: refcounted label if @vec equiv is in tree
794 * else NULL if @vec equiv is not in tree
795 */
796 static struct aa_label *vec_find(struct aa_profile **vec, int n)
797 {
798 struct aa_labelset *ls;
799 struct aa_label *label;
800 unsigned long flags;
801
802 AA_BUG(!vec);
803 AA_BUG(!*vec);
804 AA_BUG(n <= 0);
805
806 ls = vec_labelset(vec, n);
807 read_lock_irqsave(&ls->lock, flags);
808 label = __vec_find(vec, n);
809 labelstats_inc(sread);
810 read_unlock_irqrestore(&ls->lock, flags);
811
812 return label;
813 }
814
815 /* requires sort and merge done first */
816 static struct aa_label *vec_create_and_insert_label(struct aa_profile **vec,
817 int len, gfp_t gfp)
818 {
819 struct aa_label *label = NULL;
820 struct aa_labelset *ls;
821 unsigned long flags;
822 struct aa_label *new;
823 int i;
824
825 AA_BUG(!vec);
826
827 if (len == 1)
828 return aa_get_label(&vec[0]->label);
829
830 ls = labels_set(&vec[len - 1]->label);
831
832 /* TODO: enable when read side is lockless
833 * check if label exists before taking locks
834 */
835 new = aa_label_alloc(len, NULL, gfp);
836 if (!new)
837 return NULL;
838
839 for (i = 0; i < len; i++) {
840 new->vec[i] = aa_get_profile(vec[i]);
841 }
842 write_lock_irqsave(&ls->lock, flags);
843 label = __label_insert(ls, new, false);
844 write_unlock_irqrestore(&ls->lock, flags);
845 aa_put_label(new);
846
847 return label;
848 }
849
850 struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
851 gfp_t gfp)
852 {
853 struct aa_label *label = vec_find(vec, len);
854 if (label)
855 return label;
856
857 return vec_create_and_insert_label(vec, len, gfp);
858 }
859
860 /**
861 * aa_label_find - find label @label in label set
862 * @label: label to find (NOT NULL)
863 *
864 * Requires: caller to hold a valid ref on l
865 *
866 * Returns: refcounted @label if @label is in tree
867 * refcounted label that is equiv to @label in tree
868 * else NULL if @label or equiv is not in tree
869 */
870 struct aa_label *aa_label_find(struct aa_label *label)
871 {
872 AA_BUG(!label);
873
874 return vec_find(label->vec, label->size);
875 }
876
877
878 /**
879 * aa_label_insert - insert label @label into @ls or return existing label
880 * @ls - labelset to insert @label into
881 * @label - label to insert
882 *
883 * Requires: caller to hold a valid ref on @label
884 *
885 * Returns: ref counted @label if successful in inserting @label
886 * else ref counted equivalent label that is already in the set
887 */
888 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *label)
889 {
890 struct aa_label *l;
891 unsigned long flags;
892
893 AA_BUG(!ls);
894 AA_BUG(!label);
895
896 /* check if label exists before taking lock */
897 if (!label_is_stale(label)) {
898 read_lock_irqsave(&ls->lock, flags);
899 l = __label_find(label);
900 read_unlock_irqrestore(&ls->lock, flags);
901 labelstats_inc(fread);
902 if (l)
903 return l;
904 }
905
906 write_lock_irqsave(&ls->lock, flags);
907 l = __label_insert(ls, label, false);
908 write_unlock_irqrestore(&ls->lock, flags);
909
910 return l;
911 }
912
913
914 /**
915 * aa_label_next_in_merge - find the next profile when merging @a and @b
916 * @I: label iterator
917 * @a: label to merge
918 * @b: label to merge
919 *
920 * Returns: next profile
921 * else null if no more profiles
922 */
923 struct aa_profile *aa_label_next_in_merge(struct label_it *I,
924 struct aa_label *a,
925 struct aa_label *b)
926 {
927 AA_BUG(!a);
928 AA_BUG(!b);
929 AA_BUG(!I);
930 AA_BUG(I->i < 0);
931 AA_BUG(I->i > a->size);
932 AA_BUG(I->j < 0);
933 AA_BUG(I->j > b->size);
934
935 if (I->i < a->size) {
936 if (I->j < b->size) {
937 int res = profile_cmp(a->vec[I->i], b->vec[I->j]);
938 if (res > 0)
939 return b->vec[(I->j)++];
940 if (res == 0)
941 (I->j)++;
942 }
943
944 return a->vec[(I->i)++];
945 }
946
947 if (I->j < b->size)
948 return b->vec[(I->j)++];
949
950 return NULL;
951 }
952
953 /**
954 * label_merge_cmp - cmp of @a merging with @b against @z for set ordering
955 * @a: label to merge then compare (NOT NULL)
956 * @b: label to merge then compare (NOT NULL)
957 * @z: label to compare merge against (NOT NULL)
958 *
959 * Assumes: using the most recent versions of @a, @b, and @z
960 *
961 * Returns: <0 if a < b
962 * ==0 if a == b
963 * >0 if a > b
964 */
965 static int label_merge_cmp(struct aa_label *a, struct aa_label *b,
966 struct aa_label *z)
967 {
968 struct aa_profile *p = NULL;
969 struct label_it i = { };
970 int k;
971
972 AA_BUG(!a);
973 AA_BUG(!b);
974 AA_BUG(!z);
975
976 for (k = 0;
977 k < z->size && (p = aa_label_next_in_merge(&i, a, b));
978 k++) {
979 int res = profile_cmp(p, z->vec[k]);
980
981 if (res != 0)
982 return res;
983 }
984
985 if (p)
986 return 1;
987 else if (k < z->size)
988 return -1;
989 return 0;
990 }
991
992 #if 0
993 /**
994 * label_merge_len - find the length of the merge of @a and @b
995 * @a: label to merge (NOT NULL)
996 * @b: label to merge (NOT NULL)
997 *
998 * Assumes: using newest versions of labels @a and @b
999 *
1000 * Returns: length of a label vector for merge of @a and @b
1001 */
1002 static int label_merge_len(struct aa_label *a, struct aa_label *b)
1003 {
1004 int len = a->size + b->size;
1005 int i, j;
1006
1007 AA_BUG(!a);
1008 AA_BUG(!b);
1009
1010 /* find entries in common and remove from count */
1011 for (i = j = 0; i < a->size && j < b->size; ) {
1012 int res = profile_cmp(a->vec[i], b->vec[j]);
1013 if (res == 0) {
1014 len--;
1015 i++;
1016 j++;
1017 } else if (res < 0)
1018 i++;
1019 else
1020 j++;
1021 }
1022
1023 return len;
1024 }
1025 #endif
1026
1027 /**
1028 * label_merge_insert - create a new label by merging @a and @b
1029 * @new: preallocated label to merge into (NOT NULL)
1030 * @a: label to merge with @b (NOT NULL)
1031 * @b: label to merge with @a (NOT NULL)
1032 *
1033 * Requires: preallocated proxy
1034 *
1035 * Returns: ref counted label either @new if merge is unique
1036 * @a if @b is a subset of @a
1037 * @b if @a is a subset of @b
1038 *
1039 * NOTE: will not use @new if the merge results in @new == @a or @b
1040 *
1041 * Must be used within labelset write lock to avoid racing with
1042 * setting labels stale.
1043 */
1044 static struct aa_label *label_merge_insert(struct aa_label *new,
1045 struct aa_label *a,
1046 struct aa_label *b)
1047 {
1048 struct aa_label *label;
1049 struct aa_labelset *ls;
1050 struct aa_profile *next;
1051 struct label_it i;
1052 unsigned long flags;
1053 int k = 0, invcount = 0;
1054 bool stale = false;
1055
1056 AA_BUG(!a);
1057 AA_BUG(a->size < 0);
1058 AA_BUG(!b);
1059 AA_BUG(b->size < 0);
1060 AA_BUG(!new);
1061 AA_BUG(new->size < a->size + b->size);
1062
1063 label_for_each_in_merge(i, a, b, next) {
1064 AA_BUG(!next);
1065 if (profile_is_stale(next)) {
1066 new->vec[k] = aa_get_newest_profile(next);
1067 AA_BUG(!new->vec[k]->label.proxy);
1068 AA_BUG(!new->vec[k]->label.proxy->label);
1069 if (next->label.proxy != new->vec[k]->label.proxy)
1070 invcount++;
1071 k++;
1072 stale = true;
1073 } else
1074 new->vec[k++] = aa_get_profile(next);
1075 }
1076 /* set to actual size which is <= allocated len */
1077 new->size = k;
1078 new->vec[k] = NULL;
1079
1080 if (invcount) {
1081 new->size -= aa_vec_unique(&new->vec[0], new->size,
1082 VEC_FLAG_TERMINATE);
1083 /* TODO: deal with reference labels */
1084 if (new->size == 1) {
1085 label = aa_get_label(&new->vec[0]->label);
1086 aa_put_label(new);
1087 return label;
1088 }
1089 } else if (!stale) {
1090 /* merge could be same as a || b, note: it is not possible
1091 * for new->size == a->size == b->size unless a == b */
1092 if (k == a->size)
1093 return aa_get_label(a);
1094 else if (k == b->size)
1095 return aa_get_label(b);
1096 }
1097 if (vec_unconfined(new->vec, new->size))
1098 new->flags |= FLAG_UNCONFINED;
1099 ls = labels_set(new);
1100 write_lock_irqsave(&ls->lock, flags);
1101 label = __label_insert(labels_set(new), new, false);
1102 write_unlock_irqrestore(&ls->lock, flags);
1103
1104 return label;
1105 }
1106
1107 /**
1108 * labelset_of_merge - find into which labelset a merged label should be inserted
1109 * @a: label to merge and insert
1110 * @b: label to merge and insert
1111 *
1112 * Returns: labelset that the merged label should be inserted into
1113 */
1114 static struct aa_labelset *labelset_of_merge(struct aa_label *a, struct aa_label *b)
1115 {
1116 struct aa_ns *nsa = labels_ns(a);
1117 struct aa_ns *nsb = labels_ns(b);
1118
1119 if (ns_cmp(nsa, nsb) <= 0)
1120 return &nsa->labels;
1121 return &nsb->labels;
1122 }
1123
1124 /**
1125 * __label_find_merge - find label that is equiv to merge of @a and @b
1126 * @ls: set of labels to search (NOT NULL)
1127 * @a: label to merge with @b (NOT NULL)
1128 * @b: label to merge with @a (NOT NULL)
1129 *
1130 * Requires: ls->lock read_lock held
1131 *
1132 * Returns: ref counted label that is equiv to merge of @a and @b
1133 * else NULL if merge of @a and @b is not in set
1134 */
1135 static struct aa_label *__label_find_merge(struct aa_labelset *ls,
1136 struct aa_label *a,
1137 struct aa_label *b)
1138 {
1139 struct rb_node *node;
1140
1141 AA_BUG(!ls);
1142 AA_BUG(!a);
1143 AA_BUG(!b);
1144
1145 if (a == b)
1146 return __label_find(a);
1147
1148 node = ls->root.rb_node;
1149 while (node) {
1150 struct aa_label *this = container_of(node, struct aa_label,
1151 node);
1152 int result = label_merge_cmp(a, b, this);
1153
1154 if (result < 0)
1155 node = node->rb_left;
1156 else if (result > 0)
1157 node = node->rb_right;
1158 else
1159 return aa_get_label_not0(this);
1160 }
1161
1162 return NULL;
1163 }
1164
1165
1166 /**
1167 * aa_label_find_merge - find label that is equiv to merge of @a and @b
1168 * @a: label to merge with @b (NOT NULL)
1169 * @b: label to merge with @a (NOT NULL)
1170 *
1171 * Requires: labels be fully constructed with a valid ns
1172 *
1173 * Returns: ref counted label that is equiv to merge of @a and @b
1174 * else NULL if merge of @a and @b is not in set
1175 */
1176 struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b)
1177 {
1178 struct aa_labelset *ls;
1179 struct aa_label *label, *ar = NULL, *br = NULL;
1180 unsigned long flags;
1181
1182 AA_BUG(!a);
1183 AA_BUG(!b);
1184
1185 if (label_is_stale(a))
1186 a = ar = aa_get_newest_label(a);
1187 if (label_is_stale(b))
1188 b = br = aa_get_newest_label(b);
1189 ls = labelset_of_merge(a, b);
1190 read_lock_irqsave(&ls->lock, flags);
1191 label = __label_find_merge(ls, a, b);
1192 read_unlock_irqrestore(&ls->lock, flags);
1193 aa_put_label(ar);
1194 aa_put_label(br);
1195 labelsetstats_inc(ls, msread);
1196
1197 return label;
1198 }
1199
1200 /**
1201 * aa_label_merge - attempt to insert new merged label of @a and @b
1202 * @ls: set of labels to insert label into (NOT NULL)
1203 * @a: label to merge with @b (NOT NULL)
1204 * @b: label to merge with @a (NOT NULL)
1205 * @gfp: memory allocation type
1206 *
1207 * Requires: caller to hold valid refs on @a and @b
1208 * labels be fully constructed with a valid ns
1209 *
1210 * Returns: ref counted new label if successful in inserting merge of a & b
1211 * else ref counted equivalent label that is already in the set.
1212 * else NULL if could not create label (-ENOMEM)
1213 */
1214 struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
1215 gfp_t gfp)
1216 {
1217 struct aa_label *label = NULL;
1218
1219 AA_BUG(!a);
1220 AA_BUG(!b);
1221
1222 if (a == b)
1223 return aa_get_newest_label(a);
1224
1225 /* TODO: enable when read side is lockless
1226 * check if label exists before taking locks
1227 if (!label_is_stale(a) && !label_is_stale(b))
1228 label = aa_label_find_merge(a, b);
1229 */
1230
1231 if (!label) {
1232 struct aa_label *new;
1233
1234 a = aa_get_newest_label(a);
1235 b = aa_get_newest_label(b);
1236
1237 /* could use label_merge_len(a, b), but requires double
1238 * comparison for small savings
1239 */
1240 new = aa_label_alloc(a->size + b->size, NULL, gfp);
1241 if (!new)
1242 goto out;
1243
1244 label = label_merge_insert(new, a, b);
1245 aa_put_label(new);
1246 out:
1247 aa_put_label(a);
1248 aa_put_label(b);
1249 }
1250
1251 return label;
1252 }
1253
1254 static inline bool label_is_visible(struct aa_profile *profile,
1255 struct aa_label *label)
1256 {
1257 return aa_ns_visible(profile->ns, labels_ns(label), true);
1258 }
1259
1260 /* match a profile and its associated ns component if needed
1261 * Assumes visibility test has already been done.
1262 * If a subns profile is not to be matched should be prescreened with
1263 * visibility test.
1264 */
1265 static inline unsigned int match_component(struct aa_profile *profile,
1266 struct aa_profile *tp,
1267 unsigned int state)
1268 {
1269 const char *ns_name;
1270
1271 if (profile->ns == tp->ns)
1272 return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
1273
1274 /* try matching with namespace name and then profile */
1275 ns_name = aa_ns_name(profile->ns, tp->ns, true);
1276 state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
1277 state = aa_dfa_match(profile->policy.dfa, state, ns_name);
1278 state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1);
1279 return aa_dfa_match(profile->policy.dfa, state, tp->base.hname);
1280 }
1281
1282 /**
1283 * label_component_match - find perms for full compound label
1284 * @profile: profile to find perms for
1285 * @label: label to check access permissions for
1286 * @start: state to start match in
1287 * @subns: whether to do permission checks on components in a subns
1288 * @request: permissions to request
1289 * @perms: perms struct to set
1290 *
1291 * Returns: 0 on success else ERROR
1292 *
1293 * For the label A//&B//&C this does the perm match for A//&B//&C
1294 * @perms should be preinitialized with allperms OR a previous permission
1295 * check to be stacked.
1296 */
1297 static int label_compound_match(struct aa_profile *profile,
1298 struct aa_label *label,
1299 unsigned int state, bool subns, u32 request,
1300 struct aa_perms *perms)
1301 {
1302 struct aa_profile *tp;
1303 struct label_it i;
1304
1305 /* find first subcomponent that is visible */
1306 label_for_each(i, label, tp) {
1307 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1308 continue;
1309 state = match_component(profile, tp, state);
1310 if (!state)
1311 goto fail;
1312 goto next;
1313 }
1314
1315 /* no component visible */
1316 *perms = allperms;
1317 return 0;
1318
1319 next:
1320 label_for_each_cont(i, label, tp) {
1321 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1322 continue;
1323 state = aa_dfa_match(profile->policy.dfa, state, "//&");
1324 state = match_component(profile, tp, state);
1325 if (!state)
1326 goto fail;
1327 }
1328 aa_compute_perms(profile->policy.dfa, state, perms);
1329 aa_apply_modes_to_perms(profile, perms);
1330 if ((perms->allow & request) != request)
1331 return -EACCES;
1332
1333 return 0;
1334
1335 fail:
1336 *perms = nullperms;
1337 return state;
1338 }
1339
1340 /**
1341 * label_component_match - find perms for all subcomponents of a label
1342 * @profile: profile to find perms for
1343 * @label: label to check access permissions for
1344 * @start: state to start match in
1345 * @subns: whether to do permission checks on components in a subns
1346 * @request: permissions to request
1347 * @perms: an initialized perms struct to add accumulation to
1348 *
1349 * Returns: 0 on success else ERROR
1350 *
1351 * For the label A//&B//&C this does the perm match for each of A and B and C
1352 * @perms should be preinitialized with allperms OR a previous permission
1353 * check to be stacked.
1354 */
1355 static int label_components_match(struct aa_profile *profile,
1356 struct aa_label *label, unsigned int start,
1357 bool subns, u32 request,
1358 struct aa_perms *perms)
1359 {
1360 struct aa_profile *tp;
1361 struct label_it i;
1362 struct aa_perms tmp;
1363 unsigned int state = 0;
1364
1365 /* find first subcomponent to test */
1366 label_for_each(i, label, tp) {
1367 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1368 continue;
1369 state = match_component(profile, tp, start);
1370 if (!state)
1371 goto fail;
1372 goto next;
1373 }
1374
1375 /* no subcomponents visible - no change in perms */
1376 return 0;
1377
1378 next:
1379 aa_compute_perms(profile->policy.dfa, state, &tmp);
1380 aa_apply_modes_to_perms(profile, &tmp);
1381 aa_perms_accum(perms, &tmp);
1382 label_for_each_cont(i, label, tp) {
1383 if (!aa_ns_visible(profile->ns, tp->ns, subns))
1384 continue;
1385 state = match_component(profile, tp, start);
1386 if (!state)
1387 goto fail;
1388 aa_compute_perms(profile->policy.dfa, state, &tmp);
1389 aa_apply_modes_to_perms(profile, &tmp);
1390 aa_perms_accum(perms, &tmp);
1391 }
1392
1393 if ((perms->allow & request) != request)
1394 return -EACCES;
1395
1396 return 0;
1397
1398 fail:
1399 *perms = nullperms;
1400 return -EACCES;
1401 }
1402
1403 /**
1404 * aa_label_match - do a multi-component label match
1405 * @profile: profile to match against (NOT NULL)
1406 * @label: label to match (NOT NULL)
1407 * @state: state to start in
1408 * @subns: whether to match subns components
1409 * @request: permission request
1410 * @perms: Returns computed perms (NOT NULL)
1411 *
1412 * Returns: the state the match finished in, may be the none matching state
1413 */
1414 int aa_label_match(struct aa_profile *profile, struct aa_label *label,
1415 unsigned int state, bool subns, u32 request,
1416 struct aa_perms *perms)
1417 {
1418 int error = label_compound_match(profile, label, state, subns, request,
1419 perms);
1420 if (!error)
1421 return error;
1422
1423 *perms = allperms;
1424 return label_components_match(profile, label, state, subns, request,
1425 perms);
1426 }
1427
1428
1429 /**
1430 * aa_update_label_name - update a label to have a stored name
1431 * @ns: ns being viewed from (NOT NULL)
1432 * @label: label to update (NOT NULL)
1433 * @gfp: type of memory allocation
1434 *
1435 * Requires: labels_set(label) not locked in caller
1436 *
1437 * note: only updates the label name if it does not have a name already
1438 * and if it is in the labelset
1439 */
1440 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp)
1441 {
1442 struct aa_labelset *ls;
1443 unsigned long flags;
1444 char __counted *name;
1445 bool res = false;
1446
1447 AA_BUG(!ns);
1448 AA_BUG(!label);
1449
1450 if (label->hname || labels_ns(label) != ns)
1451 return res;
1452
1453 if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) == -1)
1454 return res;
1455
1456 ls = labels_set(label);
1457 write_lock_irqsave(&ls->lock, flags);
1458 if (!label->hname && label->flags & FLAG_IN_TREE) {
1459 label->hname = name;
1460 res = true;
1461 } else
1462 aa_put_str(name);
1463 write_unlock_irqrestore(&ls->lock, flags);
1464
1465 return res;
1466 }
1467
1468 /* cached label name is present and visible
1469 * @label->hname only exists if label is namespace hierachical */
1470 static inline bool use_label_hname(struct aa_ns *ns, struct aa_label *label)
1471 {
1472 if (label->hname && labels_ns(label) == ns)
1473 return true;
1474
1475 return false;
1476 }
1477
1478 /* helper macro for snprint routines */
1479 #define update_for_len(total, len, size, str) \
1480 do { \
1481 AA_BUG(len < 0); \
1482 total += len; \
1483 len = min(len, size); \
1484 size -= len; \
1485 str += len; \
1486 } while (0)
1487
1488 /**
1489 * aa_profile_snxprint_profile - print a profile name to a buffer
1490 * @str: buffer to write to. (MAY BE NULL if @size == 0)
1491 * @size: size of buffer
1492 * @ns: namespace profile is being viewed from
1493 * @profile: profile to view (NOT NULL)
1494 * @flags: whether to include the mode string
1495 *
1496 * Returns: size of name written or would be written if larger than
1497 * available buffer
1498 *
1499 * Note: will not print anything if the profile is not visible
1500 */
1501 int aa_profile_snxprint(char *str, size_t size, struct aa_ns *ns,
1502 struct aa_profile *profile, int flags)
1503 {
1504 const char *ns_name = "";
1505
1506 AA_BUG(!str && size != 0);
1507 AA_BUG(!profile);
1508
1509 if (!ns)
1510 ns = profiles_ns(profile);
1511
1512 if (ns != profile->ns) {
1513 ns_name = aa_ns_name(ns, profile->ns, flags & FLAG_VIEW_SUBNS);
1514 if (ns_name == aa_hidden_ns_name) {
1515 if (flags & FLAG_HIDDEN_UNCONFINED)
1516 return snprintf(str, size, "%s", "unconfined");
1517 return snprintf(str, size, "%s", ns_name);
1518 }
1519 }
1520
1521 if ((flags & FLAG_SHOW_MODE) && profile != profile->ns->unconfined) {
1522 const char *modestr = aa_profile_mode_names[profile->mode];
1523 if (strlen(ns_name))
1524 return snprintf(str, size, ":%s://%s (%s)", ns_name,
1525 profile->base.hname, modestr);
1526 return snprintf(str, size, "%s (%s)", profile->base.hname,
1527 modestr);
1528 }
1529
1530 if (strlen(ns_name))
1531 return snprintf(str, size, ":%s://%s", ns_name,
1532 profile->base.hname);
1533 return snprintf(str, size, "%s", profile->base.hname);
1534 }
1535
1536 static const char *label_modename(struct aa_ns *ns, struct aa_label *label,
1537 int flags)
1538 {
1539 struct aa_profile *profile;
1540 struct label_it i;
1541 int mode = -1, count = 0;
1542
1543 label_for_each(i, label, profile) {
1544 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
1545 if (profile->mode == APPARMOR_UNCONFINED)
1546 /* special case unconfined so stacks with
1547 * unconfined don't report as mixed. ie.
1548 * profile_foo//&:ns1://unconfined (mixed)
1549 */
1550 continue;
1551 count++;
1552 if (mode == -1)
1553 mode = profile->mode;
1554 else if (mode != profile->mode)
1555 return "mixed";
1556 }
1557 }
1558
1559 if (count == 0)
1560 return "-";
1561 if (mode == -1)
1562 /* everything was unconfined */
1563 mode = APPARMOR_UNCONFINED;
1564
1565 return aa_profile_mode_names[mode];
1566 }
1567
1568 /* if any visible label is not unconfined the display_mode returns true */
1569 static inline bool display_mode(struct aa_ns *ns, struct aa_label *label,
1570 int flags)
1571 {
1572 if ((flags & FLAG_SHOW_MODE)) {
1573 struct aa_profile *profile;
1574 struct label_it i;
1575
1576 label_for_each(i, label, profile) {
1577 if (aa_ns_visible(ns, profile->ns,
1578 flags & FLAG_VIEW_SUBNS) &&
1579 profile != profile->ns->unconfined)
1580 return true;
1581 }
1582 /* only ns->unconfined in set of profiles in ns */
1583 return false;
1584 }
1585
1586 return false;
1587 }
1588
1589 /**
1590 * aa_label_snxprint - print a label name to a string buffer
1591 * @str: buffer to write to. (MAY BE NULL if @size == 0)
1592 * @size: size of buffer
1593 * @ns: namespace profile is being viewed from
1594 * @label: label to view (NOT NULL)
1595 * @flags: whether to include the mode string
1596 *
1597 * Returns: size of name written or would be written if larger than
1598 * available buffer
1599 *
1600 * Note: labels do not have to be strictly hierarchical to the ns as
1601 * objects may be shared across different namespaces and thus
1602 * pickup labeling from each ns. If a particular part of the
1603 * label is not visible it will just be excluded. And if none
1604 * of the label is visible "---" will be used.
1605 */
1606 int aa_label_snxprint(char *str, size_t size, struct aa_ns *ns,
1607 struct aa_label *label, int flags)
1608 {
1609 struct aa_profile *profile;
1610 struct label_it i;
1611 int count = 0, total = 0;
1612 size_t len;
1613
1614 AA_BUG(!str && size != 0);
1615 AA_BUG(!label);
1616
1617 if (!ns)
1618 ns = labels_ns(label);
1619
1620 label_for_each(i, label, profile) {
1621 if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) {
1622 if (count > 0) {
1623 len = snprintf(str, size, "//&");
1624 update_for_len(total, len, size, str);
1625 }
1626 len = aa_profile_snxprint(str, size, ns, profile,
1627 flags & FLAG_VIEW_SUBNS);
1628 update_for_len(total, len, size, str);
1629 count++;
1630 }
1631 }
1632
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);
1637 }
1638
1639 /* count == 1 && ... is for backwards compat where the mode
1640 * is not displayed for 'unconfined' in the current ns
1641 */
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);
1646 }
1647
1648 return total;
1649 }
1650 #undef update_for_len
1651
1652 /**
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
1659 *
1660 * Returns: size of name written or would be written if larger than
1661 * available buffer
1662 */
1663 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
1664 int flags, gfp_t gfp)
1665 {
1666 int size;
1667
1668 AA_BUG(!strp);
1669 AA_BUG(!label);
1670
1671 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1672 if (size < 0)
1673 return size;
1674
1675 *strp = kmalloc(size + 1, gfp);
1676 if (!*strp)
1677 return -ENOMEM;
1678 return aa_label_snxprint(*strp, size + 1, ns, label, flags);
1679 }
1680
1681 /**
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
1688 *
1689 * Returns: size of name written or would be written if larger than
1690 * available buffer
1691 */
1692 int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
1693 struct aa_label *label, int flags, gfp_t gfp)
1694 {
1695 int size;
1696
1697 AA_BUG(!strp);
1698 AA_BUG(!label);
1699
1700 size = aa_label_snxprint(NULL, 0, ns, label, flags);
1701 if (size < 0)
1702 return size;
1703
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);
1708 }
1709
1710
1711 void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
1712 struct aa_label *label, int flags, gfp_t gfp)
1713 {
1714 const char *str;
1715 char *name = NULL;
1716 int len;
1717
1718 AA_BUG(!ab);
1719 AA_BUG(!label);
1720
1721 if (!ns)
1722 ns = labels_ns(label);
1723
1724 if (!use_label_hname(ns, label) || display_mode(ns, label, flags)) {
1725 labelstats_inc(audit_name_alloc);
1726 len = aa_label_asxprint(&name, ns, label, flags, gfp);
1727 if (len == -1) {
1728 labelstats_inc(audit_name_fail);
1729 AA_DEBUG("label print error");
1730 return;
1731 }
1732 str = name;
1733 } else {
1734 str = (char *) label->hname;
1735 len = strlen(str);
1736 }
1737 if (audit_string_contains_control(str, len))
1738 audit_log_n_hex(ab, str, len);
1739 else
1740 audit_log_n_string(ab, str, len);
1741
1742 kfree(name);
1743 }
1744
1745 void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
1746 struct aa_label *label, int flags, gfp_t gfp)
1747 {
1748 AA_BUG(!f);
1749 AA_BUG(!label);
1750
1751 if (!ns)
1752 ns = labels_ns(label);
1753
1754 if (!use_label_hname(ns, label)) {
1755 char *str;
1756 int len;
1757
1758 labelstats_inc(seq_print_name_alloc);
1759 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1760 if (len == -1) {
1761 labelstats_inc(seq_print_name_fail);
1762 AA_DEBUG("label print error");
1763 return;
1764 }
1765 seq_printf(f, "%s", str);
1766 kfree(str);
1767 } else if (display_mode(ns, label, flags))
1768 seq_printf(f, "%s (%s)", label->hname,
1769 label_modename(ns, label, flags));
1770 else
1771 seq_printf(f, "%s", label->hname);
1772 }
1773
1774 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
1775 gfp_t gfp)
1776 {
1777 AA_BUG(!label);
1778
1779 if (!ns)
1780 ns = labels_ns(label);
1781
1782 if (!use_label_hname(ns, label)) {
1783 char *str;
1784 int len;
1785
1786 labelstats_inc(printk_name_alloc);
1787 len = aa_label_asxprint(&str, ns, label, flags, gfp);
1788 if (len == -1) {
1789 labelstats_inc(printk_name_fail);
1790 AA_DEBUG("label print error");
1791 return;
1792 }
1793 printk("%s", str);
1794 kfree(str);
1795 } else if (display_mode(ns, label, flags))
1796 printk("%s (%s)", label->hname,
1797 label_modename(ns, label, flags));
1798 else
1799 printk("%s", label->hname);
1800 }
1801
1802 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp)
1803 {
1804 struct aa_ns *ns = aa_get_current_ns();
1805 aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp);
1806 aa_put_ns(ns);
1807 }
1808
1809 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp)
1810 {
1811 struct aa_ns *ns = aa_get_current_ns();
1812 aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp);
1813 aa_put_ns(ns);
1814 }
1815
1816 void aa_label_printk(struct aa_label *label, gfp_t gfp)
1817 {
1818 struct aa_ns *ns = aa_get_current_ns();
1819 aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp);
1820 aa_put_ns(ns);
1821 }
1822
1823 static int label_count_str_entries(const char *str)
1824 {
1825 const char *split;
1826 int count = 1;
1827
1828 AA_BUG(!str);
1829
1830 for (split = strstr(str, "//&"); split; split = strstr(str, "//&")) {
1831 count++;
1832 str = split + 3;
1833 }
1834
1835 return count;
1836 }
1837
1838 /**
1839 * aa_label_parse - parse, validate and convert a text string to a label
1840 * @base: base label to use for lookups (NOT NULL)
1841 * @str: null terminated text string (NOT NULL)
1842 * @gfp: allocation type
1843 * @create: true if should create compound labels if they don't exist
1844 * @force_stack: true if should stack even if no leading &
1845 *
1846 * Returns: the matching refcounted label if present
1847 * else ERRPTR
1848 */
1849 struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
1850 gfp_t gfp, bool create, bool force_stack)
1851 {
1852 DEFINE_VEC(profile, vec);
1853 struct aa_label *label;
1854 int i, len, stack = 0, error;
1855 char *split;
1856
1857 AA_BUG(!base);
1858 AA_BUG(!str);
1859
1860 str = skip_spaces(str);
1861 len = label_count_str_entries(str);
1862 if (*str == '&' || force_stack) {
1863 /* stack on top of base */
1864 stack = base->size;
1865 len += stack;
1866 if (*str == '&')
1867 str++;
1868 }
1869 error = vec_setup(profile, vec, len, gfp);
1870 if (error)
1871 return ERR_PTR(error);
1872
1873 for (i = 0; i < stack; i++)
1874 vec[i] = aa_get_profile(base->vec[i]);
1875
1876 for (split = strstr(str, "//&"), i = stack; split && i < len; i++) {
1877 vec[i] = aa_fqlookupn_profile(base, str, split - str);
1878 if (!vec[i])
1879 goto fail;
1880 str = split + 3;
1881 split = strstr(str, "//&");
1882 }
1883 /* last element doesn't have a split so this should be the case but just to be safe */
1884 if (i < len) {
1885 vec[i] = aa_fqlookupn_profile(base, str, strlen(str));
1886 if (!vec[i])
1887 goto fail;
1888 }
1889 if (len == 1)
1890 /* no need to free vec as len < LOCAL_VEC_ENTRIES */
1891 return &vec[0]->label;
1892
1893 len -= aa_vec_unique(vec, len, VEC_FLAG_TERMINATE);
1894 /* TODO: deal with reference labels */
1895 if (len == 1) {
1896 label = aa_get_label(&vec[0]->label);
1897 goto out;
1898 }
1899
1900 if (create)
1901 label = aa_vec_find_or_create_label(vec, len, gfp);
1902 else
1903 label = vec_find(vec, len);
1904 if (!label)
1905 goto fail;
1906
1907 out:
1908 /* use adjusted len from after vec_unique, not original */
1909 vec_cleanup(profile, vec, len);
1910 return label;
1911
1912 fail:
1913 label = ERR_PTR(-ENOENT);
1914 goto out;
1915 }
1916
1917
1918 /**
1919 * aa_labelset_destroy - remove all labels from the label set
1920 * @ls: label set to cleanup (NOT NULL)
1921 *
1922 * Labels that are removed from the set may still exist beyond the set
1923 * being destroyed depending on their reference counting
1924 */
1925 void aa_labelset_destroy(struct aa_labelset *ls)
1926 {
1927 struct rb_node *node;
1928 unsigned long flags;
1929
1930 AA_BUG(!ls);
1931
1932 write_lock_irqsave(&ls->lock, flags);
1933 for (node = rb_first(&ls->root); node; node = rb_first(&ls->root)) {
1934 struct aa_label *this = rb_entry(node, struct aa_label, node);
1935 if (labels_ns(this) != root_ns)
1936 __label_remove(this,
1937 ns_unconfined(labels_ns(this)->parent));
1938 else
1939 __label_remove(this, NULL);
1940 }
1941 write_unlock_irqrestore(&ls->lock, flags);
1942 }
1943
1944 /*
1945 * @ls: labelset to init (NOT NULL)
1946 */
1947 void aa_labelset_init(struct aa_labelset *ls)
1948 {
1949 AA_BUG(!ls);
1950
1951 rwlock_init(&ls->lock);
1952 ls->root = RB_ROOT;
1953 labelstats_init(&ls);
1954 }
1955
1956 static struct aa_label *labelset_next_stale(struct aa_labelset *ls)
1957 {
1958 struct aa_label *label;
1959 struct rb_node *node;
1960 unsigned long flags;
1961
1962 AA_BUG(!ls);
1963
1964 read_lock_irqsave(&ls->lock, flags);
1965
1966 __labelset_for_each(ls, node) {
1967 label = rb_entry(node, struct aa_label, node);
1968 if ((label_is_stale(label) || vec_is_stale(label->vec, label->size)) &&
1969 aa_get_label_not0(label))
1970 goto out;
1971
1972 }
1973 label = NULL;
1974
1975 out:
1976 read_unlock_irqrestore(&ls->lock, flags);
1977
1978 return label;
1979 }
1980
1981 /**
1982 * __label_update - insert updated version of @label into labelset
1983 * @label - the label to update/repace
1984 *
1985 * Returns: new label that is up to date
1986 * else NULL on failure
1987 *
1988 * Requires: @ns lock be held
1989 *
1990 * Note: worst case is the stale @label does not get updated and has
1991 * to be updated at a later time.
1992 */
1993 static struct aa_label *__label_update(struct aa_label *label)
1994 {
1995 struct aa_label *new, *tmp;
1996 struct aa_labelset *ls;
1997 unsigned long flags;
1998 int i, invcount = 0;
1999
2000 AA_BUG(!label);
2001 AA_BUG(!mutex_is_locked(&labels_ns(label)->lock));
2002
2003 new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL);
2004 if (!new)
2005 return NULL;
2006
2007 /* while holding the ns_lock will stop profile replacement, removal,
2008 * and label updates, label merging and removal can be occuring
2009 */
2010 ls = labels_set(label);
2011 write_lock_irqsave(&ls->lock, flags);
2012 for (i = 0; i < label->size; i++) {
2013 AA_BUG(!label->vec[i]);
2014 new->vec[i] = aa_get_newest_profile(label->vec[i]);
2015 AA_BUG(!new->vec[i]);
2016 AA_BUG(!new->vec[i]->label.proxy);
2017 AA_BUG(!new->vec[i]->label.proxy->label);
2018 if (new->vec[i]->label.proxy != label->vec[i]->label.proxy)
2019 invcount++;
2020 }
2021
2022 /* updated stale label by being removed/renamed from labelset */
2023 if (invcount) {
2024 new->size -= aa_vec_unique(&new->vec[0], new->size,
2025 VEC_FLAG_TERMINATE);
2026 /* TODO: deal with reference labels */
2027 if (new->size == 1) {
2028 tmp = aa_get_label(&new->vec[0]->label);
2029 AA_BUG(tmp == label);
2030 goto remove;
2031 }
2032 if (labels_set(label) != labels_set(new)) {
2033 write_unlock_irqrestore(&ls->lock, flags);
2034 tmp = aa_label_insert(labels_set(new), new);
2035 write_lock_irqsave(&ls->lock, flags);
2036 goto remove;
2037 }
2038 } else
2039 AA_BUG(labels_ns(label) != labels_ns(new));
2040
2041 tmp = __label_insert(labels_set(label), new, true);
2042 remove:
2043 /* ensure label is removed, and redirected correctly */
2044 __label_remove(label, tmp);
2045 write_unlock_irqrestore(&ls->lock, flags);
2046
2047 aa_put_label(new);
2048
2049 return tmp;
2050 }
2051
2052 /**
2053 * __labelset_update - update labels in @ns
2054 * @ns: namespace to update labels in (NOT NULL)
2055 *
2056 * Requires: @ns lock be held
2057 *
2058 * Walk the labelset ensuring that all labels are up to date and valid
2059 * Any label that has a stale component is marked stale and replaced and
2060 * by an updated version.
2061 *
2062 * If failures happen due to memory pressures then stale labels will
2063 * be left in place until the next pass.
2064 */
2065 static void __labelset_update(struct aa_ns *ns)
2066 {
2067 struct aa_label *label;
2068
2069 AA_BUG(!ns);
2070 AA_BUG(!mutex_is_locked(&ns->lock));
2071
2072 do {
2073 label = labelset_next_stale(&ns->labels);
2074 if (label) {
2075 struct aa_label *l;
2076 l = __label_update(label);
2077 aa_put_label(l);
2078 aa_put_label(label);
2079 }
2080 } while (label);
2081 }
2082
2083 /**
2084 * __aa_labelset_udate_subtree - update all labels with a stale component
2085 * @ns: ns to start update at (NOT NULL)
2086 *
2087 * Requires: @ns lock be held
2088 *
2089 * Invalidates labels based on @p in @ns and any children namespaces.
2090 */
2091 void __aa_labelset_update_subtree(struct aa_ns *ns)
2092 {
2093 struct aa_ns *child;
2094
2095 AA_BUG(!ns);
2096 AA_BUG(!mutex_is_locked(&ns->lock));
2097
2098 __labelset_update(ns);
2099
2100 list_for_each_entry(child, &ns->sub_ns, base.list) {
2101 mutex_lock(&child->lock);
2102 __aa_labelset_update_subtree(child);
2103 mutex_unlock(&child->lock);
2104 }
2105 }