lib.h 8.1 KB

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