tracex5_kern.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <linux/ptrace.h>
  8. #include <linux/version.h>
  9. #include <uapi/linux/bpf.h>
  10. #include <uapi/linux/seccomp.h>
  11. #include "bpf_helpers.h"
  12. #define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
  13. struct bpf_map_def SEC("maps") progs = {
  14. .type = BPF_MAP_TYPE_PROG_ARRAY,
  15. .key_size = sizeof(u32),
  16. .value_size = sizeof(u32),
  17. .max_entries = 1024,
  18. };
  19. SEC("kprobe/seccomp_phase1")
  20. int bpf_prog1(struct pt_regs *ctx)
  21. {
  22. struct seccomp_data sd = {};
  23. bpf_probe_read(&sd, sizeof(sd), (void *)ctx->di);
  24. /* dispatch into next BPF program depending on syscall number */
  25. bpf_tail_call(ctx, &progs, sd.nr);
  26. /* fall through -> unknown syscall */
  27. if (sd.nr >= __NR_getuid && sd.nr <= __NR_getsid) {
  28. char fmt[] = "syscall=%d (one of get/set uid/pid/gid)\n";
  29. bpf_trace_printk(fmt, sizeof(fmt), sd.nr);
  30. }
  31. return 0;
  32. }
  33. /* we jump here when syscall number == __NR_write */
  34. PROG(__NR_write)(struct pt_regs *ctx)
  35. {
  36. struct seccomp_data sd = {};
  37. bpf_probe_read(&sd, sizeof(sd), (void *)ctx->di);
  38. if (sd.args[2] == 512) {
  39. char fmt[] = "write(fd=%d, buf=%p, size=%d)\n";
  40. bpf_trace_printk(fmt, sizeof(fmt),
  41. sd.args[0], sd.args[1], sd.args[2]);
  42. }
  43. return 0;
  44. }
  45. PROG(__NR_read)(struct pt_regs *ctx)
  46. {
  47. struct seccomp_data sd = {};
  48. bpf_probe_read(&sd, sizeof(sd), (void *)ctx->di);
  49. if (sd.args[2] > 128 && sd.args[2] <= 1024) {
  50. char fmt[] = "read(fd=%d, buf=%p, size=%d)\n";
  51. bpf_trace_printk(fmt, sizeof(fmt),
  52. sd.args[0], sd.args[1], sd.args[2]);
  53. }
  54. return 0;
  55. }
  56. PROG(__NR_mmap)(struct pt_regs *ctx)
  57. {
  58. char fmt[] = "mmap\n";
  59. bpf_trace_printk(fmt, sizeof(fmt));
  60. return 0;
  61. }
  62. char _license[] SEC("license") = "GPL";
  63. u32 _version SEC("version") = LINUX_VERSION_CODE;