task.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor task related definitions and mediation
  5. *
  6. * Copyright 2017 Canonical Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2 of the
  11. * License.
  12. */
  13. #ifndef __AA_TASK_H
  14. #define __AA_TASK_H
  15. #define task_ctx(X) ((X)->security)
  16. /*
  17. * struct aa_task_ctx - information for current task label change
  18. * @onexec: profile to transition to on next exec (MAY BE NULL)
  19. * @previous: profile the task may return to (MAY BE NULL)
  20. * @token: magic value the task must know for returning to @previous_profile
  21. */
  22. struct aa_task_ctx {
  23. struct aa_label *onexec;
  24. struct aa_label *previous;
  25. u64 token;
  26. };
  27. int aa_replace_current_label(struct aa_label *label);
  28. int aa_set_current_onexec(struct aa_label *label, bool stack);
  29. int aa_set_current_hat(struct aa_label *label, u64 token);
  30. int aa_restore_previous_label(u64 cookie);
  31. struct aa_label *aa_get_task_label(struct task_struct *task);
  32. /**
  33. * aa_alloc_task_ctx - allocate a new task_ctx
  34. * @flags: gfp flags for allocation
  35. *
  36. * Returns: allocated buffer or NULL on failure
  37. */
  38. static inline struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags)
  39. {
  40. return kzalloc(sizeof(struct aa_task_ctx), flags);
  41. }
  42. /**
  43. * aa_free_task_ctx - free a task_ctx
  44. * @ctx: task_ctx to free (MAYBE NULL)
  45. */
  46. static inline void aa_free_task_ctx(struct aa_task_ctx *ctx)
  47. {
  48. if (ctx) {
  49. aa_put_label(ctx->previous);
  50. aa_put_label(ctx->onexec);
  51. kzfree(ctx);
  52. }
  53. }
  54. /**
  55. * aa_dup_task_ctx - duplicate a task context, incrementing reference counts
  56. * @new: a blank task context (NOT NULL)
  57. * @old: the task context to copy (NOT NULL)
  58. */
  59. static inline void aa_dup_task_ctx(struct aa_task_ctx *new,
  60. const struct aa_task_ctx *old)
  61. {
  62. *new = *old;
  63. aa_get_label(new->previous);
  64. aa_get_label(new->onexec);
  65. }
  66. /**
  67. * aa_clear_task_ctx_trans - clear transition tracking info from the ctx
  68. * @ctx: task context to clear (NOT NULL)
  69. */
  70. static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
  71. {
  72. AA_BUG(!ctx);
  73. aa_put_label(ctx->previous);
  74. aa_put_label(ctx->onexec);
  75. ctx->previous = NULL;
  76. ctx->onexec = NULL;
  77. ctx->token = 0;
  78. }
  79. #endif /* __AA_TASK_H */