1
2
3
4
5
6
7
8
9
10#ifndef __AA_LABEL_H
11#define __AA_LABEL_H
12
13#include <linux/atomic.h>
14#include <linux/audit.h>
15#include <linux/rbtree.h>
16#include <linux/rcupdate.h>
17
18#include "apparmor.h"
19#include "lib.h"
20
21struct aa_ns;
22
23#define LOCAL_VEC_ENTRIES 8
24#define DEFINE_VEC(T, V) \
25 struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES]; \
26 struct aa_ ## T **(V)
27
28#define vec_setup(T, V, N, GFP) \
29({ \
30 if ((N) <= LOCAL_VEC_ENTRIES) { \
31 typeof(N) i; \
32 (V) = (_ ## V ## _localtmp); \
33 for (i = 0; i < (N); i++) \
34 (V)[i] = NULL; \
35 } else \
36 (V) = kzalloc(sizeof(struct aa_ ## T *) * (N), (GFP)); \
37 (V) ? 0 : -ENOMEM; \
38})
39
40#define vec_cleanup(T, V, N) \
41do { \
42 int i; \
43 for (i = 0; i < (N); i++) { \
44 if (!IS_ERR_OR_NULL((V)[i])) \
45 aa_put_ ## T((V)[i]); \
46 } \
47 if ((V) != _ ## V ## _localtmp) \
48 kfree(V); \
49} while (0)
50
51#define vec_last(VEC, SIZE) ((VEC)[(SIZE) - 1])
52#define vec_ns(VEC, SIZE) (vec_last((VEC), (SIZE))->ns)
53#define vec_labelset(VEC, SIZE) (&vec_ns((VEC), (SIZE))->labels)
54#define cleanup_domain_vec(V, L) cleanup_label_vec((V), (L)->size)
55
56struct aa_profile;
57#define VEC_FLAG_TERMINATE 1
58int aa_vec_unique(struct aa_profile **vec, int n, int flags);
59struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
60 gfp_t gfp);
61#define aa_sort_and_merge_vec(N, V) \
62 aa_sort_and_merge_profiles((N), (struct aa_profile **)(V))
63
64
65
66
67
68
69
70
71struct aa_labelset {
72 rwlock_t lock;
73
74 struct rb_root root;
75};
76
77#define __labelset_for_each(LS, N) \
78 for ((N) = rb_first(&(LS)->root); (N); (N) = rb_next(N))
79
80void aa_labelset_destroy(struct aa_labelset *ls);
81void aa_labelset_init(struct aa_labelset *ls);
82
83
84enum label_flags {
85 FLAG_HAT = 1,
86 FLAG_UNCONFINED = 2,
87 FLAG_NULL = 4,
88 FLAG_IX_ON_NAME_ERROR = 8,
89 FLAG_IMMUTIBLE = 0x10,
90 FLAG_USER_DEFINED = 0x20,
91 FLAG_NO_LIST_REF = 0x40,
92 FLAG_NS_COUNT = 0x80,
93 FLAG_IN_TREE = 0x100,
94 FLAG_PROFILE = 0x200,
95 FLAG_EXPLICIT = 0x400,
96 FLAG_STALE = 0x800,
97 FLAG_RENAMED = 0x1000,
98 FLAG_REVOKED = 0x2000,
99
100
101
102};
103
104struct aa_label;
105struct aa_proxy {
106 struct kref count;
107 struct aa_label __rcu *label;
108};
109
110struct label_it {
111 int i, j;
112};
113
114
115
116
117
118
119
120
121
122
123
124
125struct aa_label {
126 struct kref count;
127 struct rb_node node;
128 struct rcu_head rcu;
129 struct aa_proxy *proxy;
130 __counted char *hname;
131 long flags;
132 u32 secid;
133 int size;
134 struct aa_profile *vec[];
135};
136
137#define last_error(E, FN) \
138do { \
139 int __subE = (FN); \
140 if (__subE) \
141 (E) = __subE; \
142} while (0)
143
144#define label_isprofile(X) ((X)->flags & FLAG_PROFILE)
145#define label_unconfined(X) ((X)->flags & FLAG_UNCONFINED)
146#define unconfined(X) label_unconfined(X)
147#define label_is_stale(X) ((X)->flags & FLAG_STALE)
148#define __label_make_stale(X) ((X)->flags |= FLAG_STALE)
149#define labels_ns(X) (vec_ns(&((X)->vec[0]), (X)->size))
150#define labels_set(X) (&labels_ns(X)->labels)
151#define labels_profile(X) ((X)->vec[(X)->size - 1])
152
153
154int aa_label_next_confined(struct aa_label *l, int i);
155
156
157#define label_for_each(I, L, P) \
158 for ((I).i = 0; ((P) = (L)->vec[(I).i]); ++((I).i))
159
160
161#define label_for_each_cont(I, L, P) \
162 for (++((I).i); ((P) = (L)->vec[(I).i]); ++((I).i))
163
164#define next_comb(I, L1, L2) \
165do { \
166 (I).j++; \
167 if ((I).j >= (L2)->size) { \
168 (I).i++; \
169 (I).j = 0; \
170 } \
171} while (0)
172
173
174
175#define label_for_each_comb(I, L1, L2, P1, P2) \
176for ((I).i = (I).j = 0; \
177 ((P1) = (L1)->vec[(I).i]) && ((P2) = (L2)->vec[(I).j]); \
178 (I) = next_comb(I, L1, L2))
179
180#define fn_for_each_comb(L1, L2, P1, P2, FN) \
181({ \
182 struct label_it i; \
183 int __E = 0; \
184 label_for_each_comb(i, (L1), (L2), (P1), (P2)) { \
185 last_error(__E, (FN)); \
186 } \
187 __E; \
188})
189
190
191#define label_for_each_confined(I, L, P) \
192 for ((I).i = aa_label_next_confined((L), 0); \
193 ((P) = (L)->vec[(I).i]); \
194 (I).i = aa_label_next_confined((L), (I).i + 1))
195
196#define label_for_each_in_merge(I, A, B, P) \
197 for ((I).i = (I).j = 0; \
198 ((P) = aa_label_next_in_merge(&(I), (A), (B))); \
199 )
200
201#define label_for_each_not_in_set(I, SET, SUB, P) \
202 for ((I).i = (I).j = 0; \
203 ((P) = __aa_label_next_not_in_set(&(I), (SET), (SUB))); \
204 )
205
206#define next_in_ns(i, NS, L) \
207({ \
208 typeof(i) ___i = (i); \
209 while ((L)->vec[___i] && (L)->vec[___i]->ns != (NS)) \
210 (___i)++; \
211 (___i); \
212})
213
214#define label_for_each_in_ns(I, NS, L, P) \
215 for ((I).i = next_in_ns(0, (NS), (L)); \
216 ((P) = (L)->vec[(I).i]); \
217 (I).i = next_in_ns((I).i + 1, (NS), (L)))
218
219#define fn_for_each_in_ns(L, P, FN) \
220({ \
221 struct label_it __i; \
222 struct aa_ns *__ns = labels_ns(L); \
223 int __E = 0; \
224 label_for_each_in_ns(__i, __ns, (L), (P)) { \
225 last_error(__E, (FN)); \
226 } \
227 __E; \
228})
229
230
231#define fn_for_each_XXX(L, P, FN, ...) \
232({ \
233 struct label_it i; \
234 int __E = 0; \
235 label_for_each ## __VA_ARGS__(i, (L), (P)) { \
236 last_error(__E, (FN)); \
237 } \
238 __E; \
239})
240
241#define fn_for_each(L, P, FN) fn_for_each_XXX(L, P, FN)
242#define fn_for_each_confined(L, P, FN) fn_for_each_XXX(L, P, FN, _confined)
243
244#define fn_for_each2_XXX(L1, L2, P, FN, ...) \
245({ \
246 struct label_it i; \
247 int __E = 0; \
248 label_for_each ## __VA_ARGS__(i, (L1), (L2), (P)) { \
249 last_error(__E, (FN)); \
250 } \
251 __E; \
252})
253
254#define fn_for_each_in_merge(L1, L2, P, FN) \
255 fn_for_each2_XXX((L1), (L2), P, FN, _in_merge)
256#define fn_for_each_not_in_set(L1, L2, P, FN) \
257 fn_for_each2_XXX((L1), (L2), P, FN, _not_in_set)
258
259#define LABEL_MEDIATES(L, C) \
260({ \
261 struct aa_profile *profile; \
262 struct label_it i; \
263 int ret = 0; \
264 label_for_each(i, (L), profile) { \
265 if (PROFILE_MEDIATES(profile, (C))) { \
266 ret = 1; \
267 break; \
268 } \
269 } \
270 ret; \
271})
272
273
274void aa_labelset_destroy(struct aa_labelset *ls);
275void aa_labelset_init(struct aa_labelset *ls);
276void __aa_labelset_update_subtree(struct aa_ns *ns);
277
278void aa_label_destroy(struct aa_label *label);
279void aa_label_free(struct aa_label *label);
280void aa_label_kref(struct kref *kref);
281bool aa_label_init(struct aa_label *label, int size, gfp_t gfp);
282struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp);
283
284bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub);
285bool aa_label_is_unconfined_subset(struct aa_label *set, struct aa_label *sub);
286struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
287 struct aa_label *set,
288 struct aa_label *sub);
289bool aa_label_remove(struct aa_label *label);
290struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *l);
291bool aa_label_replace(struct aa_label *old, struct aa_label *new);
292bool aa_label_make_newest(struct aa_labelset *ls, struct aa_label *old,
293 struct aa_label *new);
294
295struct aa_label *aa_label_find(struct aa_label *l);
296
297struct aa_profile *aa_label_next_in_merge(struct label_it *I,
298 struct aa_label *a,
299 struct aa_label *b);
300struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b);
301struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
302 gfp_t gfp);
303
304
305bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp);
306
307#define FLAGS_NONE 0
308#define FLAG_SHOW_MODE 1
309#define FLAG_VIEW_SUBNS 2
310#define FLAG_HIDDEN_UNCONFINED 4
311#define FLAG_ABS_ROOT 8
312int aa_label_snxprint(char *str, size_t size, struct aa_ns *view,
313 struct aa_label *label, int flags);
314int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
315 int flags, gfp_t gfp);
316int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
317 struct aa_label *label, int flags, gfp_t gfp);
318void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
319 struct aa_label *label, int flags, gfp_t gfp);
320void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
321 struct aa_label *label, int flags, gfp_t gfp);
322void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
323 gfp_t gfp);
324void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp);
325void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp);
326void aa_label_printk(struct aa_label *label, gfp_t gfp);
327
328struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
329 size_t n, gfp_t gfp, bool create,
330 bool force_stack);
331struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
332 gfp_t gfp, bool create, bool force_stack);
333
334static inline const char *aa_label_strn_split(const char *str, int n)
335{
336 const char *pos;
337 unsigned int state;
338
339 state = aa_dfa_matchn_until(stacksplitdfa, DFA_START, str, n, &pos);
340 if (!ACCEPT_TABLE(stacksplitdfa)[state])
341 return NULL;
342
343 return pos - 3;
344}
345
346static inline const char *aa_label_str_split(const char *str)
347{
348 const char *pos;
349 unsigned int state;
350
351 state = aa_dfa_match_until(stacksplitdfa, DFA_START, str, &pos);
352 if (!ACCEPT_TABLE(stacksplitdfa)[state])
353 return NULL;
354
355 return pos - 3;
356}
357
358
359
360struct aa_perms;
361int aa_label_match(struct aa_profile *profile, struct aa_label *label,
362 unsigned int state, bool subns, u32 request,
363 struct aa_perms *perms);
364
365
366
367
368
369
370
371
372
373
374static inline struct aa_label *__aa_get_label(struct aa_label *l)
375{
376 if (l && kref_get_unless_zero(&l->count))
377 return l;
378
379 return NULL;
380}
381
382static inline struct aa_label *aa_get_label(struct aa_label *l)
383{
384 if (l)
385 kref_get(&(l->count));
386
387 return l;
388}
389
390
391
392
393
394
395
396
397
398static inline struct aa_label *aa_get_label_rcu(struct aa_label __rcu **l)
399{
400 struct aa_label *c;
401
402 rcu_read_lock();
403 do {
404 c = rcu_dereference(*l);
405 } while (c && !kref_get_unless_zero(&c->count));
406 rcu_read_unlock();
407
408 return c;
409}
410
411
412
413
414
415
416
417
418
419static inline struct aa_label *aa_get_newest_label(struct aa_label *l)
420{
421 if (!l)
422 return NULL;
423
424 if (label_is_stale(l)) {
425 struct aa_label *tmp;
426
427 AA_BUG(!l->proxy);
428 AA_BUG(!l->proxy->label);
429
430
431
432
433 tmp = aa_get_label_rcu(&l->proxy->label);
434 AA_BUG(!tmp);
435
436 return tmp;
437 }
438
439 return aa_get_label(l);
440}
441
442static inline void aa_put_label(struct aa_label *l)
443{
444 if (l)
445 kref_put(&l->count, aa_label_kref);
446}
447
448
449struct aa_proxy *aa_alloc_proxy(struct aa_label *l, gfp_t gfp);
450void aa_proxy_kref(struct kref *kref);
451
452static inline struct aa_proxy *aa_get_proxy(struct aa_proxy *proxy)
453{
454 if (proxy)
455 kref_get(&(proxy->count));
456
457 return proxy;
458}
459
460static inline void aa_put_proxy(struct aa_proxy *proxy)
461{
462 if (proxy)
463 kref_put(&proxy->count, aa_proxy_kref);
464}
465
466void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new);
467
468#endif
469