]>
Commit | Line | Data |
---|---|---|
80594fc2 JJ |
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.name); | |
116 | AA_BUG(!b->base.name); | |
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.name, b->base.name); | |
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; 1 < 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 | if (profile_is_stale(next)) { | |
1065 | new->vec[k] = aa_get_newest_profile(next); | |
1066 | if (next->label.proxy != new->vec[k]->label.proxy) | |
1067 | invcount++; | |
1068 | k++; | |
1069 | stale = true; | |
1070 | } else | |
1071 | new->vec[k++] = aa_get_profile(next); | |
1072 | } | |
1073 | /* set to actual size which is <= allocated len */ | |
1074 | new->size = k; | |
1075 | new->vec[k] = NULL; | |
1076 | ||
1077 | if (invcount) { | |
1078 | new->size -= aa_vec_unique(&new->vec[0], new->size, | |
1079 | VEC_FLAG_TERMINATE); | |
1080 | } else if (!stale) { | |
1081 | /* merge could be same as a || b, note: it is not possible | |
1082 | * for new->size == a->size == b->size unless a == b */ | |
1083 | if (k == a->size) | |
1084 | return aa_get_label(a); | |
1085 | else if (k == b->size) | |
1086 | return aa_get_label(b); | |
1087 | } | |
1088 | if (vec_unconfined(new->vec, new->size)) | |
1089 | new->flags |= FLAG_UNCONFINED; | |
1090 | ls = labels_set(new); | |
1091 | write_lock_irqsave(&ls->lock, flags); | |
1092 | label = __label_insert(labels_set(new), new, false); | |
1093 | write_unlock_irqrestore(&ls->lock, flags); | |
1094 | ||
1095 | return label; | |
1096 | } | |
1097 | ||
1098 | /** | |
1099 | * labelset_of_merge - find into which labelset a merged label should be inserted | |
1100 | * @a: label to merge and insert | |
1101 | * @b: label to merge and insert | |
1102 | * | |
1103 | * Returns: labelset that the merged label should be inserted into | |
1104 | */ | |
1105 | static struct aa_labelset *labelset_of_merge(struct aa_label *a, struct aa_label *b) | |
1106 | { | |
1107 | struct aa_ns *nsa = labels_ns(a); | |
1108 | struct aa_ns *nsb = labels_ns(b); | |
1109 | ||
1110 | if (ns_cmp(nsa, nsb) <= 0) | |
1111 | return &nsa->labels; | |
1112 | return &nsb->labels; | |
1113 | } | |
1114 | ||
1115 | /** | |
1116 | * __label_find_merge - find label that is equiv to merge of @a and @b | |
1117 | * @ls: set of labels to search (NOT NULL) | |
1118 | * @a: label to merge with @b (NOT NULL) | |
1119 | * @b: label to merge with @a (NOT NULL) | |
1120 | * | |
1121 | * Requires: ls->lock read_lock held | |
1122 | * | |
1123 | * Returns: ref counted label that is equiv to merge of @a and @b | |
1124 | * else NULL if merge of @a and @b is not in set | |
1125 | */ | |
1126 | static struct aa_label *__label_find_merge(struct aa_labelset *ls, | |
1127 | struct aa_label *a, | |
1128 | struct aa_label *b) | |
1129 | { | |
1130 | struct rb_node *node; | |
1131 | ||
1132 | AA_BUG(!ls); | |
1133 | AA_BUG(!a); | |
1134 | AA_BUG(!b); | |
1135 | ||
1136 | if (a == b) | |
1137 | return __label_find(a); | |
1138 | ||
1139 | node = ls->root.rb_node; | |
1140 | while (node) { | |
1141 | struct aa_label *this = container_of(node, struct aa_label, | |
1142 | node); | |
1143 | int result = label_merge_cmp(a, b, this); | |
1144 | ||
1145 | if (result < 0) | |
1146 | node = node->rb_left; | |
1147 | else if (result > 0) | |
1148 | node = node->rb_right; | |
1149 | else | |
1150 | return aa_get_label_not0(this); | |
1151 | } | |
1152 | ||
1153 | return NULL; | |
1154 | } | |
1155 | ||
1156 | ||
1157 | /** | |
1158 | * aa_label_find_merge - find label that is equiv to merge of @a and @b | |
1159 | * @a: label to merge with @b (NOT NULL) | |
1160 | * @b: label to merge with @a (NOT NULL) | |
1161 | * | |
1162 | * Requires: labels be fully constructed with a valid ns | |
1163 | * | |
1164 | * Returns: ref counted label that is equiv to merge of @a and @b | |
1165 | * else NULL if merge of @a and @b is not in set | |
1166 | */ | |
1167 | struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b) | |
1168 | { | |
1169 | struct aa_labelset *ls; | |
1170 | struct aa_label *label, *ar = NULL, *br = NULL; | |
1171 | unsigned long flags; | |
1172 | ||
1173 | AA_BUG(!a); | |
1174 | AA_BUG(!b); | |
1175 | ||
1176 | if (label_is_stale(a)) | |
1177 | a = ar = aa_get_newest_label(a); | |
1178 | if (label_is_stale(b)) | |
1179 | b = br = aa_get_newest_label(b); | |
1180 | ls = labelset_of_merge(a, b); | |
1181 | read_lock_irqsave(&ls->lock, flags); | |
1182 | label = __label_find_merge(ls, a, b); | |
1183 | read_unlock_irqrestore(&ls->lock, flags); | |
1184 | aa_put_label(ar); | |
1185 | aa_put_label(br); | |
1186 | labelsetstats_inc(ls, msread); | |
1187 | ||
1188 | return label; | |
1189 | } | |
1190 | ||
1191 | /** | |
1192 | * aa_label_merge - attempt to insert new merged label of @a and @b | |
1193 | * @ls: set of labels to insert label into (NOT NULL) | |
1194 | * @a: label to merge with @b (NOT NULL) | |
1195 | * @b: label to merge with @a (NOT NULL) | |
1196 | * @gfp: memory allocation type | |
1197 | * | |
1198 | * Requires: caller to hold valid refs on @a and @b | |
1199 | * labels be fully constructed with a valid ns | |
1200 | * | |
1201 | * Returns: ref counted new label if successful in inserting merge of a & b | |
1202 | * else ref counted equivalent label that is already in the set. | |
1203 | * else NULL if could not create label (-ENOMEM) | |
1204 | */ | |
1205 | struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b, | |
1206 | gfp_t gfp) | |
1207 | { | |
1208 | struct aa_label *label = NULL; | |
1209 | ||
1210 | AA_BUG(!a); | |
1211 | AA_BUG(!b); | |
1212 | ||
1213 | if (a == b) | |
1214 | return aa_get_newest_label(a); | |
1215 | ||
1216 | /* TODO: enable when read side is lockless | |
1217 | * check if label exists before taking locks | |
1218 | if (!label_is_stale(a) && !label_is_stale(b)) | |
1219 | label = aa_label_find_merge(a, b); | |
1220 | */ | |
1221 | ||
1222 | if (!label) { | |
1223 | struct aa_label *new; | |
1224 | ||
1225 | a = aa_get_newest_label(a); | |
1226 | b = aa_get_newest_label(b); | |
1227 | ||
1228 | /* could use label_merge_len(a, b), but requires double | |
1229 | * comparison for small savings | |
1230 | */ | |
1231 | new = aa_label_alloc(a->size + b->size, NULL, gfp); | |
1232 | if (!new) | |
1233 | goto out; | |
1234 | ||
1235 | label = label_merge_insert(new, a, b); | |
1236 | aa_put_label(new); | |
1237 | out: | |
1238 | aa_put_label(a); | |
1239 | aa_put_label(b); | |
1240 | } | |
1241 | ||
1242 | return label; | |
1243 | } | |
1244 | ||
1245 | static inline bool label_is_visible(struct aa_profile *profile, | |
1246 | struct aa_label *label) | |
1247 | { | |
1248 | return aa_ns_visible(profile->ns, labels_ns(label), true); | |
1249 | } | |
1250 | ||
1251 | /* match a profile and its associated ns component if needed | |
1252 | * Assumes visibility test has already been done. | |
1253 | * If a subns profile is not to be matched should be prescreened with | |
1254 | * visibility test. | |
1255 | */ | |
1256 | static inline unsigned int match_component(struct aa_profile *profile, | |
1257 | struct aa_profile *tp, | |
1258 | unsigned int state) | |
1259 | { | |
1260 | const char *ns_name; | |
1261 | ||
1262 | if (profile->ns == tp->ns) | |
1263 | return aa_dfa_match(profile->policy.dfa, state, tp->base.hname); | |
1264 | ||
1265 | /* try matching with namespace name and then profile */ | |
1266 | ns_name = aa_ns_name(profile->ns, tp->ns, true); | |
1267 | state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1); | |
1268 | state = aa_dfa_match(profile->policy.dfa, state, ns_name); | |
1269 | state = aa_dfa_match_len(profile->policy.dfa, state, ":", 1); | |
1270 | return aa_dfa_match(profile->policy.dfa, state, tp->base.hname); | |
1271 | } | |
1272 | ||
1273 | /** | |
1274 | * label_component_match - find perms for full compound label | |
1275 | * @profile: profile to find perms for | |
1276 | * @label: label to check access permissions for | |
1277 | * @start: state to start match in | |
1278 | * @subns: whether to do permission checks on components in a subns | |
1279 | * @request: permissions to request | |
1280 | * @perms: perms struct to set | |
1281 | * | |
1282 | * Returns: 0 on success else ERROR | |
1283 | * | |
1284 | * For the label A//&B//&C this does the perm match for A//&B//&C | |
1285 | * @perms should be preinitialized with allperms OR a previous permission | |
1286 | * check to be stacked. | |
1287 | */ | |
1288 | static int label_compound_match(struct aa_profile *profile, | |
1289 | struct aa_label *label, | |
1290 | unsigned int state, bool subns, u32 request, | |
1291 | struct aa_perms *perms) | |
1292 | { | |
1293 | struct aa_profile *tp; | |
1294 | struct label_it i; | |
1295 | ||
1296 | /* find first subcomponent that is visible */ | |
1297 | label_for_each(i, label, tp) { | |
1298 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
1299 | continue; | |
1300 | state = match_component(profile, tp, state); | |
1301 | if (!state) | |
1302 | goto fail; | |
1303 | goto next; | |
1304 | } | |
1305 | ||
1306 | /* no component visible */ | |
1307 | *perms = allperms; | |
1308 | return 0; | |
1309 | ||
1310 | next: | |
1311 | label_for_each_cont(i, label, tp) { | |
1312 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
1313 | continue; | |
1314 | state = aa_dfa_match(profile->policy.dfa, state, "//&"); | |
1315 | state = match_component(profile, tp, state); | |
1316 | if (!state) | |
1317 | goto fail; | |
1318 | } | |
1319 | aa_compute_perms(profile->policy.dfa, state, perms); | |
1320 | aa_apply_modes_to_perms(profile, perms); | |
1321 | if ((perms->allow & request) != request) | |
1322 | return -EACCES; | |
1323 | ||
1324 | return 0; | |
1325 | ||
1326 | fail: | |
1327 | *perms = nullperms; | |
1328 | return state; | |
1329 | } | |
1330 | ||
1331 | /** | |
1332 | * label_component_match - find perms for all subcomponents of a label | |
1333 | * @profile: profile to find perms for | |
1334 | * @label: label to check access permissions for | |
1335 | * @start: state to start match in | |
1336 | * @subns: whether to do permission checks on components in a subns | |
1337 | * @request: permissions to request | |
1338 | * @perms: an initialized perms struct to add accumulation to | |
1339 | * | |
1340 | * Returns: 0 on success else ERROR | |
1341 | * | |
1342 | * For the label A//&B//&C this does the perm match for each of A and B and C | |
1343 | * @perms should be preinitialized with allperms OR a previous permission | |
1344 | * check to be stacked. | |
1345 | */ | |
1346 | static int label_components_match(struct aa_profile *profile, | |
1347 | struct aa_label *label, unsigned int start, | |
1348 | bool subns, u32 request, | |
1349 | struct aa_perms *perms) | |
1350 | { | |
1351 | struct aa_profile *tp; | |
1352 | struct label_it i; | |
1353 | struct aa_perms tmp; | |
1354 | unsigned int state = 0; | |
1355 | ||
1356 | /* find first subcomponent to test */ | |
1357 | label_for_each(i, label, tp) { | |
1358 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
1359 | continue; | |
1360 | state = match_component(profile, tp, start); | |
1361 | if (!state) | |
1362 | goto fail; | |
1363 | goto next; | |
1364 | } | |
1365 | ||
1366 | /* no subcomponents visible - no change in perms */ | |
1367 | return 0; | |
1368 | ||
1369 | next: | |
1370 | aa_compute_perms(profile->policy.dfa, state, &tmp); | |
1371 | aa_apply_modes_to_perms(profile, &tmp); | |
1372 | aa_perms_accum(perms, &tmp); | |
1373 | label_for_each_cont(i, label, tp) { | |
1374 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
1375 | continue; | |
1376 | state = match_component(profile, tp, start); | |
1377 | if (!state) | |
1378 | goto fail; | |
1379 | aa_compute_perms(profile->policy.dfa, state, &tmp); | |
1380 | aa_apply_modes_to_perms(profile, &tmp); | |
1381 | aa_perms_accum(perms, &tmp); | |
1382 | } | |
1383 | ||
1384 | if ((perms->allow & request) != request) | |
1385 | return -EACCES; | |
1386 | ||
1387 | return 0; | |
1388 | ||
1389 | fail: | |
1390 | *perms = nullperms; | |
1391 | return -EACCES; | |
1392 | } | |
1393 | ||
1394 | /** | |
1395 | * aa_label_match - do a multi-component label match | |
1396 | * @profile: profile to match against (NOT NULL) | |
1397 | * @label: label to match (NOT NULL) | |
1398 | * @state: state to start in | |
1399 | * @subns: whether to match subns components | |
1400 | * @request: permission request | |
1401 | * @perms: Returns computed perms (NOT NULL) | |
1402 | * | |
1403 | * Returns: the state the match finished in, may be the none matching state | |
1404 | */ | |
1405 | int aa_label_match(struct aa_profile *profile, struct aa_label *label, | |
1406 | unsigned int state, bool subns, u32 request, | |
1407 | struct aa_perms *perms) | |
1408 | { | |
1409 | int error = label_compound_match(profile, label, state, subns, request, | |
1410 | perms); | |
1411 | if (!error) | |
1412 | return error; | |
1413 | ||
1414 | *perms = allperms; | |
1415 | return label_components_match(profile, label, state, subns, request, | |
1416 | perms); | |
1417 | } | |
1418 | ||
1419 | ||
1420 | /** | |
1421 | * aa_update_label_name - update a label to have a stored name | |
1422 | * @ns: ns being viewed from (NOT NULL) | |
1423 | * @label: label to update (NOT NULL) | |
1424 | * @gfp: type of memory allocation | |
1425 | * | |
1426 | * Requires: labels_set(label) not locked in caller | |
1427 | * | |
1428 | * note: only updates the label name if it does not have a name already | |
1429 | * and if it is in the labelset | |
1430 | */ | |
1431 | bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp) | |
1432 | { | |
1433 | struct aa_labelset *ls; | |
1434 | unsigned long flags; | |
1435 | char __counted *name; | |
1436 | bool res = false; | |
1437 | ||
1438 | AA_BUG(!ns); | |
1439 | AA_BUG(!label); | |
1440 | ||
1441 | if (label->hname || labels_ns(label) != ns) | |
1442 | return res; | |
1443 | ||
1444 | if (aa_label_acntsxprint(&name, ns, label, FLAGS_NONE, gfp) == -1) | |
1445 | return res; | |
1446 | ||
1447 | ls = labels_set(label); | |
1448 | write_lock_irqsave(&ls->lock, flags); | |
1449 | if (!label->hname && label->flags & FLAG_IN_TREE) { | |
1450 | label->hname = name; | |
1451 | res = true; | |
1452 | } else | |
1453 | aa_put_str(name); | |
1454 | write_unlock_irqrestore(&ls->lock, flags); | |
1455 | ||
1456 | return res; | |
1457 | } | |
1458 | ||
1459 | /* cached label name is present and visible | |
1460 | * @label->hname only exists if label is namespace hierachical */ | |
1461 | static inline bool use_label_hname(struct aa_ns *ns, struct aa_label *label) | |
1462 | { | |
1463 | if (label->hname && labels_ns(label) == ns) | |
1464 | return true; | |
1465 | ||
1466 | return false; | |
1467 | } | |
1468 | ||
1469 | /* helper macro for snprint routines */ | |
1470 | #define update_for_len(total, len, size, str) \ | |
1471 | do { \ | |
1472 | AA_BUG(len < 0); \ | |
1473 | total += len; \ | |
1474 | len = min(len, size); \ | |
1475 | size -= len; \ | |
1476 | str += len; \ | |
1477 | } while (0) | |
1478 | ||
1479 | /** | |
1480 | * aa_profile_snxprint_profile - print a profile name to a buffer | |
1481 | * @str: buffer to write to. (MAY BE NULL if @size == 0) | |
1482 | * @size: size of buffer | |
1483 | * @ns: namespace profile is being viewed from | |
1484 | * @profile: profile to view (NOT NULL) | |
1485 | * @flags: whether to include the mode string | |
1486 | * | |
1487 | * Returns: size of name written or would be written if larger than | |
1488 | * available buffer | |
1489 | * | |
1490 | * Note: will not print anything if the profile is not visible | |
1491 | */ | |
1492 | int aa_profile_snxprint(char *str, size_t size, struct aa_ns *ns, | |
1493 | struct aa_profile *profile, int flags) | |
1494 | { | |
1495 | const char *ns_name = ""; | |
1496 | ||
1497 | AA_BUG(!str && size != 0); | |
1498 | AA_BUG(!profile); | |
1499 | ||
1500 | if (!ns) | |
1501 | ns = profiles_ns(profile); | |
1502 | ||
1503 | if (ns != profile->ns) { | |
1504 | ns_name = aa_ns_name(ns, profile->ns, flags & FLAG_VIEW_SUBNS); | |
1505 | if (ns_name == aa_hidden_ns_name) { | |
1506 | if (flags & FLAG_HIDDEN_UNCONFINED) | |
1507 | return snprintf(str, size, "%s", "unconfined"); | |
1508 | return snprintf(str, size, "%s", ns_name); | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | if ((flags & FLAG_SHOW_MODE) && profile != profile->ns->unconfined) { | |
1513 | const char *modestr = aa_profile_mode_names[profile->mode]; | |
1514 | if (strlen(ns_name)) | |
1515 | return snprintf(str, size, ":%s://%s (%s)", ns_name, | |
1516 | profile->base.hname, modestr); | |
1517 | return snprintf(str, size, "%s (%s)", profile->base.hname, | |
1518 | modestr); | |
1519 | } | |
1520 | ||
1521 | if (strlen(ns_name)) | |
1522 | return snprintf(str, size, ":%s://%s", ns_name, | |
1523 | profile->base.hname); | |
1524 | return snprintf(str, size, "%s", profile->base.hname); | |
1525 | } | |
1526 | ||
1527 | static const char *label_modename(struct aa_ns *ns, struct aa_label *label, | |
1528 | int flags) | |
1529 | { | |
1530 | struct aa_profile *profile; | |
1531 | struct label_it i; | |
1532 | const char *modestr = NULL; | |
1533 | int count = 0; | |
1534 | ||
1535 | label_for_each(i, label, profile) { | |
1536 | if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) { | |
1537 | const char *tmp_modestr; | |
1538 | count++; | |
1539 | tmp_modestr = aa_profile_mode_names[profile->mode]; | |
1540 | if (!modestr) | |
1541 | modestr = tmp_modestr; | |
1542 | else if (modestr != tmp_modestr) | |
1543 | return "mixed"; | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | if (count == 0) | |
1548 | return "-"; | |
1549 | ||
1550 | return modestr; | |
1551 | } | |
1552 | ||
1553 | /* if any visible label is not unconfined the display_mode returns true */ | |
1554 | static inline bool display_mode(struct aa_ns *ns, struct aa_label *label, | |
1555 | int flags) | |
1556 | { | |
1557 | if ((flags & FLAG_SHOW_MODE)) { | |
1558 | struct aa_profile *profile; | |
1559 | struct label_it i; | |
1560 | ||
1561 | label_for_each(i, label, profile) { | |
1562 | if (aa_ns_visible(ns, profile->ns, | |
1563 | flags & FLAG_VIEW_SUBNS) && | |
1564 | profile != profile->ns->unconfined) | |
1565 | return true; | |
1566 | } | |
1567 | /* only ns->unconfined in set of profiles in ns */ | |
1568 | return false; | |
1569 | } | |
1570 | ||
1571 | return false; | |
1572 | } | |
1573 | ||
1574 | /** | |
1575 | * aa_label_snxprint - print a label name to a string buffer | |
1576 | * @str: buffer to write to. (MAY BE NULL if @size == 0) | |
1577 | * @size: size of buffer | |
1578 | * @ns: namespace profile is being viewed from | |
1579 | * @label: label to view (NOT NULL) | |
1580 | * @flags: whether to include the mode string | |
1581 | * | |
1582 | * Returns: size of name written or would be written if larger than | |
1583 | * available buffer | |
1584 | * | |
1585 | * Note: labels do not have to be strictly hierarchical to the ns as | |
1586 | * objects may be shared across different namespaces and thus | |
1587 | * pickup labeling from each ns. If a particular part of the | |
1588 | * label is not visible it will just be excluded. And if none | |
1589 | * of the label is visible "---" will be used. | |
1590 | */ | |
1591 | int aa_label_snxprint(char *str, size_t size, struct aa_ns *ns, | |
1592 | struct aa_label *label, int flags) | |
1593 | { | |
1594 | struct aa_profile *profile; | |
1595 | struct label_it i; | |
1596 | int count = 0, total = 0; | |
1597 | size_t len; | |
1598 | ||
1599 | AA_BUG(!str && size != 0); | |
1600 | AA_BUG(!label); | |
1601 | ||
1602 | if (!ns) | |
1603 | ns = labels_ns(label); | |
1604 | ||
1605 | label_for_each(i, label, profile) { | |
1606 | if (aa_ns_visible(ns, profile->ns, flags & FLAG_VIEW_SUBNS)) { | |
1607 | if (count > 0) { | |
1608 | len = snprintf(str, size, "//&"); | |
1609 | update_for_len(total, len, size, str); | |
1610 | } | |
1611 | len = aa_profile_snxprint(str, size, ns, profile, | |
1612 | flags & FLAG_VIEW_SUBNS); | |
1613 | update_for_len(total, len, size, str); | |
1614 | count++; | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | if (count == 0) { | |
1619 | if (flags & FLAG_HIDDEN_UNCONFINED) | |
1620 | return snprintf(str, size, "%s", "unconfined"); | |
1621 | return snprintf(str, size, "%s", aa_hidden_ns_name); | |
1622 | } | |
1623 | ||
1624 | /* count == 1 && ... is for backwards compat where the mode | |
1625 | * is not displayed for 'unconfined' in the current ns | |
1626 | */ | |
1627 | if (display_mode(ns, label, flags)) { | |
1628 | len = snprintf(str, size, " (%s)", | |
1629 | label_modename(ns, label, flags)); | |
1630 | update_for_len(total, len, size, str); | |
1631 | } | |
1632 | ||
1633 | return total; | |
1634 | } | |
1635 | #undef update_for_len | |
1636 | ||
1637 | /** | |
1638 | * aa_label_asxprint - allocate a string buffer and print label into it | |
1639 | * @strp: Returns - the allocated buffer with the label name. (NOT NULL) | |
1640 | * @ns: namespace profile is being viewed from | |
1641 | * @label: label to view (NOT NULL) | |
1642 | * @flags: flags controlling what label info is printed | |
1643 | * @gfp: kernel memory allocation type | |
1644 | * | |
1645 | * Returns: size of name written or would be written if larger than | |
1646 | * available buffer | |
1647 | */ | |
1648 | int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label, | |
1649 | int flags, gfp_t gfp) | |
1650 | { | |
1651 | int size; | |
1652 | ||
1653 | AA_BUG(!strp); | |
1654 | AA_BUG(!label); | |
1655 | ||
1656 | size = aa_label_snxprint(NULL, 0, ns, label, flags); | |
1657 | if (size < 0) | |
1658 | return size; | |
1659 | ||
1660 | *strp = kmalloc(size + 1, gfp); | |
1661 | if (!*strp) | |
1662 | return -ENOMEM; | |
1663 | return aa_label_snxprint(*strp, size + 1, ns, label, flags); | |
1664 | } | |
1665 | ||
1666 | /** | |
1667 | * aa_label_acntsxprint - allocate a __counted string buffer and print label | |
1668 | * @strp: buffer to write to. (MAY BE NULL if @size == 0) | |
1669 | * @ns: namespace profile is being viewed from | |
1670 | * @label: label to view (NOT NULL) | |
1671 | * @flags: flags controlling what label info is printed | |
1672 | * @gfp: kernel memory allocation type | |
1673 | * | |
1674 | * Returns: size of name written or would be written if larger than | |
1675 | * available buffer | |
1676 | */ | |
1677 | int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns, | |
1678 | struct aa_label *label, int flags, gfp_t gfp) | |
1679 | { | |
1680 | int size; | |
1681 | ||
1682 | AA_BUG(!strp); | |
1683 | AA_BUG(!label); | |
1684 | ||
1685 | size = aa_label_snxprint(NULL, 0, ns, label, flags); | |
1686 | if (size < 0) | |
1687 | return size; | |
1688 | ||
1689 | *strp = aa_str_alloc(size + 1, gfp); | |
1690 | if (!*strp) | |
1691 | return -ENOMEM; | |
1692 | return aa_label_snxprint(*strp, size + 1, ns, label, flags); | |
1693 | } | |
1694 | ||
1695 | ||
1696 | void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns, | |
1697 | struct aa_label *label, int flags, gfp_t gfp) | |
1698 | { | |
1699 | const char *str; | |
1700 | char *name = NULL; | |
1701 | int len; | |
1702 | ||
1703 | AA_BUG(!ab); | |
1704 | AA_BUG(!label); | |
1705 | ||
1706 | if (!ns) | |
1707 | ns = labels_ns(label); | |
1708 | ||
1709 | if (!use_label_hname(ns, label) || display_mode(ns, label, flags)) { | |
1710 | labelstats_inc(audit_name_alloc); | |
1711 | len = aa_label_asxprint(&name, ns, label, flags, gfp); | |
1712 | if (len == -1) { | |
1713 | labelstats_inc(audit_name_fail); | |
1714 | AA_DEBUG("label print error"); | |
1715 | return; | |
1716 | } | |
1717 | str = name; | |
1718 | } else { | |
1719 | str = (char *) label->hname; | |
1720 | len = strlen(str); | |
1721 | } | |
1722 | if (audit_string_contains_control(str, len)) | |
1723 | audit_log_n_hex(ab, str, len); | |
1724 | else | |
1725 | audit_log_n_string(ab, str, len); | |
1726 | ||
1727 | kfree(name); | |
1728 | } | |
1729 | ||
1730 | void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns, | |
1731 | struct aa_label *label, int flags, gfp_t gfp) | |
1732 | { | |
1733 | AA_BUG(!f); | |
1734 | AA_BUG(!label); | |
1735 | ||
1736 | if (!ns) | |
1737 | ns = labels_ns(label); | |
1738 | ||
1739 | if (!use_label_hname(ns, label)) { | |
1740 | char *str; | |
1741 | int len; | |
1742 | ||
1743 | labelstats_inc(seq_print_name_alloc); | |
1744 | len = aa_label_asxprint(&str, ns, label, flags, gfp); | |
1745 | if (len == -1) { | |
1746 | labelstats_inc(seq_print_name_fail); | |
1747 | AA_DEBUG("label print error"); | |
1748 | return; | |
1749 | } | |
1750 | seq_printf(f, "%s", str); | |
1751 | kfree(str); | |
1752 | } else if (display_mode(ns, label, flags)) | |
1753 | seq_printf(f, "%s (%s)", label->hname, | |
1754 | label_modename(ns, label, flags)); | |
1755 | else | |
1756 | seq_printf(f, "%s", label->hname); | |
1757 | } | |
1758 | ||
1759 | void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags, | |
1760 | gfp_t gfp) | |
1761 | { | |
1762 | AA_BUG(!label); | |
1763 | ||
1764 | if (!ns) | |
1765 | ns = labels_ns(label); | |
1766 | ||
1767 | if (!use_label_hname(ns, label)) { | |
1768 | char *str; | |
1769 | int len; | |
1770 | ||
1771 | labelstats_inc(printk_name_alloc); | |
1772 | len = aa_label_asxprint(&str, ns, label, flags, gfp); | |
1773 | if (len == -1) { | |
1774 | labelstats_inc(printk_name_fail); | |
1775 | AA_DEBUG("label print error"); | |
1776 | return; | |
1777 | } | |
1778 | printk("%s", str); | |
1779 | kfree(str); | |
1780 | } else if (display_mode(ns, label, flags)) | |
1781 | printk("%s (%s)", label->hname, | |
1782 | label_modename(ns, label, flags)); | |
1783 | else | |
1784 | printk("%s", label->hname); | |
1785 | } | |
1786 | ||
1787 | void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp) | |
1788 | { | |
1789 | struct aa_ns *ns = aa_get_current_ns(); | |
1790 | aa_label_xaudit(ab, ns, label, FLAG_VIEW_SUBNS, gfp); | |
1791 | aa_put_ns(ns); | |
1792 | } | |
1793 | ||
1794 | void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp) | |
1795 | { | |
1796 | struct aa_ns *ns = aa_get_current_ns(); | |
1797 | aa_label_seq_xprint(f, ns, label, FLAG_VIEW_SUBNS, gfp); | |
1798 | aa_put_ns(ns); | |
1799 | } | |
1800 | ||
1801 | void aa_label_printk(struct aa_label *label, gfp_t gfp) | |
1802 | { | |
1803 | struct aa_ns *ns = aa_get_current_ns(); | |
1804 | aa_label_xprintk(ns, label, FLAG_VIEW_SUBNS, gfp); | |
1805 | aa_put_ns(ns); | |
1806 | } | |
1807 | ||
1808 | static int label_count_str_entries(const char *str) | |
1809 | { | |
1810 | const char *split; | |
1811 | int count = 1; | |
1812 | ||
1813 | AA_BUG(!str); | |
1814 | ||
1815 | for (split = strstr(str, "//&"); split; split = strstr(str, "//&")) { | |
1816 | count++; | |
1817 | str = split + 3; | |
1818 | } | |
1819 | ||
1820 | return count; | |
1821 | } | |
1822 | ||
1823 | /** | |
1824 | * aa_label_parse - parse, validate and convert a text string to a label | |
1825 | * @base: base label to use for lookups (NOT NULL) | |
1826 | * @str: null terminated text string (NOT NULL) | |
1827 | * @gfp: allocation type | |
1828 | * @create: true if should create compound labels if they don't exist | |
1829 | * @force_stack: true if should stack even if no leading & | |
1830 | * | |
1831 | * Returns: the matching refcounted label if present | |
1832 | * else ERRPTR | |
1833 | */ | |
1834 | struct aa_label *aa_label_parse(struct aa_label *base, const char *str, | |
1835 | gfp_t gfp, bool create, bool force_stack) | |
1836 | { | |
1837 | DEFINE_VEC(profile, vec); | |
1838 | struct aa_label *label; | |
1839 | int i, len, stack = 0, error; | |
1840 | char *split; | |
1841 | ||
1842 | AA_BUG(!base); | |
1843 | AA_BUG(!str); | |
1844 | ||
1845 | str = skip_spaces(str); | |
1846 | len = label_count_str_entries(str); | |
1847 | if (*str == '&' || force_stack) { | |
1848 | /* stack on top of base */ | |
1849 | stack = base->size; | |
1850 | len += stack; | |
1851 | if (*str == '&') | |
1852 | str++; | |
1853 | } | |
1854 | error = vec_setup(profile, vec, len, gfp); | |
1855 | if (error) | |
1856 | return ERR_PTR(error); | |
1857 | ||
1858 | for (i = 0; i < stack; i++) | |
1859 | vec[i] = aa_get_profile(base->vec[i]); | |
1860 | ||
1861 | for (split = strstr(str, "//&"), i = stack; split && i < len; i++) { | |
1862 | vec[i] = aa_fqlookupn_profile(base, str, split - str); | |
1863 | if (!vec[i]) | |
1864 | goto fail; | |
1865 | str = split + 3; | |
1866 | split = strstr(str, "//&"); | |
1867 | } | |
1868 | /* last element doesn't have a split so this should be the case but just to be safe */ | |
1869 | if (i < len) { | |
1870 | vec[i] = aa_fqlookupn_profile(base, str, strlen(str)); | |
1871 | if (!vec[i]) | |
1872 | goto fail; | |
1873 | } | |
1874 | if (len == 1) | |
1875 | /* no need to free vec as len < LOCAL_VEC_ENTRIES */ | |
1876 | return &vec[0]->label; | |
1877 | ||
1878 | len -= aa_vec_unique(vec, len, VEC_FLAG_TERMINATE); | |
1879 | ||
1880 | if (create) | |
1881 | label = aa_vec_find_or_create_label(vec, len, gfp); | |
1882 | else | |
1883 | label = vec_find(vec, len); | |
1884 | if (!label) | |
1885 | goto fail; | |
1886 | ||
1887 | out: | |
1888 | /* use adjusted len from after vec_unique, not original */ | |
1889 | vec_cleanup(profile, vec, len); | |
1890 | return label; | |
1891 | ||
1892 | fail: | |
1893 | label = ERR_PTR(-ENOENT); | |
1894 | goto out; | |
1895 | } | |
1896 | ||
1897 | ||
1898 | /** | |
1899 | * aa_labelset_destroy - remove all labels from the label set | |
1900 | * @ls: label set to cleanup (NOT NULL) | |
1901 | * | |
1902 | * Labels that are removed from the set may still exist beyond the set | |
1903 | * being destroyed depending on their reference counting | |
1904 | */ | |
1905 | void aa_labelset_destroy(struct aa_labelset *ls) | |
1906 | { | |
1907 | struct rb_node *node; | |
1908 | unsigned long flags; | |
1909 | ||
1910 | AA_BUG(!ls); | |
1911 | ||
1912 | write_lock_irqsave(&ls->lock, flags); | |
1913 | for (node = rb_first(&ls->root); node; node = rb_first(&ls->root)) { | |
1914 | struct aa_label *this = rb_entry(node, struct aa_label, node); | |
1915 | if (labels_ns(this) != root_ns) | |
1916 | __label_remove(this, | |
1917 | ns_unconfined(labels_ns(this)->parent)); | |
1918 | else | |
1919 | __label_remove(this, NULL); | |
1920 | } | |
1921 | write_unlock_irqrestore(&ls->lock, flags); | |
1922 | } | |
1923 | ||
1924 | /* | |
1925 | * @ls: labelset to init (NOT NULL) | |
1926 | */ | |
1927 | void aa_labelset_init(struct aa_labelset *ls) | |
1928 | { | |
1929 | AA_BUG(!ls); | |
1930 | ||
1931 | rwlock_init(&ls->lock); | |
1932 | ls->root = RB_ROOT; | |
1933 | labelstats_init(&ls); | |
1934 | } | |
1935 | ||
1936 | static struct aa_label *labelset_next_stale(struct aa_labelset *ls) | |
1937 | { | |
1938 | struct aa_label *label; | |
1939 | struct rb_node *node; | |
1940 | unsigned long flags; | |
1941 | ||
1942 | AA_BUG(!ls); | |
1943 | ||
1944 | read_lock_irqsave(&ls->lock, flags); | |
1945 | ||
1946 | __labelset_for_each(ls, node) { | |
1947 | label = rb_entry(node, struct aa_label, node); | |
1948 | if ((label_is_stale(label) || vec_is_stale(label->vec, label->size)) && | |
1949 | aa_get_label_not0(label)) | |
1950 | goto out; | |
1951 | ||
1952 | } | |
1953 | label = NULL; | |
1954 | ||
1955 | out: | |
1956 | read_unlock_irqrestore(&ls->lock, flags); | |
1957 | ||
1958 | return label; | |
1959 | } | |
1960 | ||
1961 | /** | |
1962 | * __label_update - insert updated version of @label into labelset | |
1963 | * @label - the label to update/repace | |
1964 | * | |
1965 | * Returns: new label that is up to date | |
1966 | * else NULL on failure | |
1967 | * | |
1968 | * Requires: @ns lock be held | |
1969 | * | |
1970 | * Note: worst case is the stale @label does not get updated and has | |
1971 | * to be updated at a later time. | |
1972 | */ | |
1973 | static struct aa_label *__label_update(struct aa_label *label) | |
1974 | { | |
1975 | struct aa_label *new, *tmp; | |
1976 | struct aa_labelset *ls; | |
1977 | struct aa_profile *p; | |
1978 | struct label_it i; | |
1979 | unsigned long flags; | |
1980 | int invcount = 0; | |
1981 | ||
1982 | AA_BUG(!label); | |
1983 | AA_BUG(!mutex_is_locked(&labels_ns(label)->lock)); | |
1984 | ||
1985 | new = aa_label_alloc(label->size, label->proxy, GFP_KERNEL); | |
1986 | if (!new) | |
1987 | return NULL; | |
1988 | ||
1989 | /* while holding the ns_lock will stop profile replacement, removal, | |
1990 | * and label updates, label merging and removal can be occuring | |
1991 | */ | |
1992 | ls = labels_set(label); | |
1993 | write_lock_irqsave(&ls->lock, flags); | |
1994 | label_for_each(i, label, p) { | |
1995 | new->vec[i.i] = aa_get_newest_profile(p); | |
e578b7c4 | 1996 | if (new->vec[i.i]->label.proxy != p->label.proxy) |
80594fc2 JJ |
1997 | invcount++; |
1998 | } | |
1999 | ||
2000 | /* updated stale label by being removed/renamed from labelset */ | |
2001 | if (invcount) { | |
2002 | new->size -= aa_vec_unique(&new->vec[0], new->size, | |
2003 | VEC_FLAG_TERMINATE); | |
2004 | if (labels_set(label) != labels_set(new)) { | |
2005 | write_unlock_irqrestore(&ls->lock, flags); | |
2006 | tmp = aa_label_insert(labels_set(new), new); | |
2007 | write_lock_irqsave(&ls->lock, flags); | |
2008 | goto remove; | |
2009 | } | |
2010 | } else | |
2011 | AA_BUG(labels_ns(label) != labels_ns(new)); | |
2012 | ||
2013 | tmp = __label_insert(labels_set(label), new, true); | |
2014 | remove: | |
2015 | /* ensure label is removed, and redirected correctly */ | |
2016 | __label_remove(label, tmp); | |
2017 | write_unlock_irqrestore(&ls->lock, flags); | |
2018 | ||
2019 | aa_put_label(new); | |
2020 | ||
2021 | return tmp; | |
2022 | } | |
2023 | ||
2024 | /** | |
2025 | * __labelset_update - update labels in @ns | |
2026 | * @ns: namespace to update labels in (NOT NULL) | |
2027 | * | |
2028 | * Requires: @ns lock be held | |
2029 | * | |
2030 | * Walk the labelset ensuring that all labels are up to date and valid | |
2031 | * Any label that has a stale component is marked stale and replaced and | |
2032 | * by an updated version. | |
2033 | * | |
2034 | * If failures happen due to memory pressures then stale labels will | |
2035 | * be left in place until the next pass. | |
2036 | */ | |
2037 | static void __labelset_update(struct aa_ns *ns) | |
2038 | { | |
2039 | struct aa_label *label; | |
2040 | ||
2041 | AA_BUG(!ns); | |
2042 | AA_BUG(!mutex_is_locked(&ns->lock)); | |
2043 | ||
2044 | do { | |
2045 | label = labelset_next_stale(&ns->labels); | |
2046 | if (label) { | |
2047 | struct aa_label *l; | |
2048 | l = __label_update(label); | |
2049 | aa_put_label(l); | |
2050 | aa_put_label(label); | |
2051 | } | |
2052 | } while (label); | |
2053 | } | |
2054 | ||
2055 | /** | |
2056 | * __aa_labelset_udate_subtree - update all labels with a stale component | |
2057 | * @ns: ns to start update at (NOT NULL) | |
2058 | * | |
2059 | * Requires: @ns lock be held | |
2060 | * | |
2061 | * Invalidates labels based on @p in @ns and any children namespaces. | |
2062 | */ | |
2063 | void __aa_labelset_update_subtree(struct aa_ns *ns) | |
2064 | { | |
2065 | struct aa_ns *child; | |
2066 | ||
2067 | AA_BUG(!ns); | |
2068 | AA_BUG(!mutex_is_locked(&ns->lock)); | |
2069 | ||
2070 | __labelset_update(ns); | |
2071 | ||
2072 | list_for_each_entry(child, &ns->sub_ns, base.list) { | |
2073 | mutex_lock(&child->lock); | |
2074 | __aa_labelset_update_subtree(child); | |
2075 | mutex_unlock(&child->lock); | |
2076 | } | |
2077 | } |