ipc.c 3.0 KB

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