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 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) \ 41 do { \ 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 56 struct aa_profile; 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, 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 /* struct aa_labelset - set of labels for a namespace 66 * 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 69 * the set. 70 */ 71 struct 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 80 enum label_flags { 81 FLAG_HAT = 1, /* profile is a hat */ 82 FLAG_UNCONFINED = 2, /* label unconfined only if all */ 83 FLAG_NULL = 4, /* profile is null learning profile */ 84 FLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */ 85 FLAG_IMMUTIBLE = 0x10, /* don't allow changes/replacement */ 86 FLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */ 87 FLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */ 88 FLAG_NS_COUNT = 0x80, /* carries NS ref count */ 89 FLAG_IN_TREE = 0x100, /* label is in tree */ 90 FLAG_PROFILE = 0x200, /* label is a profile */ 91 FLAG_EXPLICIT = 0x400, /* explicit static label */ 92 FLAG_STALE = 0x800, /* replaced/removed */ 93 FLAG_RENAMED = 0x1000, /* label has renaming in it */ 94 FLAG_REVOKED = 0x2000, /* label has revocation in it */ 95 FLAG_DEBUG1 = 0x4000, 96 FLAG_DEBUG2 = 0x8000, 97 98 /* These flags must correspond with PATH_flags */ 99 /* TODO: add new path flags */ 100 }; 101 102 struct aa_label; 103 struct aa_proxy { 104 struct kref count; 105 struct aa_label __rcu *label; 106 }; 107 108 struct label_it { 109 int i, j; 110 }; 111 112 /* struct aa_label - lazy labeling struct 113 * @count: ref count of active users 114 * @node: rbtree position 115 * @rcu: rcu callback struct 116 * @proxy: is set to the label that replaced this label 117 * @hname: text representation of the label (MAYBE_NULL) 118 * @flags: stale and other flags - values may change under label set lock 119 * @secid: secid that references this label 120 * @size: number of entries in @ent[] 121 * @ent: set of profiles for label, actual size determined by @size 122 */ 123 struct aa_label { 124 struct kref count; 125 struct rb_node node; 126 struct rcu_head rcu; 127 struct aa_proxy *proxy; 128 __counted char *hname; 129 long flags; 130 u32 secid; 131 int size; 132 struct aa_profile *vec[]; 133 }; 134 135 #define last_error(E, FN) \ 136 do { \ 137 int __subE = (FN); \ 138 if (__subE) \ 139 (E) = __subE; \ 140 } while (0) 141 142 #define label_isprofile(X) ((X)->flags & FLAG_PROFILE) 143 #define label_unconfined(X) ((X)->flags & FLAG_UNCONFINED) 144 #define unconfined(X) label_unconfined(X) 145 #define label_is_stale(X) ((X)->flags & FLAG_STALE) 146 #define __label_make_stale(X) ((X)->flags |= FLAG_STALE) 147 #define labels_ns(X) (vec_ns(&((X)->vec[0]), (X)->size)) 148 #define labels_set(X) (&labels_ns(X)->labels) 149 #define labels_view(X) labels_ns(X) 150 #define labels_profile(X) ((X)->vec[(X)->size - 1]) 151 152 153 int aa_label_next_confined(struct aa_label *l, int i); 154 155 /* for each profile in a label */ 156 #define label_for_each(I, L, P) \ 157 for ((I).i = 0; ((P) = (L)->vec[(I).i]); ++((I).i)) 158 159 /* assumes break/goto ended label_for_each */ 160 #define label_for_each_cont(I, L, P) \ 161 for (++((I).i); ((P) = (L)->vec[(I).i]); ++((I).i)) 162 163 164 165 /* for each profile that is enforcing confinement in a label */ 166 #define label_for_each_confined(I, L, P) \ 167 for ((I).i = aa_label_next_confined((L), 0); \ 168 ((P) = (L)->vec[(I).i]); \ 169 (I).i = aa_label_next_confined((L), (I).i + 1)) 170 171 #define label_for_each_in_merge(I, A, B, P) \ 172 for ((I).i = (I).j = 0; \ 173 ((P) = aa_label_next_in_merge(&(I), (A), (B))); \ 174 ) 175 176 #define label_for_each_not_in_set(I, SET, SUB, P) \ 177 for ((I).i = (I).j = 0; \ 178 ((P) = __aa_label_next_not_in_set(&(I), (SET), (SUB))); \ 179 ) 180 181 #define next_in_ns(i, NS, L) \ 182 ({ \ 183 typeof(i) ___i = (i); \ 184 while ((L)->vec[___i] && (L)->vec[___i]->ns != (NS)) \ 185 (___i)++; \ 186 (___i); \ 187 }) 188 189 #define label_for_each_in_ns(I, NS, L, P) \ 190 for ((I).i = next_in_ns(0, (NS), (L)); \ 191 ((P) = (L)->vec[(I).i]); \ 192 (I).i = next_in_ns((I).i + 1, (NS), (L))) 193 194 #define fn_for_each_in_ns(L, P, FN) \ 195 ({ \ 196 struct label_it __i; \ 197 struct aa_ns *__ns = labels_ns(L); \ 198 int __E = 0; \ 199 label_for_each_in_ns(__i, __ns, (L), (P)) { \ 200 last_error(__E, (FN)); \ 201 } \ 202 __E; \ 203 }) 204 205 206 #define fn_for_each_XXX(L, P, FN, ...) \ 207 ({ \ 208 struct label_it i; \ 209 int __E = 0; \ 210 label_for_each ## __VA_ARGS__(i, (L), (P)) { \ 211 last_error(__E, (FN)); \ 212 } \ 213 __E; \ 214 }) 215 216 #define fn_for_each(L, P, FN) fn_for_each_XXX(L, P, FN) 217 #define fn_for_each_confined(L, P, FN) fn_for_each_XXX(L, P, FN, _confined) 218 219 #define fn_for_each2_XXX(L1, L2, P, FN, ...) \ 220 ({ \ 221 struct label_it i; \ 222 int __E = 0; \ 223 label_for_each ## __VA_ARGS__(i, (L1), (L2), (P)) { \ 224 last_error(__E, (FN)); \ 225 } \ 226 __E; \ 227 }) 228 229 #define fn_for_each_in_merge(L1, L2, P, FN) \ 230 fn_for_each2_XXX((L1), (L2), P, FN, _in_merge) 231 #define fn_for_each_not_in_set(L1, L2, P, FN) \ 232 fn_for_each2_XXX((L1), (L2), P, FN, _not_in_set) 233 234 #define LABEL_MEDIATES(L, C) \ 235 ({ \ 236 struct aa_profile *profile; \ 237 struct label_it i; \ 238 int ret = 0; \ 239 label_for_each(i, (L), profile) { \ 240 if (RULE_MEDIATES(&profile->rules, (C))) { \ 241 ret = 1; \ 242 break; \ 243 } \ 244 } \ 245 ret; \ 246 }) 247 248 249 void aa_labelset_destroy(struct aa_labelset *ls); 250 void aa_labelset_init(struct aa_labelset *ls); 251 void __aa_labelset_update_subtree(struct aa_ns *ns); 252 253 void aa_label_destroy(struct aa_label *label); 254 void aa_label_free(struct aa_label *label); 255 void aa_label_kref(struct kref *kref); 256 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp); 257 struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp); 258 259 bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub); 260 bool aa_label_is_unconfined_subset(struct aa_label *set, struct aa_label *sub); 261 struct aa_profile *__aa_label_next_not_in_set(struct label_it *I, 262 struct aa_label *set, 263 struct aa_label *sub); 264 bool aa_label_remove(struct aa_label *label); 265 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *l); 266 bool aa_label_replace(struct aa_label *old, struct aa_label *new); 267 bool aa_label_make_newest(struct aa_labelset *ls, struct aa_label *old, 268 struct aa_label *new); 269 270 struct aa_profile *aa_label_next_in_merge(struct label_it *I, 271 struct aa_label *a, 272 struct aa_label *b); 273 struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b); 274 struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b, 275 gfp_t gfp); 276 277 278 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp); 279 280 #define FLAGS_NONE 0 281 #define FLAG_SHOW_MODE 1 282 #define FLAG_VIEW_SUBNS 2 283 #define FLAG_HIDDEN_UNCONFINED 4 284 #define FLAG_ABS_ROOT 8 285 int aa_label_snxprint(char *str, size_t size, struct aa_ns *view, 286 struct aa_label *label, int flags); 287 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label, 288 int flags, gfp_t gfp); 289 int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns, 290 struct aa_label *label, int flags, gfp_t gfp); 291 void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns, 292 struct aa_label *label, int flags, gfp_t gfp); 293 void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns, 294 struct aa_label *label, int flags, gfp_t gfp); 295 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags, 296 gfp_t gfp); 297 void aa_label_printk(struct aa_label *label, gfp_t gfp); 298 299 struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str, 300 size_t n, gfp_t gfp, bool create, 301 bool force_stack); 302 struct aa_label *aa_label_parse(struct aa_label *base, const char *str, 303 gfp_t gfp, bool create, bool force_stack); 304 305 static inline const char *aa_label_strn_split(const char *str, int n) 306 { 307 const char *pos; 308 aa_state_t state; 309 310 state = aa_dfa_matchn_until(stacksplitdfa, DFA_START, str, n, &pos); 311 if (!ACCEPT_TABLE(stacksplitdfa)[state]) 312 return NULL; 313 314 return pos - 3; 315 } 316 317 static inline const char *aa_label_str_split(const char *str) 318 { 319 const char *pos; 320 aa_state_t state; 321 322 state = aa_dfa_match_until(stacksplitdfa, DFA_START, str, &pos); 323 if (!ACCEPT_TABLE(stacksplitdfa)[state]) 324 return NULL; 325 326 return pos - 3; 327 } 328 329 330 331 struct aa_perms; 332 struct aa_ruleset; 333 int aa_label_match(struct aa_profile *profile, struct aa_ruleset *rules, 334 struct aa_label *label, aa_state_t state, bool subns, 335 u32 request, struct aa_perms *perms); 336 337 338 /** 339 * __aa_get_label - get a reference count to uncounted label reference 340 * @l: reference to get a count on 341 * 342 * Returns: pointer to reference OR NULL if race is lost and reference is 343 * being repeated. 344 * Requires: lock held, and the return code MUST be checked 345 */ 346 static inline struct aa_label *__aa_get_label(struct aa_label *l) 347 { 348 if (l && kref_get_unless_zero(&l->count)) 349 return l; 350 351 return NULL; 352 } 353 354 static inline struct aa_label *aa_get_label(struct aa_label *l) 355 { 356 if (l) 357 kref_get(&(l->count)); 358 359 return l; 360 } 361 362 363 /** 364 * aa_get_label_rcu - increment refcount on a label that can be replaced 365 * @l: pointer to label that can be replaced (NOT NULL) 366 * 367 * Returns: pointer to a refcounted label. 368 * else NULL if no label 369 */ 370 static inline struct aa_label *aa_get_label_rcu(struct aa_label __rcu **l) 371 { 372 struct aa_label *c; 373 374 rcu_read_lock(); 375 do { 376 c = rcu_dereference(*l); 377 } while (c && !kref_get_unless_zero(&c->count)); 378 rcu_read_unlock(); 379 380 return c; 381 } 382 383 /** 384 * aa_get_newest_label - find the newest version of @l 385 * @l: the label to check for newer versions of 386 * 387 * Returns: refcounted newest version of @l taking into account 388 * replacement, renames and removals 389 * return @l. 390 */ 391 static inline struct aa_label *aa_get_newest_label(struct aa_label *l) 392 { 393 if (!l) 394 return NULL; 395 396 if (label_is_stale(l)) { 397 struct aa_label *tmp; 398 399 AA_BUG(!l->proxy); 400 AA_BUG(!l->proxy->label); 401 /* BUG: only way this can happen is @l ref count and its 402 * replacement count have gone to 0 and are on their way 403 * to destruction. ie. we have a refcounting error 404 */ 405 tmp = aa_get_label_rcu(&l->proxy->label); 406 AA_BUG(!tmp); 407 408 return tmp; 409 } 410 411 return aa_get_label(l); 412 } 413 414 static inline void aa_put_label(struct aa_label *l) 415 { 416 if (l) 417 kref_put(&l->count, aa_label_kref); 418 } 419 420 421 struct aa_proxy *aa_alloc_proxy(struct aa_label *l, gfp_t gfp); 422 void aa_proxy_kref(struct kref *kref); 423 424 static inline struct aa_proxy *aa_get_proxy(struct aa_proxy *proxy) 425 { 426 if (proxy) 427 kref_get(&(proxy->count)); 428 429 return proxy; 430 } 431 432 static inline void aa_put_proxy(struct aa_proxy *proxy) 433 { 434 if (proxy) 435 kref_put(&proxy->count, aa_proxy_kref); 436 } 437 438 void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new); 439 440 #endif /* __AA_LABEL_H */ 441