ipc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "include/ipc.h"
  21. /* call back to audit ptrace fields */
  22. static void audit_cb(struct audit_buffer *ab, void *va)
  23. {
  24. struct common_audit_data *sa = va;
  25. audit_log_format(ab, " peer=");
  26. audit_log_untrustedstring(ab, aad(sa)->peer->base.hname);
  27. }
  28. /**
  29. * aa_audit_ptrace - do auditing for ptrace
  30. * @profile: profile being enforced (NOT NULL)
  31. * @target: profile being traced (NOT NULL)
  32. * @error: error condition
  33. *
  34. * Returns: %0 or error code
  35. */
  36. static int aa_audit_ptrace(struct aa_profile *profile,
  37. struct aa_profile *target, int error)
  38. {
  39. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE);
  40. aad(&sa)->peer = target;
  41. aad(&sa)->error = error;
  42. return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_cb);
  43. }
  44. /**
  45. * aa_may_ptrace - test if tracer task can trace the tracee
  46. * @tracer: profile of the task doing the tracing (NOT NULL)
  47. * @tracee: task to be traced
  48. * @mode: whether PTRACE_MODE_READ || PTRACE_MODE_ATTACH
  49. *
  50. * Returns: %0 else error code if permission denied or error
  51. */
  52. int aa_may_ptrace(struct aa_profile *tracer, struct aa_profile *tracee,
  53. unsigned int mode)
  54. {
  55. /* TODO: currently only based on capability, not extended ptrace
  56. * rules,
  57. * Test mode for PTRACE_MODE_READ || PTRACE_MODE_ATTACH
  58. */
  59. if (unconfined(tracer) || tracer == tracee)
  60. return 0;
  61. /* log this capability request */
  62. return aa_capable(tracer, CAP_SYS_PTRACE, 1);
  63. }
  64. /**
  65. * aa_ptrace - do ptrace permission check and auditing
  66. * @tracer: task doing the tracing (NOT NULL)
  67. * @tracee: task being traced (NOT NULL)
  68. * @mode: ptrace mode either PTRACE_MODE_READ || PTRACE_MODE_ATTACH
  69. *
  70. * Returns: %0 else error code if permission denied or error
  71. */
  72. int aa_ptrace(struct task_struct *tracer, struct task_struct *tracee,
  73. unsigned int mode)
  74. {
  75. /*
  76. * tracer can ptrace tracee when
  77. * - tracer is unconfined ||
  78. * - tracer is in complain mode
  79. * - tracer has rules allowing it to trace tracee currently this is:
  80. * - confined by the same profile ||
  81. * - tracer profile has CAP_SYS_PTRACE
  82. */
  83. struct aa_profile *tracer_p = aa_get_task_profile(tracer);
  84. int error = 0;
  85. if (!unconfined(tracer_p)) {
  86. struct aa_profile *tracee_p = aa_get_task_profile(tracee);
  87. error = aa_may_ptrace(tracer_p, tracee_p, mode);
  88. error = aa_audit_ptrace(tracer_p, tracee_p, error);
  89. aa_put_profile(tracee_p);
  90. }
  91. aa_put_profile(tracer_p);
  92. return error;
  93. }