1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */
3 
4 #include <string.h>
5 #include <linux/bpf.h>
6 #include <linux/limits.h>
7 #include <test_progs.h>
8 #include "trace_helpers.h"
9 #include "test_fill_link_info.skel.h"
10 
11 #define TP_CAT "sched"
12 #define TP_NAME "sched_switch"
13 
14 static const char *kmulti_syms[] = {
15 	"bpf_fentry_test2",
16 	"bpf_fentry_test1",
17 	"bpf_fentry_test3",
18 };
19 #define KMULTI_CNT ARRAY_SIZE(kmulti_syms)
20 static __u64 kmulti_addrs[KMULTI_CNT];
21 
22 #define KPROBE_FUNC "bpf_fentry_test1"
23 static __u64 kprobe_addr;
24 
25 #define UPROBE_FILE "/proc/self/exe"
26 static ssize_t uprobe_offset;
27 /* uprobe attach point */
28 static noinline void uprobe_func(void)
29 {
30 	asm volatile ("");
31 }
32 
33 static int verify_perf_link_info(int fd, enum bpf_perf_event_type type, long addr,
34 				 ssize_t offset, ssize_t entry_offset)
35 {
36 	struct bpf_link_info info;
37 	__u32 len = sizeof(info);
38 	char buf[PATH_MAX];
39 	int err;
40 
41 	memset(&info, 0, sizeof(info));
42 	buf[0] = '\0';
43 
44 again:
45 	err = bpf_link_get_info_by_fd(fd, &info, &len);
46 	if (!ASSERT_OK(err, "get_link_info"))
47 		return -1;
48 
49 	if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_PERF_EVENT, "link_type"))
50 		return -1;
51 	if (!ASSERT_EQ(info.perf_event.type, type, "perf_type_match"))
52 		return -1;
53 
54 	switch (info.perf_event.type) {
55 	case BPF_PERF_EVENT_KPROBE:
56 	case BPF_PERF_EVENT_KRETPROBE:
57 		ASSERT_EQ(info.perf_event.kprobe.offset, offset, "kprobe_offset");
58 
59 		/* In case kernel.kptr_restrict is not permitted or MAX_SYMS is reached */
60 		if (addr)
61 			ASSERT_EQ(info.perf_event.kprobe.addr, addr + entry_offset,
62 				  "kprobe_addr");
63 
64 		if (!info.perf_event.kprobe.func_name) {
65 			ASSERT_EQ(info.perf_event.kprobe.name_len, 0, "name_len");
66 			info.perf_event.kprobe.func_name = ptr_to_u64(&buf);
67 			info.perf_event.kprobe.name_len = sizeof(buf);
68 			goto again;
69 		}
70 
71 		err = strncmp(u64_to_ptr(info.perf_event.kprobe.func_name), KPROBE_FUNC,
72 			      strlen(KPROBE_FUNC));
73 		ASSERT_EQ(err, 0, "cmp_kprobe_func_name");
74 		break;
75 	case BPF_PERF_EVENT_TRACEPOINT:
76 		if (!info.perf_event.tracepoint.tp_name) {
77 			ASSERT_EQ(info.perf_event.tracepoint.name_len, 0, "name_len");
78 			info.perf_event.tracepoint.tp_name = ptr_to_u64(&buf);
79 			info.perf_event.tracepoint.name_len = sizeof(buf);
80 			goto again;
81 		}
82 
83 		err = strncmp(u64_to_ptr(info.perf_event.tracepoint.tp_name), TP_NAME,
84 			      strlen(TP_NAME));
85 		ASSERT_EQ(err, 0, "cmp_tp_name");
86 		break;
87 	case BPF_PERF_EVENT_UPROBE:
88 	case BPF_PERF_EVENT_URETPROBE:
89 		ASSERT_EQ(info.perf_event.uprobe.offset, offset, "uprobe_offset");
90 
91 		if (!info.perf_event.uprobe.file_name) {
92 			ASSERT_EQ(info.perf_event.uprobe.name_len, 0, "name_len");
93 			info.perf_event.uprobe.file_name = ptr_to_u64(&buf);
94 			info.perf_event.uprobe.name_len = sizeof(buf);
95 			goto again;
96 		}
97 
98 		err = strncmp(u64_to_ptr(info.perf_event.uprobe.file_name), UPROBE_FILE,
99 			      strlen(UPROBE_FILE));
100 			ASSERT_EQ(err, 0, "cmp_file_name");
101 		break;
102 	default:
103 		err = -1;
104 		break;
105 	}
106 	return err;
107 }
108 
109 static void kprobe_fill_invalid_user_buffer(int fd)
110 {
111 	struct bpf_link_info info;
112 	__u32 len = sizeof(info);
113 	int err;
114 
115 	memset(&info, 0, sizeof(info));
116 
117 	info.perf_event.kprobe.func_name = 0x1; /* invalid address */
118 	err = bpf_link_get_info_by_fd(fd, &info, &len);
119 	ASSERT_EQ(err, -EINVAL, "invalid_buff_and_len");
120 
121 	info.perf_event.kprobe.name_len = 64;
122 	err = bpf_link_get_info_by_fd(fd, &info, &len);
123 	ASSERT_EQ(err, -EFAULT, "invalid_buff");
124 
125 	info.perf_event.kprobe.func_name = 0;
126 	err = bpf_link_get_info_by_fd(fd, &info, &len);
127 	ASSERT_EQ(err, -EINVAL, "invalid_len");
128 
129 	ASSERT_EQ(info.perf_event.kprobe.addr, 0, "func_addr");
130 	ASSERT_EQ(info.perf_event.kprobe.offset, 0, "func_offset");
131 	ASSERT_EQ(info.perf_event.type, 0, "type");
132 }
133 
134 static void test_kprobe_fill_link_info(struct test_fill_link_info *skel,
135 				       enum bpf_perf_event_type type,
136 				       bool invalid)
137 {
138 	DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
139 		.attach_mode = PROBE_ATTACH_MODE_LINK,
140 		.retprobe = type == BPF_PERF_EVENT_KRETPROBE,
141 	);
142 	ssize_t entry_offset = 0;
143 	struct bpf_link *link;
144 	int link_fd, err;
145 
146 	link = bpf_program__attach_kprobe_opts(skel->progs.kprobe_run, KPROBE_FUNC, &opts);
147 	if (!ASSERT_OK_PTR(link, "attach_kprobe"))
148 		return;
149 
150 	link_fd = bpf_link__fd(link);
151 	if (!invalid) {
152 		/* See also arch_adjust_kprobe_addr(). */
153 		if (skel->kconfig->CONFIG_X86_KERNEL_IBT)
154 			entry_offset = 4;
155 		err = verify_perf_link_info(link_fd, type, kprobe_addr, 0, entry_offset);
156 		ASSERT_OK(err, "verify_perf_link_info");
157 	} else {
158 		kprobe_fill_invalid_user_buffer(link_fd);
159 	}
160 	bpf_link__destroy(link);
161 }
162 
163 static void test_tp_fill_link_info(struct test_fill_link_info *skel)
164 {
165 	struct bpf_link *link;
166 	int link_fd, err;
167 
168 	link = bpf_program__attach_tracepoint(skel->progs.tp_run, TP_CAT, TP_NAME);
169 	if (!ASSERT_OK_PTR(link, "attach_tp"))
170 		return;
171 
172 	link_fd = bpf_link__fd(link);
173 	err = verify_perf_link_info(link_fd, BPF_PERF_EVENT_TRACEPOINT, 0, 0, 0);
174 	ASSERT_OK(err, "verify_perf_link_info");
175 	bpf_link__destroy(link);
176 }
177 
178 static void test_uprobe_fill_link_info(struct test_fill_link_info *skel,
179 				       enum bpf_perf_event_type type)
180 {
181 	struct bpf_link *link;
182 	int link_fd, err;
183 
184 	link = bpf_program__attach_uprobe(skel->progs.uprobe_run,
185 					  type == BPF_PERF_EVENT_URETPROBE,
186 					  0, /* self pid */
187 					  UPROBE_FILE, uprobe_offset);
188 	if (!ASSERT_OK_PTR(link, "attach_uprobe"))
189 		return;
190 
191 	link_fd = bpf_link__fd(link);
192 	err = verify_perf_link_info(link_fd, type, 0, uprobe_offset, 0);
193 	ASSERT_OK(err, "verify_perf_link_info");
194 	bpf_link__destroy(link);
195 }
196 
197 static int verify_kmulti_link_info(int fd, bool retprobe)
198 {
199 	struct bpf_link_info info;
200 	__u32 len = sizeof(info);
201 	__u64 addrs[KMULTI_CNT];
202 	int flags, i, err;
203 
204 	memset(&info, 0, sizeof(info));
205 
206 again:
207 	err = bpf_link_get_info_by_fd(fd, &info, &len);
208 	if (!ASSERT_OK(err, "get_link_info"))
209 		return -1;
210 
211 	if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_KPROBE_MULTI, "kmulti_type"))
212 		return -1;
213 
214 	ASSERT_EQ(info.kprobe_multi.count, KMULTI_CNT, "func_cnt");
215 	flags = info.kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN;
216 	if (!retprobe)
217 		ASSERT_EQ(flags, 0, "kmulti_flags");
218 	else
219 		ASSERT_NEQ(flags, 0, "kretmulti_flags");
220 
221 	if (!info.kprobe_multi.addrs) {
222 		info.kprobe_multi.addrs = ptr_to_u64(addrs);
223 		goto again;
224 	}
225 	for (i = 0; i < KMULTI_CNT; i++)
226 		ASSERT_EQ(addrs[i], kmulti_addrs[i], "kmulti_addrs");
227 	return 0;
228 }
229 
230 static void verify_kmulti_invalid_user_buffer(int fd)
231 {
232 	struct bpf_link_info info;
233 	__u32 len = sizeof(info);
234 	__u64 addrs[KMULTI_CNT];
235 	int err, i;
236 
237 	memset(&info, 0, sizeof(info));
238 
239 	info.kprobe_multi.count = KMULTI_CNT;
240 	err = bpf_link_get_info_by_fd(fd, &info, &len);
241 	ASSERT_EQ(err, -EINVAL, "no_addr");
242 
243 	info.kprobe_multi.addrs = ptr_to_u64(addrs);
244 	info.kprobe_multi.count = 0;
245 	err = bpf_link_get_info_by_fd(fd, &info, &len);
246 	ASSERT_EQ(err, -EINVAL, "no_cnt");
247 
248 	for (i = 0; i < KMULTI_CNT; i++)
249 		addrs[i] = 0;
250 	info.kprobe_multi.count = KMULTI_CNT - 1;
251 	err = bpf_link_get_info_by_fd(fd, &info, &len);
252 	ASSERT_EQ(err, -ENOSPC, "smaller_cnt");
253 	for (i = 0; i < KMULTI_CNT - 1; i++)
254 		ASSERT_EQ(addrs[i], kmulti_addrs[i], "kmulti_addrs");
255 	ASSERT_EQ(addrs[i], 0, "kmulti_addrs");
256 
257 	for (i = 0; i < KMULTI_CNT; i++)
258 		addrs[i] = 0;
259 	info.kprobe_multi.count = KMULTI_CNT + 1;
260 	err = bpf_link_get_info_by_fd(fd, &info, &len);
261 	ASSERT_EQ(err, 0, "bigger_cnt");
262 	for (i = 0; i < KMULTI_CNT; i++)
263 		ASSERT_EQ(addrs[i], kmulti_addrs[i], "kmulti_addrs");
264 
265 	info.kprobe_multi.count = KMULTI_CNT;
266 	info.kprobe_multi.addrs = 0x1; /* invalid addr */
267 	err = bpf_link_get_info_by_fd(fd, &info, &len);
268 	ASSERT_EQ(err, -EFAULT, "invalid_buff");
269 }
270 
271 static int symbols_cmp_r(const void *a, const void *b)
272 {
273 	const char **str_a = (const char **) a;
274 	const char **str_b = (const char **) b;
275 
276 	return strcmp(*str_a, *str_b);
277 }
278 
279 static void test_kprobe_multi_fill_link_info(struct test_fill_link_info *skel,
280 					     bool retprobe, bool invalid)
281 {
282 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
283 	struct bpf_link *link;
284 	int link_fd, err;
285 
286 	opts.syms = kmulti_syms;
287 	opts.cnt = KMULTI_CNT;
288 	opts.retprobe = retprobe;
289 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.kmulti_run, NULL, &opts);
290 	if (!ASSERT_OK_PTR(link, "attach_kprobe_multi"))
291 		return;
292 
293 	link_fd = bpf_link__fd(link);
294 	if (!invalid) {
295 		err = verify_kmulti_link_info(link_fd, retprobe);
296 		ASSERT_OK(err, "verify_kmulti_link_info");
297 	} else {
298 		verify_kmulti_invalid_user_buffer(link_fd);
299 	}
300 	bpf_link__destroy(link);
301 }
302 
303 void test_fill_link_info(void)
304 {
305 	struct test_fill_link_info *skel;
306 	int i;
307 
308 	skel = test_fill_link_info__open_and_load();
309 	if (!ASSERT_OK_PTR(skel, "skel_open"))
310 		return;
311 
312 	/* load kallsyms to compare the addr */
313 	if (!ASSERT_OK(load_kallsyms_refresh(), "load_kallsyms_refresh"))
314 		goto cleanup;
315 
316 	kprobe_addr = ksym_get_addr(KPROBE_FUNC);
317 	if (test__start_subtest("kprobe_link_info"))
318 		test_kprobe_fill_link_info(skel, BPF_PERF_EVENT_KPROBE, false);
319 	if (test__start_subtest("kretprobe_link_info"))
320 		test_kprobe_fill_link_info(skel, BPF_PERF_EVENT_KRETPROBE, false);
321 	if (test__start_subtest("kprobe_invalid_ubuff"))
322 		test_kprobe_fill_link_info(skel, BPF_PERF_EVENT_KPROBE, true);
323 	if (test__start_subtest("tracepoint_link_info"))
324 		test_tp_fill_link_info(skel);
325 
326 	uprobe_offset = get_uprobe_offset(&uprobe_func);
327 	if (test__start_subtest("uprobe_link_info"))
328 		test_uprobe_fill_link_info(skel, BPF_PERF_EVENT_UPROBE);
329 	if (test__start_subtest("uretprobe_link_info"))
330 		test_uprobe_fill_link_info(skel, BPF_PERF_EVENT_URETPROBE);
331 
332 	qsort(kmulti_syms, KMULTI_CNT, sizeof(kmulti_syms[0]), symbols_cmp_r);
333 	for (i = 0; i < KMULTI_CNT; i++)
334 		kmulti_addrs[i] = ksym_get_addr(kmulti_syms[i]);
335 	if (test__start_subtest("kprobe_multi_link_info"))
336 		test_kprobe_multi_fill_link_info(skel, false, false);
337 	if (test__start_subtest("kretprobe_multi_link_info"))
338 		test_kprobe_multi_fill_link_info(skel, true, false);
339 	if (test__start_subtest("kprobe_multi_invalid_ubuff"))
340 		test_kprobe_multi_fill_link_info(skel, true, true);
341 
342 cleanup:
343 	test_fill_link_info__destroy(skel);
344 }
345