cred.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor contexts used to associate "labels" to objects.
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #ifndef __AA_CONTEXT_H
  15. #define __AA_CONTEXT_H
  16. #include <linux/cred.h>
  17. #include <linux/slab.h>
  18. #include <linux/sched.h>
  19. #include "label.h"
  20. #include "policy_ns.h"
  21. #include "task.h"
  22. #define cred_label(X) ((X)->security)
  23. /**
  24. * aa_cred_raw_label - obtain cred's label
  25. * @cred: cred to obtain label from (NOT NULL)
  26. *
  27. * Returns: confining label
  28. *
  29. * does NOT increment reference count
  30. */
  31. static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
  32. {
  33. struct aa_label *label = cred_label(cred);
  34. AA_BUG(!label);
  35. return label;
  36. }
  37. /**
  38. * aa_get_newest_cred_label - obtain the newest label on a cred
  39. * @cred: cred to obtain label from (NOT NULL)
  40. *
  41. * Returns: newest version of confining label
  42. */
  43. static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
  44. {
  45. return aa_get_newest_label(aa_cred_raw_label(cred));
  46. }
  47. /**
  48. * __aa_task_raw_label - retrieve another task's label
  49. * @task: task to query (NOT NULL)
  50. *
  51. * Returns: @task's label without incrementing its ref count
  52. *
  53. * If @task != current needs to be called in RCU safe critical section
  54. */
  55. static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
  56. {
  57. return aa_cred_raw_label(__task_cred(task));
  58. }
  59. /**
  60. * aa_current_raw_label - find the current tasks confining label
  61. *
  62. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  63. *
  64. * This fn will not update the tasks cred to the most up to date version
  65. * of the label so it is safe to call when inside of locks.
  66. */
  67. static inline struct aa_label *aa_current_raw_label(void)
  68. {
  69. return aa_cred_raw_label(current_cred());
  70. }
  71. /**
  72. * aa_get_current_label - get the newest version of the current tasks label
  73. *
  74. * Returns: newest version of confining label (NOT NULL)
  75. *
  76. * This fn will not update the tasks cred, so it is safe inside of locks
  77. *
  78. * The returned reference must be put with aa_put_label()
  79. */
  80. static inline struct aa_label *aa_get_current_label(void)
  81. {
  82. struct aa_label *l = aa_current_raw_label();
  83. if (label_is_stale(l))
  84. return aa_get_newest_label(l);
  85. return aa_get_label(l);
  86. }
  87. #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
  88. /**
  89. * end_label_crit_section - put a reference found with begin_current_label..
  90. * @label: label reference to put
  91. *
  92. * Should only be used with a reference obtained with
  93. * begin_current_label_crit_section and never used in situations where the
  94. * task cred may be updated
  95. */
  96. static inline void end_current_label_crit_section(struct aa_label *label)
  97. {
  98. if (label != aa_current_raw_label())
  99. aa_put_label(label);
  100. }
  101. /**
  102. * __begin_current_label_crit_section - current's confining label
  103. *
  104. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  105. *
  106. * safe to call inside locks
  107. *
  108. * The returned reference must be put with __end_current_label_crit_section()
  109. * This must NOT be used if the task cred could be updated within the
  110. * critical section between __begin_current_label_crit_section() ..
  111. * __end_current_label_crit_section()
  112. */
  113. static inline struct aa_label *__begin_current_label_crit_section(void)
  114. {
  115. struct aa_label *label = aa_current_raw_label();
  116. if (label_is_stale(label))
  117. label = aa_get_newest_label(label);
  118. return label;
  119. }
  120. /**
  121. * begin_current_label_crit_section - current's confining label and update it
  122. *
  123. * Returns: up to date confining label or the ns unconfined label (NOT NULL)
  124. *
  125. * Not safe to call inside locks
  126. *
  127. * The returned reference must be put with end_current_label_crit_section()
  128. * This must NOT be used if the task cred could be updated within the
  129. * critical section between begin_current_label_crit_section() ..
  130. * end_current_label_crit_section()
  131. */
  132. static inline struct aa_label *begin_current_label_crit_section(void)
  133. {
  134. struct aa_label *label = aa_current_raw_label();
  135. might_sleep();
  136. if (label_is_stale(label)) {
  137. label = aa_get_newest_label(label);
  138. if (aa_replace_current_label(label) == 0)
  139. /* task cred will keep the reference */
  140. aa_put_label(label);
  141. }
  142. return label;
  143. }
  144. static inline struct aa_ns *aa_get_current_ns(void)
  145. {
  146. struct aa_label *label;
  147. struct aa_ns *ns;
  148. label = __begin_current_label_crit_section();
  149. ns = aa_get_ns(labels_ns(label));
  150. __end_current_label_crit_section(label);
  151. return ns;
  152. }
  153. #endif /* __AA_CONTEXT_H */