0001
0002 #include <stdio.h>
0003 #include <stdlib.h>
0004 #include <unistd.h>
0005 #include <linux/filter.h>
0006 #include <linux/seccomp.h>
0007 #include <sys/prctl.h>
0008 #include <bpf/bpf.h>
0009 #include <bpf/libbpf.h>
0010 #include "trace_helpers.h"
0011 #include "bpf_util.h"
0012
0013 #ifdef __mips__
0014 #define MAX_ENTRIES 6000
0015 #else
0016 #define MAX_ENTRIES 1024
0017 #endif
0018
0019
0020
0021
0022 static void install_accept_all_seccomp(void)
0023 {
0024 struct sock_filter filter[] = {
0025 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
0026 };
0027 struct sock_fprog prog = {
0028 .len = (unsigned short)ARRAY_SIZE(filter),
0029 .filter = filter,
0030 };
0031 if (prctl(PR_SET_SECCOMP, 2, &prog))
0032 perror("prctl");
0033 }
0034
0035 int main(int ac, char **argv)
0036 {
0037 struct bpf_link *link = NULL;
0038 struct bpf_program *prog;
0039 struct bpf_object *obj;
0040 int key, fd, progs_fd;
0041 const char *section;
0042 char filename[256];
0043 FILE *f;
0044
0045 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0046 obj = bpf_object__open_file(filename, NULL);
0047 if (libbpf_get_error(obj)) {
0048 fprintf(stderr, "ERROR: opening BPF object file failed\n");
0049 return 0;
0050 }
0051
0052 prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
0053 if (!prog) {
0054 printf("finding a prog in obj file failed\n");
0055 goto cleanup;
0056 }
0057
0058
0059 if (bpf_object__load(obj)) {
0060 fprintf(stderr, "ERROR: loading BPF object file failed\n");
0061 goto cleanup;
0062 }
0063
0064 link = bpf_program__attach(prog);
0065 if (libbpf_get_error(link)) {
0066 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
0067 link = NULL;
0068 goto cleanup;
0069 }
0070
0071 progs_fd = bpf_object__find_map_fd_by_name(obj, "progs");
0072 if (progs_fd < 0) {
0073 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
0074 goto cleanup;
0075 }
0076
0077 bpf_object__for_each_program(prog, obj) {
0078 section = bpf_program__section_name(prog);
0079
0080 if (sscanf(section, "kprobe/%d", &key) != 1)
0081 continue;
0082
0083 fd = bpf_program__fd(prog);
0084 bpf_map_update_elem(progs_fd, &key, &fd, BPF_ANY);
0085 }
0086
0087 install_accept_all_seccomp();
0088
0089 f = popen("dd if=/dev/zero of=/dev/null count=5", "r");
0090 (void) f;
0091
0092 read_trace_pipe();
0093
0094 cleanup:
0095 bpf_link__destroy(link);
0096 bpf_object__close(obj);
0097 return 0;
0098 }