summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/kfunc_call.c
blob: 5af1ee8f0e6ee2589e72e7f4bed15f4ea9f2857c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include <test_progs.h>
#include <network_helpers.h>
#include "kfunc_call_fail.skel.h"
#include "kfunc_call_test.skel.h"
#include "kfunc_call_test.lskel.h"
#include "kfunc_call_test_subprog.skel.h"
#include "kfunc_call_test_subprog.lskel.h"
#include "kfunc_call_destructive.skel.h"

#include "cap_helpers.h"

static size_t log_buf_sz = 1048576; /* 1 MB */
static char obj_log_buf[1048576];

enum kfunc_test_type {
	tc_test = 0,
	syscall_test,
	syscall_null_ctx_test,
};

struct kfunc_test_params {
	const char *prog_name;
	unsigned long lskel_prog_desc_offset;
	int retval;
	enum kfunc_test_type test_type;
	const char *expected_err_msg;
};

#define __BPF_TEST_SUCCESS(name, __retval, type) \
	{ \
	  .prog_name = #name, \
	  .lskel_prog_desc_offset = offsetof(struct kfunc_call_test_lskel, progs.name), \
	  .retval = __retval, \
	  .test_type = type, \
	  .expected_err_msg = NULL, \
	}

#define __BPF_TEST_FAIL(name, __retval, type, error_msg) \
	{ \
	  .prog_name = #name, \
	  .lskel_prog_desc_offset = 0 /* unused when test is failing */, \
	  .retval = __retval, \
	  .test_type = type, \
	  .expected_err_msg = error_msg, \
	}

#define TC_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, tc_test)
#define SYSCALL_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, syscall_test)
#define SYSCALL_NULL_CTX_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, syscall_null_ctx_test)

#define TC_FAIL(name, retval, error_msg) __BPF_TEST_FAIL(name, retval, tc_test, error_msg)
#define SYSCALL_NULL_CTX_FAIL(name, retval, error_msg) \
	__BPF_TEST_FAIL(name, retval, syscall_null_ctx_test, error_msg)

static struct kfunc_test_params kfunc_tests[] = {
	/* failure cases:
	 * if retval is 0 -> the program will fail to load and the error message is an error
	 * if retval is not 0 -> the program can be loaded but running it will gives the
	 *                       provided return value. The error message is thus the one
	 *                       from a successful load
	 */
	SYSCALL_NULL_CTX_FAIL(kfunc_syscall_test_fail, -EINVAL, "processed 4 insns"),
	SYSCALL_NULL_CTX_FAIL(kfunc_syscall_test_null_fail, -EINVAL, "processed 4 insns"),
	TC_FAIL(kfunc_call_test_get_mem_fail_rdonly, 0, "R0 cannot write into rdonly_mem"),
	TC_FAIL(kfunc_call_test_get_mem_fail_use_after_free, 0, "invalid mem access 'scalar'"),
	TC_FAIL(kfunc_call_test_get_mem_fail_oob, 0, "min value is outside of the allowed memory range"),
	TC_FAIL(kfunc_call_test_get_mem_fail_not_const, 0, "is not a const"),
	TC_FAIL(kfunc_call_test_mem_acquire_fail, 0, "acquire kernel function does not return PTR_TO_BTF_ID"),

	/* success cases */
	TC_TEST(kfunc_call_test1, 12),
	TC_TEST(kfunc_call_test2, 3),
	TC_TEST(kfunc_call_test_ref_btf_id, 0),
	TC_TEST(kfunc_call_test_get_mem, 42),
	SYSCALL_TEST(kfunc_syscall_test, 0),
	SYSCALL_NULL_CTX_TEST(kfunc_syscall_test_null, 0),
};

struct syscall_test_args {
	__u8 data[16];
	size_t size;
};

static void verify_success(struct kfunc_test_params *param)
{
	struct kfunc_call_test_lskel *lskel = NULL;
	LIBBPF_OPTS(bpf_test_run_opts, topts);
	struct bpf_prog_desc *lskel_prog;
	struct kfunc_call_test *skel;
	struct bpf_program *prog;
	int prog_fd, err;
	struct syscall_test_args args = {
		.size = 10,
	};

	switch (param->test_type) {
	case syscall_test:
		topts.ctx_in = &args;
		topts.ctx_size_in = sizeof(args);
		/* fallthrough */
	case syscall_null_ctx_test:
		break;
	case tc_test:
		topts.data_in = &pkt_v4;
		topts.data_size_in = sizeof(pkt_v4);
		topts.repeat = 1;
		break;
	}

	/* first test with normal libbpf */
	skel = kfunc_call_test__open_and_load();
	if (!ASSERT_OK_PTR(skel, "skel"))
		return;

	prog = bpf_object__find_program_by_name(skel->obj, param->prog_name);
	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
		goto cleanup;

	prog_fd = bpf_program__fd(prog);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, param->prog_name))
		goto cleanup;

	if (!ASSERT_EQ(topts.retval, param->retval, "retval"))
		goto cleanup;

	/* second test with light skeletons */
	lskel = kfunc_call_test_lskel__open_and_load();
	if (!ASSERT_OK_PTR(lskel, "lskel"))
		goto cleanup;

	lskel_prog = (struct bpf_prog_desc *)((char *)lskel + param->lskel_prog_desc_offset);

	prog_fd = lskel_prog->prog_fd;
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, param->prog_name))
		goto cleanup;

	ASSERT_EQ(topts.retval, param->retval, "retval");

cleanup:
	kfunc_call_test__destroy(skel);
	if (lskel)
		kfunc_call_test_lskel__destroy(lskel);
}

static void verify_fail(struct kfunc_test_params *param)
{
	LIBBPF_OPTS(bpf_object_open_opts, opts);
	LIBBPF_OPTS(bpf_test_run_opts, topts);
	struct bpf_program *prog;
	struct kfunc_call_fail *skel;
	int prog_fd, err;
	struct syscall_test_args args = {
		.size = 10,
	};

	opts.kernel_log_buf = obj_log_buf;
	opts.kernel_log_size = log_buf_sz;
	opts.kernel_log_level = 1;

	switch (param->test_type) {
	case syscall_test:
		topts.ctx_in = &args;
		topts.ctx_size_in = sizeof(args);
		/* fallthrough */
	case syscall_null_ctx_test:
		break;
	case tc_test:
		topts.data_in = &pkt_v4;
		topts.data_size_in = sizeof(pkt_v4);
		break;
		topts.repeat = 1;
	}

	skel = kfunc_call_fail__open_opts(&opts);
	if (!ASSERT_OK_PTR(skel, "kfunc_call_fail__open_opts"))
		goto cleanup;

	prog = bpf_object__find_program_by_name(skel->obj, param->prog_name);
	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
		goto cleanup;

	bpf_program__set_autoload(prog, true);

	err = kfunc_call_fail__load(skel);
	if (!param->retval) {
		/* the verifier is supposed to complain and refuses to load */
		if (!ASSERT_ERR(err, "unexpected load success"))
			goto out_err;

	} else {
		/* the program is loaded but must dynamically fail */
		if (!ASSERT_OK(err, "unexpected load error"))
			goto out_err;

		prog_fd = bpf_program__fd(prog);
		err = bpf_prog_test_run_opts(prog_fd, &topts);
		if (!ASSERT_EQ(err, param->retval, param->prog_name))
			goto out_err;
	}

out_err:
	if (!ASSERT_OK_PTR(strstr(obj_log_buf, param->expected_err_msg), "expected_err_msg")) {
		fprintf(stderr, "Expected err_msg: %s\n", param->expected_err_msg);
		fprintf(stderr, "Verifier output: %s\n", obj_log_buf);
	}

cleanup:
	kfunc_call_fail__destroy(skel);
}

static void test_main(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(kfunc_tests); i++) {
		if (!test__start_subtest(kfunc_tests[i].prog_name))
			continue;

		if (!kfunc_tests[i].expected_err_msg)
			verify_success(&kfunc_tests[i]);
		else
			verify_fail(&kfunc_tests[i]);
	}
}

static void test_subprog(void)
{
	struct kfunc_call_test_subprog *skel;
	int prog_fd, err;
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.data_in = &pkt_v4,
		.data_size_in = sizeof(pkt_v4),
		.repeat = 1,
	);

	skel = kfunc_call_test_subprog__open_and_load();
	if (!ASSERT_OK_PTR(skel, "skel"))
		return;

	prog_fd = bpf_program__fd(skel->progs.kfunc_call_test1);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "bpf_prog_test_run(test1)");
	ASSERT_EQ(topts.retval, 10, "test1-retval");
	ASSERT_NEQ(skel->data->active_res, -1, "active_res");
	ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res");

	kfunc_call_test_subprog__destroy(skel);
}

static void test_subprog_lskel(void)
{
	struct kfunc_call_test_subprog_lskel *skel;
	int prog_fd, err;
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.data_in = &pkt_v4,
		.data_size_in = sizeof(pkt_v4),
		.repeat = 1,
	);

	skel = kfunc_call_test_subprog_lskel__open_and_load();
	if (!ASSERT_OK_PTR(skel, "skel"))
		return;

	prog_fd = skel->progs.kfunc_call_test1.prog_fd;
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "bpf_prog_test_run(test1)");
	ASSERT_EQ(topts.retval, 10, "test1-retval");
	ASSERT_NEQ(skel->data->active_res, -1, "active_res");
	ASSERT_EQ(skel->data->sk_state_res, BPF_TCP_CLOSE, "sk_state_res");

	kfunc_call_test_subprog_lskel__destroy(skel);
}

static int test_destructive_open_and_load(void)
{
	struct kfunc_call_destructive *skel;
	int err;

	skel = kfunc_call_destructive__open();
	if (!ASSERT_OK_PTR(skel, "prog_open"))
		return -1;

	err = kfunc_call_destructive__load(skel);

	kfunc_call_destructive__destroy(skel);

	return err;
}

static void test_destructive(void)
{
	__u64 save_caps = 0;

	ASSERT_OK(test_destructive_open_and_load(), "successful_load");

	if (!ASSERT_OK(cap_disable_effective(1ULL << CAP_SYS_BOOT, &save_caps), "drop_caps"))
		return;

	ASSERT_EQ(test_destructive_open_and_load(), -13, "no_caps_failure");

	cap_enable_effective(save_caps, NULL);
}

void test_kfunc_call(void)
{
	test_main();

	if (test__start_subtest("subprog"))
		test_subprog();

	if (test__start_subtest("subprog_lskel"))
		test_subprog_lskel();

	if (test__start_subtest("destructive"))
		test_destructive();
}