xref: /linux/samples/bpf/tracex5_user.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 #include <stdio.h>
2 #include <linux/bpf.h>
3 #include <unistd.h>
4 #include <linux/filter.h>
5 #include <linux/seccomp.h>
6 #include <sys/prctl.h>
7 #include "libbpf.h"
8 #include "bpf_load.h"
9 
10 /* install fake seccomp program to enable seccomp code path inside the kernel,
11  * so that our kprobe attached to seccomp_phase1() can be triggered
12  */
13 static void install_accept_all_seccomp(void)
14 {
15 	struct sock_filter filter[] = {
16 		BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
17 	};
18 	struct sock_fprog prog = {
19 		.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
20 		.filter = filter,
21 	};
22 	if (prctl(PR_SET_SECCOMP, 2, &prog))
23 		perror("prctl");
24 }
25 
26 int main(int ac, char **argv)
27 {
28 	FILE *f;
29 	char filename[256];
30 
31 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
32 
33 	if (load_bpf_file(filename)) {
34 		printf("%s", bpf_log_buf);
35 		return 1;
36 	}
37 
38 	install_accept_all_seccomp();
39 
40 	f = popen("dd if=/dev/zero of=/dev/null count=5", "r");
41 	(void) f;
42 
43 	read_trace_pipe();
44 
45 	return 0;
46 }
47