Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 00b85860feb8 ("bpf: Rewrite kfunc argument handling")
Signed-off-by: Maxim Mikityanskiy <maxim@isovalent.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/bpf/20240909133909.1315460-1-maxim@isovalent.com

authored by

Maxim Mikityanskiy and committed by
Andrii Nakryiko
bee109b7 f028d771

+11 -2
+2 -1
kernel/bpf/verifier.c
··· 12132 12132 switch (kf_arg_type) { 12133 12133 case KF_ARG_PTR_TO_CTX: 12134 12134 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)); 12136 12137 return -EINVAL; 12137 12138 } 12138 12139
+1
tools/testing/selftests/bpf/prog_tests/kfunc_call.c
··· 68 68 TC_FAIL(kfunc_call_test_get_mem_fail_oob, 0, "min value is outside of the allowed memory range"), 69 69 TC_FAIL(kfunc_call_test_get_mem_fail_not_const, 0, "is not a const"), 70 70 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"), 71 72 72 73 /* success cases */ 73 74 TC_TEST(kfunc_call_test1, 12),
+7
tools/testing/selftests/bpf/progs/kfunc_call_fail.c
··· 150 150 return ret; 151 151 } 152 152 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 + 153 160 char _license[] SEC("license") = "GPL";
+1 -1
tools/testing/selftests/bpf/verifier/calls.c
··· 76 76 }, 77 77 .prog_type = BPF_PROG_TYPE_SCHED_CLS, 78 78 .result = REJECT, 79 - .errstr = "arg#0 expected pointer to ctx, but got PTR", 79 + .errstr = "arg#0 expected pointer to ctx, but got fp", 80 80 .fixup_kfunc_btf_id = { 81 81 { "bpf_kfunc_call_test_pass_ctx", 2 }, 82 82 },