xref: /linux/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
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 };
27 
28 void test_read_vsyscall(void)
29 {
30 	struct read_vsyscall *skel;
31 	unsigned int i;
32 	int err;
33 
34 #if !defined(__x86_64__)
35 	test__skip();
36 	return;
37 #endif
38 	skel = read_vsyscall__open_and_load();
39 	if (!ASSERT_OK_PTR(skel, "read_vsyscall open_load"))
40 		return;
41 
42 	skel->bss->target_pid = getpid();
43 	err = read_vsyscall__attach(skel);
44 	if (!ASSERT_EQ(err, 0, "read_vsyscall attach"))
45 		goto out;
46 
47 	/* userspace may don't have vsyscall page due to LEGACY_VSYSCALL_NONE,
48 	 * but it doesn't affect the returned error codes.
49 	 */
50 	skel->bss->user_ptr = (void *)VSYSCALL_ADDR;
51 	usleep(1);
52 
53 	for (i = 0; i < ARRAY_SIZE(all_read); i++)
54 		ASSERT_EQ(skel->bss->read_ret[i], all_read[i].ret, all_read[i].name);
55 out:
56 	read_vsyscall__destroy(skel);
57 }
58