cred.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  56. * __aa_task_raw_label - retrieve another task's label
  57. * @task: task to query (NOT NULL)
  58. *
  59. * Returns: @task's label without incrementing its ref count
  60. *
  61. * If @task != current needs to be called in RCU safe critical section
  62. */
  63. static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
  64. {
  65. return aa_cred_raw_label(__task_cred(task));
  66. }
  67. /**
  68. * aa_current_raw_label - find the current tasks confining label
  69. *
  70. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  71. *
  72. * This fn will not update the tasks cred to the most up to date version
  73. * of the label so it is safe to call when inside of locks.
  74. */
  75. static inline struct aa_label *aa_current_raw_label(void)
  76. {
  77. return aa_cred_raw_label(current_cred());
  78. }
  79. /**
  80. * aa_get_current_label - get the newest version of the current tasks label
  81. *
  82. * Returns: newest version of confining label (NOT NULL)
  83. *
  84. * This fn will not update the tasks cred, so it is safe inside of locks
  85. *
  86. * The returned reference must be put with aa_put_label()
  87. */
  88. static inline struct aa_label *aa_get_current_label(void)
  89. {
  90. struct aa_label *l = aa_current_raw_label();
  91. if (label_is_stale(l))
  92. return aa_get_newest_label(l);
  93. return aa_get_label(l);
  94. }
  95. #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
  96. /**
  97. * end_label_crit_section - put a reference found with begin_current_label..
  98. * @label: label reference to put
  99. *
  100. * Should only be used with a reference obtained with
  101. * begin_current_label_crit_section and never used in situations where the
  102. * task cred may be updated
  103. */
  104. static inline void end_current_label_crit_section(struct aa_label *label)
  105. {
  106. if (label != aa_current_raw_label())
  107. aa_put_label(label);
  108. }
  109. /**
  110. * __begin_current_label_crit_section - current's confining label
  111. *
  112. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  113. *
  114. * safe to call inside locks
  115. *
  116. * The returned reference must be put with __end_current_label_crit_section()
  117. * This must NOT be used if the task cred could be updated within the
  118. * critical section between __begin_current_label_crit_section() ..
  119. * __end_current_label_crit_section()
  120. */
  121. static inline struct aa_label *__begin_current_label_crit_section(void)
  122. {
  123. struct aa_label *label = aa_current_raw_label();
  124. if (label_is_stale(label))
  125. label = aa_get_newest_label(label);
  126. return label;
  127. }
  128. /**
  129. * begin_current_label_crit_section - current's confining label and update it
  130. *
  131. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  132. *
  133. * Not safe to call inside locks
  134. *
  135. * The returned reference must be put with end_current_label_crit_section()
  136. * This must NOT be used if the task cred could be updated within the
  137. * critical section between begin_current_label_crit_section() ..
  138. * end_current_label_crit_section()
  139. */
  140. static inline struct aa_label *begin_current_label_crit_section(void)
  141. {
  142. struct aa_label *label = aa_current_raw_label();
  143. might_sleep();
  144. if (label_is_stale(label)) {
  145. label = aa_get_newest_label(label);
  146. if (aa_replace_current_label(label) == 0)
  147. /* task cred will keep the reference */
  148. aa_put_label(label);
  149. }
  150. return label;
  151. }
  152. static inline struct aa_ns *aa_get_current_ns(void)
  153. {
  154. struct aa_label *label;
  155. struct aa_ns *ns;
  156. label = __begin_current_label_crit_section();
  157. ns = aa_get_ns(labels_ns(label));
  158. __end_current_label_crit_section(label);
  159. return ns;
  160. }
  161. #endif /* __AA_CONTEXT_H */