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