/tools/perf/examples/bpf/augmented_raw_syscalls.c

https://github.com/tekkamanninja/linux · C · 294 lines · 170 code · 54 blank · 70 comment · 20 complexity · 6de318ebe3aca79d700cc6d898d1b3e8 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Augment the raw_syscalls tracepoints with the contents of the pointer arguments.
  4. *
  5. * Test it with:
  6. *
  7. * perf trace -e tools/perf/examples/bpf/augmented_raw_syscalls.c cat /etc/passwd > /dev/null
  8. *
  9. * This exactly matches what is marshalled into the raw_syscall:sys_enter
  10. * payload expected by the 'perf trace' beautifiers.
  11. *
  12. * For now it just uses the existing tracepoint augmentation code in 'perf
  13. * trace', in the next csets we'll hook up these with the sys_enter/sys_exit
  14. * code that will combine entry/exit in a strace like way.
  15. */
  16. #include <unistd.h>
  17. #include <linux/limits.h>
  18. #include <linux/socket.h>
  19. #include <pid_filter.h>
  20. /* bpf-output associated map */
  21. bpf_map(__augmented_syscalls__, PERF_EVENT_ARRAY, int, u32, __NR_CPUS__);
  22. /*
  23. * string_args_len: one per syscall arg, 0 means not a string or don't copy it,
  24. * PATH_MAX for copying everything, any other value to limit
  25. * it a la 'strace -s strsize'.
  26. */
  27. struct syscall {
  28. bool enabled;
  29. u16 string_args_len[6];
  30. };
  31. bpf_map(syscalls, ARRAY, int, struct syscall, 512);
  32. /*
  33. * What to augment at entry?
  34. *
  35. * Pointer arg payloads (filenames, etc) passed from userspace to the kernel
  36. */
  37. bpf_map(syscalls_sys_enter, PROG_ARRAY, u32, u32, 512);
  38. /*
  39. * What to augment at exit?
  40. *
  41. * Pointer arg payloads returned from the kernel (struct stat, etc) to userspace.
  42. */
  43. bpf_map(syscalls_sys_exit, PROG_ARRAY, u32, u32, 512);
  44. struct syscall_enter_args {
  45. unsigned long long common_tp_fields;
  46. long syscall_nr;
  47. unsigned long args[6];
  48. };
  49. struct syscall_exit_args {
  50. unsigned long long common_tp_fields;
  51. long syscall_nr;
  52. long ret;
  53. };
  54. struct augmented_arg {
  55. unsigned int size;
  56. int err;
  57. char value[PATH_MAX];
  58. };
  59. pid_filter(pids_filtered);
  60. struct augmented_args_payload {
  61. struct syscall_enter_args args;
  62. union {
  63. struct {
  64. struct augmented_arg arg, arg2;
  65. };
  66. struct sockaddr_storage saddr;
  67. };
  68. };
  69. // We need more tmp space than the BPF stack can give us
  70. bpf_map(augmented_args_tmp, PERCPU_ARRAY, int, struct augmented_args_payload, 1);
  71. static inline struct augmented_args_payload *augmented_args_payload(void)
  72. {
  73. int key = 0;
  74. return bpf_map_lookup_elem(&augmented_args_tmp, &key);
  75. }
  76. static inline int augmented__output(void *ctx, struct augmented_args_payload *args, int len)
  77. {
  78. /* If perf_event_output fails, return non-zero so that it gets recorded unaugmented */
  79. return perf_event_output(ctx, &__augmented_syscalls__, BPF_F_CURRENT_CPU, args, len);
  80. }
  81. static inline
  82. unsigned int augmented_arg__read_str(struct augmented_arg *augmented_arg, const void *arg, unsigned int arg_len)
  83. {
  84. unsigned int augmented_len = sizeof(*augmented_arg);
  85. int string_len = probe_read_str(&augmented_arg->value, arg_len, arg);
  86. augmented_arg->size = augmented_arg->err = 0;
  87. /*
  88. * probe_read_str may return < 0, e.g. -EFAULT
  89. * So we leave that in the augmented_arg->size that userspace will
  90. */
  91. if (string_len > 0) {
  92. augmented_len -= sizeof(augmented_arg->value) - string_len;
  93. augmented_len &= sizeof(augmented_arg->value) - 1;
  94. augmented_arg->size = string_len;
  95. } else {
  96. /*
  97. * So that username notice the error while still being able
  98. * to skip this augmented arg record
  99. */
  100. augmented_arg->err = string_len;
  101. augmented_len = offsetof(struct augmented_arg, value);
  102. }
  103. return augmented_len;
  104. }
  105. SEC("!raw_syscalls:unaugmented")
  106. int syscall_unaugmented(struct syscall_enter_args *args)
  107. {
  108. return 1;
  109. }
  110. /*
  111. * These will be tail_called from SEC("raw_syscalls:sys_enter"), so will find in
  112. * augmented_args_tmp what was read by that raw_syscalls:sys_enter and go
  113. * on from there, reading the first syscall arg as a string, i.e. open's
  114. * filename.
  115. */
  116. SEC("!syscalls:sys_enter_connect")
  117. int sys_enter_connect(struct syscall_enter_args *args)
  118. {
  119. struct augmented_args_payload *augmented_args = augmented_args_payload();
  120. const void *sockaddr_arg = (const void *)args->args[1];
  121. unsigned int socklen = args->args[2];
  122. unsigned int len = sizeof(augmented_args->args);
  123. if (augmented_args == NULL)
  124. return 1; /* Failure: don't filter */
  125. if (socklen > sizeof(augmented_args->saddr))
  126. socklen = sizeof(augmented_args->saddr);
  127. probe_read(&augmented_args->saddr, socklen, sockaddr_arg);
  128. return augmented__output(args, augmented_args, len + socklen);
  129. }
  130. SEC("!syscalls:sys_enter_sendto")
  131. int sys_enter_sendto(struct syscall_enter_args *args)
  132. {
  133. struct augmented_args_payload *augmented_args = augmented_args_payload();
  134. const void *sockaddr_arg = (const void *)args->args[4];
  135. unsigned int socklen = args->args[5];
  136. unsigned int len = sizeof(augmented_args->args);
  137. if (augmented_args == NULL)
  138. return 1; /* Failure: don't filter */
  139. if (socklen > sizeof(augmented_args->saddr))
  140. socklen = sizeof(augmented_args->saddr);
  141. probe_read(&augmented_args->saddr, socklen, sockaddr_arg);
  142. return augmented__output(args, augmented_args, len + socklen);
  143. }
  144. SEC("!syscalls:sys_enter_open")
  145. int sys_enter_open(struct syscall_enter_args *args)
  146. {
  147. struct augmented_args_payload *augmented_args = augmented_args_payload();
  148. const void *filename_arg = (const void *)args->args[0];
  149. unsigned int len = sizeof(augmented_args->args);
  150. if (augmented_args == NULL)
  151. return 1; /* Failure: don't filter */
  152. len += augmented_arg__read_str(&augmented_args->arg, filename_arg, sizeof(augmented_args->arg.value));
  153. return augmented__output(args, augmented_args, len);
  154. }
  155. SEC("!syscalls:sys_enter_openat")
  156. int sys_enter_openat(struct syscall_enter_args *args)
  157. {
  158. struct augmented_args_payload *augmented_args = augmented_args_payload();
  159. const void *filename_arg = (const void *)args->args[1];
  160. unsigned int len = sizeof(augmented_args->args);
  161. if (augmented_args == NULL)
  162. return 1; /* Failure: don't filter */
  163. len += augmented_arg__read_str(&augmented_args->arg, filename_arg, sizeof(augmented_args->arg.value));
  164. return augmented__output(args, augmented_args, len);
  165. }
  166. SEC("!syscalls:sys_enter_rename")
  167. int sys_enter_rename(struct syscall_enter_args *args)
  168. {
  169. struct augmented_args_payload *augmented_args = augmented_args_payload();
  170. const void *oldpath_arg = (const void *)args->args[0],
  171. *newpath_arg = (const void *)args->args[1];
  172. unsigned int len = sizeof(augmented_args->args), oldpath_len;
  173. if (augmented_args == NULL)
  174. return 1; /* Failure: don't filter */
  175. oldpath_len = augmented_arg__read_str(&augmented_args->arg, oldpath_arg, sizeof(augmented_args->arg.value));
  176. len += oldpath_len + augmented_arg__read_str((void *)(&augmented_args->arg) + oldpath_len, newpath_arg, sizeof(augmented_args->arg.value));
  177. return augmented__output(args, augmented_args, len);
  178. }
  179. SEC("!syscalls:sys_enter_renameat")
  180. int sys_enter_renameat(struct syscall_enter_args *args)
  181. {
  182. struct augmented_args_payload *augmented_args = augmented_args_payload();
  183. const void *oldpath_arg = (const void *)args->args[1],
  184. *newpath_arg = (const void *)args->args[3];
  185. unsigned int len = sizeof(augmented_args->args), oldpath_len;
  186. if (augmented_args == NULL)
  187. return 1; /* Failure: don't filter */
  188. oldpath_len = augmented_arg__read_str(&augmented_args->arg, oldpath_arg, sizeof(augmented_args->arg.value));
  189. len += oldpath_len + augmented_arg__read_str((void *)(&augmented_args->arg) + oldpath_len, newpath_arg, sizeof(augmented_args->arg.value));
  190. return augmented__output(args, augmented_args, len);
  191. }
  192. SEC("raw_syscalls:sys_enter")
  193. int sys_enter(struct syscall_enter_args *args)
  194. {
  195. struct augmented_args_payload *augmented_args;
  196. /*
  197. * We start len, the amount of data that will be in the perf ring
  198. * buffer, if this is not filtered out by one of pid_filter__has(),
  199. * syscall->enabled, etc, with the non-augmented raw syscall payload,
  200. * i.e. sizeof(augmented_args->args).
  201. *
  202. * We'll add to this as we add augmented syscalls right after that
  203. * initial, non-augmented raw_syscalls:sys_enter payload.
  204. */
  205. unsigned int len = sizeof(augmented_args->args);
  206. struct syscall *syscall;
  207. if (pid_filter__has(&pids_filtered, getpid()))
  208. return 0;
  209. augmented_args = augmented_args_payload();
  210. if (augmented_args == NULL)
  211. return 1;
  212. probe_read(&augmented_args->args, sizeof(augmented_args->args), args);
  213. /*
  214. * Jump to syscall specific augmenter, even if the default one,
  215. * "!raw_syscalls:unaugmented" that will just return 1 to return the
  216. * unaugmented tracepoint payload.
  217. */
  218. bpf_tail_call(args, &syscalls_sys_enter, augmented_args->args.syscall_nr);
  219. // If not found on the PROG_ARRAY syscalls map, then we're filtering it:
  220. return 0;
  221. }
  222. SEC("raw_syscalls:sys_exit")
  223. int sys_exit(struct syscall_exit_args *args)
  224. {
  225. struct syscall_exit_args exit_args;
  226. if (pid_filter__has(&pids_filtered, getpid()))
  227. return 0;
  228. probe_read(&exit_args, sizeof(exit_args), args);
  229. /*
  230. * Jump to syscall specific return augmenter, even if the default one,
  231. * "!raw_syscalls:unaugmented" that will just return 1 to return the
  232. * unaugmented tracepoint payload.
  233. */
  234. bpf_tail_call(args, &syscalls_sys_exit, exit_args.syscall_nr);
  235. /*
  236. * If not found on the PROG_ARRAY syscalls map, then we're filtering it:
  237. */
  238. return 0;
  239. }
  240. license(GPL);