xref: /linux/tools/testing/selftests/bpf/prog_tests/kfunc_call.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
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 	TC_TEST(kfunc_call_ctx, 0),
82 };
83 
84 struct syscall_test_args {
85 	__u8 data[16];
86 	size_t size;
87 };
88 
89 static void verify_success(struct kfunc_test_params *param)
90 {
91 	struct kfunc_call_test_lskel *lskel = NULL;
92 	LIBBPF_OPTS(bpf_test_run_opts, topts);
93 	struct bpf_prog_desc *lskel_prog;
94 	struct kfunc_call_test *skel;
95 	struct bpf_program *prog;
96 	int prog_fd, err;
97 	struct syscall_test_args args = {
98 		.size = 10,
99 	};
100 
101 	switch (param->test_type) {
102 	case syscall_test:
103 		topts.ctx_in = &args;
104 		topts.ctx_size_in = sizeof(args);
105 		/* fallthrough */
106 	case syscall_null_ctx_test:
107 		break;
108 	case tc_test:
109 		topts.data_in = &pkt_v4;
110 		topts.data_size_in = sizeof(pkt_v4);
111 		topts.repeat = 1;
112 		break;
113 	}
114 
115 	/* first test with normal libbpf */
116 	skel = kfunc_call_test__open_and_load();
117 	if (!ASSERT_OK_PTR(skel, "skel"))
118 		return;
119 
120 	prog = bpf_object__find_program_by_name(skel->obj, param->prog_name);
121 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
122 		goto cleanup;
123 
124 	prog_fd = bpf_program__fd(prog);
125 	err = bpf_prog_test_run_opts(prog_fd, &topts);
126 	if (!ASSERT_OK(err, param->prog_name))
127 		goto cleanup;
128 
129 	if (!ASSERT_EQ(topts.retval, param->retval, "retval"))
130 		goto cleanup;
131 
132 	/* second test with light skeletons */
133 	lskel = kfunc_call_test_lskel__open_and_load();
134 	if (!ASSERT_OK_PTR(lskel, "lskel"))
135 		goto cleanup;
136 
137 	lskel_prog = (struct bpf_prog_desc *)((char *)lskel + param->lskel_prog_desc_offset);
138 
139 	prog_fd = lskel_prog->prog_fd;
140 	err = bpf_prog_test_run_opts(prog_fd, &topts);
141 	if (!ASSERT_OK(err, param->prog_name))
142 		goto cleanup;
143 
144 	ASSERT_EQ(topts.retval, param->retval, "retval");
145 
146 cleanup:
147 	kfunc_call_test__destroy(skel);
148 	if (lskel)
149 		kfunc_call_test_lskel__destroy(lskel);
150 }
151 
152 static void verify_fail(struct kfunc_test_params *param)
153 {
154 	LIBBPF_OPTS(bpf_object_open_opts, opts);
155 	LIBBPF_OPTS(bpf_test_run_opts, topts);
156 	struct bpf_program *prog;
157 	struct kfunc_call_fail *skel;
158 	int prog_fd, err;
159 	struct syscall_test_args args = {
160 		.size = 10,
161 	};
162 
163 	opts.kernel_log_buf = obj_log_buf;
164 	opts.kernel_log_size = log_buf_sz;
165 	opts.kernel_log_level = 1;
166 
167 	switch (param->test_type) {
168 	case syscall_test:
169 		topts.ctx_in = &args;
170 		topts.ctx_size_in = sizeof(args);
171 		/* fallthrough */
172 	case syscall_null_ctx_test:
173 		break;
174 	case tc_test:
175 		topts.data_in = &pkt_v4;
176 		topts.data_size_in = sizeof(pkt_v4);
177 		topts.repeat = 1;
178 		break;
179 	}
180 
181 	skel = kfunc_call_fail__open_opts(&opts);
182 	if (!ASSERT_OK_PTR(skel, "kfunc_call_fail__open_opts"))
183 		goto cleanup;
184 
185 	prog = bpf_object__find_program_by_name(skel->obj, param->prog_name);
186 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
187 		goto cleanup;
188 
189 	bpf_program__set_autoload(prog, true);
190 
191 	err = kfunc_call_fail__load(skel);
192 	if (!param->retval) {
193 		/* the verifier is supposed to complain and refuses to load */
194 		if (!ASSERT_ERR(err, "unexpected load success"))
195 			goto out_err;
196 
197 	} else {
198 		/* the program is loaded but must dynamically fail */
199 		if (!ASSERT_OK(err, "unexpected load error"))
200 			goto out_err;
201 
202 		prog_fd = bpf_program__fd(prog);
203 		err = bpf_prog_test_run_opts(prog_fd, &topts);
204 		if (!ASSERT_EQ(err, param->retval, param->prog_name))
205 			goto out_err;
206 	}
207 
208 out_err:
209 	if (!ASSERT_OK_PTR(strstr(obj_log_buf, param->expected_err_msg), "expected_err_msg")) {
210 		fprintf(stderr, "Expected err_msg: %s\n", param->expected_err_msg);
211 		fprintf(stderr, "Verifier output: %s\n", obj_log_buf);
212 	}
213 
214 cleanup:
215 	kfunc_call_fail__destroy(skel);
216 }
217 
218 static void test_main(void)
219 {
220 	int i;
221 
222 	for (i = 0; i < ARRAY_SIZE(kfunc_tests); i++) {
223 		if (!test__start_subtest(kfunc_tests[i].prog_name))
224 			continue;
225 
226 		if (!kfunc_tests[i].expected_err_msg)
227 			verify_success(&kfunc_tests[i]);
228 		else
229 			verify_fail(&kfunc_tests[i]);
230 	}
231 }
232 
233 static void test_subprog(void)
234 {
235 	struct kfunc_call_test_subprog *skel;
236 	int prog_fd, err;
237 	LIBBPF_OPTS(bpf_test_run_opts, topts,
238 		.data_in = &pkt_v4,
239 		.data_size_in = sizeof(pkt_v4),
240 		.repeat = 1,
241 	);
242 
243 	skel = kfunc_call_test_subprog__open_and_load();
244 	if (!ASSERT_OK_PTR(skel, "skel"))
245 		return;
246 
247 	prog_fd = bpf_program__fd(skel->progs.kfunc_call_test1);
248 	err = bpf_prog_test_run_opts(prog_fd, &topts);
249 	ASSERT_OK(err, "bpf_prog_test_run(test1)");
250 	ASSERT_EQ(topts.retval, 10, "test1-retval");
251 	ASSERT_NEQ(skel->data->active_res, -1, "active_res");
252 	ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res");
253 
254 	kfunc_call_test_subprog__destroy(skel);
255 }
256 
257 static void test_subprog_lskel(void)
258 {
259 	struct kfunc_call_test_subprog_lskel *skel;
260 	int prog_fd, err;
261 	LIBBPF_OPTS(bpf_test_run_opts, topts,
262 		.data_in = &pkt_v4,
263 		.data_size_in = sizeof(pkt_v4),
264 		.repeat = 1,
265 	);
266 
267 	skel = kfunc_call_test_subprog_lskel__open_and_load();
268 	if (!ASSERT_OK_PTR(skel, "skel"))
269 		return;
270 
271 	prog_fd = skel->progs.kfunc_call_test1.prog_fd;
272 	err = bpf_prog_test_run_opts(prog_fd, &topts);
273 	ASSERT_OK(err, "bpf_prog_test_run(test1)");
274 	ASSERT_EQ(topts.retval, 10, "test1-retval");
275 	ASSERT_NEQ(skel->data->active_res, -1, "active_res");
276 	ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res");
277 
278 	kfunc_call_test_subprog_lskel__destroy(skel);
279 }
280 
281 static int test_destructive_open_and_load(void)
282 {
283 	struct kfunc_call_destructive *skel;
284 	int err;
285 
286 	skel = kfunc_call_destructive__open();
287 	if (!ASSERT_OK_PTR(skel, "prog_open"))
288 		return -1;
289 
290 	err = kfunc_call_destructive__load(skel);
291 
292 	kfunc_call_destructive__destroy(skel);
293 
294 	return err;
295 }
296 
297 static void test_destructive(void)
298 {
299 	__u64 save_caps = 0;
300 
301 	ASSERT_OK(test_destructive_open_and_load(), "successful_load");
302 
303 	if (!ASSERT_OK(cap_disable_effective(1ULL << CAP_SYS_BOOT, &save_caps), "drop_caps"))
304 		return;
305 
306 	ASSERT_EQ(test_destructive_open_and_load(), -13, "no_caps_failure");
307 
308 	cap_enable_effective(save_caps, NULL);
309 }
310 
311 void test_kfunc_call(void)
312 {
313 	test_main();
314 
315 	if (test__start_subtest("subprog"))
316 		test_subprog();
317 
318 	if (test__start_subtest("subprog_lskel"))
319 		test_subprog_lskel();
320 
321 	if (test__start_subtest("destructive"))
322 		test_destructive();
323 }
324