bpf: Fix a potential use-after-free
PATCH 1/1 bpf: Fix a potential use-after-free
Refcounting in the check_pseudo_btf_id() function is incorrect:
the __check_pseudo_btf_id() function might get called with a zero refcounted btf. Fix this, and patch related code accordingly.
--- kernel/bpf/verifier.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
kernel/bpf/verifier.c +17 -16
@@ -21333,10 +21333,9 @@ static int find_btf_percpu_datasec(struct btf *btf) }/* -* Add btf to the used_btfs array and return the index. (If the btf was -* already added, then just return the index.) Upon successful insertion -* increase btf refcnt, and, if present, also refcount the corresponding -* kernel module. +* Add btf to the used_btfs array and return the index. If the btf was +* already added, then just return the index. If needed, refcount the +* corresponding kernel module. */ static int __add_used_btf (struct bpf_verifier_env *env,struct btf *btf) {
@@ -21354,8 +21353,6 @@ static int __add_used_btf(struct bpf_verifier_env *env, struct btf *btf)return -E2BIG ; } -btf_get (btf); - btf_mod = &env->used_btfs[env->used_btf_cnt]; btf_mod->btf = btf; btf_mod->module =NULL ;
@@ -21363,10 +21360,8 @@ static int __add_used_btf(struct bpf_verifier_env *env, struct btf *btf)/* if we reference variables from kernel module, bump its refcount */ if (btf_is_module (btf)) { btf_mod->module =btf_try_get_module (btf); -if (!btf_mod->module) { -btf_put (btf); +if (!btf_mod->module)return -ENXIO ; - } }return env->used_btf_cnt++;
@@ -21466,9 +21461,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, btf_fd = insn[1 ].imm;if (btf_fd) { -CLASS (fd, f)(btf_fd); - - btf =__btf_get_by_fd (f); + btf =btf_get_by_fd (btf_fd);if (IS_ERR (btf)) {verbose (env,"invalid module BTF object FD specified.\n" );return -EINVAL ;
@@ -21478,17 +21471,23 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,verbose (env,"kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n" );return -EINVAL ; } +btf_get (btf_vmlinux); btf = btf_vmlinux; } err =__check_pseudo_btf_id (env, insn, aux, btf);if (err) -return err; + goto err_put; err =__add_used_btf (env, btf);if (err <0 ) -return err; + goto err_put; +return 0 ; + +err_put: +btf_put (btf); +return err; }static bool is_tracing_prog_type (enum bpf_prog_type type)
@@ -25368,11 +25367,13 @@ static int add_fd_from_fd_array(struct bpf_verifier_env *env, int fd)return 0 ; } - btf =__btf_get_by_fd (f); + btf =btf_get_by_fd (fd);if (!IS_ERR (btf)) { err =__add_used_btf (env, btf); -if (err <0 ) +if (err <0 ) { +btf_put (btf);return err; + }return 0 ; }
Signature · 1 line
2.34.1
[email protected]
AI reviewCan btf leak a reference here when __add_used_btf() returns a non-negative value for an already-recorded btf?
Quoted 3 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 20 lines
kernel/bpf/verifier.c +2
-3@@ -21466,9 +21461,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, btf_fd = insn[1 ].imm;if (btf_fd) { -CLASS (fd, f)(btf_fd); - - btf =__btf_get_by_fd (f); + btf =btf_get_by_fd (btf_fd);if (IS_ERR (btf)) {verbose (env,"invalid module BTF object FD specified.\n" );return -EINVAL ;@@ -21478,17 +21471,23 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,verbose (env,"kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n" );return -EINVAL ; } +btf_get (btf_vmlinux); btf = btf_vmlinux; } err =__check_pseudo_btf_id (env, insn, aux, btf);
kernel/bpf/verifier.c static int check_pseudo_btf_id(struct bpf_verifier_env *env, · line 21486
Quoted 10 lines
kernel/bpf/verifier.c +3
-2if (err) -return err; + goto err_put; err =__add_used_btf (env, btf);if (err <0 ) -return err; + goto err_put; +return 0 ;
Can btf leak a reference here when __add_used_btf() returns a non-negative value for an already-recorded btf?
Looking at __add_used_btf():
for (i = 0; i < env->used_btf_cnt; i++)
if (env->used_btfs[i].btf == btf)
return i;If the btf was already added to used_btfs (e.g., two BPF_PSEUDO_BTF_ID instructions reference the same btf), __add_used_btf() returns the existing index without consuming the reference.
In that case, check_pseudo_btf_id() took a reference via btf_get_by_fd() or btf_get(btf_vmlinux), but the "return 0" path does not release it.
The original code before commit 76145f725532 handled this case:
for (i = 0; i < env->used_btf_cnt; i++) {
if (env->used_btfs[i].btf == btf) {
btf_put(btf);
return 0;
}
}Should something like this be restored, perhaps by adding btf_put(btf) when __add_used_btf() returns a non-negative value indicating the btf was already recorded?
---
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/21796744040
AI-authorship-score: low
AI-authorship-explanation: The commit has standard kernel formatting, addresses a real syzbot-reported bug, and the code changes follow conventional kernel patterns without any AI-typical verbosity or over-explanation.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: BTF reference count leak when the same btf is referenced multiple times can cause memory to never be freed, but does not cause crashes or security issues.
Quoted 63 lines · On Sun, Feb 8, 2026 at 11:52 AM <[email protected]> wrote:
Quoted 4 lines
kernel/bpf/verifier.c +0
-0[ ... ]
Quoted 20 lines
kernel/bpf/verifier.c +2
-3@@ -21466,9 +21461,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, btf_fd = insn[1 ].imm;if (btf_fd) { -CLASS (fd, f)(btf_fd); - - btf =__btf_get_by_fd (f); + btf =btf_get_by_fd (btf_fd);if (IS_ERR (btf)) {verbose (env,"invalid module BTF object FD specified.\n" );return -EINVAL ;@@ -21478,17 +21471,23 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,verbose (env,"kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n" );return -EINVAL ; } +btf_get (btf_vmlinux); btf = btf_vmlinux; } err =__check_pseudo_btf_id (env, insn, aux, btf);kernel/bpf/verifier.c static int check_pseudo_btf_id(struct bpf_verifier_env *env, · line 21486
Quoted 10 lines
kernel/bpf/verifier.c +3
-2if (err) -return err; + goto err_put; err =__add_used_btf (env, btf);if (err <0 ) -return err; + goto err_put; +return 0 ;Can btf leak a reference here when
__add_used_btf()returns a non-negative value for an already-recorded btf?Looking at
__add_used_btf():for (i = 0; i < env->used_btf_cnt; i++) if (env->used_btfs[i].btf == btf) return i;If the btf was already added to
used_btfs(e.g., twoBPF_PSEUDO_BTF_IDinstructions reference the same btf),__add_used_btf()returns the existing index without consuming the reference.In that case,
check_pseudo_btf_id()took a reference viabtf_get_by_fd()orbtf_get(btf_vmlinux), but the "return 0" path does not release it.The original code before commit
76145f725532handled this case:for (i = 0; i < env->used_btf_cnt; i++) { if (env->used_btfs[i].btf == btf) { btf_put(btf); return 0; } }
Quoted 3 lines
Should something like this be restored, perhaps by adding
btf_put(btf)when__add_used_btf()returns a non-negative value indicating the btf was already recorded?
Yes, thanks robot, something like this. (I prefer to put it back in __add_used_btf().)
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/21796744040
AI-authorship-score: low
AI-authorship-explanation: The commit has standard kernel formatting, addresses a real syzbot-reported bug, and the code changes follow conventional kernel patterns without any AI-typical verbosity or over-explanation.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: BTF reference count leak when the same btf is referenced multiple times can cause memory to never be freed, but does not cause crashes or security issues.