1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 #include <network_helpers.h>
5 #include <bpf/btf.h>
6 
7 typedef int (*test_cb)(struct bpf_object *obj);
8 
9 static int check_data_map(struct bpf_object *obj, int prog_cnt, bool reset)
10 {
11 	struct bpf_map *data_map = NULL, *map;
12 	__u64 *result = NULL;
13 	const int zero = 0;
14 	__u32 duration = 0;
15 	int ret = -1, i;
16 
17 	result = malloc((prog_cnt + 32 /* spare */) * sizeof(__u64));
18 	if (CHECK(!result, "alloc_memory", "failed to alloc memory"))
19 		return -ENOMEM;
20 
21 	bpf_object__for_each_map(map, obj)
22 		if (bpf_map__is_internal(map)) {
23 			data_map = map;
24 			break;
25 		}
26 	if (CHECK(!data_map, "find_data_map", "data map not found\n"))
27 		goto out;
28 
29 	ret = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, result);
30 	if (CHECK(ret, "get_result",
31 		  "failed to get output data: %d\n", ret))
32 		goto out;
33 
34 	for (i = 0; i < prog_cnt; i++) {
35 		if (CHECK(result[i] != 1, "result",
36 			  "fexit_bpf2bpf result[%d] failed err %llu\n",
37 			  i, result[i]))
38 			goto out;
39 		result[i] = 0;
40 	}
41 	if (reset) {
42 		ret = bpf_map_update_elem(bpf_map__fd(data_map), &zero, result, 0);
43 		if (CHECK(ret, "reset_result", "failed to reset result\n"))
44 			goto out;
45 	}
46 
47 	ret = 0;
48 out:
49 	free(result);
50 	return ret;
51 }
52 
53 static void test_fexit_bpf2bpf_common(const char *obj_file,
54 				      const char *target_obj_file,
55 				      int prog_cnt,
56 				      const char **prog_name,
57 				      bool run_prog,
58 				      test_cb cb)
59 {
60 	struct bpf_object *obj = NULL, *tgt_obj;
61 	__u32 retval, tgt_prog_id, info_len;
62 	struct bpf_prog_info prog_info = {};
63 	struct bpf_program **prog = NULL;
64 	struct bpf_link **link = NULL;
65 	int err, tgt_fd, i;
66 	struct btf *btf;
67 
68 	err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC,
69 			    &tgt_obj, &tgt_fd);
70 	if (!ASSERT_OK(err, "tgt_prog_load"))
71 		return;
72 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
73 			    .attach_prog_fd = tgt_fd,
74 			   );
75 
76 	info_len = sizeof(prog_info);
77 	err = bpf_obj_get_info_by_fd(tgt_fd, &prog_info, &info_len);
78 	if (!ASSERT_OK(err, "tgt_fd_get_info"))
79 		goto close_prog;
80 
81 	tgt_prog_id = prog_info.id;
82 	btf = bpf_object__btf(tgt_obj);
83 
84 	link = calloc(sizeof(struct bpf_link *), prog_cnt);
85 	if (!ASSERT_OK_PTR(link, "link_ptr"))
86 		goto close_prog;
87 
88 	prog = calloc(sizeof(struct bpf_program *), prog_cnt);
89 	if (!ASSERT_OK_PTR(prog, "prog_ptr"))
90 		goto close_prog;
91 
92 	obj = bpf_object__open_file(obj_file, &opts);
93 	if (!ASSERT_OK_PTR(obj, "obj_open"))
94 		goto close_prog;
95 
96 	err = bpf_object__load(obj);
97 	if (!ASSERT_OK(err, "obj_load"))
98 		goto close_prog;
99 
100 	for (i = 0; i < prog_cnt; i++) {
101 		struct bpf_link_info link_info;
102 		char *tgt_name;
103 		__s32 btf_id;
104 
105 		tgt_name = strstr(prog_name[i], "/");
106 		if (!ASSERT_OK_PTR(tgt_name, "tgt_name"))
107 			goto close_prog;
108 		btf_id = btf__find_by_name_kind(btf, tgt_name + 1, BTF_KIND_FUNC);
109 
110 		prog[i] = bpf_object__find_program_by_title(obj, prog_name[i]);
111 		if (!ASSERT_OK_PTR(prog[i], prog_name[i]))
112 			goto close_prog;
113 
114 		link[i] = bpf_program__attach_trace(prog[i]);
115 		if (!ASSERT_OK_PTR(link[i], "attach_trace"))
116 			goto close_prog;
117 
118 		info_len = sizeof(link_info);
119 		memset(&link_info, 0, sizeof(link_info));
120 		err = bpf_obj_get_info_by_fd(bpf_link__fd(link[i]),
121 					     &link_info, &info_len);
122 		ASSERT_OK(err, "link_fd_get_info");
123 		ASSERT_EQ(link_info.tracing.attach_type,
124 			  bpf_program__get_expected_attach_type(prog[i]),
125 			  "link_attach_type");
126 		ASSERT_EQ(link_info.tracing.target_obj_id, tgt_prog_id, "link_tgt_obj_id");
127 		ASSERT_EQ(link_info.tracing.target_btf_id, btf_id, "link_tgt_btf_id");
128 	}
129 
130 	if (cb) {
131 		err = cb(obj);
132 		if (err)
133 			goto close_prog;
134 	}
135 
136 	if (!run_prog)
137 		goto close_prog;
138 
139 	err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6),
140 				NULL, NULL, &retval, NULL);
141 	ASSERT_OK(err, "prog_run");
142 	ASSERT_EQ(retval, 0, "prog_run_ret");
143 
144 	if (check_data_map(obj, prog_cnt, false))
145 		goto close_prog;
146 
147 close_prog:
148 	for (i = 0; i < prog_cnt; i++)
149 		if (!IS_ERR_OR_NULL(link[i]))
150 			bpf_link__destroy(link[i]);
151 	if (!IS_ERR_OR_NULL(obj))
152 		bpf_object__close(obj);
153 	bpf_object__close(tgt_obj);
154 	free(link);
155 	free(prog);
156 }
157 
158 static void test_target_no_callees(void)
159 {
160 	const char *prog_name[] = {
161 		"fexit/test_pkt_md_access",
162 	};
163 	test_fexit_bpf2bpf_common("./fexit_bpf2bpf_simple.o",
164 				  "./test_pkt_md_access.o",
165 				  ARRAY_SIZE(prog_name),
166 				  prog_name, true, NULL);
167 }
168 
169 static void test_target_yes_callees(void)
170 {
171 	const char *prog_name[] = {
172 		"fexit/test_pkt_access",
173 		"fexit/test_pkt_access_subprog1",
174 		"fexit/test_pkt_access_subprog2",
175 		"fexit/test_pkt_access_subprog3",
176 	};
177 	test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
178 				  "./test_pkt_access.o",
179 				  ARRAY_SIZE(prog_name),
180 				  prog_name, true, NULL);
181 }
182 
183 static void test_func_replace(void)
184 {
185 	const char *prog_name[] = {
186 		"fexit/test_pkt_access",
187 		"fexit/test_pkt_access_subprog1",
188 		"fexit/test_pkt_access_subprog2",
189 		"fexit/test_pkt_access_subprog3",
190 		"freplace/get_skb_len",
191 		"freplace/get_skb_ifindex",
192 		"freplace/get_constant",
193 		"freplace/test_pkt_write_access_subprog",
194 	};
195 	test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
196 				  "./test_pkt_access.o",
197 				  ARRAY_SIZE(prog_name),
198 				  prog_name, true, NULL);
199 }
200 
201 static void test_func_replace_verify(void)
202 {
203 	const char *prog_name[] = {
204 		"freplace/do_bind",
205 	};
206 	test_fexit_bpf2bpf_common("./freplace_connect4.o",
207 				  "./connect4_prog.o",
208 				  ARRAY_SIZE(prog_name),
209 				  prog_name, false, NULL);
210 }
211 
212 static int test_second_attach(struct bpf_object *obj)
213 {
214 	const char *prog_name = "freplace/get_constant";
215 	const char *tgt_name = prog_name + 9; /* cut off freplace/ */
216 	const char *tgt_obj_file = "./test_pkt_access.o";
217 	struct bpf_program *prog = NULL;
218 	struct bpf_object *tgt_obj;
219 	__u32 duration = 0, retval;
220 	struct bpf_link *link;
221 	int err = 0, tgt_fd;
222 
223 	prog = bpf_object__find_program_by_title(obj, prog_name);
224 	if (CHECK(!prog, "find_prog", "prog %s not found\n", prog_name))
225 		return -ENOENT;
226 
227 	err = bpf_prog_load(tgt_obj_file, BPF_PROG_TYPE_UNSPEC,
228 			    &tgt_obj, &tgt_fd);
229 	if (CHECK(err, "second_prog_load", "file %s err %d errno %d\n",
230 		  tgt_obj_file, err, errno))
231 		return err;
232 
233 	link = bpf_program__attach_freplace(prog, tgt_fd, tgt_name);
234 	if (CHECK(IS_ERR(link), "second_link", "failed to attach second link prog_fd %d tgt_fd %d\n", bpf_program__fd(prog), tgt_fd))
235 		goto out;
236 
237 	err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6),
238 				NULL, NULL, &retval, &duration);
239 	if (CHECK(err || retval, "ipv6",
240 		  "err %d errno %d retval %d duration %d\n",
241 		  err, errno, retval, duration))
242 		goto out;
243 
244 	err = check_data_map(obj, 1, true);
245 	if (err)
246 		goto out;
247 
248 out:
249 	bpf_link__destroy(link);
250 	bpf_object__close(tgt_obj);
251 	return err;
252 }
253 
254 static void test_func_replace_multi(void)
255 {
256 	const char *prog_name[] = {
257 		"freplace/get_constant",
258 	};
259 	test_fexit_bpf2bpf_common("./freplace_get_constant.o",
260 				  "./test_pkt_access.o",
261 				  ARRAY_SIZE(prog_name),
262 				  prog_name, true, test_second_attach);
263 }
264 
265 static void test_fmod_ret_freplace(void)
266 {
267 	struct bpf_object *freplace_obj = NULL, *pkt_obj, *fmod_obj = NULL;
268 	const char *freplace_name = "./freplace_get_constant.o";
269 	const char *fmod_ret_name = "./fmod_ret_freplace.o";
270 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
271 	const char *tgt_name = "./test_pkt_access.o";
272 	struct bpf_link *freplace_link = NULL;
273 	struct bpf_program *prog;
274 	__u32 duration = 0;
275 	int err, pkt_fd;
276 
277 	err = bpf_prog_load(tgt_name, BPF_PROG_TYPE_UNSPEC,
278 			    &pkt_obj, &pkt_fd);
279 	/* the target prog should load fine */
280 	if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
281 		  tgt_name, err, errno))
282 		return;
283 	opts.attach_prog_fd = pkt_fd;
284 
285 	freplace_obj = bpf_object__open_file(freplace_name, &opts);
286 	if (CHECK(IS_ERR_OR_NULL(freplace_obj), "freplace_obj_open",
287 		  "failed to open %s: %ld\n", freplace_name,
288 		  PTR_ERR(freplace_obj)))
289 		goto out;
290 
291 	err = bpf_object__load(freplace_obj);
292 	if (CHECK(err, "freplace_obj_load", "err %d\n", err))
293 		goto out;
294 
295 	prog = bpf_program__next(NULL, freplace_obj);
296 	freplace_link = bpf_program__attach_trace(prog);
297 	if (CHECK(IS_ERR(freplace_link), "freplace_attach_trace", "failed to link\n"))
298 		goto out;
299 
300 	opts.attach_prog_fd = bpf_program__fd(prog);
301 	fmod_obj = bpf_object__open_file(fmod_ret_name, &opts);
302 	if (CHECK(IS_ERR_OR_NULL(fmod_obj), "fmod_obj_open",
303 		  "failed to open %s: %ld\n", fmod_ret_name,
304 		  PTR_ERR(fmod_obj)))
305 		goto out;
306 
307 	err = bpf_object__load(fmod_obj);
308 	if (CHECK(!err, "fmod_obj_load", "loading fmod_ret should fail\n"))
309 		goto out;
310 
311 out:
312 	bpf_link__destroy(freplace_link);
313 	bpf_object__close(freplace_obj);
314 	bpf_object__close(fmod_obj);
315 	bpf_object__close(pkt_obj);
316 }
317 
318 
319 static void test_func_sockmap_update(void)
320 {
321 	const char *prog_name[] = {
322 		"freplace/cls_redirect",
323 	};
324 	test_fexit_bpf2bpf_common("./freplace_cls_redirect.o",
325 				  "./test_cls_redirect.o",
326 				  ARRAY_SIZE(prog_name),
327 				  prog_name, false, NULL);
328 }
329 
330 static void test_obj_load_failure_common(const char *obj_file,
331 					  const char *target_obj_file)
332 
333 {
334 	/*
335 	 * standalone test that asserts failure to load freplace prog
336 	 * because of invalid return code.
337 	 */
338 	struct bpf_object *obj = NULL, *pkt_obj;
339 	int err, pkt_fd;
340 	__u32 duration = 0;
341 
342 	err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC,
343 			    &pkt_obj, &pkt_fd);
344 	/* the target prog should load fine */
345 	if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
346 		  target_obj_file, err, errno))
347 		return;
348 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
349 			    .attach_prog_fd = pkt_fd,
350 			   );
351 
352 	obj = bpf_object__open_file(obj_file, &opts);
353 	if (CHECK(IS_ERR_OR_NULL(obj), "obj_open",
354 		  "failed to open %s: %ld\n", obj_file,
355 		  PTR_ERR(obj)))
356 		goto close_prog;
357 
358 	/* It should fail to load the program */
359 	err = bpf_object__load(obj);
360 	if (CHECK(!err, "bpf_obj_load should fail", "err %d\n", err))
361 		goto close_prog;
362 
363 close_prog:
364 	if (!IS_ERR_OR_NULL(obj))
365 		bpf_object__close(obj);
366 	bpf_object__close(pkt_obj);
367 }
368 
369 static void test_func_replace_return_code(void)
370 {
371 	/* test invalid return code in the replaced program */
372 	test_obj_load_failure_common("./freplace_connect_v4_prog.o",
373 				     "./connect4_prog.o");
374 }
375 
376 static void test_func_map_prog_compatibility(void)
377 {
378 	/* test with spin lock map value in the replaced program */
379 	test_obj_load_failure_common("./freplace_attach_probe.o",
380 				     "./test_attach_probe.o");
381 }
382 
383 void test_fexit_bpf2bpf(void)
384 {
385 	if (test__start_subtest("target_no_callees"))
386 		test_target_no_callees();
387 	if (test__start_subtest("target_yes_callees"))
388 		test_target_yes_callees();
389 	if (test__start_subtest("func_replace"))
390 		test_func_replace();
391 	if (test__start_subtest("func_replace_verify"))
392 		test_func_replace_verify();
393 	if (test__start_subtest("func_sockmap_update"))
394 		test_func_sockmap_update();
395 	if (test__start_subtest("func_replace_return_code"))
396 		test_func_replace_return_code();
397 	if (test__start_subtest("func_map_prog_compatibility"))
398 		test_func_map_prog_compatibility();
399 	if (test__start_subtest("func_replace_multi"))
400 		test_func_replace_multi();
401 	if (test__start_subtest("fmod_ret_freplace"))
402 		test_fmod_ret_freplace();
403 }
404