cred.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_cred_raw_label - obtain cred's label
  33. * @cred: cred to obtain label from (NOT NULL)
  34. *
  35. * Returns: confining label
  36. *
  37. * does NOT increment reference count
  38. */
  39. static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
  40. {
  41. struct aa_label *label = cred_label(cred);
  42. AA_BUG(!label);
  43. return label;
  44. }
  45. /**
  46. * aa_get_newest_cred_label - obtain the newest label on a cred
  47. * @cred: cred to obtain label from (NOT NULL)
  48. *
  49. * Returns: newest version of confining label
  50. */
  51. static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
  52. {
  53. return aa_get_newest_label(aa_cred_raw_label(cred));
  54. }
  55. static inline struct aa_label *aa_get_newest_cred_label_condref(const struct cred *cred,
  56. bool *needput)
  57. {
  58. struct aa_label *l = aa_cred_raw_label(cred);
  59. if (unlikely(label_is_stale(l))) {
  60. *needput = true;
  61. return aa_get_newest_label(l);
  62. }
  63. *needput = false;
  64. return l;
  65. }
  66. static inline void aa_put_label_condref(struct aa_label *l, bool needput)
  67. {
  68. if (unlikely(needput))
  69. aa_put_label(l);
  70. }
  71. /**
  72. * aa_current_raw_label - find the current tasks confining label
  73. *
  74. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  75. *
  76. * This fn will not update the tasks cred to the most up to date version
  77. * of the label so it is safe to call when inside of locks.
  78. */
  79. static inline struct aa_label *aa_current_raw_label(void)
  80. {
  81. return aa_cred_raw_label(current_cred());
  82. }
  83. /**
  84. * aa_get_current_label - get the newest version of the current tasks label
  85. *
  86. * Returns: newest version of confining label (NOT NULL)
  87. *
  88. * This fn will not update the tasks cred, so it is safe inside of locks
  89. *
  90. * The returned reference must be put with aa_put_label()
  91. */
  92. static inline struct aa_label *aa_get_current_label(void)
  93. {
  94. struct aa_label *l = aa_current_raw_label();
  95. if (label_is_stale(l))
  96. return aa_get_newest_label(l);
  97. return aa_get_label(l);
  98. }
  99. #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
  100. /**
  101. * end_label_crit_section - put a reference found with begin_current_label..
  102. * @label: label reference to put
  103. *
  104. * Should only be used with a reference obtained with
  105. * begin_current_label_crit_section and never used in situations where the
  106. * task cred may be updated
  107. */
  108. static inline void end_current_label_crit_section(struct aa_label *label)
  109. {
  110. if (label != aa_current_raw_label())
  111. aa_put_label(label);
  112. }
  113. /**
  114. * __begin_current_label_crit_section - current's confining label
  115. *
  116. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  117. *
  118. * safe to call inside locks
  119. *
  120. * The returned reference must be put with __end_current_label_crit_section()
  121. * This must NOT be used if the task cred could be updated within the
  122. * critical section between __begin_current_label_crit_section() ..
  123. * __end_current_label_crit_section()
  124. */
  125. static inline struct aa_label *__begin_current_label_crit_section(void)
  126. {
  127. struct aa_label *label = aa_current_raw_label();
  128. if (label_is_stale(label))
  129. label = aa_get_newest_label(label);
  130. return label;
  131. }
  132. /**
  133. * begin_current_label_crit_section - current's confining label and update it
  134. *
  135. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  136. *
  137. * Not safe to call inside locks
  138. *
  139. * The returned reference must be put with end_current_label_crit_section()
  140. * This must NOT be used if the task cred could be updated within the
  141. * critical section between begin_current_label_crit_section() ..
  142. * end_current_label_crit_section()
  143. */
  144. static inline struct aa_label *begin_current_label_crit_section(void)
  145. {
  146. struct aa_label *label = aa_current_raw_label();
  147. might_sleep();
  148. if (label_is_stale(label)) {
  149. label = aa_get_newest_label(label);
  150. if (aa_replace_current_label(label) == 0)
  151. /* task cred will keep the reference */
  152. aa_put_label(label);
  153. }
  154. return label;
  155. }
  156. static inline struct aa_ns *aa_get_current_ns(void)
  157. {
  158. struct aa_label *label;
  159. struct aa_ns *ns;
  160. label = __begin_current_label_crit_section();
  161. ns = aa_get_ns(labels_ns(label));
  162. __end_current_label_crit_section(label);
  163. return ns;
  164. }
  165. #endif /* __AA_CONTEXT_H */