1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * AppArmor security module
5 * This file contains AppArmor label definitions
7 * Copyright 2017 Canonical Ltd.
13 #include <linux/atomic.h>
14 #include <linux/audit.h>
15 #include <linux/rbtree.h>
16 #include <linux/rcupdate.h>
23 #define LOCAL_VEC_ENTRIES 8
24 #define DEFINE_VEC(T, V) \
25 struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES]; \
28 #define vec_setup(T, V, N, GFP) \
30 if ((N) <= LOCAL_VEC_ENTRIES) { \
32 (V) = (_ ## V ## _localtmp); \
33 for (i = 0; i < (N); i++) \
36 (V) = kzalloc(sizeof(struct aa_ ## T *) * (N), (GFP)); \
40 #define vec_cleanup(T, V, N) \
43 for (i = 0; i < (N); i++) { \
44 if (!IS_ERR_OR_NULL((V)[i])) \
45 aa_put_ ## T((V)[i]); \
47 if ((V) != _ ## V ## _localtmp) \
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)
57 #define VEC_FLAG_TERMINATE 1
58 int aa_vec_unique(struct aa_profile **vec, int n, int flags);
59 struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
61 #define aa_sort_and_merge_vec(N, V) \
62 aa_sort_and_merge_profiles((N), (struct aa_profile **)(V))
65 /* struct aa_labelset - set of labels for a namespace
67 * Labels are reference counted; aa_labelset does not contribute to label
68 * reference counts. Once a label's last refcount is put it is removed from
77 #define __labelset_for_each(LS, N) \
78 for ((N) = rb_first(&(LS)->root); (N); (N) = rb_next(N))
80 void aa_labelset_destroy(struct aa_labelset *ls);
81 void aa_labelset_init(struct aa_labelset *ls);
85 FLAG_HAT = 1, /* profile is a hat */
86 FLAG_UNCONFINED = 2, /* label unconfined only if all */
87 FLAG_NULL = 4, /* profile is null learning profile */
88 FLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */
89 FLAG_IMMUTIBLE = 0x10, /* don't allow changes/replacement */
90 FLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */
91 FLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */
92 FLAG_NS_COUNT = 0x80, /* carries NS ref count */
93 FLAG_IN_TREE = 0x100, /* label is in tree */
94 FLAG_PROFILE = 0x200, /* label is a profile */
95 FLAG_EXPLICIT = 0x400, /* explicit static label */
96 FLAG_STALE = 0x800, /* replaced/removed */
97 FLAG_RENAMED = 0x1000, /* label has renaming in it */
98 FLAG_REVOKED = 0x2000, /* label has revocation in it */
100 /* These flags must correspond with PATH_flags */
101 /* TODO: add new path flags */
107 struct aa_label __rcu *label;
114 /* struct aa_label - lazy labeling struct
115 * @count: ref count of active users
116 * @node: rbtree position
117 * @rcu: rcu callback struct
118 * @proxy: is set to the label that replaced this label
119 * @hname: text representation of the label (MAYBE_NULL)
120 * @flags: stale and other flags - values may change under label set lock
121 * @secid: secid that references this label
122 * @size: number of entries in @ent[]
123 * @ent: set of profiles for label, actual size determined by @size
129 struct aa_proxy *proxy;
130 __counted char *hname;
134 struct aa_profile *vec[];
137 #define last_error(E, FN) \
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])
154 int aa_label_next_confined(struct aa_label *l, int i);
156 /* for each profile in a label */
157 #define label_for_each(I, L, P) \
158 for ((I).i = 0; ((P) = (L)->vec[(I).i]); ++((I).i))
160 /* assumes break/goto ended label_for_each */
161 #define label_for_each_cont(I, L, P) \
162 for (++((I).i); ((P) = (L)->vec[(I).i]); ++((I).i))
164 #define next_comb(I, L1, L2) \
167 if ((I).j >= (L2)->size) { \
174 /* for each combination of P1 in L1, and P2 in L2 */
175 #define label_for_each_comb(I, L1, L2, P1, P2) \
176 for ((I).i = (I).j = 0; \
177 ((P1) = (L1)->vec[(I).i]) && ((P2) = (L2)->vec[(I).j]); \
178 (I) = next_comb(I, L1, L2))
180 #define fn_for_each_comb(L1, L2, P1, P2, FN) \
184 label_for_each_comb(i, (L1), (L2), (P1), (P2)) { \
185 last_error(__E, (FN)); \
190 /* for each profile that is enforcing confinement in a label */
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))
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))); \
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))); \
206 #define next_in_ns(i, NS, L) \
208 typeof(i) ___i = (i); \
209 while ((L)->vec[___i] && (L)->vec[___i]->ns != (NS)) \
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)))
219 #define fn_for_each_in_ns(L, P, FN) \
221 struct label_it __i; \
222 struct aa_ns *__ns = labels_ns(L); \
224 label_for_each_in_ns(__i, __ns, (L), (P)) { \
225 last_error(__E, (FN)); \
231 #define fn_for_each_XXX(L, P, FN, ...) \
235 label_for_each ## __VA_ARGS__(i, (L), (P)) { \
236 last_error(__E, (FN)); \
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)
244 #define fn_for_each2_XXX(L1, L2, P, FN, ...) \
248 label_for_each ## __VA_ARGS__(i, (L1), (L2), (P)) { \
249 last_error(__E, (FN)); \
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)
259 #define LABEL_MEDIATES(L, C) \
261 struct aa_profile *profile; \
264 label_for_each(i, (L), profile) { \
265 if (PROFILE_MEDIATES(profile, (C))) { \
274 void aa_labelset_destroy(struct aa_labelset *ls);
275 void aa_labelset_init(struct aa_labelset *ls);
276 void __aa_labelset_update_subtree(struct aa_ns *ns);
278 void aa_label_free(struct aa_label *label);
279 void aa_label_kref(struct kref *kref);
280 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp);
281 struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp);
283 bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub);
284 bool aa_label_is_unconfined_subset(struct aa_label *set, struct aa_label *sub);
285 struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
286 struct aa_label *set,
287 struct aa_label *sub);
288 bool aa_label_remove(struct aa_label *label);
289 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *l);
290 bool aa_label_replace(struct aa_label *old, struct aa_label *new);
291 bool aa_label_make_newest(struct aa_labelset *ls, struct aa_label *old,
292 struct aa_label *new);
294 struct aa_label *aa_label_find(struct aa_label *l);
296 struct aa_profile *aa_label_next_in_merge(struct label_it *I,
299 struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b);
300 struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
304 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp);
307 #define FLAG_SHOW_MODE 1
308 #define FLAG_VIEW_SUBNS 2
309 #define FLAG_HIDDEN_UNCONFINED 4
310 #define FLAG_ABS_ROOT 8
311 int aa_label_snxprint(char *str, size_t size, struct aa_ns *view,
312 struct aa_label *label, int flags);
313 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
314 int flags, gfp_t gfp);
315 int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
316 struct aa_label *label, int flags, gfp_t gfp);
317 void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
318 struct aa_label *label, int flags, gfp_t gfp);
319 void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
320 struct aa_label *label, int flags, gfp_t gfp);
321 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
323 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp);
324 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp);
325 void aa_label_printk(struct aa_label *label, gfp_t gfp);
327 struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
328 size_t n, gfp_t gfp, bool create,
330 struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
331 gfp_t gfp, bool create, bool force_stack);
333 static inline const char *aa_label_strn_split(const char *str, int n)
338 state = aa_dfa_matchn_until(stacksplitdfa, DFA_START, str, n, &pos);
339 if (!ACCEPT_TABLE(stacksplitdfa)[state])
345 static inline const char *aa_label_str_split(const char *str)
350 state = aa_dfa_match_until(stacksplitdfa, DFA_START, str, &pos);
351 if (!ACCEPT_TABLE(stacksplitdfa)[state])
360 int aa_label_match(struct aa_profile *profile, struct aa_label *label,
361 unsigned int state, bool subns, u32 request,
362 struct aa_perms *perms);
366 * __aa_get_label - get a reference count to uncounted label reference
367 * @l: reference to get a count on
369 * Returns: pointer to reference OR NULL if race is lost and reference is
371 * Requires: lock held, and the return code MUST be checked
373 static inline struct aa_label *__aa_get_label(struct aa_label *l)
375 if (l && kref_get_unless_zero(&l->count))
381 static inline struct aa_label *aa_get_label(struct aa_label *l)
384 kref_get(&(l->count));
391 * aa_get_label_rcu - increment refcount on a label that can be replaced
392 * @l: pointer to label that can be replaced (NOT NULL)
394 * Returns: pointer to a refcounted label.
395 * else NULL if no label
397 static inline struct aa_label *aa_get_label_rcu(struct aa_label __rcu **l)
403 c = rcu_dereference(*l);
404 } while (c && !kref_get_unless_zero(&c->count));
411 * aa_get_newest_label - find the newest version of @l
412 * @l: the label to check for newer versions of
414 * Returns: refcounted newest version of @l taking into account
415 * replacement, renames and removals
418 static inline struct aa_label *aa_get_newest_label(struct aa_label *l)
423 if (label_is_stale(l)) {
424 struct aa_label *tmp;
427 AA_BUG(!l->proxy->label);
428 /* BUG: only way this can happen is @l ref count and its
429 * replacement count have gone to 0 and are on their way
430 * to destruction. ie. we have a refcounting error
432 tmp = aa_get_label_rcu(&l->proxy->label);
438 return aa_get_label(l);
441 static inline void aa_put_label(struct aa_label *l)
444 kref_put(&l->count, aa_label_kref);
448 struct aa_proxy *aa_alloc_proxy(struct aa_label *l, gfp_t gfp);
449 void aa_proxy_kref(struct kref *kref);
451 static inline struct aa_proxy *aa_get_proxy(struct aa_proxy *proxy)
454 kref_get(&(proxy->count));
459 static inline void aa_put_proxy(struct aa_proxy *proxy)
462 kref_put(&proxy->count, aa_proxy_kref);
465 void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new);
467 #endif /* __AA_LABEL_H */