PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/security/apparmor/include/label.h

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