| 1 |
|
| 2 | |
| 3 | * AppArmor security module
|
| 4 | *
|
| 5 | * This file contains AppArmor label definitions
|
| 6 | *
|
| 7 | * Copyright 2017 Canonical Ltd.
|
| 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 |
|
| 21 | struct aa_ns;
|
| 22 | struct aa_ruleset;
|
| 23 |
|
| 24 | #define LOCAL_VEC_ENTRIES 8
|
| 25 | #define DEFINE_VEC(T, V) \
|
| 26 | struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES + 1]; \
|
| 27 | struct aa_ ## T **(V)
|
| 28 |
|
| 29 | #define vec_setup(T, V, N, GFP) \
|
| 30 | ({ \
|
| 31 | if ((N) <= LOCAL_VEC_ENTRIES) { \
|
| 32 | typeof(N) i; \
|
| 33 | (V) = (_ ## V ## _localtmp); \
|
| 34 | for (i = 0; i <= (N); i++) \
|
| 35 | (V)[i] = NULL; \
|
| 36 | } else \
|
| 37 | (V) = kzalloc_objs(struct aa_ ## T *, (N) + 1, (GFP)); \
|
| 38 | (V) ? 0 : -ENOMEM; \
|
| 39 | })
|
| 40 |
|
| 41 | #define vec_cleanup(T, V, N) \
|
| 42 | do { \
|
| 43 | int i; \
|
| 44 | for (i = 0; i < (N); i++) { \
|
| 45 | if (!IS_ERR_OR_NULL((V)[i])) \
|
| 46 | aa_put_ ## T((V)[i]); \
|
| 47 | } \
|
| 48 | if ((V) != _ ## V ## _localtmp) \
|
| 49 | kfree(V); \
|
| 50 | } while (0)
|
| 51 |
|
| 52 | #define vec_last(VEC, SIZE) ((VEC)[(SIZE) - 1])
|
| 53 | #define vec_ns(VEC, SIZE) (vec_last((VEC), (SIZE))->ns)
|
| 54 | #define vec_labelset(VEC, SIZE) (&vec_ns((VEC), (SIZE))->labels)
|
| 55 | #define cleanup_domain_vec(V, L) cleanup_label_vec((V), (L)->size)
|
| 56 |
|
| 57 | struct aa_profile;
|
| 58 | #define VEC_FLAG_TERMINATE 1
|
| 59 | int aa_vec_unique(struct aa_profile **vec, int n, int flags);
|
| 60 | struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
|
| 61 | gfp_t gfp);
|
| 62 | #define aa_sort_and_merge_vec(N, V) \
|
| 63 | aa_sort_and_merge_profiles((N), (struct aa_profile **)(V))
|
| 64 |
|
| 65 |
|
| 66 | |
| 67 | *
|
| 68 | * Labels are reference counted; aa_labelset does not contribute to label
|
| 69 | * reference counts. Once a label's last refcount is put it is removed from
|
| 70 | * the set.
|
| 71 |
|
| 72 | struct aa_labelset {
|
| 73 | rwlock_t lock;
|
| 74 |
|
| 75 | struct rb_root root;
|
| 76 | };
|
| 77 |
|
| 78 | #define __labelset_for_each(LS, N) \
|
| 79 | for ((N) = rb_first(&(LS)->root); (N); (N) = rb_next(N))
|
| 80 |
|
| 81 | enum label_flags {
|
| 82 | FLAG_HAT = 1,
|
| 83 | FLAG_UNCONFINED = 2,
|
| 84 | FLAG_NULL = 4,
|
| 85 | FLAG_IX_ON_NAME_ERROR = 8,
|
| 86 | FLAG_IMMUTIBLE = 0x10,
|
| 87 | FLAG_USER_DEFINED = 0x20,
|
| 88 | FLAG_NO_LIST_REF = 0x40,
|
| 89 | FLAG_NS_COUNT = 0x80,
|
| 90 | FLAG_IN_TREE = 0x100,
|
| 91 | FLAG_PROFILE = 0x200,
|
| 92 | FLAG_EXPLICIT = 0x400,
|
| 93 | FLAG_STALE = 0x800,
|
| 94 | FLAG_RENAMED = 0x1000,
|
| 95 | FLAG_REVOKED = 0x2000,
|
| 96 | FLAG_DEBUG1 = 0x4000,
|
| 97 | FLAG_DEBUG2 = 0x8000,
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 | };
|
| 102 |
|
| 103 | struct aa_label;
|
| 104 | struct aa_proxy {
|
| 105 | struct aa_common_ref count;
|
| 106 | struct aa_label __rcu *label;
|
| 107 | };
|
| 108 |
|
| 109 | struct label_it {
|
| 110 | int i, j;
|
| 111 | };
|
| 112 |
|
| 113 | |
| 114 | * @count: ref count of active users
|
| 115 | * @node: rbtree position
|
| 116 | * @rcu: rcu callback struct
|
| 117 | * @proxy: is set to the label that replaced this label
|
| 118 | * @hname: text representation of the label (MAYBE_NULL)
|
| 119 | * @flags: stale and other flags - values may change under label set lock
|
| 120 | * @secid: secid that references this label
|
| 121 | * @size: number of entries in @ent[]
|
| 122 | * @mediates: bitmask for label_mediates
|
| 123 | * profile: label vec when embedded in a profile FLAG_PROFILE is set
|
| 124 | * rules: variable length rules in a profile FLAG_PROFILE is set
|
| 125 | * vec: vector of profiles comprising the compound label
|
| 126 |
|
| 127 | struct aa_label {
|
| 128 | struct aa_common_ref count;
|
| 129 | struct rb_node node;
|
| 130 | struct rcu_head rcu;
|
| 131 | struct aa_proxy *proxy;
|
| 132 | __counted char *hname;
|
| 133 | long flags;
|
| 134 | u32 secid;
|
| 135 | int size;
|
| 136 | u64 mediates;
|
| 137 | union {
|
| 138 | struct {
|
| 139 | |
| 140 | * rules[] is determined by the profile
|
| 141 | * profile[1] is poison or null as guard
|
| 142 |
|
| 143 | struct aa_profile *profile[2];
|
| 144 | DECLARE_FLEX_ARRAY(struct aa_ruleset *, rules);
|
| 145 | };
|
| 146 | DECLARE_FLEX_ARRAY(struct aa_profile *, vec);
|
| 147 | };
|
| 148 | };
|
| 149 |
|
| 150 | #define last_error(E, FN) \
|
| 151 | do { \
|
| 152 | int __subE = (FN); \
|
| 153 | if (__subE) \
|
| 154 | (E) = __subE; \
|
| 155 | } while (0)
|
| 156 |
|
| 157 | #define label_isprofile(X) ((X)->flags & FLAG_PROFILE)
|
| 158 | #define label_unconfined(X) ((X)->flags & FLAG_UNCONFINED)
|
| 159 | #define unconfined(X) label_unconfined(X)
|
| 160 | #define label_is_stale(X) ((X)->flags & FLAG_STALE)
|
| 161 | #define __label_make_stale(X) ((X)->flags |= FLAG_STALE)
|
| 162 | #define labels_ns(X) (vec_ns(&((X)->vec[0]), (X)->size))
|
| 163 | #define labels_set(X) (&labels_ns(X)->labels)
|
| 164 | #define labels_view(X) labels_ns(X)
|
| 165 | #define labels_profile(X) ((X)->vec[(X)->size - 1])
|
| 166 |
|
| 167 |
|
| 168 | int aa_label_next_confined(const struct aa_label *l, int i);
|
| 169 |
|
| 170 |
|
| 171 | #define label_for_each(I, L, P) \
|
| 172 | for ((I).i = 0; ((P) = (L)->vec[(I).i]); ++((I).i))
|
| 173 |
|
| 174 |
|
| 175 | #define label_for_each_cont(I, L, P) \
|
| 176 | for (++((I).i); ((P) = (L)->vec[(I).i]); ++((I).i))
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 | #define label_for_each_confined(I, L, P) \
|
| 182 | for ((I).i = aa_label_next_confined((L), 0); \
|
| 183 | ((P) = (L)->vec[(I).i]); \
|
| 184 | (I).i = aa_label_next_confined((L), (I).i + 1))
|
| 185 |
|
| 186 | #define label_for_each_in_merge(I, A, B, P) \
|
| 187 | for ((I).i = (I).j = 0; \
|
| 188 | ((P) = aa_label_next_in_merge(&(I), (A), (B))); \
|
| 189 | )
|
| 190 |
|
| 191 | #define label_for_each_not_in_set(I, SET, SUB, P) \
|
| 192 | for ((I).i = (I).j = 0; \
|
| 193 | ((P) = __aa_label_next_not_in_set(&(I), (SET), (SUB))); \
|
| 194 | )
|
| 195 |
|
| 196 | #define next_in_ns(i, NS, L) \
|
| 197 | ({ \
|
| 198 | typeof(i) ___i = (i); \
|
| 199 | while ((L)->vec[___i] && (L)->vec[___i]->ns != (NS)) \
|
| 200 | (___i)++; \
|
| 201 | (___i); \
|
| 202 | })
|
| 203 |
|
| 204 | #define label_for_each_in_ns(I, NS, L, P) \
|
| 205 | for ((I).i = next_in_ns(0, (NS), (L)); \
|
| 206 | ((P) = (L)->vec[(I).i]); \
|
| 207 | (I).i = next_in_ns((I).i + 1, (NS), (L)))
|
| 208 |
|
| 209 | #define fn_for_each_in_ns(L, P, FN) \
|
| 210 | ({ \
|
| 211 | struct label_it __i; \
|
| 212 | struct aa_ns *__ns = labels_ns(L); \
|
| 213 | int __E = 0; \
|
| 214 | label_for_each_in_ns(__i, __ns, (L), (P)) { \
|
| 215 | last_error(__E, (FN)); \
|
| 216 | } \
|
| 217 | __E; \
|
| 218 | })
|
| 219 |
|
| 220 |
|
| 221 | #define fn_for_each_XXX(L, P, FN, ...) \
|
| 222 | ({ \
|
| 223 | struct label_it i; \
|
| 224 | int __E = 0; \
|
| 225 | label_for_each ## __VA_ARGS__(i, (L), (P)) { \
|
| 226 | last_error(__E, (FN)); \
|
| 227 | } \
|
| 228 | __E; \
|
| 229 | })
|
| 230 |
|
| 231 | #define fn_for_each(L, P, FN) fn_for_each_XXX(L, P, FN)
|
| 232 | #define fn_for_each_confined(L, P, FN) fn_for_each_XXX(L, P, FN, _confined)
|
| 233 |
|
| 234 | #define fn_for_each2_XXX(L1, L2, P, FN, ...) \
|
| 235 | ({ \
|
| 236 | struct label_it i; \
|
| 237 | int __E = 0; \
|
| 238 | label_for_each ## __VA_ARGS__(i, (L1), (L2), (P)) { \
|
| 239 | last_error(__E, (FN)); \
|
| 240 | } \
|
| 241 | __E; \
|
| 242 | })
|
| 243 |
|
| 244 | #define fn_for_each_in_merge(L1, L2, P, FN) \
|
| 245 | fn_for_each2_XXX((L1), (L2), P, FN, _in_merge)
|
| 246 | #define fn_for_each_not_in_set(L1, L2, P, FN) \
|
| 247 | fn_for_each2_XXX((L1), (L2), P, FN, _not_in_set)
|
| 248 |
|
| 249 | static inline bool label_mediates(const struct aa_label *L, unsigned char C)
|
| 250 | {
|
| 251 | return (L)->mediates & (((u64) 1) << (C));
|
| 252 | }
|
| 253 |
|
| 254 | static inline bool label_mediates_safe(const struct aa_label *L, unsigned char C)
|
| 255 | {
|
| 256 | if (C > AA_CLASS_LAST)
|
| 257 | return false;
|
| 258 | return label_mediates(L, C);
|
| 259 | }
|
| 260 |
|
| 261 | void aa_labelset_destroy(struct aa_labelset *ls);
|
| 262 | void aa_labelset_init(struct aa_labelset *ls);
|
| 263 | void __aa_labelset_update_subtree(struct aa_ns *ns);
|
| 264 |
|
| 265 | void aa_label_destroy(struct aa_label *label);
|
| 266 | void aa_label_free(struct aa_label *label);
|
| 267 | void aa_label_kref(struct kref *kref);
|
| 268 | bool aa_label_init(struct aa_label *label, int size, gfp_t gfp);
|
| 269 | struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp);
|
| 270 |
|
| 271 | bool aa_label_is_subset(const struct aa_label *set, const struct aa_label *sub);
|
| 272 | bool aa_label_is_unconfined_subset(const struct aa_label *set,
|
| 273 | const struct aa_label *sub);
|
| 274 | struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
|
| 275 | const struct aa_label *set,
|
| 276 | const struct aa_label *sub);
|
| 277 | bool aa_label_remove(struct aa_label *label);
|
| 278 | struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *l);
|
| 279 | bool aa_label_replace(struct aa_label *old, struct aa_label *new);
|
| 280 | bool aa_label_make_newest(struct aa_labelset *ls, struct aa_label *old,
|
| 281 | struct aa_label *new);
|
| 282 |
|
| 283 | struct aa_profile *aa_label_next_in_merge(struct label_it *I,
|
| 284 | const struct aa_label *a,
|
| 285 | const struct aa_label *b);
|
| 286 | struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b);
|
| 287 | struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
|
| 288 | gfp_t gfp);
|
| 289 |
|
| 290 |
|
| 291 | bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp);
|
| 292 |
|
| 293 | #define FLAGS_NONE 0
|
| 294 | #define FLAG_SHOW_MODE 1
|
| 295 | #define FLAG_VIEW_SUBNS 2
|
| 296 | #define FLAG_HIDDEN_UNCONFINED 4
|
| 297 | #define FLAG_ABS_ROOT 8
|
| 298 | int aa_label_snxprint(char *str, size_t size, struct aa_ns *view,
|
| 299 | struct aa_label *label, int flags);
|
| 300 | int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
|
| 301 | int flags, gfp_t gfp);
|
| 302 | int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
|
| 303 | struct aa_label *label, int flags, gfp_t gfp);
|
| 304 | void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
|
| 305 | struct aa_label *label, int flags, gfp_t gfp);
|
| 306 | void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
|
| 307 | struct aa_label *label, int flags, gfp_t gfp);
|
| 308 | void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
|
| 309 | gfp_t gfp);
|
| 310 | void aa_label_printk(struct aa_label *label, gfp_t gfp);
|
| 311 |
|
| 312 | struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
|
| 313 | size_t n, gfp_t gfp, bool create,
|
| 314 | bool force_stack);
|
| 315 | struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
|
| 316 | gfp_t gfp, bool create, bool force_stack);
|
| 317 |
|
| 318 | static inline const char *aa_label_strn_split(const char *str, int n)
|
| 319 | {
|
| 320 | const char *pos;
|
| 321 | aa_state_t state;
|
| 322 |
|
| 323 | state = aa_dfa_matchn_until(stacksplitdfa, DFA_START, str, n, &pos);
|
| 324 | if (!ACCEPT_TABLE(stacksplitdfa)[state])
|
| 325 | return NULL;
|
| 326 |
|
| 327 | return pos - 3;
|
| 328 | }
|
| 329 |
|
| 330 | static inline const char *aa_label_str_split(const char *str)
|
| 331 | {
|
| 332 | const char *pos;
|
| 333 | aa_state_t state;
|
| 334 |
|
| 335 | state = aa_dfa_match_until(stacksplitdfa, DFA_START, str, &pos);
|
| 336 | if (!ACCEPT_TABLE(stacksplitdfa)[state])
|
| 337 | return NULL;
|
| 338 |
|
| 339 | return pos - 3;
|
| 340 | }
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 | struct aa_perms;
|
| 345 | struct aa_ruleset;
|
| 346 | int aa_label_match(const struct aa_profile *profile, struct aa_ruleset *rules,
|
| 347 | struct aa_label *label, aa_state_t state, bool subns,
|
| 348 | u32 request, struct aa_perms *perms);
|
| 349 |
|
| 350 |
|
| 351 | |
| 352 | * __aa_get_label - get a reference count to uncounted label reference
|
| 353 | * @l: reference to get a count on
|
| 354 | *
|
| 355 | * Returns: pointer to reference OR NULL if race is lost and reference is
|
| 356 | * being repeated.
|
| 357 | * Requires: lock held, and the return code MUST be checked
|
| 358 |
|
| 359 | static inline struct aa_label *__aa_get_label(struct aa_label *l)
|
| 360 | {
|
| 361 | if (l && kref_get_unless_zero(&l->count.count))
|
| 362 | return l;
|
| 363 |
|
| 364 | return NULL;
|
| 365 | }
|
| 366 |
|
| 367 | static inline struct aa_label *aa_get_label(struct aa_label *l)
|
| 368 | {
|
| 369 | if (l)
|
| 370 | kref_get(&(l->count.count));
|
| 371 |
|
| 372 | return l;
|
| 373 | }
|
| 374 |
|
| 375 |
|
| 376 | |
| 377 | * aa_get_label_rcu - increment refcount on a label that can be replaced
|
| 378 | * @l: pointer to label that can be replaced (NOT NULL)
|
| 379 | *
|
| 380 | * Returns: pointer to a refcounted label.
|
| 381 | * else NULL if no label
|
| 382 |
|
| 383 | static inline struct aa_label *aa_get_label_rcu(struct aa_label __rcu **l)
|
| 384 | {
|
| 385 | struct aa_label *c;
|
| 386 |
|
| 387 | rcu_read_lock();
|
| 388 | do {
|
| 389 | c = rcu_dereference(*l);
|
| 390 | } while (c && !kref_get_unless_zero(&c->count.count));
|
| 391 | rcu_read_unlock();
|
| 392 |
|
| 393 | return c;
|
| 394 | }
|
| 395 |
|
| 396 | |
| 397 | * aa_get_newest_label - find the newest version of @l
|
| 398 | * @l: the label to check for newer versions of
|
| 399 | *
|
| 400 | * Returns: refcounted newest version of @l taking into account
|
| 401 | * replacement, renames and removals
|
| 402 | * return @l.
|
| 403 |
|
| 404 | static inline struct aa_label *aa_get_newest_label(struct aa_label *l)
|
| 405 | {
|
| 406 | if (!l)
|
| 407 | return NULL;
|
| 408 |
|
| 409 | if (label_is_stale(l)) {
|
| 410 | struct aa_label *tmp;
|
| 411 |
|
| 412 | AA_BUG(!l->proxy);
|
| 413 | AA_BUG(!l->proxy->label);
|
| 414 | |
| 415 | * replacement count have gone to 0 and are on their way
|
| 416 | * to destruction. ie. we have a refcounting error
|
| 417 |
|
| 418 | tmp = aa_get_label_rcu(&l->proxy->label);
|
| 419 | AA_BUG(!tmp);
|
| 420 |
|
| 421 | return tmp;
|
| 422 | }
|
| 423 |
|
| 424 | return aa_get_label(l);
|
| 425 | }
|
| 426 |
|
| 427 | |
| 428 | * aa_get_newest_label_condref - find the newest version of @l
|
| 429 | * @l: the label to check for newer versions of
|
| 430 | * @needput: returns whether the reference needs put
|
| 431 | *
|
| 432 | * Returns: refcounted newest version of @l taking into account
|
| 433 | * replacement, renames and removals
|
| 434 | * return @l.
|
| 435 |
|
| 436 | static inline struct aa_label *aa_get_newest_label_condref(struct aa_label *l,
|
| 437 | bool *needput)
|
| 438 | {
|
| 439 | if (l && unlikely(label_is_stale(l))) {
|
| 440 | struct aa_label *tmp;
|
| 441 |
|
| 442 | AA_BUG(!l->proxy);
|
| 443 | AA_BUG(!l->proxy->label);
|
| 444 | |
| 445 | * replacement count have gone to 0 and are on their way
|
| 446 | * to destruction. ie. we have a refcounting error
|
| 447 |
|
| 448 | tmp = aa_get_label_rcu(&l->proxy->label);
|
| 449 | AA_BUG(!tmp);
|
| 450 |
|
| 451 | *needput = true;
|
| 452 | return tmp;
|
| 453 | }
|
| 454 |
|
| 455 | *needput = false;
|
| 456 | return l;
|
| 457 | }
|
| 458 |
|
| 459 | static inline void aa_put_label(struct aa_label *l)
|
| 460 | {
|
| 461 | if (l)
|
| 462 | kref_put(&l->count.count, aa_label_kref);
|
| 463 | }
|
| 464 |
|
| 465 |
|
| 466 | static inline bool __aa_subj_label_is_cached(const struct aa_label *subj_label,
|
| 467 | const struct aa_label *obj_label)
|
| 468 | {
|
| 469 | return aa_label_is_subset(obj_label, subj_label);
|
| 470 | }
|
| 471 |
|
| 472 |
|
| 473 | struct aa_proxy *aa_alloc_proxy(struct aa_label *l, gfp_t gfp);
|
| 474 | void aa_proxy_kref(struct kref *kref);
|
| 475 |
|
| 476 | static inline struct aa_proxy *aa_get_proxy(struct aa_proxy *proxy)
|
| 477 | {
|
| 478 | if (proxy)
|
| 479 | kref_get(&(proxy->count.count));
|
| 480 |
|
| 481 | return proxy;
|
| 482 | }
|
| 483 |
|
| 484 | static inline void aa_put_proxy(struct aa_proxy *proxy)
|
| 485 | {
|
| 486 | if (proxy)
|
| 487 | kref_put(&proxy->count.count, aa_proxy_kref);
|
| 488 | }
|
| 489 |
|
| 490 | void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new);
|
| 491 |
|
| 492 | #endif
|