ipc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor ipc mediation
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2017 Canonical Ltd.
  9. */
  10. #include <linux/gfp.h>
  11. #include <linux/ptrace.h>
  12. #include "include/audit.h"
  13. #include "include/capability.h"
  14. #include "include/cred.h"
  15. #include "include/policy.h"
  16. #include "include/ipc.h"
  17. #include "include/sig_names.h"
  18. /**
  19. * audit_ptrace_mask - convert mask to permission string
  20. * @mask: permission mask to convert
  21. *
  22. * Returns: pointer to static string
  23. */
  24. static const char *audit_ptrace_mask(u32 mask)
  25. {
  26. switch (mask) {
  27. case MAY_READ:
  28. return "read";
  29. case MAY_WRITE:
  30. return "trace";
  31. case AA_MAY_BE_READ:
  32. return "readby";
  33. case AA_MAY_BE_TRACED:
  34. return "tracedby";
  35. }
  36. return "";
  37. }
  38. /* call back to audit ptrace fields */
  39. static void audit_ptrace_cb(struct audit_buffer *ab, void *va)
  40. {
  41. struct common_audit_data *sa = va;
  42. if (aad(sa)->request & AA_PTRACE_PERM_MASK) {
  43. audit_log_format(ab, " requested_mask=\"%s\"",
  44. audit_ptrace_mask(aad(sa)->request));
  45. if (aad(sa)->denied & AA_PTRACE_PERM_MASK) {
  46. audit_log_format(ab, " denied_mask=\"%s\"",
  47. audit_ptrace_mask(aad(sa)->denied));
  48. }
  49. }
  50. audit_log_format(ab, " peer=");
  51. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  52. FLAGS_NONE, GFP_ATOMIC);
  53. }
  54. /* assumes check for PROFILE_MEDIATES is already done */
  55. /* TODO: conditionals */
  56. static int profile_ptrace_perm(struct aa_profile *profile,
  57. struct aa_label *peer, u32 request,
  58. struct common_audit_data *sa)
  59. {
  60. struct aa_perms perms = { };
  61. aad(sa)->peer = peer;
  62. aa_profile_match_label(profile, peer, AA_CLASS_PTRACE, request,
  63. &perms);
  64. aa_apply_modes_to_perms(profile, &perms);
  65. return aa_check_perms(profile, &perms, request, sa, audit_ptrace_cb);
  66. }
  67. static int profile_tracee_perm(struct aa_profile *tracee,
  68. struct aa_label *tracer, u32 request,
  69. struct common_audit_data *sa)
  70. {
  71. if (profile_unconfined(tracee) || unconfined(tracer) ||
  72. !PROFILE_MEDIATES(tracee, AA_CLASS_PTRACE))
  73. return 0;
  74. return profile_ptrace_perm(tracee, tracer, request, sa);
  75. }
  76. static int profile_tracer_perm(struct aa_profile *tracer,
  77. struct aa_label *tracee, u32 request,
  78. struct common_audit_data *sa)
  79. {
  80. if (profile_unconfined(tracer))
  81. return 0;
  82. if (PROFILE_MEDIATES(tracer, AA_CLASS_PTRACE))
  83. return profile_ptrace_perm(tracer, tracee, request, sa);
  84. /* profile uses the old style capability check for ptrace */
  85. if (&tracer->label == tracee)
  86. return 0;
  87. aad(sa)->label = &tracer->label;
  88. aad(sa)->peer = tracee;
  89. aad(sa)->request = 0;
  90. aad(sa)->error = aa_capable(&tracer->label, CAP_SYS_PTRACE,
  91. CAP_OPT_NONE);
  92. return aa_audit(AUDIT_APPARMOR_AUTO, tracer, sa, audit_ptrace_cb);
  93. }
  94. /**
  95. * aa_may_ptrace - test if tracer task can trace the tracee
  96. * @tracer: label of the task doing the tracing (NOT NULL)
  97. * @tracee: task label to be traced
  98. * @request: permission request
  99. *
  100. * Returns: %0 else error code if permission denied or error
  101. */
  102. int aa_may_ptrace(struct aa_label *tracer, struct aa_label *tracee,
  103. u32 request)
  104. {
  105. struct aa_profile *profile;
  106. u32 xrequest = request << PTRACE_PERM_SHIFT;
  107. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE);
  108. return xcheck_labels(tracer, tracee, profile,
  109. profile_tracer_perm(profile, tracee, request, &sa),
  110. profile_tracee_perm(profile, tracer, xrequest, &sa));
  111. }
  112. static inline int map_signal_num(int sig)
  113. {
  114. if (sig > SIGRTMAX)
  115. return SIGUNKNOWN;
  116. else if (sig >= SIGRTMIN)
  117. return sig - SIGRTMIN + SIGRT_BASE;
  118. else if (sig < MAXMAPPED_SIG)
  119. return sig_map[sig];
  120. return SIGUNKNOWN;
  121. }
  122. /**
  123. * audit_signal_mask - convert mask to permission string
  124. * @mask: permission mask to convert
  125. *
  126. * Returns: pointer to static string
  127. */
  128. static const char *audit_signal_mask(u32 mask)
  129. {
  130. if (mask & MAY_READ)
  131. return "receive";
  132. if (mask & MAY_WRITE)
  133. return "send";
  134. return "";
  135. }
  136. /**
  137. * audit_cb - call back for signal specific audit fields
  138. * @ab: audit_buffer (NOT NULL)
  139. * @va: audit struct to audit values of (NOT NULL)
  140. */
  141. static void audit_signal_cb(struct audit_buffer *ab, void *va)
  142. {
  143. struct common_audit_data *sa = va;
  144. if (aad(sa)->request & AA_SIGNAL_PERM_MASK) {
  145. audit_log_format(ab, " requested_mask=\"%s\"",
  146. audit_signal_mask(aad(sa)->request));
  147. if (aad(sa)->denied & AA_SIGNAL_PERM_MASK) {
  148. audit_log_format(ab, " denied_mask=\"%s\"",
  149. audit_signal_mask(aad(sa)->denied));
  150. }
  151. }
  152. if (aad(sa)->signal == SIGUNKNOWN)
  153. audit_log_format(ab, "signal=unknown(%d)",
  154. aad(sa)->unmappedsig);
  155. else if (aad(sa)->signal < MAXMAPPED_SIGNAME)
  156. audit_log_format(ab, " signal=%s", sig_names[aad(sa)->signal]);
  157. else
  158. audit_log_format(ab, " signal=rtmin+%d",
  159. aad(sa)->signal - SIGRT_BASE);
  160. audit_log_format(ab, " peer=");
  161. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  162. FLAGS_NONE, GFP_ATOMIC);
  163. }
  164. static int profile_signal_perm(struct aa_profile *profile,
  165. struct aa_label *peer, u32 request,
  166. struct common_audit_data *sa)
  167. {
  168. struct aa_perms perms;
  169. unsigned int state;
  170. if (profile_unconfined(profile) ||
  171. !PROFILE_MEDIATES(profile, AA_CLASS_SIGNAL))
  172. return 0;
  173. aad(sa)->peer = peer;
  174. /* TODO: secondary cache check <profile, profile, perm> */
  175. state = aa_dfa_next(profile->policy.dfa,
  176. profile->policy.start[AA_CLASS_SIGNAL],
  177. aad(sa)->signal);
  178. aa_label_match(profile, peer, state, false, request, &perms);
  179. aa_apply_modes_to_perms(profile, &perms);
  180. return aa_check_perms(profile, &perms, request, sa, audit_signal_cb);
  181. }
  182. int aa_may_signal(struct aa_label *sender, struct aa_label *target, int sig)
  183. {
  184. struct aa_profile *profile;
  185. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SIGNAL);
  186. aad(&sa)->signal = map_signal_num(sig);
  187. aad(&sa)->unmappedsig = sig;
  188. return xcheck_labels(sender, target, profile,
  189. profile_signal_perm(profile, target, MAY_WRITE, &sa),
  190. profile_signal_perm(profile, sender, MAY_READ, &sa));
  191. }