context.h 5.0 KB

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