Skip to content

Commit bee109b

Browse files
gentoo-rootanakryiko
authored andcommitted
bpf: Fix error message on kfunc arg type mismatch
When "arg#%d expected pointer to ctx, but got %s" error is printed, both template parts actually point to the type of the argument, therefore, it will also say "but got PTR", regardless of what was the actual register type. Fix the message to print the register type in the second part of the template, change the existing test to adapt to the new format, and add a new test to test the case when arg is a pointer to context, but reg is a scalar. Fixes: 00b8586 ("bpf: Rewrite kfunc argument handling") Signed-off-by: Maxim Mikityanskiy <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f028d77 commit bee109b

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

kernel/bpf/verifier.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12132,7 +12132,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1213212132
switch (kf_arg_type) {
1213312133
case KF_ARG_PTR_TO_CTX:
1213412134
if (reg->type != PTR_TO_CTX) {
12135-
verbose(env, "arg#%d expected pointer to ctx, but got %s\n", i, btf_type_str(t));
12135+
verbose(env, "arg#%d expected pointer to ctx, but got %s\n",
12136+
i, reg_type_str(env, reg->type));
1213612137
return -EINVAL;
1213712138
}
1213812139

tools/testing/selftests/bpf/prog_tests/kfunc_call.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static struct kfunc_test_params kfunc_tests[] = {
6868
TC_FAIL(kfunc_call_test_get_mem_fail_oob, 0, "min value is outside of the allowed memory range"),
6969
TC_FAIL(kfunc_call_test_get_mem_fail_not_const, 0, "is not a const"),
7070
TC_FAIL(kfunc_call_test_mem_acquire_fail, 0, "acquire kernel function does not return PTR_TO_BTF_ID"),
71+
TC_FAIL(kfunc_call_test_pointer_arg_type_mismatch, 0, "arg#0 expected pointer to ctx, but got scalar"),
7172

7273
/* success cases */
7374
TC_TEST(kfunc_call_test1, 12),

tools/testing/selftests/bpf/progs/kfunc_call_fail.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,11 @@ int kfunc_call_test_mem_acquire_fail(struct __sk_buff *skb)
150150
return ret;
151151
}
152152

153+
SEC("?tc")
154+
int kfunc_call_test_pointer_arg_type_mismatch(struct __sk_buff *skb)
155+
{
156+
bpf_kfunc_call_test_pass_ctx((void *)10);
157+
return 0;
158+
}
159+
153160
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/verifier/calls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
},
7777
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
7878
.result = REJECT,
79-
.errstr = "arg#0 expected pointer to ctx, but got PTR",
79+
.errstr = "arg#0 expected pointer to ctx, but got fp",
8080
.fixup_kfunc_btf_id = {
8181
{ "bpf_kfunc_call_test_pass_ctx", 2 },
8282
},

0 commit comments

Comments
 (0)