label.h 13 KB

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