0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef __AA_LABEL_H
0011 #define __AA_LABEL_H
0012
0013 #include <linux/atomic.h>
0014 #include <linux/audit.h>
0015 #include <linux/rbtree.h>
0016 #include <linux/rcupdate.h>
0017
0018 #include "apparmor.h"
0019 #include "lib.h"
0020
0021 struct aa_ns;
0022
0023 #define LOCAL_VEC_ENTRIES 8
0024 #define DEFINE_VEC(T, V) \
0025 struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES]; \
0026 struct aa_ ## T **(V)
0027
0028 #define vec_setup(T, V, N, GFP) \
0029 ({ \
0030 if ((N) <= LOCAL_VEC_ENTRIES) { \
0031 typeof(N) i; \
0032 (V) = (_ ## V ## _localtmp); \
0033 for (i = 0; i < (N); i++) \
0034 (V)[i] = NULL; \
0035 } else \
0036 (V) = kzalloc(sizeof(struct aa_ ## T *) * (N), (GFP)); \
0037 (V) ? 0 : -ENOMEM; \
0038 })
0039
0040 #define vec_cleanup(T, V, N) \
0041 do { \
0042 int i; \
0043 for (i = 0; i < (N); i++) { \
0044 if (!IS_ERR_OR_NULL((V)[i])) \
0045 aa_put_ ## T((V)[i]); \
0046 } \
0047 if ((V) != _ ## V ## _localtmp) \
0048 kfree(V); \
0049 } while (0)
0050
0051 #define vec_last(VEC, SIZE) ((VEC)[(SIZE) - 1])
0052 #define vec_ns(VEC, SIZE) (vec_last((VEC), (SIZE))->ns)
0053 #define vec_labelset(VEC, SIZE) (&vec_ns((VEC), (SIZE))->labels)
0054 #define cleanup_domain_vec(V, L) cleanup_label_vec((V), (L)->size)
0055
0056 struct aa_profile;
0057 #define VEC_FLAG_TERMINATE 1
0058 int aa_vec_unique(struct aa_profile **vec, int n, int flags);
0059 struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
0060 gfp_t gfp);
0061 #define aa_sort_and_merge_vec(N, V) \
0062 aa_sort_and_merge_profiles((N), (struct aa_profile **)(V))
0063
0064
0065
0066
0067
0068
0069
0070
0071 struct aa_labelset {
0072 rwlock_t lock;
0073
0074 struct rb_root root;
0075 };
0076
0077 #define __labelset_for_each(LS, N) \
0078 for ((N) = rb_first(&(LS)->root); (N); (N) = rb_next(N))
0079
0080 enum label_flags {
0081 FLAG_HAT = 1,
0082 FLAG_UNCONFINED = 2,
0083 FLAG_NULL = 4,
0084 FLAG_IX_ON_NAME_ERROR = 8,
0085 FLAG_IMMUTIBLE = 0x10,
0086 FLAG_USER_DEFINED = 0x20,
0087 FLAG_NO_LIST_REF = 0x40,
0088 FLAG_NS_COUNT = 0x80,
0089 FLAG_IN_TREE = 0x100,
0090 FLAG_PROFILE = 0x200,
0091 FLAG_EXPLICIT = 0x400,
0092 FLAG_STALE = 0x800,
0093 FLAG_RENAMED = 0x1000,
0094 FLAG_REVOKED = 0x2000,
0095 FLAG_DEBUG1 = 0x4000,
0096 FLAG_DEBUG2 = 0x8000,
0097
0098
0099
0100 };
0101
0102 struct aa_label;
0103 struct aa_proxy {
0104 struct kref count;
0105 struct aa_label __rcu *label;
0106 };
0107
0108 struct label_it {
0109 int i, j;
0110 };
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 struct aa_label {
0124 struct kref count;
0125 struct rb_node node;
0126 struct rcu_head rcu;
0127 struct aa_proxy *proxy;
0128 __counted char *hname;
0129 long flags;
0130 u32 secid;
0131 int size;
0132 struct aa_profile *vec[];
0133 };
0134
0135 #define last_error(E, FN) \
0136 do { \
0137 int __subE = (FN); \
0138 if (__subE) \
0139 (E) = __subE; \
0140 } while (0)
0141
0142 #define label_isprofile(X) ((X)->flags & FLAG_PROFILE)
0143 #define label_unconfined(X) ((X)->flags & FLAG_UNCONFINED)
0144 #define unconfined(X) label_unconfined(X)
0145 #define label_is_stale(X) ((X)->flags & FLAG_STALE)
0146 #define __label_make_stale(X) ((X)->flags |= FLAG_STALE)
0147 #define labels_ns(X) (vec_ns(&((X)->vec[0]), (X)->size))
0148 #define labels_set(X) (&labels_ns(X)->labels)
0149 #define labels_view(X) labels_ns(X)
0150 #define labels_profile(X) ((X)->vec[(X)->size - 1])
0151
0152
0153 int aa_label_next_confined(struct aa_label *l, int i);
0154
0155
0156 #define label_for_each(I, L, P) \
0157 for ((I).i = 0; ((P) = (L)->vec[(I).i]); ++((I).i))
0158
0159
0160 #define label_for_each_cont(I, L, P) \
0161 for (++((I).i); ((P) = (L)->vec[(I).i]); ++((I).i))
0162
0163 #define next_comb(I, L1, L2) \
0164 do { \
0165 (I).j++; \
0166 if ((I).j >= (L2)->size) { \
0167 (I).i++; \
0168 (I).j = 0; \
0169 } \
0170 } while (0)
0171
0172
0173
0174 #define label_for_each_comb(I, L1, L2, P1, P2) \
0175 for ((I).i = (I).j = 0; \
0176 ((P1) = (L1)->vec[(I).i]) && ((P2) = (L2)->vec[(I).j]); \
0177 (I) = next_comb(I, L1, L2))
0178
0179 #define fn_for_each_comb(L1, L2, P1, P2, FN) \
0180 ({ \
0181 struct label_it i; \
0182 int __E = 0; \
0183 label_for_each_comb(i, (L1), (L2), (P1), (P2)) { \
0184 last_error(__E, (FN)); \
0185 } \
0186 __E; \
0187 })
0188
0189
0190 #define label_for_each_confined(I, L, P) \
0191 for ((I).i = aa_label_next_confined((L), 0); \
0192 ((P) = (L)->vec[(I).i]); \
0193 (I).i = aa_label_next_confined((L), (I).i + 1))
0194
0195 #define label_for_each_in_merge(I, A, B, P) \
0196 for ((I).i = (I).j = 0; \
0197 ((P) = aa_label_next_in_merge(&(I), (A), (B))); \
0198 )
0199
0200 #define label_for_each_not_in_set(I, SET, SUB, P) \
0201 for ((I).i = (I).j = 0; \
0202 ((P) = __aa_label_next_not_in_set(&(I), (SET), (SUB))); \
0203 )
0204
0205 #define next_in_ns(i, NS, L) \
0206 ({ \
0207 typeof(i) ___i = (i); \
0208 while ((L)->vec[___i] && (L)->vec[___i]->ns != (NS)) \
0209 (___i)++; \
0210 (___i); \
0211 })
0212
0213 #define label_for_each_in_ns(I, NS, L, P) \
0214 for ((I).i = next_in_ns(0, (NS), (L)); \
0215 ((P) = (L)->vec[(I).i]); \
0216 (I).i = next_in_ns((I).i + 1, (NS), (L)))
0217
0218 #define fn_for_each_in_ns(L, P, FN) \
0219 ({ \
0220 struct label_it __i; \
0221 struct aa_ns *__ns = labels_ns(L); \
0222 int __E = 0; \
0223 label_for_each_in_ns(__i, __ns, (L), (P)) { \
0224 last_error(__E, (FN)); \
0225 } \
0226 __E; \
0227 })
0228
0229
0230 #define fn_for_each_XXX(L, P, FN, ...) \
0231 ({ \
0232 struct label_it i; \
0233 int __E = 0; \
0234 label_for_each ## __VA_ARGS__(i, (L), (P)) { \
0235 last_error(__E, (FN)); \
0236 } \
0237 __E; \
0238 })
0239
0240 #define fn_for_each(L, P, FN) fn_for_each_XXX(L, P, FN)
0241 #define fn_for_each_confined(L, P, FN) fn_for_each_XXX(L, P, FN, _confined)
0242
0243 #define fn_for_each2_XXX(L1, L2, P, FN, ...) \
0244 ({ \
0245 struct label_it i; \
0246 int __E = 0; \
0247 label_for_each ## __VA_ARGS__(i, (L1), (L2), (P)) { \
0248 last_error(__E, (FN)); \
0249 } \
0250 __E; \
0251 })
0252
0253 #define fn_for_each_in_merge(L1, L2, P, FN) \
0254 fn_for_each2_XXX((L1), (L2), P, FN, _in_merge)
0255 #define fn_for_each_not_in_set(L1, L2, P, FN) \
0256 fn_for_each2_XXX((L1), (L2), P, FN, _not_in_set)
0257
0258 #define LABEL_MEDIATES(L, C) \
0259 ({ \
0260 struct aa_profile *profile; \
0261 struct label_it i; \
0262 int ret = 0; \
0263 label_for_each(i, (L), profile) { \
0264 if (PROFILE_MEDIATES(profile, (C))) { \
0265 ret = 1; \
0266 break; \
0267 } \
0268 } \
0269 ret; \
0270 })
0271
0272
0273 void aa_labelset_destroy(struct aa_labelset *ls);
0274 void aa_labelset_init(struct aa_labelset *ls);
0275 void __aa_labelset_update_subtree(struct aa_ns *ns);
0276
0277 void aa_label_destroy(struct aa_label *label);
0278 void aa_label_free(struct aa_label *label);
0279 void aa_label_kref(struct kref *kref);
0280 bool aa_label_init(struct aa_label *label, int size, gfp_t gfp);
0281 struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp);
0282
0283 bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub);
0284 bool aa_label_is_unconfined_subset(struct aa_label *set, struct aa_label *sub);
0285 struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
0286 struct aa_label *set,
0287 struct aa_label *sub);
0288 bool aa_label_remove(struct aa_label *label);
0289 struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *l);
0290 bool aa_label_replace(struct aa_label *old, struct aa_label *new);
0291 bool aa_label_make_newest(struct aa_labelset *ls, struct aa_label *old,
0292 struct aa_label *new);
0293
0294 struct aa_label *aa_label_find(struct aa_label *l);
0295
0296 struct aa_profile *aa_label_next_in_merge(struct label_it *I,
0297 struct aa_label *a,
0298 struct aa_label *b);
0299 struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b);
0300 struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
0301 gfp_t gfp);
0302
0303
0304 bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp);
0305
0306 #define FLAGS_NONE 0
0307 #define FLAG_SHOW_MODE 1
0308 #define FLAG_VIEW_SUBNS 2
0309 #define FLAG_HIDDEN_UNCONFINED 4
0310 #define FLAG_ABS_ROOT 8
0311 int aa_label_snxprint(char *str, size_t size, struct aa_ns *view,
0312 struct aa_label *label, int flags);
0313 int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
0314 int flags, gfp_t gfp);
0315 int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
0316 struct aa_label *label, int flags, gfp_t gfp);
0317 void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
0318 struct aa_label *label, int flags, gfp_t gfp);
0319 void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
0320 struct aa_label *label, int flags, gfp_t gfp);
0321 void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
0322 gfp_t gfp);
0323 void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp);
0324 void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp);
0325 void aa_label_printk(struct aa_label *label, gfp_t gfp);
0326
0327 struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
0328 size_t n, gfp_t gfp, bool create,
0329 bool force_stack);
0330 struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
0331 gfp_t gfp, bool create, bool force_stack);
0332
0333 static inline const char *aa_label_strn_split(const char *str, int n)
0334 {
0335 const char *pos;
0336 unsigned int state;
0337
0338 state = aa_dfa_matchn_until(stacksplitdfa, DFA_START, str, n, &pos);
0339 if (!ACCEPT_TABLE(stacksplitdfa)[state])
0340 return NULL;
0341
0342 return pos - 3;
0343 }
0344
0345 static inline const char *aa_label_str_split(const char *str)
0346 {
0347 const char *pos;
0348 unsigned int state;
0349
0350 state = aa_dfa_match_until(stacksplitdfa, DFA_START, str, &pos);
0351 if (!ACCEPT_TABLE(stacksplitdfa)[state])
0352 return NULL;
0353
0354 return pos - 3;
0355 }
0356
0357
0358
0359 struct aa_perms;
0360 int aa_label_match(struct aa_profile *profile, struct aa_label *label,
0361 unsigned int state, bool subns, u32 request,
0362 struct aa_perms *perms);
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373 static inline struct aa_label *__aa_get_label(struct aa_label *l)
0374 {
0375 if (l && kref_get_unless_zero(&l->count))
0376 return l;
0377
0378 return NULL;
0379 }
0380
0381 static inline struct aa_label *aa_get_label(struct aa_label *l)
0382 {
0383 if (l)
0384 kref_get(&(l->count));
0385
0386 return l;
0387 }
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397 static inline struct aa_label *aa_get_label_rcu(struct aa_label __rcu **l)
0398 {
0399 struct aa_label *c;
0400
0401 rcu_read_lock();
0402 do {
0403 c = rcu_dereference(*l);
0404 } while (c && !kref_get_unless_zero(&c->count));
0405 rcu_read_unlock();
0406
0407 return c;
0408 }
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418 static inline struct aa_label *aa_get_newest_label(struct aa_label *l)
0419 {
0420 if (!l)
0421 return NULL;
0422
0423 if (label_is_stale(l)) {
0424 struct aa_label *tmp;
0425
0426 AA_BUG(!l->proxy);
0427 AA_BUG(!l->proxy->label);
0428
0429
0430
0431
0432 tmp = aa_get_label_rcu(&l->proxy->label);
0433 AA_BUG(!tmp);
0434
0435 return tmp;
0436 }
0437
0438 return aa_get_label(l);
0439 }
0440
0441 static inline void aa_put_label(struct aa_label *l)
0442 {
0443 if (l)
0444 kref_put(&l->count, aa_label_kref);
0445 }
0446
0447
0448 struct aa_proxy *aa_alloc_proxy(struct aa_label *l, gfp_t gfp);
0449 void aa_proxy_kref(struct kref *kref);
0450
0451 static inline struct aa_proxy *aa_get_proxy(struct aa_proxy *proxy)
0452 {
0453 if (proxy)
0454 kref_get(&(proxy->count));
0455
0456 return proxy;
0457 }
0458
0459 static inline void aa_put_proxy(struct aa_proxy *proxy)
0460 {
0461 if (proxy)
0462 kref_put(&proxy->count, aa_proxy_kref);
0463 }
0464
0465 void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new);
0466
0467 #endif