file.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <asm-generic/fcntl.h>
  2. struct file_event {
  3. __u32 type;
  4. __u32 pid;
  5. __u64 fd;
  6. };
  7. struct {
  8. __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
  9. __uint(key_size, sizeof(int));
  10. __uint(value_size, sizeof(int));
  11. } file_events SEC(".maps");
  12. struct {
  13. __uint(type, BPF_MAP_TYPE_HASH);
  14. __uint(key_size, sizeof(__u64));
  15. __uint(value_size, sizeof(__u32));
  16. __uint(max_entries, 10240);
  17. } open_file_info SEC(".maps");
  18. struct trace_event_raw_sys_enter__stub {
  19. __u64 unused;
  20. long int id;
  21. long unsigned int args[6];
  22. };
  23. struct trace_event_raw_sys_exit__stub {
  24. __u64 unused;
  25. long int id;
  26. long int ret;
  27. };
  28. static __always_inline
  29. int trace_enter(struct trace_event_raw_sys_enter__stub* ctx, int at)
  30. {
  31. int flags = (int)ctx->args[at+1];
  32. if (!(flags & O_ACCMODE & (O_WRONLY | O_RDWR))) {
  33. return 0;
  34. }
  35. char p[7];
  36. // long res = bpf_probe_read_str(&p, sizeof(p), (void *)ctx->args[at]);
  37. if (p[0]=='/' && p[1]=='p' && p[2]=='r' && p[3]=='o' && p[4]=='c' && p[5]=='/') {
  38. return 0;
  39. }
  40. if (p[0]=='/' && p[1]=='d' && p[2]=='e' && p[3]=='v' && p[4]=='/') {
  41. return 0;
  42. }
  43. if (p[0]=='/' && p[1]=='s' && p[2]=='y' && p[3]=='s' && p[4]=='/') {
  44. return 0;
  45. }
  46. __u64 id = bpf_get_current_pid_tgid();
  47. __u32 v = 1;
  48. bpf_map_update_elem(&open_file_info, &id, &v, BPF_ANY);
  49. return 0;
  50. }
  51. static __always_inline
  52. int trace_exit(struct trace_event_raw_sys_exit__stub* ctx)
  53. {
  54. __u64 id = bpf_get_current_pid_tgid();
  55. if (!bpf_map_lookup_elem(&open_file_info, &id)) {
  56. return 0;
  57. }
  58. bpf_map_delete_elem(&open_file_info, &id);
  59. if (ctx->ret < 0) {
  60. return 0;
  61. }
  62. struct file_event e = {
  63. .type = EVENT_TYPE_FILE_OPEN,
  64. .pid = id >> 32,
  65. .fd = ctx->ret,
  66. };
  67. bpf_perf_event_output(ctx, &file_events, BPF_F_CURRENT_CPU, &e, sizeof(e));
  68. return 0;
  69. }
  70. #if defined(__TARGET_ARCH_x86)
  71. SEC("tracepoint/syscalls/sys_enter_open")
  72. int sys_enter_open(struct trace_event_raw_sys_enter__stub* ctx)
  73. {
  74. return trace_enter(ctx, 0);
  75. }
  76. SEC("tracepoint/syscalls/sys_exit_open")
  77. int sys_exit_open(struct trace_event_raw_sys_exit__stub* ctx)
  78. {
  79. return trace_exit(ctx);
  80. }
  81. #endif
  82. SEC("tracepoint/syscalls/sys_enter_openat")
  83. int sys_enter_openat(struct trace_event_raw_sys_enter__stub* ctx)
  84. {
  85. return trace_enter(ctx, 1);
  86. }
  87. SEC("tracepoint/syscalls/sys_exit_openat")
  88. int sys_exit_openat(struct trace_event_raw_sys_exit__stub* ctx)
  89. {
  90. return trace_exit(ctx);
  91. }