bpf: Allow void return type for global subprogs
Subsystems (4)
Emil Tsalapatis
reviewerPATCH 1/1 bpf: Allow void return type for global subprogs
Global subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type.
Also adjust existing selftests that were ensuring that global subprogs with void return types fail to verify. These tests should now load successfully.
--- kernel/bpf/btf.c | 7 +++-- kernel/bpf/verifier.c | 29 ++++++++++++++++++- .../selftests/bpf/progs/exceptions_fail.c | 2 +- .../selftests/bpf/progs/test_global_func7.c | 2 +- 4 files changed, 34 insertions(+), 6 deletions(-)
kernel/bpf/btf.c +4 -3
@@ -7835,15 +7835,16 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) tname, nargs, MAX_BPF_FUNC_REG_ARGS);return -EINVAL ; } -/* check that function returns int, exception cb also requires this */ +/* check that function is void or returns int, exception cb also requires this */ t =btf_type_by_id (btf, t->type);while (btf_type_is_modifier (t)) t =btf_type_by_id (btf, t->type); -if (!btf_type_is_int (t) && !btf_is_any_enum (t)) { +if (!btf_type_is_void (t) && !btf_type_is_int (t) && !btf_is_any_enum (t)) {if (!is_global)return -EINVAL ;bpf_log (log, -"Global function %s() doesn't return scalar. Only those are supported.\n" , +"Global function %s() return value not void or scalar. " +"Only those are supported.\n" , tname);return -EINVAL ; }
kernel/bpf/verifier.c +28 -1
@@ -443,6 +443,29 @@ static bool subprog_is_global(const struct bpf_verifier_env *env, int subprog)return aux && aux[subprog].linkage ==BTF_FUNC_GLOBAL ; } +static bool subprog_is_void (struct bpf_verifier_env *env,int subprog) +{ +const struct btf_type *type, *func, *func_proto; +const struct btf *btf = env->prog->aux->btf; +u32 btf_id; + + btf_id = env->prog->aux->func_info[subprog].type_id; + + func =btf_type_by_id (btf, btf_id); +if (verifier_bug_if (!func, env,"btf_id %u not found" , btf_id)) +return false; + + func_proto =btf_type_by_id (btf, func->type); +if (verifier_bug_if (!func_proto, env,"btf_id %u not found" , func->type)) +return false; + + type =btf_type_skip_modifiers (btf, func_proto->type,NULL ); +if (verifier_bug_if (!type, env,"btf_id %u not found" , func_proto->type)) +return false; + +return btf_type_is_void (type); +} +static const char *subprog_name (const struct bpf_verifier_env *env,int subprog) {struct bpf_func_info *info;
@@ -10875,7 +10898,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,subprog_aux (env, subprog)->called = true;clear_caller_saved_regs (env, caller->regs); -/* All global functions return a 64-bit SCALAR_VALUE */ +/* All non-void global functions return a 64-bit SCALAR_VALUE. */ mark_reg_unknown (env, caller->regs,BPF_REG_0 ); caller->regs[BPF_REG_0 ].subreg_def =DEF_NOT_SUBREG ;
@@ -17786,6 +17809,10 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const chardefault :break ; } + }else { +/* If this is a void global subprog, there is no return value. */ +if (subprog_is_global (env, frame->subprogno) &&subprog_is_void (env, frame->subprogno)) +return 0 ; }/* eBPF calling convention is such that R0 is used
tools/testing/selftests/bpf/progs/exceptions_fail.c +1 -1
@@ -51,7 +51,7 @@ __noinline int exception_cb_ok_arg_small(int a) SEC("?tc") __exception_cb(exception_cb_bad_ret_type) -__failure __msg("Global function exception_cb_bad_ret_type() doesn't return scalar.") +__failure __msg("Global function exception_cb_bad_ret_type() return value not void or scalar.")int reject_exception_cb_type_1 (struct __sk_buff *ctx) {bpf_throw (0 );
tools/testing/selftests/bpf/progs/test_global_func7.c +1 -1
@@ -12,7 +12,7 @@ void foo(struct __sk_buff *skb) } SEC("tc") -__failure __msg("foo() doesn't return scalar") +__successint global_func7 (struct __sk_buff *skb) {foo (skb);
Signature · 1 line
2.49.0
[email protected]
AI reviewThe comment was updated to say "non-void" but mark_reg_unknown() is still called unconditionally for all global functions. Should this skip marking R0 as SCALAR_VALUE when the callee is a void…
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 10903
Quoted 8 lines
kernel/bpf/verifier.c +1
-1@@ -10875,7 +10898,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,subprog_aux (env, subprog)->called = true;clear_caller_saved_regs (env, caller->regs); -/* All global functions return a 64-bit SCALAR_VALUE */ +/* All non-void global functions return a 64-bit SCALAR_VALUE. */ mark_reg_unknown (env, caller->regs,BPF_REG_0 ); caller->regs[BPF_REG_0 ].subreg_def =DEF_NOT_SUBREG ;
The comment was updated to say "non-void" but mark_reg_unknown() is still called unconditionally for all global functions. Should this skip marking R0 as SCALAR_VALUE when the callee is a void function?
As written, after calling a void global function, R0 is marked as a valid scalar that the caller can use, even though the callee never set it.
The new subprog_is_void() helper is used in check_return_code() to bypass the R0 validation at the callee's exit, but check_func_call() does not use it to avoid marking R0 at the call site.
Something like this might be needed here:
if (!subprog_is_void(env, subprog)) {
mark_reg_unknown(env, caller->regs, BPF_REG_0);
caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
}---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/21418326339
AI-authorship-score: low
AI-authorship-explanation: The code follows standard kernel conventions with appropriate use of existing patterns and helper functions, showing familiarity with the BPF verifier internals.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: The verifier incorrectly marks R0 as a valid scalar after calling a void global function, allowing programs to use an undefined value, though it does not directly cause crashes or security issues.
Emil Tsalapatis
reviewerQuoted 27 lines · bot+bpf-ci
Quoted 4 lines
kernel/bpf/verifier.c +0
-0[ ... ]
kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 10903
Quoted 8 lines
kernel/bpf/verifier.c +1
-1@@ -10875,7 +10898,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,subprog_aux (env, subprog)->called = true;clear_caller_saved_regs (env, caller->regs); -/* All global functions return a 64-bit SCALAR_VALUE */ +/* All non-void global functions return a 64-bit SCALAR_VALUE. */ mark_reg_unknown (env, caller->regs,BPF_REG_0 ); caller->regs[BPF_REG_0 ].subreg_def =DEF_NOT_SUBREG ;The comment was updated to say "non-void" but
mark_reg_unknown()is still called unconditionally for all global functions. Should this skip marking R0 asSCALAR_VALUEwhen the callee is a void function?As written, after calling a void global function, R0 is marked as a valid scalar that the caller can use, even though the callee never set it.
The new
subprog_is_void()helper is used incheck_return_code()to bypass the R0 validation at the callee's exit, butcheck_func_call()does not use it to avoid marking R0 at the call site.
Quoted 7 lines
Something like this might be needed here:
if (!subprog_is_void(env, subprog)) { mark_reg_unknown(env, caller->regs, BPF_REG_0); caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; }
The comment update is inaccurate, will undo the change.
Quoted 12 lines
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21418326339
AI-authorship-score: low
AI-authorship-explanation: The code follows standard kernel conventions with appropriate use of existing patterns and helper functions, showing familiarity with the BPF verifier internals.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: The verifier incorrectly marks R0 as a valid scalar after calling a void global function, allowing programs to use an undefined value, though it does not directly cause crashes or security issues.
Kumar Kartikeya Dwivedi
maintainerQuoted 25 lines · Emil Tsalapatis
Quoted 23 lines · bot+bpf-ci
Quoted 4 lines
kernel/bpf/verifier.c +0
-0[ ... ]
kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 10903
Quoted 8 lines
kernel/bpf/verifier.c +1
-1@@ -10875,7 +10898,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,subprog_aux (env, subprog)->called = true;clear_caller_saved_regs (env, caller->regs); -/* All global functions return a 64-bit SCALAR_VALUE */ +/* All non-void global functions return a 64-bit SCALAR_VALUE. */ mark_reg_unknown (env, caller->regs,BPF_REG_0 ); caller->regs[BPF_REG_0 ].subreg_def =DEF_NOT_SUBREG ;The comment was updated to say "non-void" but
mark_reg_unknown()is still called unconditionally for all global functions. Should this skip marking R0 asSCALAR_VALUEwhen the callee is a void function?As written, after calling a void global function, R0 is marked as a valid scalar that the caller can use, even though the callee never set it.
Quoted 7 lines
Quoted 7 lines
The new
subprog_is_void()helper is used incheck_return_code()to bypass the R0 validation at the callee's exit, butcheck_func_call()does not use it to avoid marking R0 at the call site.Something like this might be needed here:
if (!subprog_is_void(env, subprog)) {
I would also change the name of this function to something like "subprog_returns_void".
Quoted 20 lines
Quoted 4 lines
mark_reg_unknown(env, caller->regs, BPF_REG_0); caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; }The comment update is inaccurate, will undo the change.
Quoted 12 lines
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21418326339
AI-authorship-score: low
AI-authorship-explanation: The code follows standard kernel conventions with appropriate use of existing patterns and helper functions, showing familiarity with the BPF verifier internals.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: The verifier incorrectly marks R0 as a valid scalar after calling a void global function, allowing programs to use an undefined value, though it does not directly cause crashes or security issues.
Kumar Kartikeya Dwivedi
maintainerQuoted 11 lines · Emil Tsalapatis
Global subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type.
Also adjust existing selftests that were ensuring that global subprogs with void return types fail to verify. These tests should now load successfully.
Cc Kumar Kartikeya Dwivedi <[email protected]>Signed-off-by Emil Tsalapatis <[email protected]>---
Looks sane, apart from the renaming nit, could you also incorporate a test to ensure freplace logic fails when return types for global functions don't match?
We didn't have global functions with void returns before.
Quoted 1 line
[...]
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Emil Tsalapatis
Global subprogs are currently not allowed to return void. Adjust verifier logic to allow global functions with a void return type.
Also adjust existing selftests that were ensuring that global subprogs with void return types fail to verify. These tests should now load successfully.
Quoted 8 lines
Quoted 3 lines
Cc Kumar Kartikeya Dwivedi <[email protected]>Signed-off-by Emil Tsalapatis <[email protected]>---
Looks sane, apart from the renaming nit, could you also incorporate a test to ensure freplace logic fails when return types for global functions don't match?
We didn't have global functions with void returns before.
and also a test that R0 is not accessible after returning from void global.
clear_caller_saved_regs() plus what AI suggested should do it, but a test is still needed.
pw-bot: cr