lib.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor lib definitions
  6. *
  7. * 2017 Canonical Ltd.
  8. */
  9. #ifndef __AA_LIB_H
  10. #define __AA_LIB_H
  11. #include <linux/slab.h>
  12. #include <linux/fs.h>
  13. #include <linux/lsm_hooks.h>
  14. #include "match.h"
  15. extern struct aa_dfa *stacksplitdfa;
  16. /*
  17. * DEBUG remains global (no per profile flag) since it is mostly used in sysctl
  18. * which is not related to profile accesses.
  19. */
  20. #define DEBUG_ON (aa_g_debug)
  21. /*
  22. * split individual debug cases out in preparation for finer grained
  23. * debug controls in the future.
  24. */
  25. #define AA_DEBUG_LABEL DEBUG_ON
  26. #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args)
  27. #define AA_DEBUG(fmt, args...) \
  28. do { \
  29. if (DEBUG_ON) \
  30. pr_debug_ratelimited("AppArmor: " fmt, ##args); \
  31. } while (0)
  32. #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X)
  33. #define AA_BUG(X, args...) \
  34. do { \
  35. _Pragma("GCC diagnostic ignored \"-Wformat-zero-length\""); \
  36. AA_BUG_FMT((X), "" args); \
  37. _Pragma("GCC diagnostic warning \"-Wformat-zero-length\""); \
  38. } while (0)
  39. #ifdef CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS
  40. #define AA_BUG_FMT(X, fmt, args...) \
  41. WARN((X), "AppArmor WARN %s: (" #X "): " fmt, __func__, ##args)
  42. #else
  43. #define AA_BUG_FMT(X, fmt, args...) \
  44. do { \
  45. BUILD_BUG_ON_INVALID(X); \
  46. no_printk(fmt, ##args); \
  47. } while (0)
  48. #endif
  49. #define AA_ERROR(fmt, args...) \
  50. pr_err_ratelimited("AppArmor: " fmt, ##args)
  51. /* Flag indicating whether initialization completed */
  52. extern int apparmor_initialized;
  53. /* fn's in lib */
  54. const char *skipn_spaces(const char *str, size_t n);
  55. char *aa_split_fqname(char *args, char **ns_name);
  56. const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
  57. size_t *ns_len);
  58. void aa_info_message(const char *str);
  59. /* Security blob offsets */
  60. extern struct lsm_blob_sizes apparmor_blob_sizes;
  61. /**
  62. * aa_strneq - compare null terminated @str to a non null terminated substring
  63. * @str: a null terminated string
  64. * @sub: a substring, not necessarily null terminated
  65. * @len: length of @sub to compare
  66. *
  67. * The @str string must be full consumed for this to be considered a match
  68. */
  69. static inline bool aa_strneq(const char *str, const char *sub, int len)
  70. {
  71. return !strncmp(str, sub, len) && !str[len];
  72. }
  73. /**
  74. * aa_dfa_null_transition - step to next state after null character
  75. * @dfa: the dfa to match against
  76. * @start: the state of the dfa to start matching in
  77. *
  78. * aa_dfa_null_transition transitions to the next state after a null
  79. * character which is not used in standard matching and is only
  80. * used to separate pairs.
  81. */
  82. static inline aa_state_t aa_dfa_null_transition(struct aa_dfa *dfa,
  83. aa_state_t start)
  84. {
  85. /* the null transition only needs the string's null terminator byte */
  86. return aa_dfa_next(dfa, start, 0);
  87. }
  88. static inline bool path_mediated_fs(struct dentry *dentry)
  89. {
  90. return !(dentry->d_sb->s_flags & SB_NOUSER);
  91. }
  92. struct aa_str_table {
  93. int size;
  94. char **table;
  95. };
  96. void aa_free_str_table(struct aa_str_table *table);
  97. struct counted_str {
  98. struct kref count;
  99. char name[];
  100. };
  101. #define str_to_counted(str) \
  102. ((struct counted_str *)(str - offsetof(struct counted_str, name)))
  103. #define __counted /* atm just a notation */
  104. void aa_str_kref(struct kref *kref);
  105. char *aa_str_alloc(int size, gfp_t gfp);
  106. static inline __counted char *aa_get_str(__counted char *str)
  107. {
  108. if (str)
  109. kref_get(&(str_to_counted(str)->count));
  110. return str;
  111. }
  112. static inline void aa_put_str(__counted char *str)
  113. {
  114. if (str)
  115. kref_put(&str_to_counted(str)->count, aa_str_kref);
  116. }
  117. /* struct aa_policy - common part of both namespaces and profiles
  118. * @name: name of the object
  119. * @hname - The hierarchical name
  120. * @list: list policy object is on
  121. * @profiles: head of the profiles list contained in the object
  122. */
  123. struct aa_policy {
  124. const char *name;
  125. __counted char *hname;
  126. struct list_head list;
  127. struct list_head profiles;
  128. };
  129. /**
  130. * basename - find the last component of an hname
  131. * @name: hname to find the base profile name component of (NOT NULL)
  132. *
  133. * Returns: the tail (base profile name) name component of an hname
  134. */
  135. static inline const char *basename(const char *hname)
  136. {
  137. char *split;
  138. hname = strim((char *)hname);
  139. for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
  140. hname = split + 2;
  141. return hname;
  142. }
  143. /**
  144. * __policy_find - find a policy by @name on a policy list
  145. * @head: list to search (NOT NULL)
  146. * @name: name to search for (NOT NULL)
  147. *
  148. * Requires: rcu_read_lock be held
  149. *
  150. * Returns: unrefcounted policy that match @name or NULL if not found
  151. */
  152. static inline struct aa_policy *__policy_find(struct list_head *head,
  153. const char *name)
  154. {
  155. struct aa_policy *policy;
  156. list_for_each_entry_rcu(policy, head, list) {
  157. if (!strcmp(policy->name, name))
  158. return policy;
  159. }
  160. return NULL;
  161. }
  162. /**
  163. * __policy_strn_find - find a policy that's name matches @len chars of @str
  164. * @head: list to search (NOT NULL)
  165. * @str: string to search for (NOT NULL)
  166. * @len: length of match required
  167. *
  168. * Requires: rcu_read_lock be held
  169. *
  170. * Returns: unrefcounted policy that match @str or NULL if not found
  171. *
  172. * if @len == strlen(@strlen) then this is equiv to __policy_find
  173. * other wise it allows searching for policy by a partial match of name
  174. */
  175. static inline struct aa_policy *__policy_strn_find(struct list_head *head,
  176. const char *str, int len)
  177. {
  178. struct aa_policy *policy;
  179. list_for_each_entry_rcu(policy, head, list) {
  180. if (aa_strneq(policy->name, str, len))
  181. return policy;
  182. }
  183. return NULL;
  184. }
  185. bool aa_policy_init(struct aa_policy *policy, const char *prefix,
  186. const char *name, gfp_t gfp);
  187. void aa_policy_destroy(struct aa_policy *policy);
  188. /*
  189. * fn_label_build - abstract out the build of a label transition
  190. * @L: label the transition is being computed for
  191. * @P: profile parameter derived from L by this macro, can be passed to FN
  192. * @GFP: memory allocation type to use
  193. * @FN: fn to call for each profile transition. @P is set to the profile
  194. *
  195. * Returns: new label on success
  196. * ERR_PTR if build @FN fails
  197. * NULL if label_build fails due to low memory conditions
  198. *
  199. * @FN must return a label or ERR_PTR on failure. NULL is not allowed
  200. */
  201. #define fn_label_build(L, P, GFP, FN) \
  202. ({ \
  203. __label__ __do_cleanup, __done; \
  204. struct aa_label *__new_; \
  205. \
  206. if ((L)->size > 1) { \
  207. /* TODO: add cache of transitions already done */ \
  208. struct label_it __i; \
  209. int __j, __k, __count; \
  210. DEFINE_VEC(label, __lvec); \
  211. DEFINE_VEC(profile, __pvec); \
  212. if (vec_setup(label, __lvec, (L)->size, (GFP))) { \
  213. __new_ = NULL; \
  214. goto __done; \
  215. } \
  216. __j = 0; \
  217. label_for_each(__i, (L), (P)) { \
  218. __new_ = (FN); \
  219. AA_BUG(!__new_); \
  220. if (IS_ERR(__new_)) \
  221. goto __do_cleanup; \
  222. __lvec[__j++] = __new_; \
  223. } \
  224. for (__j = __count = 0; __j < (L)->size; __j++) \
  225. __count += __lvec[__j]->size; \
  226. if (!vec_setup(profile, __pvec, __count, (GFP))) { \
  227. for (__j = __k = 0; __j < (L)->size; __j++) { \
  228. label_for_each(__i, __lvec[__j], (P)) \
  229. __pvec[__k++] = aa_get_profile(P); \
  230. } \
  231. __count -= aa_vec_unique(__pvec, __count, 0); \
  232. if (__count > 1) { \
  233. __new_ = aa_vec_find_or_create_label(__pvec,\
  234. __count, (GFP)); \
  235. /* only fails if out of Mem */ \
  236. if (!__new_) \
  237. __new_ = NULL; \
  238. } else \
  239. __new_ = aa_get_label(&__pvec[0]->label); \
  240. vec_cleanup(profile, __pvec, __count); \
  241. } else \
  242. __new_ = NULL; \
  243. __do_cleanup: \
  244. vec_cleanup(label, __lvec, (L)->size); \
  245. } else { \
  246. (P) = labels_profile(L); \
  247. __new_ = (FN); \
  248. } \
  249. __done: \
  250. if (!__new_) \
  251. AA_DEBUG("label build failed\n"); \
  252. (__new_); \
  253. })
  254. #define __fn_build_in_ns(NS, P, NS_FN, OTHER_FN) \
  255. ({ \
  256. struct aa_label *__new; \
  257. if ((P)->ns != (NS)) \
  258. __new = (OTHER_FN); \
  259. else \
  260. __new = (NS_FN); \
  261. (__new); \
  262. })
  263. #define fn_label_build_in_ns(L, P, GFP, NS_FN, OTHER_FN) \
  264. ({ \
  265. fn_label_build((L), (P), (GFP), \
  266. __fn_build_in_ns(labels_ns(L), (P), (NS_FN), (OTHER_FN))); \
  267. })
  268. #endif /* __AA_LIB_H */