etcsnoop.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Augment the filename syscalls with the contents of the filename pointer argument
  4. * filtering only those that do not start with /etc/.
  5. *
  6. * Test it with:
  7. *
  8. * perf trace -e tools/perf/examples/bpf/augmented_syscalls.c cat /etc/passwd > /dev/null
  9. *
  10. * It'll catch some openat syscalls related to the dynamic linked and
  11. * the last one should be the one for '/etc/passwd'.
  12. *
  13. * This matches what is marshalled into the raw_syscall:sys_enter payload
  14. * expected by the 'perf trace' beautifiers, and can be used by them unmodified,
  15. * which will be done as that feature is implemented in the next csets, for now
  16. * it will appear in a dump done by the default tracepoint handler in 'perf trace',
  17. * that uses bpf_output__fprintf() to just dump those contents, as done with
  18. * the bpf-output event associated with the __bpf_output__ map declared in
  19. * tools/perf/include/bpf/stdio.h.
  20. */
  21. #include <stdio.h>
  22. struct bpf_map SEC("maps") __augmented_syscalls__ = {
  23. .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  24. .key_size = sizeof(int),
  25. .value_size = sizeof(u32),
  26. .max_entries = __NR_CPUS__,
  27. };
  28. struct augmented_filename {
  29. int size;
  30. int reserved;
  31. char value[64];
  32. };
  33. #define augmented_filename_syscall_enter(syscall) \
  34. struct augmented_enter_##syscall##_args { \
  35. struct syscall_enter_##syscall##_args args; \
  36. struct augmented_filename filename; \
  37. }; \
  38. int syscall_enter(syscall)(struct syscall_enter_##syscall##_args *args) \
  39. { \
  40. char etc[6] = "/etc/"; \
  41. struct augmented_enter_##syscall##_args augmented_args = { .filename.reserved = 0, }; \
  42. probe_read(&augmented_args.args, sizeof(augmented_args.args), args); \
  43. augmented_args.filename.size = probe_read_str(&augmented_args.filename.value, \
  44. sizeof(augmented_args.filename.value), \
  45. args->filename_ptr); \
  46. if (__builtin_memcmp(augmented_args.filename.value, etc, 4) != 0) \
  47. return 0; \
  48. perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, \
  49. &augmented_args, \
  50. (sizeof(augmented_args) - sizeof(augmented_args.filename.value) + \
  51. augmented_args.filename.size)); \
  52. return 0; \
  53. }
  54. struct syscall_enter_openat_args {
  55. unsigned long long common_tp_fields;
  56. long syscall_nr;
  57. long dfd;
  58. char *filename_ptr;
  59. long flags;
  60. long mode;
  61. };
  62. augmented_filename_syscall_enter(openat);
  63. struct syscall_enter_open_args {
  64. unsigned long long common_tp_fields;
  65. long syscall_nr;
  66. char *filename_ptr;
  67. long flags;
  68. long mode;
  69. };
  70. augmented_filename_syscall_enter(open);
  71. license(GPL);