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 size_t log_buf_sz = 1048576; /* 1 MB */
14 static char obj_log_buf[1048576];
15 
16 static struct {
17 	const char *prog_name;
18 	const char *expected_verifier_err_msg;
19 	int expected_runtime_err;
20 } kfunc_dynptr_tests[] = {
21 	{"dynptr_type_not_supp",
22 	 "arg#0 pointer type STRUCT bpf_dynptr_kern points to unsupported dynamic pointer type", 0},
23 	{"not_valid_dynptr",
24 	 "arg#0 pointer type STRUCT bpf_dynptr_kern must be valid and initialized", 0},
25 	{"not_ptr_to_stack", "arg#0 pointer type STRUCT bpf_dynptr_kern not to stack", 0},
26 	{"dynptr_data_null", NULL, -EBADMSG},
27 };
28 
29 static bool kfunc_not_supported;
30 
31 static int libbpf_print_cb(enum libbpf_print_level level, const char *fmt,
32 			   va_list args)
33 {
34 	if (strcmp(fmt, "libbpf: extern (func ksym) '%s': not found in kernel or module BTFs\n"))
35 		return 0;
36 
37 	if (strcmp(va_arg(args, char *), "bpf_verify_pkcs7_signature"))
38 		return 0;
39 
40 	kfunc_not_supported = true;
41 	return 0;
42 }
43 
44 static void verify_fail(const char *prog_name, const char *expected_err_msg)
45 {
46 	struct test_kfunc_dynptr_param *skel;
47 	LIBBPF_OPTS(bpf_object_open_opts, opts);
48 	libbpf_print_fn_t old_print_cb;
49 	struct bpf_program *prog;
50 	int err;
51 
52 	opts.kernel_log_buf = obj_log_buf;
53 	opts.kernel_log_size = log_buf_sz;
54 	opts.kernel_log_level = 1;
55 
56 	skel = test_kfunc_dynptr_param__open_opts(&opts);
57 	if (!ASSERT_OK_PTR(skel, "test_kfunc_dynptr_param__open_opts"))
58 		goto cleanup;
59 
60 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
61 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
62 		goto cleanup;
63 
64 	bpf_program__set_autoload(prog, true);
65 
66 	bpf_map__set_max_entries(skel->maps.ringbuf, getpagesize());
67 
68 	kfunc_not_supported = false;
69 
70 	old_print_cb = libbpf_set_print(libbpf_print_cb);
71 	err = test_kfunc_dynptr_param__load(skel);
72 	libbpf_set_print(old_print_cb);
73 
74 	if (err < 0 && kfunc_not_supported) {
75 		fprintf(stderr,
76 		  "%s:SKIP:bpf_verify_pkcs7_signature() kfunc not supported\n",
77 		  __func__);
78 		test__skip();
79 		goto cleanup;
80 	}
81 
82 	if (!ASSERT_ERR(err, "unexpected load success"))
83 		goto cleanup;
84 
85 	if (!ASSERT_OK_PTR(strstr(obj_log_buf, expected_err_msg), "expected_err_msg")) {
86 		fprintf(stderr, "Expected err_msg: %s\n", expected_err_msg);
87 		fprintf(stderr, "Verifier output: %s\n", obj_log_buf);
88 	}
89 
90 cleanup:
91 	test_kfunc_dynptr_param__destroy(skel);
92 }
93 
94 static void verify_success(const char *prog_name, int expected_runtime_err)
95 {
96 	struct test_kfunc_dynptr_param *skel;
97 	libbpf_print_fn_t old_print_cb;
98 	struct bpf_program *prog;
99 	struct bpf_link *link;
100 	__u32 next_id;
101 	int err;
102 
103 	skel = test_kfunc_dynptr_param__open();
104 	if (!ASSERT_OK_PTR(skel, "test_kfunc_dynptr_param__open"))
105 		return;
106 
107 	skel->bss->pid = getpid();
108 
109 	bpf_map__set_max_entries(skel->maps.ringbuf, getpagesize());
110 
111 	kfunc_not_supported = false;
112 
113 	old_print_cb = libbpf_set_print(libbpf_print_cb);
114 	err = test_kfunc_dynptr_param__load(skel);
115 	libbpf_set_print(old_print_cb);
116 
117 	if (err < 0 && kfunc_not_supported) {
118 		fprintf(stderr,
119 		  "%s:SKIP:bpf_verify_pkcs7_signature() kfunc not supported\n",
120 		  __func__);
121 		test__skip();
122 		goto cleanup;
123 	}
124 
125 	if (!ASSERT_OK(err, "test_kfunc_dynptr_param__load"))
126 		goto cleanup;
127 
128 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
129 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
130 		goto cleanup;
131 
132 	link = bpf_program__attach(prog);
133 	if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
134 		goto cleanup;
135 
136 	err = bpf_prog_get_next_id(0, &next_id);
137 
138 	bpf_link__destroy(link);
139 
140 	if (!ASSERT_OK(err, "bpf_prog_get_next_id"))
141 		goto cleanup;
142 
143 	ASSERT_EQ(skel->bss->err, expected_runtime_err, "err");
144 
145 cleanup:
146 	test_kfunc_dynptr_param__destroy(skel);
147 }
148 
149 void test_kfunc_dynptr_param(void)
150 {
151 	int i;
152 
153 	for (i = 0; i < ARRAY_SIZE(kfunc_dynptr_tests); i++) {
154 		if (!test__start_subtest(kfunc_dynptr_tests[i].prog_name))
155 			continue;
156 
157 		if (kfunc_dynptr_tests[i].expected_verifier_err_msg)
158 			verify_fail(kfunc_dynptr_tests[i].prog_name,
159 			  kfunc_dynptr_tests[i].expected_verifier_err_msg);
160 		else
161 			verify_success(kfunc_dynptr_tests[i].prog_name,
162 				kfunc_dynptr_tests[i].expected_runtime_err);
163 	}
164 }
165