tracex5_kern.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <uapi/linux/unistd.h>
  12. #include "syscall_nrs.h"
  13. #include "bpf_helpers.h"
  14. #define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
  15. struct bpf_map_def SEC("maps") progs = {
  16. .type = BPF_MAP_TYPE_PROG_ARRAY,
  17. .key_size = sizeof(u32),
  18. .value_size = sizeof(u32),
  19. #ifdef __mips__
  20. .max_entries = 6000, /* MIPS n64 syscalls start at 5000 */
  21. #else
  22. .max_entries = 1024,
  23. #endif
  24. };
  25. SEC("kprobe/__seccomp_filter")
  26. int bpf_prog1(struct pt_regs *ctx)
  27. {
  28. int sc_nr = (int)PT_REGS_PARM1(ctx);
  29. /* dispatch into next BPF program depending on syscall number */
  30. bpf_tail_call(ctx, &progs, sc_nr);
  31. /* fall through -> unknown syscall */
  32. if (sc_nr >= __NR_getuid && sc_nr <= __NR_getsid) {
  33. char fmt[] = "syscall=%d (one of get/set uid/pid/gid)\n";
  34. bpf_trace_printk(fmt, sizeof(fmt), sc_nr);
  35. }
  36. return 0;
  37. }
  38. /* we jump here when syscall number == __NR_write */
  39. PROG(SYS__NR_write)(struct pt_regs *ctx)
  40. {
  41. struct seccomp_data sd;
  42. bpf_probe_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
  43. if (sd.args[2] == 512) {
  44. char fmt[] = "write(fd=%d, buf=%p, size=%d)\n";
  45. bpf_trace_printk(fmt, sizeof(fmt),
  46. sd.args[0], sd.args[1], sd.args[2]);
  47. }
  48. return 0;
  49. }
  50. PROG(SYS__NR_read)(struct pt_regs *ctx)
  51. {
  52. struct seccomp_data sd;
  53. bpf_probe_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
  54. if (sd.args[2] > 128 && sd.args[2] <= 1024) {
  55. char fmt[] = "read(fd=%d, buf=%p, size=%d)\n";
  56. bpf_trace_printk(fmt, sizeof(fmt),
  57. sd.args[0], sd.args[1], sd.args[2]);
  58. }
  59. return 0;
  60. }
  61. PROG(SYS__NR_mmap)(struct pt_regs *ctx)
  62. {
  63. char fmt[] = "mmap\n";
  64. bpf_trace_printk(fmt, sizeof(fmt));
  65. return 0;
  66. }
  67. char _license[] SEC("license") = "GPL";
  68. u32 _version SEC("version") = LINUX_VERSION_CODE;