context.h 4.3 KB

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