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