1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25bacd780SAlexei Starovoitov #include <stdio.h>
3bc1a8597SDaniel T. Lee #include <stdlib.h>
45bacd780SAlexei Starovoitov #include <unistd.h>
55bacd780SAlexei Starovoitov #include <linux/filter.h>
65bacd780SAlexei Starovoitov #include <linux/seccomp.h>
75bacd780SAlexei Starovoitov #include <sys/prctl.h>
82bf3e2efSJakub Kicinski #include <bpf/bpf.h>
9bc1a8597SDaniel T. Lee #include <bpf/libbpf.h>
1024a6034aSDaniel T. Lee #include "trace_helpers.h"
11b1fc28b3SLinkui Xiao #include "bpf_util.h"
125bacd780SAlexei Starovoitov
13bc1a8597SDaniel T. Lee #ifdef __mips__
14bc1a8597SDaniel T. Lee #define MAX_ENTRIES 6000 /* MIPS n64 syscalls start at 5000 */
15bc1a8597SDaniel T. Lee #else
16bc1a8597SDaniel T. Lee #define MAX_ENTRIES 1024
17bc1a8597SDaniel T. Lee #endif
18bc1a8597SDaniel T. Lee
195bacd780SAlexei Starovoitov /* install fake seccomp program to enable seccomp code path inside the kernel,
205bacd780SAlexei Starovoitov * so that our kprobe attached to seccomp_phase1() can be triggered
215bacd780SAlexei Starovoitov */
install_accept_all_seccomp(void)225bacd780SAlexei Starovoitov static void install_accept_all_seccomp(void)
235bacd780SAlexei Starovoitov {
245bacd780SAlexei Starovoitov struct sock_filter filter[] = {
255bacd780SAlexei Starovoitov BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
265bacd780SAlexei Starovoitov };
275bacd780SAlexei Starovoitov struct sock_fprog prog = {
28b1fc28b3SLinkui Xiao .len = (unsigned short)ARRAY_SIZE(filter),
295bacd780SAlexei Starovoitov .filter = filter,
305bacd780SAlexei Starovoitov };
315bacd780SAlexei Starovoitov if (prctl(PR_SET_SECCOMP, 2, &prog))
325bacd780SAlexei Starovoitov perror("prctl");
335bacd780SAlexei Starovoitov }
345bacd780SAlexei Starovoitov
main(int ac,char ** argv)355bacd780SAlexei Starovoitov int main(int ac, char **argv)
365bacd780SAlexei Starovoitov {
37bc1a8597SDaniel T. Lee struct bpf_link *link = NULL;
38bc1a8597SDaniel T. Lee struct bpf_program *prog;
39bc1a8597SDaniel T. Lee struct bpf_object *obj;
40bc1a8597SDaniel T. Lee int key, fd, progs_fd;
41698584dfSDaniel T. Lee const char *section;
42bc1a8597SDaniel T. Lee char filename[256];
43bc1a8597SDaniel T. Lee FILE *f;
445bacd780SAlexei Starovoitov
45*4a0ee788SDaniel T. Lee snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
46bc1a8597SDaniel T. Lee obj = bpf_object__open_file(filename, NULL);
47bc1a8597SDaniel T. Lee if (libbpf_get_error(obj)) {
48bc1a8597SDaniel T. Lee fprintf(stderr, "ERROR: opening BPF object file failed\n");
49bc1a8597SDaniel T. Lee return 0;
50bc1a8597SDaniel T. Lee }
51bc1a8597SDaniel T. Lee
52bc1a8597SDaniel T. Lee prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
53bc1a8597SDaniel T. Lee if (!prog) {
54bc1a8597SDaniel T. Lee printf("finding a prog in obj file failed\n");
55bc1a8597SDaniel T. Lee goto cleanup;
56bc1a8597SDaniel T. Lee }
57bc1a8597SDaniel T. Lee
58bc1a8597SDaniel T. Lee /* load BPF program */
59bc1a8597SDaniel T. Lee if (bpf_object__load(obj)) {
60bc1a8597SDaniel T. Lee fprintf(stderr, "ERROR: loading BPF object file failed\n");
61bc1a8597SDaniel T. Lee goto cleanup;
62bc1a8597SDaniel T. Lee }
63bc1a8597SDaniel T. Lee
64bc1a8597SDaniel T. Lee link = bpf_program__attach(prog);
65bc1a8597SDaniel T. Lee if (libbpf_get_error(link)) {
66bc1a8597SDaniel T. Lee fprintf(stderr, "ERROR: bpf_program__attach failed\n");
67bc1a8597SDaniel T. Lee link = NULL;
68bc1a8597SDaniel T. Lee goto cleanup;
69bc1a8597SDaniel T. Lee }
70bc1a8597SDaniel T. Lee
71bc1a8597SDaniel T. Lee progs_fd = bpf_object__find_map_fd_by_name(obj, "progs");
72bc1a8597SDaniel T. Lee if (progs_fd < 0) {
73bc1a8597SDaniel T. Lee fprintf(stderr, "ERROR: finding a map in obj file failed\n");
74bc1a8597SDaniel T. Lee goto cleanup;
75bc1a8597SDaniel T. Lee }
76bc1a8597SDaniel T. Lee
77bc1a8597SDaniel T. Lee bpf_object__for_each_program(prog, obj) {
78698584dfSDaniel T. Lee section = bpf_program__section_name(prog);
79bc1a8597SDaniel T. Lee /* register only syscalls to PROG_ARRAY */
80698584dfSDaniel T. Lee if (sscanf(section, "kprobe/%d", &key) != 1)
81bc1a8597SDaniel T. Lee continue;
82bc1a8597SDaniel T. Lee
83bc1a8597SDaniel T. Lee fd = bpf_program__fd(prog);
84bc1a8597SDaniel T. Lee bpf_map_update_elem(progs_fd, &key, &fd, BPF_ANY);
855bacd780SAlexei Starovoitov }
865bacd780SAlexei Starovoitov
875bacd780SAlexei Starovoitov install_accept_all_seccomp();
885bacd780SAlexei Starovoitov
895bacd780SAlexei Starovoitov f = popen("dd if=/dev/zero of=/dev/null count=5", "r");
905bacd780SAlexei Starovoitov (void) f;
915bacd780SAlexei Starovoitov
925bacd780SAlexei Starovoitov read_trace_pipe();
935bacd780SAlexei Starovoitov
94bc1a8597SDaniel T. Lee cleanup:
95bc1a8597SDaniel T. Lee bpf_link__destroy(link);
96bc1a8597SDaniel T. Lee bpf_object__close(obj);
975bacd780SAlexei Starovoitov return 0;
985bacd780SAlexei Starovoitov }
99