lib.h 7.9 KB

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