xref: /linux/tools/testing/selftests/bpf/progs/read_vsyscall.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1be66d791SHou Tao // SPDX-License-Identifier: GPL-2.0
2be66d791SHou Tao /* Copyright (C) 2024. Huawei Technologies Co., Ltd */
3*ddc3d988SJordan Rome #include "vmlinux.h"
4be66d791SHou Tao #include <linux/types.h>
5be66d791SHou Tao #include <bpf/bpf_helpers.h>
6be66d791SHou Tao 
7be66d791SHou Tao #include "bpf_misc.h"
8be66d791SHou Tao 
9be66d791SHou Tao int target_pid = 0;
10be66d791SHou Tao void *user_ptr = 0;
11*ddc3d988SJordan Rome int read_ret[9];
12be66d791SHou Tao 
13be66d791SHou Tao char _license[] SEC("license") = "GPL";
14be66d791SHou Tao 
15*ddc3d988SJordan Rome /*
16*ddc3d988SJordan Rome  * This is the only kfunc, the others are helpers
17*ddc3d988SJordan Rome  */
18*ddc3d988SJordan Rome int bpf_copy_from_user_str(void *dst, u32, const void *, u64) __weak __ksym;
19*ddc3d988SJordan Rome 
20be66d791SHou Tao SEC("fentry/" SYS_PREFIX "sys_nanosleep")
do_probe_read(void * ctx)21be66d791SHou Tao int do_probe_read(void *ctx)
22be66d791SHou Tao {
23be66d791SHou Tao 	char buf[8];
24be66d791SHou Tao 
25be66d791SHou Tao 	if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
26be66d791SHou Tao 		return 0;
27be66d791SHou Tao 
28be66d791SHou Tao 	read_ret[0] = bpf_probe_read_kernel(buf, sizeof(buf), user_ptr);
29be66d791SHou Tao 	read_ret[1] = bpf_probe_read_kernel_str(buf, sizeof(buf), user_ptr);
30be66d791SHou Tao 	read_ret[2] = bpf_probe_read(buf, sizeof(buf), user_ptr);
31be66d791SHou Tao 	read_ret[3] = bpf_probe_read_str(buf, sizeof(buf), user_ptr);
32be66d791SHou Tao 	read_ret[4] = bpf_probe_read_user(buf, sizeof(buf), user_ptr);
33be66d791SHou Tao 	read_ret[5] = bpf_probe_read_user_str(buf, sizeof(buf), user_ptr);
34be66d791SHou Tao 
35be66d791SHou Tao 	return 0;
36be66d791SHou Tao }
37be66d791SHou Tao 
38be66d791SHou Tao SEC("fentry.s/" SYS_PREFIX "sys_nanosleep")
do_copy_from_user(void * ctx)39be66d791SHou Tao int do_copy_from_user(void *ctx)
40be66d791SHou Tao {
41be66d791SHou Tao 	char buf[8];
42be66d791SHou Tao 
43be66d791SHou Tao 	if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
44be66d791SHou Tao 		return 0;
45be66d791SHou Tao 
46be66d791SHou Tao 	read_ret[6] = bpf_copy_from_user(buf, sizeof(buf), user_ptr);
47be66d791SHou Tao 	read_ret[7] = bpf_copy_from_user_task(buf, sizeof(buf), user_ptr,
48be66d791SHou Tao 					      bpf_get_current_task_btf(), 0);
49*ddc3d988SJordan Rome 	read_ret[8] = bpf_copy_from_user_str((char *)buf, sizeof(buf), user_ptr, 0);
50be66d791SHou Tao 
51be66d791SHou Tao 	return 0;
52be66d791SHou Tao }
53