tracex5_kern.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_filter")
  20. int bpf_prog1(struct pt_regs *ctx)
  21. {
  22. int sc_nr = (int)PT_REGS_PARM1(ctx);
  23. /* dispatch into next BPF program depending on syscall number */
  24. bpf_tail_call(ctx, &progs, sc_nr);
  25. /* fall through -> unknown syscall */
  26. if (sc_nr >= __NR_getuid && sc_nr <= __NR_getsid) {
  27. char fmt[] = "syscall=%d (one of get/set uid/pid/gid)\n";
  28. bpf_trace_printk(fmt, sizeof(fmt), sc_nr);
  29. }
  30. return 0;
  31. }
  32. /* we jump here when syscall number == __NR_write */
  33. PROG(__NR_write)(struct pt_regs *ctx)
  34. {
  35. struct seccomp_data sd;
  36. bpf_probe_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
  37. if (sd.args[2] == 512) {
  38. char fmt[] = "write(fd=%d, buf=%p, size=%d)\n";
  39. bpf_trace_printk(fmt, sizeof(fmt),
  40. sd.args[0], sd.args[1], sd.args[2]);
  41. }
  42. return 0;
  43. }
  44. PROG(__NR_read)(struct pt_regs *ctx)
  45. {
  46. struct seccomp_data sd;
  47. bpf_probe_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
  48. if (sd.args[2] > 128 && sd.args[2] <= 1024) {
  49. char fmt[] = "read(fd=%d, buf=%p, size=%d)\n";
  50. bpf_trace_printk(fmt, sizeof(fmt),
  51. sd.args[0], sd.args[1], sd.args[2]);
  52. }
  53. return 0;
  54. }
  55. PROG(__NR_mmap)(struct pt_regs *ctx)
  56. {
  57. char fmt[] = "mmap\n";
  58. bpf_trace_printk(fmt, sizeof(fmt));
  59. return 0;
  60. }
  61. char _license[] SEC("license") = "GPL";
  62. u32 _version SEC("version") = LINUX_VERSION_CODE;