1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <test_progs.h>
4 #include <network_helpers.h>
5 #include "kfunc_call_fail.skel.h"
6 #include "kfunc_call_test.skel.h"
7 #include "kfunc_call_test.lskel.h"
8 #include "kfunc_call_test_subprog.skel.h"
9 #include "kfunc_call_test_subprog.lskel.h"
10 #include "kfunc_call_destructive.skel.h"
11 
12 #include "cap_helpers.h"
13 
14 static size_t log_buf_sz = 1048576; /* 1 MB */
15 static char obj_log_buf[1048576];
16 
17 enum kfunc_test_type {
18 	tc_test = 0,
19 	syscall_test,
20 	syscall_null_ctx_test,
21 };
22 
23 struct kfunc_test_params {
24 	const char *prog_name;
25 	unsigned long lskel_prog_desc_offset;
26 	int retval;
27 	enum kfunc_test_type test_type;
28 	const char *expected_err_msg;
29 };
30 
31 #define __BPF_TEST_SUCCESS(name, __retval, type) \
32 	{ \
33 	  .prog_name = #name, \
34 	  .lskel_prog_desc_offset = offsetof(struct kfunc_call_test_lskel, progs.name), \
35 	  .retval = __retval, \
36 	  .test_type = type, \
37 	  .expected_err_msg = NULL, \
38 	}
39 
40 #define __BPF_TEST_FAIL(name, __retval, type, error_msg) \
41 	{ \
42 	  .prog_name = #name, \
43 	  .lskel_prog_desc_offset = 0 /* unused when test is failing */, \
44 	  .retval = __retval, \
45 	  .test_type = type, \
46 	  .expected_err_msg = error_msg, \
47 	}
48 
49 #define TC_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, tc_test)
50 #define SYSCALL_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, syscall_test)
51 #define SYSCALL_NULL_CTX_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, syscall_null_ctx_test)
52 
53 #define TC_FAIL(name, retval, error_msg) __BPF_TEST_FAIL(name, retval, tc_test, error_msg)
54 #define SYSCALL_NULL_CTX_FAIL(name, retval, error_msg) \
55 	__BPF_TEST_FAIL(name, retval, syscall_null_ctx_test, error_msg)
56 
57 static struct kfunc_test_params kfunc_tests[] = {
58 	/* failure cases:
59 	 * if retval is 0 -> the program will fail to load and the error message is an error
60 	 * if retval is not 0 -> the program can be loaded but running it will gives the
61 	 *                       provided return value. The error message is thus the one
62 	 *                       from a successful load
63 	 */
64 	SYSCALL_NULL_CTX_FAIL(kfunc_syscall_test_fail, -EINVAL, "processed 4 insns"),
65 	SYSCALL_NULL_CTX_FAIL(kfunc_syscall_test_null_fail, -EINVAL, "processed 4 insns"),
66 	TC_FAIL(kfunc_call_test_get_mem_fail_rdonly, 0, "R0 cannot write into rdonly_mem"),
67 	TC_FAIL(kfunc_call_test_get_mem_fail_use_after_free, 0, "invalid mem access 'scalar'"),
68 	TC_FAIL(kfunc_call_test_get_mem_fail_oob, 0, "min value is outside of the allowed memory range"),
69 	TC_FAIL(kfunc_call_test_get_mem_fail_not_const, 0, "is not a const"),
70 	TC_FAIL(kfunc_call_test_mem_acquire_fail, 0, "acquire kernel function does not return PTR_TO_BTF_ID"),
71 
72 	/* success cases */
73 	TC_TEST(kfunc_call_test1, 12),
74 	TC_TEST(kfunc_call_test2, 3),
75 	TC_TEST(kfunc_call_test4, -1234),
76 	TC_TEST(kfunc_call_test_ref_btf_id, 0),
77 	TC_TEST(kfunc_call_test_get_mem, 42),
78 	SYSCALL_TEST(kfunc_syscall_test, 0),
79 	SYSCALL_NULL_CTX_TEST(kfunc_syscall_test_null, 0),
80 	TC_TEST(kfunc_call_test_static_unused_arg, 0),
81 };
82 
83 struct syscall_test_args {
84 	__u8 data[16];
85 	size_t size;
86 };
87 
88 static void verify_success(struct kfunc_test_params *param)
89 {
90 	struct kfunc_call_test_lskel *lskel = NULL;
91 	LIBBPF_OPTS(bpf_test_run_opts, topts);
92 	struct bpf_prog_desc *lskel_prog;
93 	struct kfunc_call_test *skel;
94 	struct bpf_program *prog;
95 	int prog_fd, err;
96 	struct syscall_test_args args = {
97 		.size = 10,
98 	};
99 
100 	switch (param->test_type) {
101 	case syscall_test:
102 		topts.ctx_in = &args;
103 		topts.ctx_size_in = sizeof(args);
104 		/* fallthrough */
105 	case syscall_null_ctx_test:
106 		break;
107 	case tc_test:
108 		topts.data_in = &pkt_v4;
109 		topts.data_size_in = sizeof(pkt_v4);
110 		topts.repeat = 1;
111 		break;
112 	}
113 
114 	/* first test with normal libbpf */
115 	skel = kfunc_call_test__open_and_load();
116 	if (!ASSERT_OK_PTR(skel, "skel"))
117 		return;
118 
119 	prog = bpf_object__find_program_by_name(skel->obj, param->prog_name);
120 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
121 		goto cleanup;
122 
123 	prog_fd = bpf_program__fd(prog);
124 	err = bpf_prog_test_run_opts(prog_fd, &topts);
125 	if (!ASSERT_OK(err, param->prog_name))
126 		goto cleanup;
127 
128 	if (!ASSERT_EQ(topts.retval, param->retval, "retval"))
129 		goto cleanup;
130 
131 	/* second test with light skeletons */
132 	lskel = kfunc_call_test_lskel__open_and_load();
133 	if (!ASSERT_OK_PTR(lskel, "lskel"))
134 		goto cleanup;
135 
136 	lskel_prog = (struct bpf_prog_desc *)((char *)lskel + param->lskel_prog_desc_offset);
137 
138 	prog_fd = lskel_prog->prog_fd;
139 	err = bpf_prog_test_run_opts(prog_fd, &topts);
140 	if (!ASSERT_OK(err, param->prog_name))
141 		goto cleanup;
142 
143 	ASSERT_EQ(topts.retval, param->retval, "retval");
144 
145 cleanup:
146 	kfunc_call_test__destroy(skel);
147 	if (lskel)
148 		kfunc_call_test_lskel__destroy(lskel);
149 }
150 
151 static void verify_fail(struct kfunc_test_params *param)
152 {
153 	LIBBPF_OPTS(bpf_object_open_opts, opts);
154 	LIBBPF_OPTS(bpf_test_run_opts, topts);
155 	struct bpf_program *prog;
156 	struct kfunc_call_fail *skel;
157 	int prog_fd, err;
158 	struct syscall_test_args args = {
159 		.size = 10,
160 	};
161 
162 	opts.kernel_log_buf = obj_log_buf;
163 	opts.kernel_log_size = log_buf_sz;
164 	opts.kernel_log_level = 1;
165 
166 	switch (param->test_type) {
167 	case syscall_test:
168 		topts.ctx_in = &args;
169 		topts.ctx_size_in = sizeof(args);
170 		/* fallthrough */
171 	case syscall_null_ctx_test:
172 		break;
173 	case tc_test:
174 		topts.data_in = &pkt_v4;
175 		topts.data_size_in = sizeof(pkt_v4);
176 		topts.repeat = 1;
177 		break;
178 	}
179 
180 	skel = kfunc_call_fail__open_opts(&opts);
181 	if (!ASSERT_OK_PTR(skel, "kfunc_call_fail__open_opts"))
182 		goto cleanup;
183 
184 	prog = bpf_object__find_program_by_name(skel->obj, param->prog_name);
185 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
186 		goto cleanup;
187 
188 	bpf_program__set_autoload(prog, true);
189 
190 	err = kfunc_call_fail__load(skel);
191 	if (!param->retval) {
192 		/* the verifier is supposed to complain and refuses to load */
193 		if (!ASSERT_ERR(err, "unexpected load success"))
194 			goto out_err;
195 
196 	} else {
197 		/* the program is loaded but must dynamically fail */
198 		if (!ASSERT_OK(err, "unexpected load error"))
199 			goto out_err;
200 
201 		prog_fd = bpf_program__fd(prog);
202 		err = bpf_prog_test_run_opts(prog_fd, &topts);
203 		if (!ASSERT_EQ(err, param->retval, param->prog_name))
204 			goto out_err;
205 	}
206 
207 out_err:
208 	if (!ASSERT_OK_PTR(strstr(obj_log_buf, param->expected_err_msg), "expected_err_msg")) {
209 		fprintf(stderr, "Expected err_msg: %s\n", param->expected_err_msg);
210 		fprintf(stderr, "Verifier output: %s\n", obj_log_buf);
211 	}
212 
213 cleanup:
214 	kfunc_call_fail__destroy(skel);
215 }
216 
217 static void test_main(void)
218 {
219 	int i;
220 
221 	for (i = 0; i < ARRAY_SIZE(kfunc_tests); i++) {
222 		if (!test__start_subtest(kfunc_tests[i].prog_name))
223 			continue;
224 
225 		if (!kfunc_tests[i].expected_err_msg)
226 			verify_success(&kfunc_tests[i]);
227 		else
228 			verify_fail(&kfunc_tests[i]);
229 	}
230 }
231 
232 static void test_subprog(void)
233 {
234 	struct kfunc_call_test_subprog *skel;
235 	int prog_fd, err;
236 	LIBBPF_OPTS(bpf_test_run_opts, topts,
237 		.data_in = &pkt_v4,
238 		.data_size_in = sizeof(pkt_v4),
239 		.repeat = 1,
240 	);
241 
242 	skel = kfunc_call_test_subprog__open_and_load();
243 	if (!ASSERT_OK_PTR(skel, "skel"))
244 		return;
245 
246 	prog_fd = bpf_program__fd(skel->progs.kfunc_call_test1);
247 	err = bpf_prog_test_run_opts(prog_fd, &topts);
248 	ASSERT_OK(err, "bpf_prog_test_run(test1)");
249 	ASSERT_EQ(topts.retval, 10, "test1-retval");
250 	ASSERT_NEQ(skel->data->active_res, -1, "active_res");
251 	ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res");
252 
253 	kfunc_call_test_subprog__destroy(skel);
254 }
255 
256 static void test_subprog_lskel(void)
257 {
258 	struct kfunc_call_test_subprog_lskel *skel;
259 	int prog_fd, err;
260 	LIBBPF_OPTS(bpf_test_run_opts, topts,
261 		.data_in = &pkt_v4,
262 		.data_size_in = sizeof(pkt_v4),
263 		.repeat = 1,
264 	);
265 
266 	skel = kfunc_call_test_subprog_lskel__open_and_load();
267 	if (!ASSERT_OK_PTR(skel, "skel"))
268 		return;
269 
270 	prog_fd = skel->progs.kfunc_call_test1.prog_fd;
271 	err = bpf_prog_test_run_opts(prog_fd, &topts);
272 	ASSERT_OK(err, "bpf_prog_test_run(test1)");
273 	ASSERT_EQ(topts.retval, 10, "test1-retval");
274 	ASSERT_NEQ(skel->data->active_res, -1, "active_res");
275 	ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res");
276 
277 	kfunc_call_test_subprog_lskel__destroy(skel);
278 }
279 
280 static int test_destructive_open_and_load(void)
281 {
282 	struct kfunc_call_destructive *skel;
283 	int err;
284 
285 	skel = kfunc_call_destructive__open();
286 	if (!ASSERT_OK_PTR(skel, "prog_open"))
287 		return -1;
288 
289 	err = kfunc_call_destructive__load(skel);
290 
291 	kfunc_call_destructive__destroy(skel);
292 
293 	return err;
294 }
295 
296 static void test_destructive(void)
297 {
298 	__u64 save_caps = 0;
299 
300 	ASSERT_OK(test_destructive_open_and_load(), "successful_load");
301 
302 	if (!ASSERT_OK(cap_disable_effective(1ULL << CAP_SYS_BOOT, &save_caps), "drop_caps"))
303 		return;
304 
305 	ASSERT_EQ(test_destructive_open_and_load(), -13, "no_caps_failure");
306 
307 	cap_enable_effective(save_caps, NULL);
308 }
309 
310 void test_kfunc_call(void)
311 {
312 	test_main();
313 
314 	if (test__start_subtest("subprog"))
315 		test_subprog();
316 
317 	if (test__start_subtest("subprog_lskel"))
318 		test_subprog_lskel();
319 
320 	if (test__start_subtest("destructive"))
321 		test_destructive();
322 }
323