lib.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. char *aa_split_fqname(char *args, char **ns_name);
  53. const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
  54. size_t *ns_len);
  55. void aa_info_message(const char *str);
  56. void *__aa_kvmalloc(size_t size, gfp_t flags);
  57. static inline void *kvmalloc(size_t size)
  58. {
  59. return __aa_kvmalloc(size, 0);
  60. }
  61. static inline void *kvzalloc(size_t size)
  62. {
  63. return __aa_kvmalloc(size, __GFP_ZERO);
  64. }
  65. /**
  66. * aa_strneq - compare null terminated @str to a non null terminated substring
  67. * @str: a null terminated string
  68. * @sub: a substring, not necessarily null terminated
  69. * @len: length of @sub to compare
  70. *
  71. * The @str string must be full consumed for this to be considered a match
  72. */
  73. static inline bool aa_strneq(const char *str, const char *sub, int len)
  74. {
  75. return !strncmp(str, sub, len) && !str[len];
  76. }
  77. /**
  78. * aa_dfa_null_transition - step to next state after null character
  79. * @dfa: the dfa to match against
  80. * @start: the state of the dfa to start matching in
  81. *
  82. * aa_dfa_null_transition transitions to the next state after a null
  83. * character which is not used in standard matching and is only
  84. * used to separate pairs.
  85. */
  86. static inline unsigned int aa_dfa_null_transition(struct aa_dfa *dfa,
  87. unsigned int start)
  88. {
  89. /* the null transition only needs the string's null terminator byte */
  90. return aa_dfa_next(dfa, start, 0);
  91. }
  92. static inline bool path_mediated_fs(struct dentry *dentry)
  93. {
  94. return !(dentry->d_sb->s_flags & MS_NOUSER);
  95. }
  96. /* struct aa_policy - common part of both namespaces and profiles
  97. * @name: name of the object
  98. * @hname - The hierarchical name
  99. * @list: list policy object is on
  100. * @profiles: head of the profiles list contained in the object
  101. */
  102. struct aa_policy {
  103. const char *name;
  104. const char *hname;
  105. struct list_head list;
  106. struct list_head profiles;
  107. };
  108. /**
  109. * basename - find the last component of an hname
  110. * @name: hname to find the base profile name component of (NOT NULL)
  111. *
  112. * Returns: the tail (base profile name) name component of an hname
  113. */
  114. static inline const char *basename(const char *hname)
  115. {
  116. char *split;
  117. hname = strim((char *)hname);
  118. for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
  119. hname = split + 2;
  120. return hname;
  121. }
  122. /**
  123. * __policy_find - find a policy by @name on a policy list
  124. * @head: list to search (NOT NULL)
  125. * @name: name to search for (NOT NULL)
  126. *
  127. * Requires: rcu_read_lock be held
  128. *
  129. * Returns: unrefcounted policy that match @name or NULL if not found
  130. */
  131. static inline struct aa_policy *__policy_find(struct list_head *head,
  132. const char *name)
  133. {
  134. struct aa_policy *policy;
  135. list_for_each_entry_rcu(policy, head, list) {
  136. if (!strcmp(policy->name, name))
  137. return policy;
  138. }
  139. return NULL;
  140. }
  141. /**
  142. * __policy_strn_find - find a policy that's name matches @len chars of @str
  143. * @head: list to search (NOT NULL)
  144. * @str: string to search for (NOT NULL)
  145. * @len: length of match required
  146. *
  147. * Requires: rcu_read_lock be held
  148. *
  149. * Returns: unrefcounted policy that match @str or NULL if not found
  150. *
  151. * if @len == strlen(@strlen) then this is equiv to __policy_find
  152. * other wise it allows searching for policy by a partial match of name
  153. */
  154. static inline struct aa_policy *__policy_strn_find(struct list_head *head,
  155. const char *str, int len)
  156. {
  157. struct aa_policy *policy;
  158. list_for_each_entry_rcu(policy, head, list) {
  159. if (aa_strneq(policy->name, str, len))
  160. return policy;
  161. }
  162. return NULL;
  163. }
  164. bool aa_policy_init(struct aa_policy *policy, const char *prefix,
  165. const char *name, gfp_t gfp);
  166. void aa_policy_destroy(struct aa_policy *policy);
  167. #endif /* AA_LIB_H */