ipc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor ipc mediation
  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. #include <linux/gfp.h>
  15. #include <linux/ptrace.h>
  16. #include "include/audit.h"
  17. #include "include/capability.h"
  18. #include "include/context.h"
  19. #include "include/policy.h"
  20. /* call back to audit ptrace fields */
  21. static void audit_cb(struct audit_buffer *ab, void *va)
  22. {
  23. struct common_audit_data *sa = va;
  24. audit_log_format(ab, " target=");
  25. audit_log_untrustedstring(ab, sa->aad.target);
  26. }
  27. /**
  28. * aa_audit_ptrace - do auditing for ptrace
  29. * @profile: profile being enforced (NOT NULL)
  30. * @target: profile being traced (NOT NULL)
  31. * @error: error condition
  32. *
  33. * Returns: %0 or error code
  34. */
  35. static int aa_audit_ptrace(struct aa_profile *profile,
  36. struct aa_profile *target, int error)
  37. {
  38. struct common_audit_data sa;
  39. COMMON_AUDIT_DATA_INIT(&sa, NONE);
  40. sa.aad.op = OP_PTRACE;
  41. sa.aad.target = target;
  42. sa.aad.error = error;
  43. return aa_audit(AUDIT_APPARMOR_AUTO, profile, GFP_ATOMIC, &sa,
  44. audit_cb);
  45. }
  46. /**
  47. * aa_may_ptrace - test if tracer task can trace the tracee
  48. * @tracer_task: task who will do the tracing (NOT NULL)
  49. * @tracer: profile of the task doing the tracing (NOT NULL)
  50. * @tracee: task to be traced
  51. * @mode: whether PTRACE_MODE_READ || PTRACE_MODE_ATTACH
  52. *
  53. * Returns: %0 else error code if permission denied or error
  54. */
  55. int aa_may_ptrace(struct task_struct *tracer_task, struct aa_profile *tracer,
  56. struct aa_profile *tracee, unsigned int mode)
  57. {
  58. /* TODO: currently only based on capability, not extended ptrace
  59. * rules,
  60. * Test mode for PTRACE_MODE_READ || PTRACE_MODE_ATTACH
  61. */
  62. if (unconfined(tracer) || tracer == tracee)
  63. return 0;
  64. /* log this capability request */
  65. return aa_capable(tracer_task, tracer, CAP_SYS_PTRACE, 1);
  66. }
  67. /**
  68. * aa_ptrace - do ptrace permission check and auditing
  69. * @tracer: task doing the tracing (NOT NULL)
  70. * @tracee: task being traced (NOT NULL)
  71. * @mode: ptrace mode either PTRACE_MODE_READ || PTRACE_MODE_ATTACH
  72. *
  73. * Returns: %0 else error code if permission denied or error
  74. */
  75. int aa_ptrace(struct task_struct *tracer, struct task_struct *tracee,
  76. unsigned int mode)
  77. {
  78. /*
  79. * tracer can ptrace tracee when
  80. * - tracer is unconfined ||
  81. * - tracer is in complain mode
  82. * - tracer has rules allowing it to trace tracee currently this is:
  83. * - confined by the same profile ||
  84. * - tracer profile has CAP_SYS_PTRACE
  85. */
  86. struct aa_profile *tracer_p;
  87. /* cred released below */
  88. const struct cred *cred = get_task_cred(tracer);
  89. int error = 0;
  90. tracer_p = aa_cred_profile(cred);
  91. if (!unconfined(tracer_p)) {
  92. /* lcred released below */
  93. const struct cred *lcred = get_task_cred(tracee);
  94. struct aa_profile *tracee_p = aa_cred_profile(lcred);
  95. error = aa_may_ptrace(tracer, tracer_p, tracee_p, mode);
  96. error = aa_audit_ptrace(tracer_p, tracee_p, error);
  97. put_cred(lcred);
  98. }
  99. put_cred(cred);
  100. return error;
  101. }