context.h 4.9 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 "policy.h"
  20. #define cred_cxt(X) (X)->security
  21. #define current_cxt() cred_cxt(current_cred())
  22. /* struct aa_file_cxt - the AppArmor context the file was opened in
  23. * @perms: the permission the file was opened with
  24. *
  25. * The file_cxt could currently be directly stored in file->f_security
  26. * as the profile reference is now stored in the f_cred. However the
  27. * cxt struct will expand in the future so we keep the struct.
  28. */
  29. struct aa_file_cxt {
  30. u16 allow;
  31. };
  32. /**
  33. * aa_alloc_file_context - allocate file_cxt
  34. * @gfp: gfp flags for allocation
  35. *
  36. * Returns: file_cxt or NULL on failure
  37. */
  38. static inline struct aa_file_cxt *aa_alloc_file_context(gfp_t gfp)
  39. {
  40. return kzalloc(sizeof(struct aa_file_cxt), gfp);
  41. }
  42. /**
  43. * aa_free_file_context - free a file_cxt
  44. * @cxt: file_cxt to free (MAYBE_NULL)
  45. */
  46. static inline void aa_free_file_context(struct aa_file_cxt *cxt)
  47. {
  48. if (cxt)
  49. kzfree(cxt);
  50. }
  51. /**
  52. * struct aa_task_cxt - primary label for confined tasks
  53. * @profile: the current profile (NOT NULL)
  54. * @exec: profile to transition to on next exec (MAYBE NULL)
  55. * @previous: profile the task may return to (MAYBE NULL)
  56. * @token: magic value the task must know for returning to @previous_profile
  57. *
  58. * Contains the task's current profile (which could change due to
  59. * change_hat). Plus the hat_magic needed during change_hat.
  60. *
  61. * TODO: make so a task can be confined by a stack of contexts
  62. */
  63. struct aa_task_cxt {
  64. struct aa_profile *profile;
  65. struct aa_profile *onexec;
  66. struct aa_profile *previous;
  67. u64 token;
  68. };
  69. struct aa_task_cxt *aa_alloc_task_context(gfp_t flags);
  70. void aa_free_task_context(struct aa_task_cxt *cxt);
  71. void aa_dup_task_context(struct aa_task_cxt *new,
  72. const struct aa_task_cxt *old);
  73. int aa_replace_current_profile(struct aa_profile *profile);
  74. int aa_set_current_onexec(struct aa_profile *profile);
  75. int aa_set_current_hat(struct aa_profile *profile, u64 token);
  76. int aa_restore_previous_profile(u64 cookie);
  77. struct aa_profile *aa_get_task_profile(struct task_struct *task);
  78. /**
  79. * aa_cred_profile - obtain cred's profiles
  80. * @cred: cred to obtain profiles from (NOT NULL)
  81. *
  82. * Returns: confining profile
  83. *
  84. * does NOT increment reference count
  85. */
  86. static inline struct aa_profile *aa_cred_profile(const struct cred *cred)
  87. {
  88. struct aa_task_cxt *cxt = cred_cxt(cred);
  89. BUG_ON(!cxt || !cxt->profile);
  90. return cxt->profile;
  91. }
  92. /**
  93. * __aa_task_profile - retrieve another task's profile
  94. * @task: task to query (NOT NULL)
  95. *
  96. * Returns: @task's profile without incrementing its ref count
  97. *
  98. * If @task != current needs to be called in RCU safe critical section
  99. */
  100. static inline struct aa_profile *__aa_task_profile(struct task_struct *task)
  101. {
  102. return aa_cred_profile(__task_cred(task));
  103. }
  104. /**
  105. * __aa_task_is_confined - determine if @task has any confinement
  106. * @task: task to check confinement of (NOT NULL)
  107. *
  108. * If @task != current needs to be called in RCU safe critical section
  109. */
  110. static inline bool __aa_task_is_confined(struct task_struct *task)
  111. {
  112. return !unconfined(__aa_task_profile(task));
  113. }
  114. /**
  115. * __aa_current_profile - find the current tasks confining profile
  116. *
  117. * Returns: up to date confining profile or the ns unconfined profile (NOT NULL)
  118. *
  119. * This fn will not update the tasks cred to the most up to date version
  120. * of the profile so it is safe to call when inside of locks.
  121. */
  122. static inline struct aa_profile *__aa_current_profile(void)
  123. {
  124. return aa_cred_profile(current_cred());
  125. }
  126. /**
  127. * aa_current_profile - find the current tasks confining profile and do updates
  128. *
  129. * Returns: up to date confining profile or the ns unconfined profile (NOT NULL)
  130. *
  131. * This fn will update the tasks cred structure if the profile has been
  132. * replaced. Not safe to call inside locks
  133. */
  134. static inline struct aa_profile *aa_current_profile(void)
  135. {
  136. const struct aa_task_cxt *cxt = current_cxt();
  137. struct aa_profile *profile;
  138. BUG_ON(!cxt || !cxt->profile);
  139. if (PROFILE_INVALID(cxt->profile)) {
  140. profile = aa_get_newest_profile(cxt->profile);
  141. aa_replace_current_profile(profile);
  142. aa_put_profile(profile);
  143. cxt = current_cxt();
  144. }
  145. return cxt->profile;
  146. }
  147. /**
  148. * aa_clear_task_cxt_trans - clear transition tracking info from the cxt
  149. * @cxt: task context to clear (NOT NULL)
  150. */
  151. static inline void aa_clear_task_cxt_trans(struct aa_task_cxt *cxt)
  152. {
  153. aa_put_profile(cxt->previous);
  154. aa_put_profile(cxt->onexec);
  155. cxt->previous = NULL;
  156. cxt->onexec = NULL;
  157. cxt->token = 0;
  158. }
  159. #endif /* __AA_CONTEXT_H */