task.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * @nnp: snapshot of label at time of no_new_privs
  19. * @onexec: profile to transition to on next exec (MAY BE NULL)
  20. * @previous: profile the task may return to (MAY BE NULL)
  21. * @token: magic value the task must know for returning to @previous_profile
  22. */
  23. struct aa_task_ctx {
  24. struct aa_label *nnp;
  25. struct aa_label *onexec;
  26. struct aa_label *previous;
  27. u64 token;
  28. };
  29. int aa_replace_current_label(struct aa_label *label);
  30. int aa_set_current_onexec(struct aa_label *label, bool stack);
  31. int aa_set_current_hat(struct aa_label *label, u64 token);
  32. int aa_restore_previous_label(u64 cookie);
  33. struct aa_label *aa_get_task_label(struct task_struct *task);
  34. /**
  35. * aa_alloc_task_ctx - allocate a new task_ctx
  36. * @flags: gfp flags for allocation
  37. *
  38. * Returns: allocated buffer or NULL on failure
  39. */
  40. static inline struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags)
  41. {
  42. return kzalloc(sizeof(struct aa_task_ctx), flags);
  43. }
  44. /**
  45. * aa_free_task_ctx - free a task_ctx
  46. * @ctx: task_ctx to free (MAYBE NULL)
  47. */
  48. static inline void aa_free_task_ctx(struct aa_task_ctx *ctx)
  49. {
  50. if (ctx) {
  51. aa_put_label(ctx->nnp);
  52. aa_put_label(ctx->previous);
  53. aa_put_label(ctx->onexec);
  54. kzfree(ctx);
  55. }
  56. }
  57. /**
  58. * aa_dup_task_ctx - duplicate a task context, incrementing reference counts
  59. * @new: a blank task context (NOT NULL)
  60. * @old: the task context to copy (NOT NULL)
  61. */
  62. static inline void aa_dup_task_ctx(struct aa_task_ctx *new,
  63. const struct aa_task_ctx *old)
  64. {
  65. *new = *old;
  66. aa_get_label(new->nnp);
  67. aa_get_label(new->previous);
  68. aa_get_label(new->onexec);
  69. }
  70. /**
  71. * aa_clear_task_ctx_trans - clear transition tracking info from the ctx
  72. * @ctx: task context to clear (NOT NULL)
  73. */
  74. static inline void aa_clear_task_ctx_trans(struct aa_task_ctx *ctx)
  75. {
  76. AA_BUG(!ctx);
  77. aa_put_label(ctx->previous);
  78. aa_put_label(ctx->onexec);
  79. ctx->previous = NULL;
  80. ctx->onexec = NULL;
  81. ctx->token = 0;
  82. }
  83. #endif /* __AA_TASK_H */