1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright (c) 2022 Facebook
5  * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
6  *
7  * Author: Roberto Sassu <roberto.sassu@huawei.com>
8  */
9 
10 #include <test_progs.h>
11 #include "test_kfunc_dynptr_param.skel.h"
12 
13 static struct {
14 	const char *prog_name;
15 	int expected_runtime_err;
16 } kfunc_dynptr_tests[] = {
17 	{"dynptr_data_null", -EBADMSG},
18 };
19 
20 static bool kfunc_not_supported;
21 
libbpf_print_cb(enum libbpf_print_level level,const char * fmt,va_list args)22 static int libbpf_print_cb(enum libbpf_print_level level, const char *fmt,
23 			   va_list args)
24 {
25 	if (strcmp(fmt, "libbpf: extern (func ksym) '%s': not found in kernel or module BTFs\n"))
26 		return 0;
27 
28 	if (strcmp(va_arg(args, char *), "bpf_verify_pkcs7_signature"))
29 		return 0;
30 
31 	kfunc_not_supported = true;
32 	return 0;
33 }
34 
has_pkcs7_kfunc_support(void)35 static bool has_pkcs7_kfunc_support(void)
36 {
37 	struct test_kfunc_dynptr_param *skel;
38 	libbpf_print_fn_t old_print_cb;
39 	int err;
40 
41 	skel = test_kfunc_dynptr_param__open();
42 	if (!ASSERT_OK_PTR(skel, "test_kfunc_dynptr_param__open"))
43 		return false;
44 
45 	kfunc_not_supported = false;
46 
47 	old_print_cb = libbpf_set_print(libbpf_print_cb);
48 	err = test_kfunc_dynptr_param__load(skel);
49 	libbpf_set_print(old_print_cb);
50 
51 	if (err < 0 && kfunc_not_supported) {
52 		fprintf(stderr,
53 		  "%s:SKIP:bpf_verify_pkcs7_signature() kfunc not supported\n",
54 		  __func__);
55 		test_kfunc_dynptr_param__destroy(skel);
56 		return false;
57 	}
58 
59 	test_kfunc_dynptr_param__destroy(skel);
60 
61 	return true;
62 }
63 
verify_success(const char * prog_name,int expected_runtime_err)64 static void verify_success(const char *prog_name, int expected_runtime_err)
65 {
66 	struct test_kfunc_dynptr_param *skel;
67 	struct bpf_program *prog;
68 	struct bpf_link *link;
69 	__u32 next_id;
70 	int err;
71 
72 	skel = test_kfunc_dynptr_param__open();
73 	if (!ASSERT_OK_PTR(skel, "test_kfunc_dynptr_param__open"))
74 		return;
75 
76 	skel->bss->pid = getpid();
77 
78 	err = test_kfunc_dynptr_param__load(skel);
79 
80 	if (!ASSERT_OK(err, "test_kfunc_dynptr_param__load"))
81 		goto cleanup;
82 
83 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
84 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
85 		goto cleanup;
86 
87 	link = bpf_program__attach(prog);
88 	if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
89 		goto cleanup;
90 
91 	err = bpf_prog_get_next_id(0, &next_id);
92 
93 	bpf_link__destroy(link);
94 
95 	if (!ASSERT_OK(err, "bpf_prog_get_next_id"))
96 		goto cleanup;
97 
98 	ASSERT_EQ(skel->bss->err, expected_runtime_err, "err");
99 
100 cleanup:
101 	test_kfunc_dynptr_param__destroy(skel);
102 }
103 
test_kfunc_dynptr_param(void)104 void test_kfunc_dynptr_param(void)
105 {
106 	int i;
107 
108 	if (!has_pkcs7_kfunc_support())
109 		return;
110 
111 	for (i = 0; i < ARRAY_SIZE(kfunc_dynptr_tests); i++) {
112 		if (!test__start_subtest(kfunc_dynptr_tests[i].prog_name))
113 			continue;
114 
115 		verify_success(kfunc_dynptr_tests[i].prog_name,
116 			kfunc_dynptr_tests[i].expected_runtime_err);
117 	}
118 	RUN_TESTS(test_kfunc_dynptr_param);
119 }
120