forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel.c
2160 lines (1838 loc) · 51 KB
/
label.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
* AppArmor security module
*
* This file contains AppArmor label definitions
*
* Copyright 2017 Canonical Ltd.
*/
#include <linux/audit.h>
#include <linux/seq_file.h>
#include <linux/sort.h>
#include "include/apparmor.h"
#include "include/cred.h"
#include "include/label.h"
#include "include/policy.h"
#include "include/secid.h"
/*
* the aa_label represents the set of profiles confining an object
*
* Labels maintain a reference count to the set of pointers they reference
* Labels are ref counted by
* tasks and object via the security field/security context off the field
* code - will take a ref count on a label if it needs the label
* beyond what is possible with an rcu_read_lock.
* profiles - each profile is a label
* secids - a pinned secid will keep a refcount of the label it is
* referencing
* objects - inode, files, sockets, ...
*
* Labels are not ref counted by the label set, so they maybe removed and
* freed when no longer in use.
*
*/
#define PROXY_POISON 97
#define LABEL_POISON 100
static void free_proxy(struct aa_proxy *proxy)
{
if (proxy) {
/* p->label will not updated any more as p is dead */
aa_put_label(rcu_dereference_protected(proxy->label, true));
memset(proxy, 0, sizeof(*proxy));
RCU_INIT_POINTER(proxy->label, (struct aa_label *)PROXY_POISON);
kfree(proxy);
}
}
void aa_proxy_kref(struct kref *kref)
{
struct aa_proxy *proxy = container_of(kref, struct aa_proxy, count);
free_proxy(proxy);
}
struct aa_proxy *aa_alloc_proxy(struct aa_label *label, gfp_t gfp)
{
struct aa_proxy *new;
new = kzalloc(sizeof(struct aa_proxy), gfp);
if (new) {
kref_init(&new->count);
rcu_assign_pointer(new->label, aa_get_label(label));
}
return new;
}
/* requires profile list write lock held */
void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new)
{
struct aa_label *tmp;
AA_BUG(!orig);
AA_BUG(!new);
lockdep_assert_held_write(&labels_set(orig)->lock);
tmp = rcu_dereference_protected(orig->proxy->label,
&labels_ns(orig)->lock);
rcu_assign_pointer(orig->proxy->label, aa_get_label(new));
orig->flags |= FLAG_STALE;
aa_put_label(tmp);
}
static void __proxy_share(struct aa_label *old, struct aa_label *new)
{
struct aa_proxy *proxy = new->proxy;
new->proxy = aa_get_proxy(old->proxy);
__aa_proxy_redirect(old, new);
aa_put_proxy(proxy);
}
/**
* ns_cmp - compare ns for label set ordering
* @a: ns to compare (NOT NULL)
* @b: ns to compare (NOT NULL)
*
* Returns: <0 if a < b
* ==0 if a == b
* >0 if a > b
*/
static int ns_cmp(struct aa_ns *a, struct aa_ns *b)
{
int res;
AA_BUG(!a);
AA_BUG(!b);
AA_BUG(!a->base.hname);
AA_BUG(!b->base.hname);
if (a == b)
return 0;
res = a->level - b->level;
if (res)
return res;
return strcmp(a->base.hname, b->base.hname);
}
/**
* profile_cmp - profile comparison for set ordering
* @a: profile to compare (NOT NULL)
* @b: profile to compare (NOT NULL)
*
* Returns: <0 if a < b
* ==0 if a == b
* >0 if a > b
*/
static int profile_cmp(struct aa_profile *a, struct aa_profile *b)
{
int res;
AA_BUG(!a);
AA_BUG(!b);
AA_BUG(!a->ns);
AA_BUG(!b->ns);
AA_BUG(!a->base.hname);
AA_BUG(!b->base.hname);
if (a == b || a->base.hname == b->base.hname)
return 0;
res = ns_cmp(a->ns, b->ns);
if (res)
return res;
return strcmp(a->base.hname, b->base.hname);
}
/**
* vec_cmp - label comparison for set ordering
* @a: label to compare (NOT NULL)
* @vec: vector of profiles to compare (NOT NULL)
* @n: length of @vec
*
* Returns: <0 if a < vec
* ==0 if a == vec
* >0 if a > vec
*/
static int vec_cmp(struct aa_profile **a, int an, struct aa_profile **b, int bn)
{
int i;
AA_BUG(!a);
AA_BUG(!*a);
AA_BUG(!b);
AA_BUG(!*b);
AA_BUG(an <= 0);
AA_BUG(bn <= 0);
for (i = 0; i < an && i < bn; i++) {
int res = profile_cmp(a[i], b[i]);
if (res != 0)
return res;
}
return an - bn;
}
static bool vec_is_stale(struct aa_profile **vec, int n)
{
int i;
AA_BUG(!vec);
for (i = 0; i < n; i++) {
if (profile_is_stale(vec[i]))
return true;
}
return false;
}
static bool vec_unconfined(struct aa_profile **vec, int n)
{
int i;
AA_BUG(!vec);
for (i = 0; i < n; i++) {
if (!profile_unconfined(vec[i]))
return false;
}
return true;
}
static int sort_cmp(const void *a, const void *b)
{
return profile_cmp(*(struct aa_profile **)a, *(struct aa_profile **)b);
}
/*
* assumes vec is sorted
* Assumes @vec has null terminator at vec[n], and will null terminate
* vec[n - dups]
*/
static inline int unique(struct aa_profile **vec, int n)
{
int i, pos, dups = 0;
AA_BUG(n < 1);
AA_BUG(!vec);
pos = 0;
for (i = 1; i < n; i++) {
int res = profile_cmp(vec[pos], vec[i]);
AA_BUG(res > 0, "vec not sorted");
if (res == 0) {
/* drop duplicate */
aa_put_profile(vec[i]);
dups++;
continue;
}
pos++;
if (dups)
vec[pos] = vec[i];
}
AA_BUG(dups < 0);
return dups;
}
/**
* aa_vec_unique - canonical sort and unique a list of profiles
* @n: number of refcounted profiles in the list (@n > 0)
* @vec: list of profiles to sort and merge
*
* Returns: the number of duplicates eliminated == references put
*
* If @flags & VEC_FLAG_TERMINATE @vec has null terminator at vec[n], and will
* null terminate vec[n - dups]
*/
int aa_vec_unique(struct aa_profile **vec, int n, int flags)
{
int i, dups = 0;
AA_BUG(n < 1);
AA_BUG(!vec);
/* vecs are usually small and inorder, have a fallback for larger */
if (n > 8) {
sort(vec, n, sizeof(struct aa_profile *), sort_cmp, NULL);
dups = unique(vec, n);
goto out;
}
/* insertion sort + unique in one */
for (i = 1; i < n; i++) {
struct aa_profile *tmp = vec[i];
int pos, j;
for (pos = i - 1 - dups; pos >= 0; pos--) {
int res = profile_cmp(vec[pos], tmp);
if (res == 0) {
/* drop duplicate entry */
aa_put_profile(tmp);
dups++;
goto continue_outer;
} else if (res < 0)
break;
}
/* pos is at entry < tmp, or index -1. Set to insert pos */
pos++;
for (j = i - dups; j > pos; j--)
vec[j] = vec[j - 1];
vec[pos] = tmp;
continue_outer:
;
}
AA_BUG(dups < 0);
out:
if (flags & VEC_FLAG_TERMINATE)
vec[n - dups] = NULL;
return dups;
}
void aa_label_destroy(struct aa_label *label)
{
AA_BUG(!label);
if (!label_isprofile(label)) {
struct aa_profile *profile;
struct label_it i;
aa_put_str(label->hname);
label_for_each(i, label, profile) {
aa_put_profile(profile);
label->vec[i.i] = (struct aa_profile *)
(LABEL_POISON + (long) i.i);
}
}
if (label->proxy) {
if (rcu_dereference_protected(label->proxy->label, true) == label)
rcu_assign_pointer(label->proxy->label, NULL);
aa_put_proxy(label->proxy);
}
aa_free_secid(label->secid);
label->proxy = (struct aa_proxy *) PROXY_POISON + 1;
}
void aa_label_free(struct aa_label *label)
{
if (!label)
return;
aa_label_destroy(label);
kfree(label);
}
static void label_free_switch(struct aa_label *label)
{
if (label->flags & FLAG_NS_COUNT)
aa_free_ns(labels_ns(label));
else if (label_isprofile(label))
aa_free_profile(labels_profile(label));
else
aa_label_free(label);
}
static void label_free_rcu(struct rcu_head *head)
{
struct aa_label *label = container_of(head, struct aa_label, rcu);
if (label->flags & FLAG_IN_TREE)
(void) aa_label_remove(label);
label_free_switch(label);
}
void aa_label_kref(struct kref *kref)
{
struct aa_label *label = container_of(kref, struct aa_label, count);
struct aa_ns *ns = labels_ns(label);
if (!ns) {
/* never live, no rcu callback needed, just using the fn */
label_free_switch(label);
return;
}
/* TODO: update labels_profile macro so it works here */
AA_BUG(label_isprofile(label) &&
on_list_rcu(&label->vec[0]->base.profiles));
AA_BUG(label_isprofile(label) &&
on_list_rcu(&label->vec[0]->base.list));
/* TODO: if compound label and not stale add to reclaim cache */
call_rcu(&label->rcu, label_free_rcu);
}
static void label_free_or_put_new(struct aa_label *label, struct aa_label *new)
{
if (label != new)
/* need to free directly to break circular ref with proxy */
aa_label_free(new);
else
aa_put_label(new);
}
bool aa_label_init(struct aa_label *label, int size, gfp_t gfp)
{
AA_BUG(!label);
AA_BUG(size < 1);
if (aa_alloc_secid(label, gfp) < 0)
return false;
label->size = size; /* doesn't include null */
label->vec[size] = NULL; /* null terminate */
kref_init(&label->count);
RB_CLEAR_NODE(&label->node);
return true;
}
/**
* aa_label_alloc - allocate a label with a profile vector of @size length
* @size: size of profile vector in the label
* @proxy: proxy to use OR null if to allocate a new one
* @gfp: memory allocation type
*
* Returns: new label
* else NULL if failed
*/
struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp)
{
struct aa_label *new;
AA_BUG(size < 1);
/* + 1 for null terminator entry on vec */
new = kzalloc(struct_size(new, vec, size + 1), gfp);
AA_DEBUG("%s (%p)\n", __func__, new);
if (!new)
goto fail;
if (!aa_label_init(new, size, gfp))
goto fail;
if (!proxy) {
proxy = aa_alloc_proxy(new, gfp);
if (!proxy)
goto fail;
} else
aa_get_proxy(proxy);
/* just set new's proxy, don't redirect proxy here if it was passed in*/
new->proxy = proxy;
return new;
fail:
kfree(new);
return NULL;
}
/**
* label_cmp - label comparison for set ordering
* @a: label to compare (NOT NULL)
* @b: label to compare (NOT NULL)
*
* Returns: <0 if a < b
* ==0 if a == b
* >0 if a > b
*/
static int label_cmp(struct aa_label *a, struct aa_label *b)
{
AA_BUG(!b);
if (a == b)
return 0;
return vec_cmp(a->vec, a->size, b->vec, b->size);
}
/* helper fn for label_for_each_confined */
int aa_label_next_confined(struct aa_label *label, int i)
{
AA_BUG(!label);
AA_BUG(i < 0);
for (; i < label->size; i++) {
if (!profile_unconfined(label->vec[i]))
return i;
}
return i;
}
/**
* aa_label_next_not_in_set - return the next profile of @sub not in @set
* @I: label iterator
* @set: label to test against
* @sub: label to if is subset of @set
*
* Returns: profile in @sub that is not in @set, with iterator set pos after
* else NULL if @sub is a subset of @set
*/
struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
struct aa_label *set,
struct aa_label *sub)
{
AA_BUG(!set);
AA_BUG(!I);
AA_BUG(I->i < 0);
AA_BUG(I->i > set->size);
AA_BUG(!sub);
AA_BUG(I->j < 0);
AA_BUG(I->j > sub->size);
while (I->j < sub->size && I->i < set->size) {
int res = profile_cmp(sub->vec[I->j], set->vec[I->i]);
if (res == 0) {
(I->j)++;
(I->i)++;
} else if (res > 0)
(I->i)++;
else
return sub->vec[(I->j)++];
}
if (I->j < sub->size)
return sub->vec[(I->j)++];
return NULL;
}
/**
* aa_label_is_subset - test if @sub is a subset of @set
* @set: label to test against
* @sub: label to test if is subset of @set
*
* Returns: true if @sub is subset of @set
* else false
*/
bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub)
{
struct label_it i = { };
AA_BUG(!set);
AA_BUG(!sub);
if (sub == set)
return true;
return __aa_label_next_not_in_set(&i, set, sub) == NULL;
}
/**
* aa_label_is_unconfined_subset - test if @sub is a subset of @set
* @set: label to test against
* @sub: label to test if is subset of @set
*
* This checks for subset but taking into account unconfined. IF
* @sub contains an unconfined profile that does not have a matching
* unconfined in @set then this will not cause the test to fail.
* Conversely we don't care about an unconfined in @set that is not in
* @sub
*
* Returns: true if @sub is special_subset of @set
* else false
*/
bool aa_label_is_unconfined_subset(struct aa_label *set, struct aa_label *sub)
{
struct label_it i = { };
struct aa_profile *p;
AA_BUG(!set);
AA_BUG(!sub);
if (sub == set)
return true;
do {
p = __aa_label_next_not_in_set(&i, set, sub);
if (p && !profile_unconfined(p))
break;
} while (p);
return p == NULL;
}
/**
* __label_remove - remove @label from the label set
* @l: label to remove
* @new: label to redirect to
*
* Requires: labels_set(@label)->lock write_lock
* Returns: true if the label was in the tree and removed
*/
static bool __label_remove(struct aa_label *label, struct aa_label *new)
{
struct aa_labelset *ls = labels_set(label);
AA_BUG(!ls);
AA_BUG(!label);
lockdep_assert_held_write(&ls->lock);
if (new)
__aa_proxy_redirect(label, new);
if (!label_is_stale(label))
__label_make_stale(label);
if (label->flags & FLAG_IN_TREE) {
rb_erase(&label->node, &ls->root);
label->flags &= ~FLAG_IN_TREE;
return true;
}
return false;
}
/**
* __label_replace - replace @old with @new in label set
* @old: label to remove from label set
* @new: label to replace @old with
*
* Requires: labels_set(@old)->lock write_lock
* valid ref count be held on @new
* Returns: true if @old was in set and replaced by @new
*
* Note: current implementation requires label set be order in such a way
* that @new directly replaces @old position in the set (ie.
* using pointer comparison of the label address would not work)
*/
static bool __label_replace(struct aa_label *old, struct aa_label *new)
{
struct aa_labelset *ls = labels_set(old);
AA_BUG(!ls);
AA_BUG(!old);
AA_BUG(!new);
lockdep_assert_held_write(&ls->lock);
AA_BUG(new->flags & FLAG_IN_TREE);
if (!label_is_stale(old))
__label_make_stale(old);
if (old->flags & FLAG_IN_TREE) {
rb_replace_node(&old->node, &new->node, &ls->root);
old->flags &= ~FLAG_IN_TREE;
new->flags |= FLAG_IN_TREE;
return true;
}
return false;
}
/**
* __label_insert - attempt to insert @l into a label set
* @ls: set of labels to insert @l into (NOT NULL)
* @label: new label to insert (NOT NULL)
* @replace: whether insertion should replace existing entry that is not stale
*
* Requires: @ls->lock
* caller to hold a valid ref on l
* if @replace is true l has a preallocated proxy associated
* Returns: @l if successful in inserting @l - with additional refcount
* else ref counted equivalent label that is already in the set,
* the else condition only happens if @replace is false
*/
static struct aa_label *__label_insert(struct aa_labelset *ls,
struct aa_label *label, bool replace)
{
struct rb_node **new, *parent = NULL;
AA_BUG(!ls);
AA_BUG(!label);
AA_BUG(labels_set(label) != ls);
lockdep_assert_held_write(&ls->lock);
AA_BUG(label->flags & FLAG_IN_TREE);
/* Figure out where to put new node */
new = &ls->root.rb_node;
while (*new) {
struct aa_label *this = rb_entry(*new, struct aa_label, node);
int result = label_cmp(label, this);
parent = *new;
if (result == 0) {
/* !__aa_get_label means queued for destruction,
* so replace in place, however the label has
* died before the replacement so do not share
* the proxy
*/
if (!replace && !label_is_stale(this)) {
if (__aa_get_label(this))
return this;
} else
__proxy_share(this, label);
AA_BUG(!__label_replace(this, label));
return aa_get_label(label);
} else if (result < 0)
new = &((*new)->rb_left);
else /* (result > 0) */
new = &((*new)->rb_right);
}
/* Add new node and rebalance tree. */
rb_link_node(&label->node, parent, new);
rb_insert_color(&label->node, &ls->root);
label->flags |= FLAG_IN_TREE;
return aa_get_label(label);
}
/**
* __vec_find - find label that matches @vec in label set
* @vec: vec of profiles to find matching label for (NOT NULL)
* @n: length of @vec
*
* Requires: @vec_labelset(vec) lock held
* caller to hold a valid ref on l
*
* Returns: ref counted @label if matching label is in tree
* ref counted label that is equiv to @l in tree
* else NULL if @vec equiv is not in tree
*/
static struct aa_label *__vec_find(struct aa_profile **vec, int n)
{
struct rb_node *node;
AA_BUG(!vec);
AA_BUG(!*vec);
AA_BUG(n <= 0);
node = vec_labelset(vec, n)->root.rb_node;
while (node) {
struct aa_label *this = rb_entry(node, struct aa_label, node);
int result = vec_cmp(this->vec, this->size, vec, n);
if (result > 0)
node = node->rb_left;
else if (result < 0)
node = node->rb_right;
else
return __aa_get_label(this);
}
return NULL;
}
/**
* __label_find - find label @label in label set
* @label: label to find (NOT NULL)
*
* Requires: labels_set(@label)->lock held
* caller to hold a valid ref on l
*
* Returns: ref counted @label if @label is in tree OR
* ref counted label that is equiv to @label in tree
* else NULL if @label or equiv is not in tree
*/
static struct aa_label *__label_find(struct aa_label *label)
{
AA_BUG(!label);
return __vec_find(label->vec, label->size);
}
/**
* aa_label_remove - remove a label from the labelset
* @label: label to remove
*
* Returns: true if @label was removed from the tree
* else @label was not in tree so it could not be removed
*/
bool aa_label_remove(struct aa_label *label)
{
struct aa_labelset *ls = labels_set(label);
unsigned long flags;
bool res;
AA_BUG(!ls);
write_lock_irqsave(&ls->lock, flags);
res = __label_remove(label, ns_unconfined(labels_ns(label)));
write_unlock_irqrestore(&ls->lock, flags);
return res;
}
/**
* aa_label_replace - replace a label @old with a new version @new
* @old: label to replace
* @new: label replacing @old
*
* Returns: true if @old was in tree and replaced
* else @old was not in tree, and @new was not inserted
*/
bool aa_label_replace(struct aa_label *old, struct aa_label *new)
{
unsigned long flags;
bool res;
if (name_is_shared(old, new) && labels_ns(old) == labels_ns(new)) {
write_lock_irqsave(&labels_set(old)->lock, flags);
if (old->proxy != new->proxy)
__proxy_share(old, new);
else
__aa_proxy_redirect(old, new);
res = __label_replace(old, new);
write_unlock_irqrestore(&labels_set(old)->lock, flags);
} else {
struct aa_label *l;
struct aa_labelset *ls = labels_set(old);
write_lock_irqsave(&ls->lock, flags);
res = __label_remove(old, new);
if (labels_ns(old) != labels_ns(new)) {
write_unlock_irqrestore(&ls->lock, flags);
ls = labels_set(new);
write_lock_irqsave(&ls->lock, flags);
}
l = __label_insert(ls, new, true);
res = (l == new);
write_unlock_irqrestore(&ls->lock, flags);
aa_put_label(l);
}
return res;
}
/**
* vec_find - find label @l in label set
* @vec: array of profiles to find equiv label for (NOT NULL)
* @n: length of @vec
*
* Returns: refcounted label if @vec equiv is in tree
* else NULL if @vec equiv is not in tree
*/
static struct aa_label *vec_find(struct aa_profile **vec, int n)
{
struct aa_labelset *ls;
struct aa_label *label;
unsigned long flags;
AA_BUG(!vec);
AA_BUG(!*vec);
AA_BUG(n <= 0);
ls = vec_labelset(vec, n);
read_lock_irqsave(&ls->lock, flags);
label = __vec_find(vec, n);
read_unlock_irqrestore(&ls->lock, flags);
return label;
}
/* requires sort and merge done first */
static struct aa_label *vec_create_and_insert_label(struct aa_profile **vec,
int len, gfp_t gfp)
{
struct aa_label *label = NULL;
struct aa_labelset *ls;
unsigned long flags;
struct aa_label *new;
int i;
AA_BUG(!vec);
if (len == 1)
return aa_get_label(&vec[0]->label);
ls = labels_set(&vec[len - 1]->label);
/* TODO: enable when read side is lockless
* check if label exists before taking locks
*/
new = aa_label_alloc(len, NULL, gfp);
if (!new)
return NULL;
for (i = 0; i < len; i++)
new->vec[i] = aa_get_profile(vec[i]);
write_lock_irqsave(&ls->lock, flags);
label = __label_insert(ls, new, false);
write_unlock_irqrestore(&ls->lock, flags);
label_free_or_put_new(label, new);
return label;
}
struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
gfp_t gfp)
{
struct aa_label *label = vec_find(vec, len);
if (label)
return label;
return vec_create_and_insert_label(vec, len, gfp);
}
/**
* aa_label_find - find label @label in label set
* @label: label to find (NOT NULL)
*
* Requires: caller to hold a valid ref on l
*
* Returns: refcounted @label if @label is in tree
* refcounted label that is equiv to @label in tree
* else NULL if @label or equiv is not in tree
*/
struct aa_label *aa_label_find(struct aa_label *label)
{
AA_BUG(!label);
return vec_find(label->vec, label->size);
}
/**
* aa_label_insert - insert label @label into @ls or return existing label
* @ls - labelset to insert @label into
* @label - label to insert
*
* Requires: caller to hold a valid ref on @label
*
* Returns: ref counted @label if successful in inserting @label
* else ref counted equivalent label that is already in the set
*/
struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *label)
{
struct aa_label *l;
unsigned long flags;
AA_BUG(!ls);
AA_BUG(!label);
/* check if label exists before taking lock */
if (!label_is_stale(label)) {
read_lock_irqsave(&ls->lock, flags);
l = __label_find(label);
read_unlock_irqrestore(&ls->lock, flags);
if (l)
return l;
}
write_lock_irqsave(&ls->lock, flags);
l = __label_insert(ls, label, false);
write_unlock_irqrestore(&ls->lock, flags);
return l;
}
/**
* aa_label_next_in_merge - find the next profile when merging @a and @b
* @I: label iterator
* @a: label to merge
* @b: label to merge
*
* Returns: next profile
* else null if no more profiles
*/
struct aa_profile *aa_label_next_in_merge(struct label_it *I,
struct aa_label *a,
struct aa_label *b)
{
AA_BUG(!a);
AA_BUG(!b);
AA_BUG(!I);
AA_BUG(I->i < 0);
AA_BUG(I->i > a->size);
AA_BUG(I->j < 0);
AA_BUG(I->j > b->size);
if (I->i < a->size) {
if (I->j < b->size) {
int res = profile_cmp(a->vec[I->i], b->vec[I->j]);
if (res > 0)
return b->vec[(I->j)++];
if (res == 0)
(I->j)++;
}
return a->vec[(I->i)++];
}
if (I->j < b->size)
return b->vec[(I->j)++];
return NULL;
}
/**
* label_merge_cmp - cmp of @a merging with @b against @z for set ordering
* @a: label to merge then compare (NOT NULL)
* @b: label to merge then compare (NOT NULL)
* @z: label to compare merge against (NOT NULL)
*
* Assumes: using the most recent versions of @a, @b, and @z
*
* Returns: <0 if a < b
* ==0 if a == b
* >0 if a > b