label.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor label definitions
  5. *
  6. * Copyright 2017 Canonical Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2 of the
  11. * License.
  12. */
  13. #ifndef __AA_LABEL_H
  14. #define __AA_LABEL_H
  15. #include <linux/atomic.h>
  16. #include <linux/audit.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/rcupdate.h>
  19. #include "apparmor.h"
  20. #include "lib.h"
  21. struct aa_ns;
  22. #define LOCAL_VEC_ENTRIES 8
  23. #define DEFINE_VEC(T, V) \
  24. struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES]; \
  25. struct aa_ ## T **(V)
  26. #define vec_setup(T, V, N, GFP) \
  27. ({ \
  28. if ((N) <= LOCAL_VEC_ENTRIES) { \
  29. typeof(N) i; \
  30. (V) = (_ ## V ## _localtmp); \
  31. for (i = 0; i < (N); i++) \
  32. (V)[i] = NULL; \
  33. } else \
  34. (V) = kzalloc(sizeof(struct aa_ ## T *) * (N), (GFP)); \
  35. (V) ? 0 : -ENOMEM; \
  36. })
  37. #define vec_cleanup(T, V, N) \
  38. do { \
  39. int i; \
  40. for (i = 0; i < (N); i++) { \
  41. if (!IS_ERR_OR_NULL((V)[i])) \
  42. aa_put_ ## T((V)[i]); \
  43. } \
  44. if ((V) != _ ## V ## _localtmp) \
  45. kfree(V); \
  46. } while (0)
  47. #define vec_last(VEC, SIZE) ((VEC)[(SIZE) - 1])
  48. #define vec_ns(VEC, SIZE) (vec_last((VEC), (SIZE))->ns)
  49. #define vec_labelset(VEC, SIZE) (&vec_ns((VEC), (SIZE))->labels)
  50. #define cleanup_domain_vec(V, L) cleanup_label_vec((V), (L)->size)
  51. struct aa_profile;
  52. #define VEC_FLAG_TERMINATE 1
  53. int aa_vec_unique(struct aa_profile **vec, int n, int flags);
  54. struct aa_label *aa_vec_find_or_create_label(struct aa_profile **vec, int len,
  55. gfp_t gfp);
  56. #define aa_sort_and_merge_vec(N, V) \
  57. aa_sort_and_merge_profiles((N), (struct aa_profile **)(V))
  58. /* struct aa_labelset - set of labels for a namespace
  59. *
  60. * Labels are reference counted; aa_labelset does not contribute to label
  61. * reference counts. Once a label's last refcount is put it is removed from
  62. * the set.
  63. */
  64. struct aa_labelset {
  65. rwlock_t lock;
  66. struct rb_root root;
  67. };
  68. #define __labelset_for_each(LS, N) \
  69. for ((N) = rb_first(&(LS)->root); (N); (N) = rb_next(N))
  70. void aa_labelset_destroy(struct aa_labelset *ls);
  71. void aa_labelset_init(struct aa_labelset *ls);
  72. enum label_flags {
  73. FLAG_HAT = 1, /* profile is a hat */
  74. FLAG_UNCONFINED = 2, /* label unconfined only if all */
  75. FLAG_NULL = 4, /* profile is null learning profile */
  76. FLAG_IX_ON_NAME_ERROR = 8, /* fallback to ix on name lookup fail */
  77. FLAG_IMMUTIBLE = 0x10, /* don't allow changes/replacement */
  78. FLAG_USER_DEFINED = 0x20, /* user based profile - lower privs */
  79. FLAG_NO_LIST_REF = 0x40, /* list doesn't keep profile ref */
  80. FLAG_NS_COUNT = 0x80, /* carries NS ref count */
  81. FLAG_IN_TREE = 0x100, /* label is in tree */
  82. FLAG_PROFILE = 0x200, /* label is a profile */
  83. FLAG_EXPLICIT = 0x400, /* explicit static label */
  84. FLAG_STALE = 0x800, /* replaced/removed */
  85. FLAG_RENAMED = 0x1000, /* label has renaming in it */
  86. FLAG_REVOKED = 0x2000, /* label has revocation in it */
  87. /* These flags must correspond with PATH_flags */
  88. /* TODO: add new path flags */
  89. };
  90. struct aa_label;
  91. struct aa_proxy {
  92. struct kref count;
  93. struct aa_label __rcu *label;
  94. };
  95. struct label_it {
  96. int i, j;
  97. };
  98. /* struct aa_label - lazy labeling struct
  99. * @count: ref count of active users
  100. * @node: rbtree position
  101. * @rcu: rcu callback struct
  102. * @proxy: is set to the label that replaced this label
  103. * @hname: text representation of the label (MAYBE_NULL)
  104. * @flags: stale and other flags - values may change under label set lock
  105. * @secid: secid that references this label
  106. * @size: number of entries in @ent[]
  107. * @ent: set of profiles for label, actual size determined by @size
  108. */
  109. struct aa_label {
  110. struct kref count;
  111. struct rb_node node;
  112. struct rcu_head rcu;
  113. struct aa_proxy *proxy;
  114. __counted char *hname;
  115. long flags;
  116. u32 secid;
  117. int size;
  118. struct aa_profile *vec[];
  119. };
  120. #define last_error(E, FN) \
  121. do { \
  122. int __subE = (FN); \
  123. if (__subE) \
  124. (E) = __subE; \
  125. } while (0)
  126. #define label_isprofile(X) ((X)->flags & FLAG_PROFILE)
  127. #define label_unconfined(X) ((X)->flags & FLAG_UNCONFINED)
  128. #define unconfined(X) label_unconfined(X)
  129. #define label_is_stale(X) ((X)->flags & FLAG_STALE)
  130. #define __label_make_stale(X) ((X)->flags |= FLAG_STALE)
  131. #define labels_ns(X) (vec_ns(&((X)->vec[0]), (X)->size))
  132. #define labels_set(X) (&labels_ns(X)->labels)
  133. #define labels_profile(X) ((X)->vec[(X)->size - 1])
  134. int aa_label_next_confined(struct aa_label *l, int i);
  135. /* for each profile in a label */
  136. #define label_for_each(I, L, P) \
  137. for ((I).i = 0; ((P) = (L)->vec[(I).i]); ++((I).i))
  138. /* assumes break/goto ended label_for_each */
  139. #define label_for_each_cont(I, L, P) \
  140. for (++((I).i); ((P) = (L)->vec[(I).i]); ++((I).i))
  141. #define next_comb(I, L1, L2) \
  142. do { \
  143. (I).j++; \
  144. if ((I).j >= (L2)->size) { \
  145. (I).i++; \
  146. (I).j = 0; \
  147. } \
  148. } while (0)
  149. /* for each combination of P1 in L1, and P2 in L2 */
  150. #define label_for_each_comb(I, L1, L2, P1, P2) \
  151. for ((I).i = (I).j = 0; \
  152. ((P1) = (L1)->vec[(I).i]) && ((P2) = (L2)->vec[(I).j]); \
  153. (I) = next_comb(I, L1, L2))
  154. #define fn_for_each_comb(L1, L2, P1, P2, FN) \
  155. ({ \
  156. struct label_it i; \
  157. int __E = 0; \
  158. label_for_each_comb(i, (L1), (L2), (P1), (P2)) { \
  159. last_error(__E, (FN)); \
  160. } \
  161. __E; \
  162. })
  163. /* for each profile that is enforcing confinement in a label */
  164. #define label_for_each_confined(I, L, P) \
  165. for ((I).i = aa_label_next_confined((L), 0); \
  166. ((P) = (L)->vec[(I).i]); \
  167. (I).i = aa_label_next_confined((L), (I).i + 1))
  168. #define label_for_each_in_merge(I, A, B, P) \
  169. for ((I).i = (I).j = 0; \
  170. ((P) = aa_label_next_in_merge(&(I), (A), (B))); \
  171. )
  172. #define label_for_each_not_in_set(I, SET, SUB, P) \
  173. for ((I).i = (I).j = 0; \
  174. ((P) = __aa_label_next_not_in_set(&(I), (SET), (SUB))); \
  175. )
  176. #define next_in_ns(i, NS, L) \
  177. ({ \
  178. typeof(i) ___i = (i); \
  179. while ((L)->vec[___i] && (L)->vec[___i]->ns != (NS)) \
  180. (___i)++; \
  181. (___i); \
  182. })
  183. #define label_for_each_in_ns(I, NS, L, P) \
  184. for ((I).i = next_in_ns(0, (NS), (L)); \
  185. ((P) = (L)->vec[(I).i]); \
  186. (I).i = next_in_ns((I).i + 1, (NS), (L)))
  187. #define fn_for_each_in_ns(L, P, FN) \
  188. ({ \
  189. struct label_it __i; \
  190. struct aa_ns *__ns = labels_ns(L); \
  191. int __E = 0; \
  192. label_for_each_in_ns(__i, __ns, (L), (P)) { \
  193. last_error(__E, (FN)); \
  194. } \
  195. __E; \
  196. })
  197. #define fn_for_each_XXX(L, P, FN, ...) \
  198. ({ \
  199. struct label_it i; \
  200. int __E = 0; \
  201. label_for_each ## __VA_ARGS__(i, (L), (P)) { \
  202. last_error(__E, (FN)); \
  203. } \
  204. __E; \
  205. })
  206. #define fn_for_each(L, P, FN) fn_for_each_XXX(L, P, FN)
  207. #define fn_for_each_confined(L, P, FN) fn_for_each_XXX(L, P, FN, _confined)
  208. #define fn_for_each2_XXX(L1, L2, P, FN, ...) \
  209. ({ \
  210. struct label_it i; \
  211. int __E = 0; \
  212. label_for_each ## __VA_ARGS__(i, (L1), (L2), (P)) { \
  213. last_error(__E, (FN)); \
  214. } \
  215. __E; \
  216. })
  217. #define fn_for_each_in_merge(L1, L2, P, FN) \
  218. fn_for_each2_XXX((L1), (L2), P, FN, _in_merge)
  219. #define fn_for_each_not_in_set(L1, L2, P, FN) \
  220. fn_for_each2_XXX((L1), (L2), P, FN, _not_in_set)
  221. #define LABEL_MEDIATES(L, C) \
  222. ({ \
  223. struct aa_profile *profile; \
  224. struct label_it i; \
  225. int ret = 0; \
  226. label_for_each(i, (L), profile) { \
  227. if (PROFILE_MEDIATES(profile, (C))) { \
  228. ret = 1; \
  229. break; \
  230. } \
  231. } \
  232. ret; \
  233. })
  234. void aa_labelset_destroy(struct aa_labelset *ls);
  235. void aa_labelset_init(struct aa_labelset *ls);
  236. void __aa_labelset_update_subtree(struct aa_ns *ns);
  237. void aa_label_free(struct aa_label *label);
  238. void aa_label_kref(struct kref *kref);
  239. bool aa_label_init(struct aa_label *label, int size, gfp_t gfp);
  240. struct aa_label *aa_label_alloc(int size, struct aa_proxy *proxy, gfp_t gfp);
  241. bool aa_label_is_subset(struct aa_label *set, struct aa_label *sub);
  242. bool aa_label_is_unconfined_subset(struct aa_label *set, struct aa_label *sub);
  243. struct aa_profile *__aa_label_next_not_in_set(struct label_it *I,
  244. struct aa_label *set,
  245. struct aa_label *sub);
  246. bool aa_label_remove(struct aa_label *label);
  247. struct aa_label *aa_label_insert(struct aa_labelset *ls, struct aa_label *l);
  248. bool aa_label_replace(struct aa_label *old, struct aa_label *new);
  249. bool aa_label_make_newest(struct aa_labelset *ls, struct aa_label *old,
  250. struct aa_label *new);
  251. struct aa_label *aa_label_find(struct aa_label *l);
  252. struct aa_profile *aa_label_next_in_merge(struct label_it *I,
  253. struct aa_label *a,
  254. struct aa_label *b);
  255. struct aa_label *aa_label_find_merge(struct aa_label *a, struct aa_label *b);
  256. struct aa_label *aa_label_merge(struct aa_label *a, struct aa_label *b,
  257. gfp_t gfp);
  258. bool aa_update_label_name(struct aa_ns *ns, struct aa_label *label, gfp_t gfp);
  259. #define FLAGS_NONE 0
  260. #define FLAG_SHOW_MODE 1
  261. #define FLAG_VIEW_SUBNS 2
  262. #define FLAG_HIDDEN_UNCONFINED 4
  263. #define FLAG_ABS_ROOT 8
  264. int aa_label_snxprint(char *str, size_t size, struct aa_ns *view,
  265. struct aa_label *label, int flags);
  266. int aa_label_asxprint(char **strp, struct aa_ns *ns, struct aa_label *label,
  267. int flags, gfp_t gfp);
  268. int aa_label_acntsxprint(char __counted **strp, struct aa_ns *ns,
  269. struct aa_label *label, int flags, gfp_t gfp);
  270. void aa_label_xaudit(struct audit_buffer *ab, struct aa_ns *ns,
  271. struct aa_label *label, int flags, gfp_t gfp);
  272. void aa_label_seq_xprint(struct seq_file *f, struct aa_ns *ns,
  273. struct aa_label *label, int flags, gfp_t gfp);
  274. void aa_label_xprintk(struct aa_ns *ns, struct aa_label *label, int flags,
  275. gfp_t gfp);
  276. void aa_label_audit(struct audit_buffer *ab, struct aa_label *label, gfp_t gfp);
  277. void aa_label_seq_print(struct seq_file *f, struct aa_label *label, gfp_t gfp);
  278. void aa_label_printk(struct aa_label *label, gfp_t gfp);
  279. struct aa_label *aa_label_strn_parse(struct aa_label *base, const char *str,
  280. size_t n, gfp_t gfp, bool create,
  281. bool force_stack);
  282. struct aa_label *aa_label_parse(struct aa_label *base, const char *str,
  283. gfp_t gfp, bool create, bool force_stack);
  284. static inline const char *aa_label_strn_split(const char *str, int n)
  285. {
  286. const char *pos;
  287. unsigned int state;
  288. state = aa_dfa_matchn_until(stacksplitdfa, DFA_START, str, n, &pos);
  289. if (!ACCEPT_TABLE(stacksplitdfa)[state])
  290. return NULL;
  291. return pos - 3;
  292. }
  293. static inline const char *aa_label_str_split(const char *str)
  294. {
  295. const char *pos;
  296. unsigned int state;
  297. state = aa_dfa_match_until(stacksplitdfa, DFA_START, str, &pos);
  298. if (!ACCEPT_TABLE(stacksplitdfa)[state])
  299. return NULL;
  300. return pos - 3;
  301. }
  302. struct aa_perms;
  303. int aa_label_match(struct aa_profile *profile, struct aa_label *label,
  304. unsigned int state, bool subns, u32 request,
  305. struct aa_perms *perms);
  306. /**
  307. * __aa_get_label - get a reference count to uncounted label reference
  308. * @l: reference to get a count on
  309. *
  310. * Returns: pointer to reference OR NULL if race is lost and reference is
  311. * being repeated.
  312. * Requires: lock held, and the return code MUST be checked
  313. */
  314. static inline struct aa_label *__aa_get_label(struct aa_label *l)
  315. {
  316. if (l && kref_get_unless_zero(&l->count))
  317. return l;
  318. return NULL;
  319. }
  320. static inline struct aa_label *aa_get_label(struct aa_label *l)
  321. {
  322. if (l)
  323. kref_get(&(l->count));
  324. return l;
  325. }
  326. /**
  327. * aa_get_label_rcu - increment refcount on a label that can be replaced
  328. * @l: pointer to label that can be replaced (NOT NULL)
  329. *
  330. * Returns: pointer to a refcounted label.
  331. * else NULL if no label
  332. */
  333. static inline struct aa_label *aa_get_label_rcu(struct aa_label __rcu **l)
  334. {
  335. struct aa_label *c;
  336. rcu_read_lock();
  337. do {
  338. c = rcu_dereference(*l);
  339. } while (c && !kref_get_unless_zero(&c->count));
  340. rcu_read_unlock();
  341. return c;
  342. }
  343. /**
  344. * aa_get_newest_label - find the newest version of @l
  345. * @l: the label to check for newer versions of
  346. *
  347. * Returns: refcounted newest version of @l taking into account
  348. * replacement, renames and removals
  349. * return @l.
  350. */
  351. static inline struct aa_label *aa_get_newest_label(struct aa_label *l)
  352. {
  353. if (!l)
  354. return NULL;
  355. if (label_is_stale(l)) {
  356. struct aa_label *tmp;
  357. AA_BUG(!l->proxy);
  358. AA_BUG(!l->proxy->label);
  359. /* BUG: only way this can happen is @l ref count and its
  360. * replacement count have gone to 0 and are on their way
  361. * to destruction. ie. we have a refcounting error
  362. */
  363. tmp = aa_get_label_rcu(&l->proxy->label);
  364. AA_BUG(!tmp);
  365. return tmp;
  366. }
  367. return aa_get_label(l);
  368. }
  369. static inline void aa_put_label(struct aa_label *l)
  370. {
  371. if (l)
  372. kref_put(&l->count, aa_label_kref);
  373. }
  374. struct aa_proxy *aa_alloc_proxy(struct aa_label *l, gfp_t gfp);
  375. void aa_proxy_kref(struct kref *kref);
  376. static inline struct aa_proxy *aa_get_proxy(struct aa_proxy *proxy)
  377. {
  378. if (proxy)
  379. kref_get(&(proxy->count));
  380. return proxy;
  381. }
  382. static inline void aa_put_proxy(struct aa_proxy *proxy)
  383. {
  384. if (proxy)
  385. kref_put(&proxy->count, aa_proxy_kref);
  386. }
  387. void __aa_proxy_redirect(struct aa_label *orig, struct aa_label *new);
  388. #endif /* __AA_LABEL_H */