ipc.c 6.2 KB

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