bpf: Fix signedness bug
PATCH 1/1 bpf: Fix signedness bug
The function acquire_reference() returns negative error codes and 'id' is an unsigned integer, so the check (id < 0) is always impossible.
Detected by Smatch:kernel/bpf/verifier.c:13115 check_kfunc_call()
warn: unsigned 'id' is never less than zero.
--- kernel/bpf/verifier.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +5 -3
@@ -13111,9 +13111,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, }mark_btf_func_reg_size (env,BPF_REG_0 ,sizeof (void *));if (is_kfunc_acquire (&meta)) { - id =acquire_reference (env, insn_idx,0 ); -if (id <0 ) -return id; + err =acquire_reference (env, insn_idx,0 ); +if (err <0 ) +return err; + id = err; + regs[BPF_REG_0 ].id = id; }else if (is_rbtree_node_type (ptr_type) ||is_list_node_type (ptr_type)) {ref_set_non_owning (env, ®s[BPF_REG_0 ]);
Signature · 3 lines
Thanks, ET https://github.com/sponsors/ethantidmore
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit message falsely claims that the variable 'id' is an unsigned integer, making the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit message falsely claims that the variable 'id' is an unsigned integer, making the error check (id < 0) impossible. This claim is demonstrably incorrect; 'id' is declared as a signed integer, meaning the patch fixes a non-existent bug. --
commit c4f0a14bac5d07880856111e136127510f48012c
Author: Ethan Tidmore <[email protected]>
bpf: Fix signedness bug
This commit refactors the error handling for acquire_reference() by using a separate err variable, claiming that the id variable is unsigned and cannot be checked for negative error codes.
Quoted 6 lines
The function
acquire_reference()returns negative error codes and 'id' is an unsigned integer, so the check (id < 0) is always impossible.Detected by Smatch:
kernel/bpf/verifier.c:13115check_kfunc_call()
warn: unsigned 'id' is never less than zero.
[Severity: Medium]
Is this description accurate? Looking at check_kfunc_call(), id appears to be explicitly declared as a signed int, not an unsigned integer.
Since acquire_reference() can return negative error codes like -ENOMEM, wouldn't the original check (id < 0) work correctly?
It looks like the Smatch warning might be a false positive, and the original code was already handling the error properly.
Quoted 7 lines
kernel/bpf/verifier.c +0
-0@@ -13167,9 +13167,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, }mark_btf_func_reg_size (env,BPF_REG_0 ,sizeof (void *));
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 13175
Quoted 10 lines
kernel/bpf/verifier.c +5
-3if (is_kfunc_acquire (&meta)) { - id =acquire_reference (env, insn_idx,0 ); -if (id <0 ) -return id; + err =acquire_reference (env, insn_idx,0 ); +if (err <0 ) +return err; + id = err; + regs[BPF_REG_0 ].id = id;
[Severity: Medium]
Is this change necessary? If id is actually a signed int, the original code would have properly caught the negative error returns from acquire_reference() without needing to introduce the err variable.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewIs this commit message accurate? Looking at check_kfunc_call() in kernel/bpf/verifier.c, the variable 'id' is declared as a signed int:
Quoted 7 lines
commit
cc27f12c12ff54b8eace339c3e130317bf205277
Author: Ethan Tidmore <[email protected]>bpf: Fix signedness bug
The function
acquire_reference()returns negative error codes and 'id' is an unsigned integer, so the check (id < 0) is always impossible.
Is this commit message accurate? Looking at check_kfunc_call() in kernel/bpf/verifier.c, the variable 'id' is declared as a signed int:
const struct btf_param *args;
u32 i, nargs, ptr_type_id;
struct btf *desc_btf;
int id;Since acquire_reference() can return negative error codes like -ENOMEM, wouldn't the original check (id < 0) work correctly?
The three other call sites of acquire_reference() in the verifier all use the same signed-int pattern without issue:
verifier.c:mark_stack_slots_dynptr():
id = acquire_reference(env, insn_idx, dynptr_id);
if (id < 0)
return id;
verifier.c:mark_stack_slots_iter():
id = acquire_reference(env, insn_idx, ITER_TYPE_TO_ID(iter_type));
if (id < 0)
return id;
verifier.c:check_helper_call():
id = acquire_reference(env, insn_idx, fn->arg_type[arg_num]);
if (id < 0)
return id;It looks like the Smatch warning might be a false positive, and the original code was already handling the error properly.
Quoted 17 lines
Detected by Smatch:
kernel/bpf/verifier.c:13115check_kfunc_call()
warn: unsigned 'id' is never less than zero.Fixes 308c7a0ae8859 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug")Signed-off-by Ethan Tidmore <[email protected]>kernel/bpf/verifier.c +0
-1@@ -13167,9 +13167,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, }mark_btf_func_reg_size (env,BPF_REG_0 ,sizeof (void *));if (is_kfunc_acquire (&meta)) { - id =acquire_reference (env, insn_idx,0 );
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 13177
Quoted 10 lines
kernel/bpf/verifier.c +5
-2-if (id <0 ) -return id; + err =acquire_reference (env, insn_idx,0 ); +if (err <0 ) +return err; + id = err; + regs[BPF_REG_0 ].id = id; }else if (is_rbtree_node_type (ptr_type) ||is_list_node_type (ptr_type)) {ref_set_non_owning (env, ®s[BPF_REG_0 ]);
The code change itself is behavior-equivalent since 'err' is also a signed int, so the patch doesn't introduce a runtime regression. But if the premise that 'id' is unsigned is wrong, should the commit message be corrected?
---
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/27050796130
Quoted 9 lines · On Fri, Jun 5, 2026 at 8:25 PM <[email protected]> wrote:
Quoted 8 lines
commit
cc27f12c12ff54b8eace339c3e130317bf205277
Author: Ethan Tidmore <[email protected]>bpf: Fix signedness bug
The function
acquire_reference()returns negative error codes and 'id' is an unsigned integer, so the check (id < 0) is always impossible.
Quoted 7 lines
Is this commit message accurate? Looking at
check_kfunc_call()inkernel/bpf/verifier.c, the variable 'id' is declared as a signed int:const struct btf_param *args; u32 i, nargs, ptr_type_id; struct btf *desc_btf; int id;
Hi,
As pointed out by the bot, the fix to the issue has been merged as part of the following patchset.
https://lore.kernel.org/bpf/[email protected]/
Thanks,
Amery
Quoted 63 lines
Since
acquire_reference()can return negative error codes like-ENOMEM, wouldn't the original check (id < 0) work correctly?The three other call sites of
acquire_reference()in the verifier all use the same signed-int pattern without issue:verifier.c:mark_stack_slots_dynptr(): id = acquire_reference(env, insn_idx, dynptr_id); if (id < 0) return id; verifier.c:mark_stack_slots_iter(): id = acquire_reference(env, insn_idx, ITER_TYPE_TO_ID(iter_type)); if (id < 0) return id; verifier.c:check_helper_call(): id = acquire_reference(env, insn_idx, fn->arg_type[arg_num]); if (id < 0) return id;It looks like the Smatch warning might be a false positive, and the original code was already handling the error properly.
Quoted 17 lines
Detected by Smatch:
kernel/bpf/verifier.c:13115check_kfunc_call()
warn: unsigned 'id' is never less than zero.Fixes 308c7a0ae8859 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug")Signed-off-by Ethan Tidmore <[email protected]>kernel/bpf/verifier.c +0
-1@@ -13167,9 +13167,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, }mark_btf_func_reg_size (env,BPF_REG_0 ,sizeof (void *));if (is_kfunc_acquire (&meta)) { - id =acquire_reference (env, insn_idx,0 );kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 13177
Quoted 10 lines
kernel/bpf/verifier.c +5
-2-if (id <0 ) -return id; + err =acquire_reference (env, insn_idx,0 ); +if (err <0 ) +return err; + id = err; + regs[BPF_REG_0 ].id = id; }else if (is_rbtree_node_type (ptr_type) ||is_list_node_type (ptr_type)) {ref_set_non_owning (env, ®s[BPF_REG_0 ]);The code change itself is behavior-equivalent since 'err' is also a signed int, so the patch doesn't introduce a runtime regression. But if the premise that 'id' is unsigned is wrong, should the commit message be corrected?
---
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/27050796130
Quoted 8 lines · bot+bpf-ci
Quoted 7 lines
commit
cc27f12c12ff54b8eace339c3e130317bf205277
Author: Ethan Tidmore <[email protected]>bpf: Fix signedness bug
The function
acquire_reference()returns negative error codes and 'id' is an unsigned integer, so the check (id < 0) is always impossible.
Quoted 7 lines
Is this commit message accurate? Looking at
check_kfunc_call()inkernel/bpf/verifier.c, the variable 'id' is declared as a signed int:const struct btf_param *args; u32 i, nargs, ptr_type_id; struct btf *desc_btf; int id;
No, 'id' is assigned as a u32:
u32 i, nargs, ptr_type_id, id;
--
Thanks,
ET
https://github.com/sponsors/ethantidmore
Quoted 13 lines · Ethan Tidmore
Quoted 11 lines · bot+bpf-ci
Quoted 7 lines
commit
cc27f12c12ff54b8eace339c3e130317bf205277
Author: Ethan Tidmore <[email protected]>bpf: Fix signedness bug
The function
acquire_reference()returns negative error codes and 'id' is an unsigned integer, so the check (id < 0) is always impossible.Is this commit message accurate? Looking at
check_kfunc_call()inkernel/bpf/verifier.c, the variable 'id' is declared as a signed int:
Quoted 6 lines
Quoted 4 lines
const struct btf_param *args; u32 i, nargs, ptr_type_id; struct btf *desc_btf; int id;No, 'id' is assigned as a u32:
You might want to pull bpf-next and check again then.
Quoted 7 lines
u32 i, nargs, ptr_type_id, id;--
Thanks,
ET
https://github.com/sponsors/ethantidmore