tracex5_user.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <linux/filter.h>
  6. #include <linux/seccomp.h>
  7. #include <sys/prctl.h>
  8. #include <bpf/bpf.h>
  9. #include <bpf/libbpf.h>
  10. #include "trace_helpers.h"
  11. #include "bpf_util.h"
  12. #ifdef __mips__
  13. #define MAX_ENTRIES 6000 /* MIPS n64 syscalls start at 5000 */
  14. #else
  15. #define MAX_ENTRIES 1024
  16. #endif
  17. /* install fake seccomp program to enable seccomp code path inside the kernel,
  18. * so that our kprobe attached to seccomp_phase1() can be triggered
  19. */
  20. static void install_accept_all_seccomp(void)
  21. {
  22. struct sock_filter filter[] = {
  23. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  24. };
  25. struct sock_fprog prog = {
  26. .len = (unsigned short)ARRAY_SIZE(filter),
  27. .filter = filter,
  28. };
  29. if (prctl(PR_SET_SECCOMP, 2, &prog))
  30. perror("prctl");
  31. }
  32. int main(int ac, char **argv)
  33. {
  34. struct bpf_link *link = NULL;
  35. struct bpf_program *prog;
  36. struct bpf_object *obj;
  37. int key, fd, progs_fd;
  38. const char *section;
  39. char filename[256];
  40. FILE *f;
  41. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  42. obj = bpf_object__open_file(filename, NULL);
  43. if (libbpf_get_error(obj)) {
  44. fprintf(stderr, "ERROR: opening BPF object file failed\n");
  45. return 0;
  46. }
  47. prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
  48. if (!prog) {
  49. printf("finding a prog in obj file failed\n");
  50. goto cleanup;
  51. }
  52. /* load BPF program */
  53. if (bpf_object__load(obj)) {
  54. fprintf(stderr, "ERROR: loading BPF object file failed\n");
  55. goto cleanup;
  56. }
  57. link = bpf_program__attach(prog);
  58. if (libbpf_get_error(link)) {
  59. fprintf(stderr, "ERROR: bpf_program__attach failed\n");
  60. link = NULL;
  61. goto cleanup;
  62. }
  63. progs_fd = bpf_object__find_map_fd_by_name(obj, "progs");
  64. if (progs_fd < 0) {
  65. fprintf(stderr, "ERROR: finding a map in obj file failed\n");
  66. goto cleanup;
  67. }
  68. bpf_object__for_each_program(prog, obj) {
  69. section = bpf_program__section_name(prog);
  70. /* register only syscalls to PROG_ARRAY */
  71. if (sscanf(section, "kprobe/%d", &key) != 1)
  72. continue;
  73. fd = bpf_program__fd(prog);
  74. bpf_map_update_elem(progs_fd, &key, &fd, BPF_ANY);
  75. }
  76. install_accept_all_seccomp();
  77. f = popen("dd if=/dev/zero of=/dev/null count=5", "r");
  78. (void) f;
  79. read_trace_pipe();
  80. cleanup:
  81. bpf_link__destroy(link);
  82. bpf_object__close(obj);
  83. return 0;
  84. }