xref: /linux/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2024. Huawei Technologies Co., Ltd */
3 #include "test_progs.h"
4 #include "read_vsyscall.skel.h"
5 
6 #if defined(__x86_64__)
7 /* For VSYSCALL_ADDR */
8 #include <asm/vsyscall.h>
9 #else
10 /* To prevent build failure on non-x86 arch */
11 #define VSYSCALL_ADDR 0UL
12 #endif
13 
14 struct read_ret_desc {
15 	const char *name;
16 	int ret;
17 } all_read[] = {
18 	{ .name = "probe_read_kernel", .ret = -ERANGE },
19 	{ .name = "probe_read_kernel_str", .ret = -ERANGE },
20 	{ .name = "probe_read", .ret = -ERANGE },
21 	{ .name = "probe_read_str", .ret = -ERANGE },
22 	{ .name = "probe_read_user", .ret = -EFAULT },
23 	{ .name = "probe_read_user_str", .ret = -EFAULT },
24 	{ .name = "copy_from_user", .ret = -EFAULT },
25 	{ .name = "copy_from_user_task", .ret = -EFAULT },
26 	{ .name = "copy_from_user_str", .ret = -EFAULT },
27 };
28 
test_read_vsyscall(void)29 void test_read_vsyscall(void)
30 {
31 	struct read_vsyscall *skel;
32 	unsigned int i;
33 	int err;
34 
35 #if !defined(__x86_64__)
36 	test__skip();
37 	return;
38 #endif
39 	skel = read_vsyscall__open_and_load();
40 	if (!ASSERT_OK_PTR(skel, "read_vsyscall open_load"))
41 		return;
42 
43 	skel->bss->target_pid = getpid();
44 	err = read_vsyscall__attach(skel);
45 	if (!ASSERT_EQ(err, 0, "read_vsyscall attach"))
46 		goto out;
47 
48 	/* userspace may don't have vsyscall page due to LEGACY_VSYSCALL_NONE,
49 	 * but it doesn't affect the returned error codes.
50 	 */
51 	skel->bss->user_ptr = (void *)VSYSCALL_ADDR;
52 	usleep(1);
53 
54 	for (i = 0; i < ARRAY_SIZE(all_read); i++)
55 		ASSERT_EQ(skel->bss->read_ret[i], all_read[i].ret, all_read[i].name);
56 out:
57 	read_vsyscall__destroy(skel);
58 }
59