bpf: Do not release trampoline image in case off unregister error
Subsystems (3)
Jiri Olsa
reviewerPATCH 1/2 bpf: Do not release trampoline image in case off unregister error
If unregister_fentry fails we still have trampoline image attached to a function, so releasing it could trigger crash. Releasing the trampoline image only when the unregister succeeds.
--- kernel/bpf/trampoline.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/bpf/trampoline.c +4 -2
@@ -618,8 +618,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutif (total ==0 ) { err =unregister_fentry (tr, orig_flags, tr->cur_image->image); -bpf_tramp_image_put (tr->cur_image); - tr->cur_image =NULL ; +if (!err) { +bpf_tramp_image_put (tr->cur_image); + tr->cur_image =NULL ; + } goto out; }
Signature · 1 line
2.53.0
Jiri Olsa
reviewerPATCH 2/2 bpf: Remove obsolete WARN_ON call
The WARN_ON call in bpf_trampoline_update could never hit, because we direct the code path with (total == 0) to out label, which effectively skips the WARN_ON call.
The WARN_ON made sense back then when it checked tr->selector, but now with total being set just inside the function it's useless.
--- kernel/bpf/trampoline.c | 1 - 1 file changed, 1 deletion(-)
kernel/bpf/trampoline.c +0 -1
@@ -687,7 +687,6 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutif (err) goto out_free; -WARN_ON (tr->cur_image && total ==0 );if (tr->cur_image)/* progs already running at this address */ err = modify_fentry(tr, orig_flags, tr->cur_image->image,
Signature · 1 line
2.53.0
Re: [PATCH bpf 2/2] bpf: Remove obsolete WARN_ON call
Quoted 11 lines · Jiri Olsa
The
WARN_ONcall inbpf_trampoline_updatecould never hit, because we direct the code path with (total == 0) to out label, which effectively skips theWARN_ONcall.The
WARN_ONmade sense back then when it checkedtr->selector, but now with total being set just inside the function it's useless.Fixes 47e79cbeea4b ("bpf: Remove bpf trampoline selector")Signed-off-by Jiri Olsa <[email protected]>
Quoted 8 lines · Jiri Olsa
If
unregister_fentryfails we still have trampoline image attached to a function, so releasing it could trigger crash. Releasing the trampoline image only when the unregister succeeds.Fixes e21aa341785c ("bpf: Fix fexit trampoline.")Signed-off-by Jiri Olsa <[email protected]>
The fix looks good.
Can we add a test case that triggers this crash without the fix?
Thanks,
Song
Quoted 10 lines · Song Liu
Quoted 8 lines · Jiri Olsa
If
unregister_fentryfails we still have trampoline image attached to a function, so releasing it could trigger crash. Releasing the trampoline image only when the unregister succeeds.Fixes e21aa341785c ("bpf: Fix fexit trampoline.")Signed-off-by Jiri Olsa <[email protected]>
Quoted 5 lines
The fix looks good.
Acked-by Song Liu <[email protected]>Can we add a test case that triggers this crash without the fix?
I don't about valid scenario where unregister_fentry would fail now, that'd be bug that we'd need to fix. We have WARN_ON_ONCE on tampoline unlink fail.
The fix is meant for when this happens let's go with un-released trampoline image rather than kernel crash.
jirka
[email protected]
AI reviewSashiko AI review found 3 potential issue(s): - [Critical] Use-After-Free of BPF programs due to leaked trampoline image - [High] Permanent memory leak of trampoline executable image - [High]…
Sashiko AI review found 3 potential issue(s):
- [Critical] Use-After-Free of BPF programs due to leaked trampoline image
- [High] Permanent memory leak of trampoline executable image
- [High] Use-After-Free of ftrace_ops and bpf_trampoline
--
commit 3c4d55e24f28fa5f7f6dd937603366805532d1a4
Author: Jiri Olsa <[email protected]>
bpf: Do not release trampoline image in case off unregister error
If unregister_fentry fails we still have trampoline image attached to a function, so releasing it could trigger crash. Releasing the trampoline image only when the unregister succeeds.
Quoted 5 lines
kernel/bpf/trampoline.c +0
-0
kernel/bpf/trampoline.c static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut · line 624
Quoted 9 lines
kernel/bpf/trampoline.c +4
-2@@ -618,8 +618,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutif (total ==0 ) { err =unregister_fentry (tr, orig_flags, tr->cur_image->image); -bpf_tramp_image_put (tr->cur_image); - tr->cur_image =NULL ; +if (!err) { +bpf_tramp_image_put (tr->cur_image); + tr->cur_image =NULL ; + }
If unregister_fentry() fails, the ftrace or fentry hook remains active because the image isn't put. Does the teardown path, like bpf_tracing_link_release(), still proceed to tear down the link and free the bpf_prog structures?
If the BPF programs are freed but the leaked JIT executable image is still attached and active, could subsequent calls to the hooked kernel function execute the trampoline image, dereference the hardcoded pointers, and execute freed BPF programs?
When unregister_fentry() fails and bpf_tramp_image_put() is skipped, does the error propagate up to the caller where bpf_trampoline_put() might eventually free the parent struct bpf_trampoline?
If the parent trampoline is destroyed without freeing tr->cur_image, does this leak the struct bpf_tramp_image and its associated JIT memory page?
Additionally, if unregister_fentry() fails for an ftrace-managed trampoline, tr->fops appears to remain registered in the ftrace subsystem.
If the link teardown ignores the error and unconditionally calls bpf_trampoline_put(tr), will direct_ops_free() free tr->fops and tr?
Could this leave the ftrace subsystem with dangling pointers in its internal tracking structures, potentially causing a use-after-free regression during subsequent ftrace operations?
Quoted 3 lines
kernel/bpf/trampoline.c +0
-0goto out; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 49 lines · On Fri, Apr 24, 2026 at 04:12:31PM +0000, [email protected] wrote:
Sashiko AI review found 3 potential issue(s):
- [Critical] Use-After-Free of BPF programs due to leaked trampoline image
- [High] Permanent memory leak of trampoline executable image
- [High] Use-After-Free offtrace_opsandbpf_trampoline
--commit
3c4d55e24f28fa5f7f6dd937603366805532d1a4
Author: Jiri Olsa <[email protected]>bpf: Do not release trampoline image in case off unregister error
If
unregister_fentryfails we still have trampoline image attached to a function, so releasing it could trigger crash. Releasing the trampoline image only when the unregister succeeds.Quoted 5 lines
kernel/bpf/trampoline.c +0
-0kernel/bpf/trampoline.c static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut · line 624
Quoted 9 lines
kernel/bpf/trampoline.c +4
-2@@ -618,8 +618,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutif (total ==0 ) { err =unregister_fentry (tr, orig_flags, tr->cur_image->image); -bpf_tramp_image_put (tr->cur_image); - tr->cur_image =NULL ; +if (!err) { +bpf_tramp_image_put (tr->cur_image); + tr->cur_image =NULL ; + }If
unregister_fentry()fails, the ftrace or fentry hook remains active because the image isn't put. Does the teardown path, likebpf_tracing_link_release(), still proceed to tear down the link and free thebpf_progstructures?If the BPF programs are freed but the leaked JIT executable image is still attached and active, could subsequent calls to the hooked kernel function execute the trampoline image, dereference the hardcoded pointers, and execute freed BPF programs?
When
unregister_fentry()fails andbpf_tramp_image_put()is skipped, does the error propagate up to the caller wherebpf_trampoline_put()might eventually free the parent structbpf_trampoline?If the parent trampoline is destroyed without freeing
tr->cur_image, does this leak the structbpf_tramp_imageand its associated JIT memory page?Additionally, if
unregister_fentry()fails for an ftrace-managed trampoline,tr->fopsappears to remain registered in the ftrace subsystem.
Quoted 6 lines
If the link teardown ignores the error and unconditionally calls
bpf_trampoline_put(tr), willdirect_ops_free()freetr->fopsand tr?Could this leave the ftrace subsystem with dangling pointers in its internal tracking structures, potentially causing a use-after-free regression during subsequent ftrace operations?
ah right, released bpf_prog and bpf_trampoline will cause crash later, maybe we could just inc bpf_prog ref and skip bpf_trampoline_put like below for the tracing link release
jirka
---
kernel/bpf/syscall.c +5 -2
@@ -3505,9 +3505,12 @@ static void bpf_tracing_link_release(struct bpf_link *link)struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); -WARN_ON_ONCE (bpf_trampoline_unlink_prog (&tr_link->link, +if (WARN_ON_ONCE (bpf_trampoline_unlink_prog (&tr_link->link, tr_link->trampoline, - tr_link->tgt_prog)); + tr_link->tgt_prog))) { +bpf_prog_inc (tr_link->link.link.prog); +return ; + }bpf_trampoline_put (tr_link->trampoline);
[email protected]
AI reviewThis isn't a bug, but there's a typo in the subject line: "case off" should be "case of".
Quoted 1 line
bpf: Do not release trampoline image in case off unregister error
This isn't a bug, but there's a typo in the subject line: "case off" should be "case of".
---
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/24898396843