1 /* SPDX-License-Identifier: GPL-2.0-only */ 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 /* struct aa_labelset - set of labels for a namespace 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, /* profile is a hat */ 83 FLAG_UNCONFINED = 2, /* label unconfined only if all */ 84 FLAG_NULL = 4, /* profile is null learning profile */ 85 FLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */ 86 FLAG_IMMUTIBLE = 0x10, /* don't allow changes/replacement */ 87 FLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */ 88 FLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */ 89 FLAG_NS_COUNT = 0x80, /* carries NS ref count */ 90 FLAG_IN_TREE = 0x100, /* label is in tree */ 91 FLAG_PROFILE = 0x200, /* label is a profile */ 92 FLAG_EXPLICIT = 0x400, /* explicit static label */ 93 FLAG_STALE = 0x800, /* replaced/removed */ 94 FLAG_RENAMED = 0x1000, /* label has renaming in it */ 95 FLAG_REVOKED = 0x2000, /* label has revocation in it */ 96 FLAG_DEBUG1 = 0x4000, 97 FLAG_DEBUG2 = 0x8000, 98 99 /* These flags must correspond with PATH_flags */ 100 /* TODO: add new path flags */ 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 /* struct aa_label_base - base info of label 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 /* only used is the label is a profile, size of 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 166 static inline struct aa_profile *labels_profile(const struct aa_label *label) 167 { 168 return label->vec[label->size - 1]; 169 } 170 171 172 int aa_label_next_confined(const struct aa_label *l, int i); 173 174 /* for each profile in a label */ 175 #define label_for_each(I, L, P) \ 176 for ((I).i = 0; ((P) = (L)->vec[(I).i]); ++((I).i)) 177 178 /* assumes break/goto ended label_for_each */ 179 #define label_for_each_cont(I, L, P) \ 180 for (++((I).i); ((P) = (L)->vec[(I).i]); ++((I).i)) 181 182 183 184 /* for each profile that is enforcing confinement in a label */ 185 #define label_for_each_confined(I, L, P) \ 186 for ((I).i = aa_label_next_confined((L), 0); \ 187 ((P) = (L)->vec[(I).i]); \ 188 (I).i = aa_label_next_confined((L), (I).i + 1)) 189 190 #define label_for_each_in_merge(I, A, B, P) \ 191 for ((I).i = (I).j = 0; \ 192 ((P) = aa_label_next_in_merge(&(I), (A), (B))); \ 193 ) 194 195 #define label_for_each_not_in_set(I, SET, SUB, P) \ 196 for ((I).i = (I).j = 0; \ 197 ((P) = __aa_label_next_not_in_set(&(I), (SET), (SUB))); \ 198 ) 199 200 #define next_in_ns(i, NS, L) \ 201 ({ \ 202 typeof(i) ___i = (i); \ 203 while ((L)->vec[___i] && (L)->vec[___i]->ns != (NS)) \ 204 (___i)++; \ 205 (___i); \ 206 }) 207 208 #define label_for_each_in_ns(I, NS, L, P) \ 209 for ((I).i = next_in_ns(0, (NS), (L)); \ 210 ((P) = (L)->vec[(I).i]); \ 211 (I).i = next_in_ns((I).i + 1, (NS), (L))) 212 213 #define fn_for_each_in_ns(L, P, FN) \ 214 ({ \ 215 struct label_it __i; \ 216 struct aa_ns *__ns = labels_ns(L); \ 217 int __E = 0; \ 218 label_for_each_in_ns(__i, __ns, (L), (P)) { \ 219 last_error(__E, (FN)); \ 220 } \ 221 __E; \ 222 }) 223 224 225 #define fn_for_each_XXX(L, P, FN, ...) \ 226 ({ \ 227 struct label_it i; \ 228 int __E = 0; \ 229 label_for_each ## __VA_ARGS__(i, (L), (P)) { \ 230 last_error(__E, (FN)); \ 231 } \ 232 __E; \ 233 }) 234 235 #define fn_for_each(L, P, FN) fn_for_each_XXX(L, P, FN) 236 #define fn_for_each_confined(L, P, FN) fn_for_each_XXX(L, P, FN, _confined) 237 238 #define fn_for_each2_XXX(L1, L2, P, FN, ...) \ 239 ({ \ 240 struct label_it i; \ 241 int __E = 0; \ 242 label_for_each ## __VA_ARGS__(i, (L1), (L2), (P)) { \ 243 last_error(__E, (FN)); \ 244 } \ 245 __E; \ 246 }) 247 248 #define fn_for_each_in_merge(L1, L2, P, FN) \ 249 fn_for_each2_XXX((L1), (L2), P, FN, _in_merge) 250 #define fn_for_each_not_in_set(L1, L2, P, FN) \ 251 fn_for_each2_XXX((L1), (L2), P, FN, _not_in_set) 252 253 static inline bool label_mediates(const struct aa_label *L, unsigned char C) 254 { 255 return (L)->mediates & (((u64) 1) << (C)); 256 } 257 258 static inline bool label_mediates_safe(const struct aa_label *L, unsigned char C) 259 { 260 if (C > AA_CLASS_LAST) 261 return false; 262 return label_mediates(L, C); 263 } 264 265 void aa_labelset_destroy(struct aa_labelset *ls); 266 void aa_labelset_init(struct aa_labelset *ls); 267 void __aa_labelset_update_subtree(struct aa_ns *ns); 268 269 void aa_label_destroy(struct aa_label *label); 270 void aa_label_free(struct aa_label *label); 271 void aa_label_kref(struct kref *kref); 272 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp); 273 struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp); 274 275 bool aa_label_is_subset(const struct aa_label *set, const struct aa_label *sub); 276 bool aa_label_is_unconfined_subset(const struct aa_label *set, 277 const struct aa_label *sub); 278 struct aa_profile *__aa_label_next_not_in_set(struct label_it *I, 279 const struct aa_label *set, 280 const struct aa_label *sub); 281 bool aa_label_remove(struct aa_label *label); 282 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *l); 283 bool aa_label_replace(struct aa_label *old, struct aa_label *new); 284 bool aa_label_make_newest(struct aa_labelset *ls, struct aa_label *old, 285 struct aa_label *new); 286 287 struct aa_profile *aa_label_next_in_merge(struct label_it *I, 288 const struct aa_label *a, 289 const struct aa_label *b); 290 struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b); 291 struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b, 292 gfp_t gfp); 293 294 295 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp); 296 297 #define FLAGS_NONE 0 298 #define FLAG_SHOW_MODE 1 299 #define FLAG_VIEW_SUBNS 2 300 #define FLAG_HIDDEN_UNCONFINED 4 301 #define FLAG_ABS_ROOT 8 302 int aa_label_snxprint(char *str, size_t size, struct aa_ns *view, 303 struct aa_label *label, int flags); 304 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label, 305 int flags, gfp_t gfp); 306 int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns, 307 struct aa_label *label, int flags, gfp_t gfp); 308 void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns, 309 struct aa_label *label, int flags, gfp_t gfp); 310 void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns, 311 struct aa_label *label, int flags, gfp_t gfp); 312 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags, 313 gfp_t gfp); 314 void aa_label_printk(struct aa_label *label, gfp_t gfp); 315 316 struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str, 317 size_t n, gfp_t gfp, bool create, 318 bool force_stack); 319 struct aa_label *aa_label_parse(struct aa_label *base, const char *str, 320 gfp_t gfp, bool create, bool force_stack); 321 322 static inline const char *aa_label_strn_split(const char *str, int n) 323 { 324 const char *pos; 325 aa_state_t state; 326 327 state = aa_dfa_matchn_until(stacksplitdfa, DFA_START, str, n, &pos); 328 if (!ACCEPT_TABLE(stacksplitdfa)[state]) 329 return NULL; 330 331 return pos - 3; 332 } 333 334 static inline const char *aa_label_str_split(const char *str) 335 { 336 const char *pos; 337 aa_state_t state; 338 339 state = aa_dfa_match_until(stacksplitdfa, DFA_START, str, &pos); 340 if (!ACCEPT_TABLE(stacksplitdfa)[state]) 341 return NULL; 342 343 return pos - 3; 344 } 345 346 347 348 struct aa_perms; 349 struct aa_ruleset; 350 int aa_label_match(const struct aa_profile *profile, struct aa_ruleset *rules, 351 struct aa_label *label, aa_state_t state, bool subns, 352 u32 request, struct aa_perms *perms); 353 354 355 /** 356 * __aa_get_label - get a reference count to uncounted label reference 357 * @l: reference to get a count on 358 * 359 * Returns: pointer to reference OR NULL if race is lost and reference is 360 * being repeated. 361 * Requires: lock held, and the return code MUST be checked 362 */ 363 static inline struct aa_label *__aa_get_label(struct aa_label *l) 364 { 365 if (l && kref_get_unless_zero(&l->count.count)) 366 return l; 367 368 return NULL; 369 } 370 371 static inline struct aa_label *aa_get_label(struct aa_label *l) 372 { 373 if (l) 374 kref_get(&(l->count.count)); 375 376 return l; 377 } 378 379 380 /** 381 * aa_get_label_rcu - increment refcount on a label that can be replaced 382 * @l: pointer to label that can be replaced (NOT NULL) 383 * 384 * Returns: pointer to a refcounted label. 385 * else NULL if no label 386 */ 387 static inline struct aa_label *aa_get_label_rcu(struct aa_label __rcu **l) 388 { 389 struct aa_label *c; 390 391 rcu_read_lock(); 392 do { 393 c = rcu_dereference(*l); 394 } while (c && !kref_get_unless_zero(&c->count.count)); 395 rcu_read_unlock(); 396 397 return c; 398 } 399 400 /** 401 * aa_get_newest_label - find the newest version of @l 402 * @l: the label to check for newer versions of 403 * 404 * Returns: refcounted newest version of @l taking into account 405 * replacement, renames and removals 406 * return @l. 407 */ 408 static inline struct aa_label *aa_get_newest_label(struct aa_label *l) 409 { 410 if (!l) 411 return NULL; 412 413 if (label_is_stale(l)) { 414 struct aa_label *tmp; 415 416 AA_BUG(!l->proxy); 417 AA_BUG(!l->proxy->label); 418 /* BUG: only way this can happen is @l ref count and its 419 * replacement count have gone to 0 and are on their way 420 * to destruction. ie. we have a refcounting error 421 */ 422 tmp = aa_get_label_rcu(&l->proxy->label); 423 AA_BUG(!tmp); 424 425 return tmp; 426 } 427 428 return aa_get_label(l); 429 } 430 431 /** 432 * aa_get_newest_label_condref - find the newest version of @l 433 * @l: the label to check for newer versions of 434 * @needput: returns whether the reference needs put 435 * 436 * Returns: refcounted newest version of @l taking into account 437 * replacement, renames and removals 438 * return @l. 439 */ 440 static inline struct aa_label *aa_get_newest_label_condref(struct aa_label *l, 441 bool *needput) 442 { 443 if (l && unlikely(label_is_stale(l))) { 444 struct aa_label *tmp; 445 446 AA_BUG(!l->proxy); 447 AA_BUG(!l->proxy->label); 448 /* BUG: only way this can happen is @l ref count and its 449 * replacement count have gone to 0 and are on their way 450 * to destruction. ie. we have a refcounting error 451 */ 452 tmp = aa_get_label_rcu(&l->proxy->label); 453 AA_BUG(!tmp); 454 455 *needput = true; 456 return tmp; 457 } 458 459 *needput = false; 460 return l; 461 } 462 463 static inline void aa_put_label(struct aa_label *l) 464 { 465 if (l) 466 kref_put(&l->count.count, aa_label_kref); 467 } 468 469 /* wrapper fn to indicate semantics of the check */ 470 static inline bool __aa_subj_label_is_cached(const struct aa_label *subj_label, 471 const struct aa_label *obj_label) 472 { 473 return aa_label_is_subset(obj_label, subj_label); 474 } 475 476 477 struct aa_proxy *aa_alloc_proxy(struct aa_label *l, gfp_t gfp); 478 void aa_proxy_kref(struct kref *kref); 479 480 static inline struct aa_proxy *aa_get_proxy(struct aa_proxy *proxy) 481 { 482 if (proxy) 483 kref_get(&(proxy->count.count)); 484 485 return proxy; 486 } 487 488 static inline void aa_put_proxy(struct aa_proxy *proxy) 489 { 490 if (proxy) 491 kref_put(&proxy->count.count, aa_proxy_kref); 492 } 493 494 void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new); 495 496 #endif /* __AA_LABEL_H */ 497