lib.h 7.7 KB

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