lib.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * split individual debug cases out in preparation for finer grained
  18. * debug controls in the future.
  19. */
  20. #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args)
  21. #define DEBUG_NONE 0
  22. #define DEBUG_LABEL_ABS_ROOT 1
  23. #define DEBUG_LABEL 2
  24. #define DEBUG_DOMAIN 4
  25. #define DEBUG_POLICY 8
  26. #define DEBUG_INTERFACE 0x10
  27. #define DEBUG_UNPACK 0x20
  28. #define DEBUG_TAGS 0x40
  29. #define DEBUG_ALL 0x7f /* update if new DEBUG_X added */
  30. #define DEBUG_PARSE_ERROR (-1)
  31. #define DEBUG_ON (aa_g_debug != DEBUG_NONE)
  32. #define DEBUG_ABS_ROOT (aa_g_debug & DEBUG_LABEL_ABS_ROOT)
  33. #define AA_DEBUG(opt, fmt, args...) \
  34. do { \
  35. if (aa_g_debug & opt) \
  36. pr_warn_ratelimited("%s: " fmt, __func__, ##args); \
  37. } while (0)
  38. #define AA_DEBUG_LABEL(LAB, X, fmt, args...) \
  39. do { \
  40. if ((LAB)->flags & FLAG_DEBUG1) \
  41. AA_DEBUG(X, fmt, ##args); \
  42. } while (0)
  43. #define AA_DEBUG_PROFILE(PROF, X, fmt...) AA_DEBUG_LABEL(&(PROF)->label, X, ##fmt)
  44. #define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X)
  45. #define AA_BUG(X, args...) \
  46. do { \
  47. _Pragma("GCC diagnostic ignored \"-Wformat-zero-length\""); \
  48. AA_BUG_FMT((X), "" args); \
  49. _Pragma("GCC diagnostic warning \"-Wformat-zero-length\""); \
  50. } while (0)
  51. #ifdef CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS
  52. #define AA_BUG_FMT(X, fmt, args...) \
  53. WARN((X), "AppArmor WARN %s: (" #X "): " fmt, __func__, ##args)
  54. #else
  55. #define AA_BUG_FMT(X, fmt, args...) \
  56. do { \
  57. BUILD_BUG_ON_INVALID(X); \
  58. no_printk(fmt, ##args); \
  59. } while (0)
  60. #endif
  61. int aa_parse_debug_params(const char *str);
  62. int aa_print_debug_params(char *buffer);
  63. #define AA_ERROR(fmt, args...) \
  64. pr_err_ratelimited("AppArmor: " fmt, ##args)
  65. /* Flag indicating whether initialization completed */
  66. extern int apparmor_initialized;
  67. /* semantic split of scope and view */
  68. #define aa_in_scope(SUBJ, OBJ) \
  69. aa_ns_visible(SUBJ, OBJ, false)
  70. #define aa_in_view(SUBJ, OBJ) \
  71. aa_ns_visible(SUBJ, OBJ, true)
  72. #define label_for_each_in_scope(I, NS, L, P) \
  73. label_for_each_in_ns(I, NS, L, P)
  74. #define fn_for_each_in_scope(L, P, FN) \
  75. fn_for_each_in_ns(L, P, FN)
  76. /* fn's in lib */
  77. const char *skipn_spaces(const char *str, size_t n);
  78. const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
  79. size_t *ns_len);
  80. void aa_info_message(const char *str);
  81. /* Security blob offsets */
  82. extern struct lsm_blob_sizes apparmor_blob_sizes;
  83. enum reftype {
  84. REF_NS,
  85. REF_PROXY,
  86. REF_RAWDATA,
  87. };
  88. /* common reference count used by data the shows up in aafs */
  89. struct aa_common_ref {
  90. struct kref count;
  91. enum reftype reftype;
  92. };
  93. /**
  94. * aa_strneq - compare null terminated @str to a non null terminated substring
  95. * @str: a null terminated string
  96. * @sub: a substring, not necessarily null terminated
  97. * @len: length of @sub to compare
  98. *
  99. * The @str string must be full consumed for this to be considered a match
  100. */
  101. static inline bool aa_strneq(const char *str, const char *sub, int len)
  102. {
  103. return !strncmp(str, sub, len) && !str[len];
  104. }
  105. /**
  106. * aa_dfa_null_transition - step to next state after null character
  107. * @dfa: the dfa to match against
  108. * @start: the state of the dfa to start matching in
  109. *
  110. * aa_dfa_null_transition transitions to the next state after a null
  111. * character which is not used in standard matching and is only
  112. * used to separate pairs.
  113. */
  114. static inline aa_state_t aa_dfa_null_transition(struct aa_dfa *dfa,
  115. aa_state_t start)
  116. {
  117. /* the null transition only needs the string's null terminator byte */
  118. return aa_dfa_next(dfa, start, 0);
  119. }
  120. static inline bool path_mediated_fs(struct dentry *dentry)
  121. {
  122. return !(dentry->d_sb->s_flags & SB_NOUSER);
  123. }
  124. struct aa_str_table_ent {
  125. int count;
  126. int size;
  127. char *strs;
  128. };
  129. struct aa_str_table {
  130. int size;
  131. struct aa_str_table_ent *table;
  132. };
  133. bool aa_resize_str_table(struct aa_str_table *t, int newsize, gfp_t gfp);
  134. void aa_destroy_str_table(struct aa_str_table *table);
  135. struct counted_str {
  136. struct kref count;
  137. char name[];
  138. };
  139. #define str_to_counted(str) \
  140. ((struct counted_str *)(str - offsetof(struct counted_str, name)))
  141. #define __counted /* atm just a notation */
  142. void aa_str_kref(struct kref *kref);
  143. char *aa_str_alloc(int size, gfp_t gfp);
  144. static inline __counted char *aa_get_str(__counted char *str)
  145. {
  146. if (str)
  147. kref_get(&(str_to_counted(str)->count));
  148. return str;
  149. }
  150. static inline void aa_put_str(__counted char *str)
  151. {
  152. if (str)
  153. kref_put(&str_to_counted(str)->count, aa_str_kref);
  154. }
  155. /* struct aa_policy - common part of both namespaces and profiles
  156. * @name: name of the object
  157. * @hname - The hierarchical name
  158. * @list: list policy object is on
  159. * @profiles: head of the profiles list contained in the object
  160. */
  161. struct aa_policy {
  162. const char *name;
  163. __counted char *hname;
  164. struct list_head list;
  165. struct list_head profiles;
  166. };
  167. /**
  168. * basename - find the last component of an hname
  169. * @hname: hname to find the base profile name component of (NOT NULL)
  170. *
  171. * Returns: the tail (base profile name) name component of an hname
  172. */
  173. static inline const char *basename(const char *hname)
  174. {
  175. char *split;
  176. hname = strim((char *)hname);
  177. for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
  178. hname = split + 2;
  179. return hname;
  180. }
  181. /**
  182. * __policy_find - find a policy by @name on a policy list
  183. * @head: list to search (NOT NULL)
  184. * @name: name to search for (NOT NULL)
  185. *
  186. * Requires: rcu_read_lock be held
  187. *
  188. * Returns: unrefcounted policy that match @name or NULL if not found
  189. */
  190. static inline struct aa_policy *__policy_find(struct list_head *head,
  191. const char *name)
  192. {
  193. struct aa_policy *policy;
  194. list_for_each_entry_rcu(policy, head, list) {
  195. if (!strcmp(policy->name, name))
  196. return policy;
  197. }
  198. return NULL;
  199. }
  200. /**
  201. * __policy_strn_find - find a policy that's name matches @len chars of @str
  202. * @head: list to search (NOT NULL)
  203. * @str: string to search for (NOT NULL)
  204. * @len: length of match required
  205. *
  206. * Requires: rcu_read_lock be held
  207. *
  208. * Returns: unrefcounted policy that match @str or NULL if not found
  209. *
  210. * if @len == strlen(@strlen) then this is equiv to __policy_find
  211. * other wise it allows searching for policy by a partial match of name
  212. */
  213. static inline struct aa_policy *__policy_strn_find(struct list_head *head,
  214. const char *str, int len)
  215. {
  216. struct aa_policy *policy;
  217. list_for_each_entry_rcu(policy, head, list) {
  218. if (aa_strneq(policy->name, str, len))
  219. return policy;
  220. }
  221. return NULL;
  222. }
  223. bool aa_policy_init(struct aa_policy *policy, const char *prefix,
  224. const char *name, gfp_t gfp);
  225. void aa_policy_destroy(struct aa_policy *policy);
  226. /*
  227. * fn_label_build - abstract out the build of a label transition
  228. * @L: label the transition is being computed for
  229. * @P: profile parameter derived from L by this macro, can be passed to FN
  230. * @GFP: memory allocation type to use
  231. * @FN: fn to call for each profile transition. @P is set to the profile
  232. *
  233. * Returns: new label on success
  234. * ERR_PTR if build @FN fails
  235. * NULL if label_build fails due to low memory conditions
  236. *
  237. * @FN must return a label or ERR_PTR on failure. NULL is not allowed
  238. */
  239. #define fn_label_build(L, P, GFP, FN) \
  240. ({ \
  241. __label__ __do_cleanup, __done; \
  242. struct aa_label *__new_; \
  243. \
  244. if ((L)->size > 1) { \
  245. /* TODO: add cache of transitions already done */ \
  246. struct label_it __i; \
  247. int __j, __k, __count; \
  248. DEFINE_VEC(label, __lvec); \
  249. DEFINE_VEC(profile, __pvec); \
  250. if (vec_setup(label, __lvec, (L)->size, (GFP))) { \
  251. __new_ = NULL; \
  252. goto __done; \
  253. } \
  254. __j = 0; \
  255. label_for_each(__i, (L), (P)) { \
  256. __new_ = (FN); \
  257. AA_BUG(!__new_); \
  258. if (IS_ERR(__new_)) \
  259. goto __do_cleanup; \
  260. __lvec[__j++] = __new_; \
  261. } \
  262. for (__j = __count = 0; __j < (L)->size; __j++) \
  263. __count += __lvec[__j]->size; \
  264. if (!vec_setup(profile, __pvec, __count, (GFP))) { \
  265. for (__j = __k = 0; __j < (L)->size; __j++) { \
  266. label_for_each(__i, __lvec[__j], (P)) \
  267. __pvec[__k++] = aa_get_profile(P); \
  268. } \
  269. __count -= aa_vec_unique(__pvec, __count, 0); \
  270. if (__count > 1) { \
  271. __new_ = aa_vec_find_or_create_label(__pvec,\
  272. __count, (GFP)); \
  273. /* only fails if out of Mem */ \
  274. if (!__new_) \
  275. __new_ = NULL; \
  276. } else \
  277. __new_ = aa_get_label(&__pvec[0]->label); \
  278. vec_cleanup(profile, __pvec, __count); \
  279. } else \
  280. __new_ = NULL; \
  281. __do_cleanup: \
  282. vec_cleanup(label, __lvec, (L)->size); \
  283. } else { \
  284. (P) = labels_profile(L); \
  285. __new_ = (FN); \
  286. } \
  287. __done: \
  288. if (!__new_) \
  289. AA_DEBUG(DEBUG_LABEL, "label build failed\n"); \
  290. (__new_); \
  291. })
  292. #define __fn_build_in_scope(NS, P, NS_FN, OTHER_FN) \
  293. ({ \
  294. struct aa_label *__new; \
  295. if ((P)->ns != (NS)) \
  296. __new = (OTHER_FN); \
  297. else \
  298. __new = (NS_FN); \
  299. (__new); \
  300. })
  301. #define fn_label_build_in_scope(L, P, GFP, NS_FN, OTHER_FN) \
  302. ({ \
  303. fn_label_build((L), (P), (GFP), \
  304. __fn_build_in_scope(labels_ns(L), (P), (NS_FN), (OTHER_FN))); \
  305. })
  306. #endif /* __AA_LIB_H */