ipc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/context.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. /* TODO: conditionals */
  61. static int profile_ptrace_perm(struct aa_profile *profile,
  62. struct aa_profile *peer, u32 request,
  63. struct common_audit_data *sa)
  64. {
  65. struct aa_perms perms = { };
  66. /* need because of peer in cross check */
  67. if (profile_unconfined(profile) ||
  68. !PROFILE_MEDIATES(profile, AA_CLASS_PTRACE))
  69. return 0;
  70. aad(sa)->peer = &peer->label;
  71. aa_profile_match_label(profile, &peer->label, AA_CLASS_PTRACE, request,
  72. &perms);
  73. aa_apply_modes_to_perms(profile, &perms);
  74. return aa_check_perms(profile, &perms, request, sa, audit_ptrace_cb);
  75. }
  76. static int cross_ptrace_perm(struct aa_profile *tracer,
  77. struct aa_profile *tracee, u32 request,
  78. struct common_audit_data *sa)
  79. {
  80. if (PROFILE_MEDIATES(tracer, AA_CLASS_PTRACE))
  81. return xcheck(profile_ptrace_perm(tracer, tracee, request, sa),
  82. profile_ptrace_perm(tracee, tracer,
  83. request << PTRACE_PERM_SHIFT,
  84. sa));
  85. /* policy uses the old style capability check for ptrace */
  86. if (profile_unconfined(tracer) || tracer == tracee)
  87. return 0;
  88. aad(sa)->label = &tracer->label;
  89. aad(sa)->peer = &tracee->label;
  90. aad(sa)->request = 0;
  91. aad(sa)->error = aa_capable(&tracer->label, CAP_SYS_PTRACE, 1);
  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. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE);
  106. return xcheck_labels_profiles(tracer, tracee, cross_ptrace_perm,
  107. request, &sa);
  108. }
  109. static inline int map_signal_num(int sig)
  110. {
  111. if (sig > SIGRTMAX)
  112. return SIGUNKNOWN;
  113. else if (sig >= SIGRTMIN)
  114. return sig - SIGRTMIN + 128; /* rt sigs mapped to 128 */
  115. else if (sig < MAXMAPPED_SIG)
  116. return sig_map[sig];
  117. return SIGUNKNOWN;
  118. }
  119. /**
  120. * audit_file_mask - convert mask to permission string
  121. * @buffer: buffer to write string to (NOT NULL)
  122. * @mask: permission mask to convert
  123. */
  124. static void audit_signal_mask(struct audit_buffer *ab, u32 mask)
  125. {
  126. if (mask & MAY_READ)
  127. audit_log_string(ab, "receive");
  128. if (mask & MAY_WRITE)
  129. audit_log_string(ab, "send");
  130. }
  131. /**
  132. * audit_cb - call back for signal specific audit fields
  133. * @ab: audit_buffer (NOT NULL)
  134. * @va: audit struct to audit values of (NOT NULL)
  135. */
  136. static void audit_signal_cb(struct audit_buffer *ab, void *va)
  137. {
  138. struct common_audit_data *sa = va;
  139. if (aad(sa)->request & AA_SIGNAL_PERM_MASK) {
  140. audit_log_format(ab, " requested_mask=");
  141. audit_signal_mask(ab, aad(sa)->request);
  142. if (aad(sa)->denied & AA_SIGNAL_PERM_MASK) {
  143. audit_log_format(ab, " denied_mask=");
  144. audit_signal_mask(ab, aad(sa)->denied);
  145. }
  146. }
  147. if (aad(sa)->signal < MAXMAPPED_SIG)
  148. audit_log_format(ab, " signal=%s", sig_names[aad(sa)->signal]);
  149. else
  150. audit_log_format(ab, " signal=rtmin+%d",
  151. aad(sa)->signal - 128);
  152. audit_log_format(ab, " peer=");
  153. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  154. FLAGS_NONE, GFP_ATOMIC);
  155. }
  156. /* TODO: update to handle compound name&name2, conditionals */
  157. static void profile_match_signal(struct aa_profile *profile, const char *label,
  158. int signal, struct aa_perms *perms)
  159. {
  160. unsigned int state;
  161. /* TODO: secondary cache check <profile, profile, perm> */
  162. state = aa_dfa_next(profile->policy.dfa,
  163. profile->policy.start[AA_CLASS_SIGNAL],
  164. signal);
  165. state = aa_dfa_match(profile->policy.dfa, state, label);
  166. aa_compute_perms(profile->policy.dfa, state, perms);
  167. }
  168. static int profile_signal_perm(struct aa_profile *profile,
  169. struct aa_profile *peer, u32 request,
  170. struct common_audit_data *sa)
  171. {
  172. struct aa_perms perms;
  173. if (profile_unconfined(profile) ||
  174. !PROFILE_MEDIATES(profile, AA_CLASS_SIGNAL))
  175. return 0;
  176. aad(sa)->peer = &peer->label;
  177. profile_match_signal(profile, peer->base.hname, aad(sa)->signal,
  178. &perms);
  179. aa_apply_modes_to_perms(profile, &perms);
  180. return aa_check_perms(profile, &perms, request, sa, audit_signal_cb);
  181. }
  182. static int aa_signal_cross_perm(struct aa_profile *sender,
  183. struct aa_profile *target,
  184. struct common_audit_data *sa)
  185. {
  186. return xcheck(profile_signal_perm(sender, target, MAY_WRITE, sa),
  187. profile_signal_perm(target, sender, MAY_READ, sa));
  188. }
  189. int aa_may_signal(struct aa_label *sender, struct aa_label *target, int sig)
  190. {
  191. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SIGNAL);
  192. aad(&sa)->signal = map_signal_num(sig);
  193. return xcheck_labels_profiles(sender, target, aa_signal_cross_perm,
  194. &sa);
  195. }