cred.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor contexts used to associate "labels" to objects.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #ifndef __AA_CONTEXT_H
  11. #define __AA_CONTEXT_H
  12. #include <linux/cred.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched.h>
  15. #include "label.h"
  16. #include "policy_ns.h"
  17. #include "task.h"
  18. static inline struct aa_label *cred_label(const struct cred *cred)
  19. {
  20. struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred;
  21. AA_BUG(!blob);
  22. return *blob;
  23. }
  24. static inline void set_cred_label(const struct cred *cred,
  25. struct aa_label *label)
  26. {
  27. struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred;
  28. AA_BUG(!blob);
  29. *blob = label;
  30. }
  31. /**
  32. * aa_get_newest_cred_label - obtain the newest label on a cred
  33. * @cred: cred to obtain label from (NOT NULL)
  34. *
  35. * Returns: newest version of confining label
  36. */
  37. static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
  38. {
  39. return aa_get_newest_label(cred_label(cred));
  40. }
  41. static inline struct aa_label *aa_get_newest_cred_label_condref(const struct cred *cred,
  42. bool *needput)
  43. {
  44. struct aa_label *l = cred_label(cred);
  45. if (unlikely(label_is_stale(l))) {
  46. *needput = true;
  47. return aa_get_newest_label(l);
  48. }
  49. *needput = false;
  50. return l;
  51. }
  52. static inline void aa_put_label_condref(struct aa_label *l, bool needput)
  53. {
  54. if (unlikely(needput))
  55. aa_put_label(l);
  56. }
  57. /**
  58. * aa_current_raw_label - find the current tasks confining label
  59. *
  60. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  61. *
  62. * This fn will not update the tasks cred to the most up to date version
  63. * of the label so it is safe to call when inside of locks.
  64. */
  65. static inline struct aa_label *aa_current_raw_label(void)
  66. {
  67. return cred_label(current_cred());
  68. }
  69. /**
  70. * aa_get_current_label - get the newest version of the current tasks label
  71. *
  72. * Returns: newest version of confining label (NOT NULL)
  73. *
  74. * This fn will not update the tasks cred, so it is safe inside of locks
  75. *
  76. * The returned reference must be put with aa_put_label()
  77. */
  78. static inline struct aa_label *aa_get_current_label(void)
  79. {
  80. struct aa_label *l = aa_current_raw_label();
  81. if (label_is_stale(l))
  82. return aa_get_newest_label(l);
  83. return aa_get_label(l);
  84. }
  85. /**
  86. * __end_cred_crit_section - end crit section begun with __begin_...
  87. * @label: label obtained from __begin_cred_crit_section
  88. * @needput: output: bool set by __begin_cred_crit_section
  89. *
  90. * While the cred passed to __begin is guaranteed to not change
  91. * and the cred and label could be passed here instead of needput
  92. * using needput with a local var makes it easier for the compiler
  93. * and processor to optimize and speculatively execute the comparison
  94. * than chasing a pointer in the cred struct.
  95. */
  96. static inline void __end_cred_crit_section(struct aa_label *label,
  97. bool needput)
  98. {
  99. if (unlikely(needput))
  100. aa_put_label(label);
  101. }
  102. /**
  103. * __begin_cred_crit_section - @cred's confining label
  104. * @cred: current's cred to start a crit section on its label
  105. * @needput: store whether the label needs to be put when ending crit section
  106. *
  107. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  108. *
  109. * safe to call inside locks
  110. *
  111. * The returned reference must be put with __end_cred_crit_section()
  112. * This must NOT be used if the task cred could be updated within the
  113. * critical section between
  114. * __begin_cred_crit_section() .. __end_cred_crit_section()
  115. *
  116. * The crit section is an optimization to avoid having to get and put
  117. * the newest version of the label. While the cred won't change and
  118. * hence the label it contains won't change, the newest version of the
  119. * label can. During the crit section the newest versions of the label
  120. * will be used until the end of the crit section.
  121. *
  122. * If the label has not been updated at the start of the crit section
  123. * no refcount is taken, the cred's refcount is enough to hold the
  124. * label for the duration of the crit section.
  125. *
  126. * If the label has been updated then a refcount will be taken and the
  127. * newest version of the label will be returned. While the cred label
  128. * and the returned label could be compared at the end of the crit
  129. * section, needput is used because it allows better optimization by
  130. * the compiler and the processor's speculative execution.
  131. */
  132. static inline struct aa_label *__begin_cred_crit_section(const struct cred *cred,
  133. bool *needput)
  134. {
  135. struct aa_label *label = cred_label(cred);
  136. if (label_is_stale(label)) {
  137. *needput = true;
  138. return aa_get_newest_label(label);
  139. }
  140. *needput = false;
  141. return label;
  142. }
  143. /**
  144. * __end_current_label_crit_section - end crit section begun with __begin_...
  145. * @label: label obtained from __begin_current_label_crit_section
  146. * @needput: output: bool set by __begin_current_label_crit_section
  147. *
  148. * wrapper around __end_cred_crit_section() to pair nicely with
  149. * __begin_current_label_crit_section()
  150. */
  151. static inline void __end_current_label_crit_section(struct aa_label *label,
  152. bool needput)
  153. {
  154. __end_cred_crit_section(label, needput);
  155. }
  156. /**
  157. * end_current_label_crit_section - put a reference found with begin_current_label..
  158. * @label: label reference to put
  159. *
  160. * Should only be used with a reference obtained with
  161. * begin_current_label_crit_section and never used in situations where the
  162. * task cred may be updated
  163. */
  164. static inline void end_current_label_crit_section(struct aa_label *label)
  165. {
  166. if (label != aa_current_raw_label())
  167. aa_put_label(label);
  168. }
  169. /**
  170. * __begin_current_label_crit_section - current's confining label
  171. * @needput: store whether the label needs to be put when ending crit section
  172. *
  173. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  174. *
  175. * safe to call inside locks
  176. *
  177. * The returned reference must be put with __end_current_label_crit_section()
  178. * This must NOT be used if the task cred could be updated within the
  179. * critical section between __begin_current_label_crit_section() ..
  180. * __end_current_label_crit_section()
  181. */
  182. static inline struct aa_label *__begin_current_label_crit_section(bool *needput)
  183. {
  184. return __begin_cred_crit_section(current_cred(), needput);
  185. }
  186. /**
  187. * begin_current_label_crit_section - current's confining label and update it
  188. *
  189. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  190. *
  191. * Not safe to call inside locks
  192. *
  193. * The returned reference must be put with end_current_label_crit_section()
  194. * This must NOT be used if the task cred could be updated within the
  195. * critical section between begin_current_label_crit_section() ..
  196. * end_current_label_crit_section()
  197. */
  198. static inline struct aa_label *begin_current_label_crit_section(void)
  199. {
  200. struct aa_label *label = aa_current_raw_label();
  201. might_sleep();
  202. if (label_is_stale(label)) {
  203. label = aa_get_newest_label(label);
  204. if (aa_replace_current_label(label) == 0)
  205. /* task cred will keep the reference */
  206. aa_put_label(label);
  207. }
  208. return label;
  209. }
  210. static inline struct aa_ns *aa_get_current_ns(void)
  211. {
  212. struct aa_label *label;
  213. struct aa_ns *ns;
  214. bool needput;
  215. label = __begin_current_label_crit_section(&needput);
  216. ns = aa_get_ns(labels_ns(label));
  217. __end_current_label_crit_section(label, needput);
  218. return ns;
  219. }
  220. #endif /* __AA_CONTEXT_H */