bpf: Fix use-after-free of progs detached from busy trampolines
v5 cover bpf: Fix use-after-free of progs detached from busy trampolines
A task running in a trampoline image can call a prog that was detached and freed in the meantime, when it slept in a sleepable prog before reaching the detached one or was preempted right before calling it. Patch 1 handles the preempted case with an RCU tasks grace period, patch 2 handles the sleeping case by patching detached progs out of the images that still call them and patch 3 adds a selftest for the sleeping case.
Since v3, only the nop of the prog that is detached is patched, in every image that isn't freed yet, like v2 did. Progs that stay attached keep running for the tasks that are in old images, ip_after_call is gone and the call to the original function is never skipped.
On riscv and loongarch a jump of any range takes several instructions, and as bpf-ci and Alexei pointed out, a task preempted in the middle of such a patch site could resume into half of the new sequence. v5 makes the skip sites a single instruction there, patched to a jal / b through a new arch_bpf_trampoline_skip() hook, like their jump labels. I could only test these two under qemu.
Tested on x86_64 under KVM and on arm64, s390x, powerpc64le, riscv64 and loongarch64 under qemu TCG, all with KASAN: the new selftest crashes the unpatched kernel and passes with the series on every one of them, along with trampoline_count, fentry/fexit, fentry_fexit, modify_return and lsm_cgroup (and the full test_progs on x86_64). On x86_64, 18 fsession progs with cookies on an 11 argument function still fit in the image. Same on bpf-next, where patch 2 has trivial context conflicts in the x86 and arm64 JITs.
Changes since v4
(https://lore.kernel.org/bpf/[email protected]/):
- riscv, loongarch: single instruction skip sites, patched through arch_bpf_trampoline_skip(), loongarch keeps its limit of 38 (bpf-ci, Alexei)
- arm64: say in bpf_arch_text_poke() why patching out a detached prog needs no synchronization (bpf-ci)
- Comment fixes (bpf-ci)
- Cc the arch maintainers and lists
Changes since v3
(https://lore.kernel.org/bpf/[email protected]/):
- Only patch the nop of the prog that is detached, in all the images that aren't freed yet, like v2 did, and explain why a list of images is needed (Alexei, bpf-ci)
- Lower BPF_MAX_TRAMP_LINKS to 36 on x86, the worst case no longer fit in a page with the nops (bpf-ci, Alexei)
- selftest: wait for the detached progs to really be freed before releasing the task, the grace period of patch 1 made the previous wait too short (bpf-ci). Detach two progs one after the other, so that the second detach has to reach an image that isn't the current one anymore
- Keep a single comment block in bpf_tramp_image_put() (bpf-ci)
Changes since v2
(https://lore.kernel.org/bpf/[email protected]/):
- Patch all the nops in bpf_tramp_image_put() instead of the detached prog's nop at detach time, drop the image list, the trampoline backpointer and ip_after_call (Alexei)
- Wait for an RCU tasks grace period before freeing progs that were linked to a trampoline, for tasks preempted right before the enter helper (Junseo, sashiko)
- Initialize the jit ctx in loongarch's arch_bpf_trampoline_size() and the dummy image in every arch_bpf_trampoline_size() (bpf-ci)
- selftest: move the userfaultfd helper to testing_helpers.c and share it with bpf_mod_race, comment fixes (bpf-ci), add a subtest where the original function runs between the sleeping prog and the detached one
Changes since v1
(https://lore.kernel.org/bpf/[email protected]/):
- Patch nops in front of detached progs instead of taking prog references from the image (Alexei)
- Lower BPF_MAX_TRAMP_LINKS on arm64, loongarch and powerpc so the image still fits in a page
- Explain that the sleepable case doesn't depend on CONFIG_PREEMPTION (Kumar, Alexei)
- Add a selftest (Jiri, Alexei)
- Add Sechang's Reported-by (Junseo, Kumar)
- Drop Leon's and Kumar's acks since the code changed entirely
Florent Revest (Anthropic) (3):
bpf: Wait for an RCU tasks grace period before freeing trampoline
progs
bpf: Skip detached progs in trampoline images that are still in use selftests/bpf: Detach a trampoline prog while a task sleeps before it
arch/arm64/net/bpf_jit_comp.c | 37 ++-- arch/loongarch/net/bpf_jit.c | 60 ++++-- arch/powerpc/net/bpf_jit_comp.c | 45 +++-- arch/riscv/net/bpf_jit_comp64.c | 56 ++++-- arch/s390/net/bpf_jit_comp.c | 46 +++-- arch/x86/net/bpf_jit_comp.c | 26 ++- include/linux/bpf.h | 45 ++++- kernel/bpf/syscall.c | 19 +- kernel/bpf/trampoline.c | 85 ++++++-- .../selftests/bpf/prog_tests/bpf_mod_race.c | 34 +--- .../bpf/prog_tests/tramp_prog_detach.c | 188 ++++++++++++++++++ .../selftests/bpf/progs/tramp_prog_detach.c | 56 ++++++ tools/testing/selftests/bpf/testing_helpers.c | 28 +++ tools/testing/selftests/bpf/testing_helpers.h | 2 + 14 files changed, 572 insertions(+), 155 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c create mode 100644 tools/testing/selftests/bpf/progs/tramp_prog_detach.c
base-commit: ab39974240a0cff765f3bb9cce81d8001ffdb144
Signature · 1 line
2.55.0
PATCH v5 1/3 bpf: Wait for an RCU tasks grace period before freeing trampoline progs
When a prog is detached from a trampoline, it is freed after an RCU grace period, or an RCU tasks trace one if it is sleepable. This covers the tasks that are running the prog, since the prog's enter helper takes the matching read lock before the prog is called. On a preemptible kernel, it doesn't cover a task that was preempted in the trampoline just before the enter helper. That task holds no lock yet, and it calls the prog after it was freed:
BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_enter_recur+0x3a5/0x3f0 Read of size 8 at addr ffffc90000055040 by task candidate/110 CPU: 1 UID: 0 PID: 110 Comm: candidate Not tainted 7.3.0-rc2-00014-g15071f2a1263-dirty #2 PREEMPT(full) Call Trace: <TASK> __bpf_prog_enter_recur+0x3a5/0x3f0 bpf_trampoline_6442509193+0x37/0xf1 __x64_sys_futex+0x9/0x410 do_syscall_64+0xb0/0x530 ...
Wait for an RCU tasks grace period before the existing one when freeing a prog that was linked to a trampoline. An RCU tasks grace period only ends once the tasks that were preempted have run again, and bpf_tramp_image_put() already relies on it to free the image. It doesn't wait for tasks that sleep in the trampoline, the next commit takes care of those.
Only progs that were linked to a trampoline can be called this way, so bpf_trampoline_add_prog() marks them and other progs are still freed as before.
--- include/linux/bpf.h | 1 + kernel/bpf/syscall.c | 19 ++++++++++++++++++- kernel/bpf/trampoline.c | 1 + 3 files changed, 20 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -1770,6 +1770,7 @@ struct bpf_prog_aux {bool offload_requested;/* Program is bound and offloaded to the netdev. */ bool attach_btf_trace;/* true if attaching to BTF-enabled raw tp */ bool attach_tracing_prog;/* true if tracing another tracing program */ +bool tramp_linked;/* true if it was ever linked to a trampoline */ bool func_proto_unreliable;bool tail_call_reachable;bool xdp_has_frags;
kernel/bpf/syscall.c +18 -1
@@ -2448,6 +2448,21 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)bpf_prog_free (aux->prog); } +/* +* Progs called from a trampoline can also be reached by a task that was +* preempted in the trampoline before the prog's enter helper took its RCU +* read lock, wait for those first. +*/ +static void __bpf_prog_put_rcu_tasks (struct rcu_head *rcu) +{ + struct bpf_prog *prog = container_of(rcu, struct bpf_prog_aux, rcu)->prog; + +if (prog->sleepable) +call_rcu_tasks_trace (rcu, __bpf_prog_put_rcu); +else +call_rcu (rcu, __bpf_prog_put_rcu); +} +static void __bpf_prog_put_noref (struct bpf_prog *prog,bool deferred) {bpf_prog_kallsyms_del_all (prog);
@@ -2461,7 +2476,9 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)btf_put (prog->aux->attach_btf);if (deferred) { -if (prog->sleepable) +if (IS_ENABLED (CONFIG_TASKS_RCU ) && prog->aux->tramp_linked) +call_rcu_tasks (&prog->aux->rcu, __bpf_prog_put_rcu_tasks); +else if (prog->sleepable)call_rcu_tasks_trace (&prog->aux->rcu, __bpf_prog_put_rcu);else call_rcu (&prog->aux->rcu, __bpf_prog_put_rcu);
kernel/bpf/trampoline.c +1 -0
@@ -907,6 +907,7 @@ static int bpf_trampoline_add_prog(struct bpf_trampoline *tr, }hlist_add_head (&node->tramp_hlist, prog_list); + node->link->prog->aux->tramp_linked = true;if (kind ==BPF_TRAMP_FSESSION ) { tr->progs_cnt[BPF_TRAMP_FENTRY ]++; fexit =fsession_exit (node);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf v5 1/3] bpf: Wait for an RCU tasks grace period before freeing trampoline progs
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
When a prog is detached from a trampoline, it is freed after an RCU grace period, or an RCU tasks trace one if it is sleepable. This covers the tasks that are running the prog, since the prog's enter helper takes the matching read lock before the prog is called. On a preemptible kernel, it doesn't cover a task that was preempted in the trampoline just before the enter helper. That task holds no lock yet, and it calls the prog after it was freed:
BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_enter_recur+0x3a5/0x3f0 Read of size 8 at addr ffffc90000055040 by task candidate/110 CPU: 1 UID: 0 PID: 110 Comm: candidate Not tainted 7.3.0-rc2-00014-g15071f2a1263-dirty #2 PREEMPT(full) Call Trace: <TASK> __bpf_prog_enter_recur+0x3a5/0x3f0 bpf_trampoline_6442509193+0x37/0xf1
Quoted 6 lines
[ ... ]Fixes e21aa341785c ("bpf: Fix fexit trampoline.")Reported-by Junseo Lim <[email protected]>Assisted-by Claude:unspecifiedSigned-off-by Florent Revest (Anthropic) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH bpf v5 1/3] bpf: Wait for an RCU tasks grace period before freeing trampoline progs
In reply to Florent Revest (Anthropic)Is this the right Fixes: target?
Quoted 28 lines
bpf: Wait for an RCU tasks grace period before freeing trampoline progs
When a prog is detached from a trampoline, it is freed after an RCU grace period, or an RCU tasks trace one if it is sleepable. This covers the tasks that are running the prog, since the prog's enter helper takes the matching read lock before the prog is called. On a preemptible kernel, it doesn't cover a task that was preempted in the trampoline just before the enter helper. That task holds no lock yet, and it calls the prog after it was freed:
BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_enter_recur+0x3a5/0x3f0 Read of size 8 at addr ffffc90000055040 by task candidate/110 CPU: 1 UID: 0 PID: 110 Comm: candidate Not tainted 7.3.0-rc2-00014-g15071f2a1263-dirty #2 PREEMPT(full) Call Trace: <TASK> __bpf_prog_enter_recur+0x3a5/0x3f0 bpf_trampoline_6442509193+0x37/0xf1 __x64_sys_futex+0x9/0x410 do_syscall_64+0xb0/0x530 ...Wait for an RCU tasks grace period before the existing one when freeing a prog that was linked to a trampoline. An RCU tasks grace period only ends once the tasks that were preempted have run again, and
bpf_tramp_image_put()already relies on it to free the image. It doesn't wait for tasks that sleep in the trampoline, the next commit takes care of those.
Quoted 5 lines
Only progs that were linked to a trampoline can be called this way, so
bpf_trampoline_add_prog()marks them and other progs are still freed as before.Fixes e21aa341785c ("bpf: Fix fexit trampoline.")
Is this the right Fixes: target?
The race being closed here (a task preempted inside the trampoline image before __bpf_prog_enter*() takes its RCU read lock, while the prog is freed after only a regular RCU or RCU tasks trace grace period) appears to predate e21aa341785c.
In fec56f5890d9 ("bpf: Introduce BPF trampoline") bpf_trampoline_update() switched images with no tasks RCU wait, and __bpf_prog_put_noref() freed the prog with a plain call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu).
Just before e21aa341785c, the synchronize_rcu_mult(call_rcu_tasks, call_rcu_tasks_trace) in bpf_trampoline_update() ran before arch_prepare_bpf_trampoline() rather than after the new image was installed, and the total == 0 unregister_fentry() path had no wait at all, so a task preempted in the old image could already reach a freed prog.
Would Fixes: fec56f5890d9 ("bpf: Introduce BPF trampoline") be more accurate, so that stable trees without e21aa341785c also pick this up? The follow-up 49ca60187380 in this series uses the same tag.
---
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/36346518503
Re: [PATCH bpf v5 1/3] bpf: Wait for an RCU tasks grace period before freeing trampoline progs
Quoted 51 lines · UTC,
Quoted 28 lines
bpf: Wait for an RCU tasks grace period before freeing trampoline progs
When a prog is detached from a trampoline, it is freed after an RCU grace period, or an RCU tasks trace one if it is sleepable. This covers the tasks that are running the prog, since the prog's enter helper takes the matching read lock before the prog is called. On a preemptible kernel, it doesn't cover a task that was preempted in the trampoline just before the enter helper. That task holds no lock yet, and it calls the prog after it was freed:
BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_enter_recur+0x3a5/0x3f0 Read of size 8 at addr ffffc90000055040 by task candidate/110 CPU: 1 UID: 0 PID: 110 Comm: candidate Not tainted 7.3.0-rc2-00014-g15071f2a1263-dirty #2 PREEMPT(full) Call Trace: <TASK> __bpf_prog_enter_recur+0x3a5/0x3f0 bpf_trampoline_6442509193+0x37/0xf1 __x64_sys_futex+0x9/0x410 do_syscall_64+0xb0/0x530 ...Wait for an RCU tasks grace period before the existing one when freeing a prog that was linked to a trampoline. An RCU tasks grace period only ends once the tasks that were preempted have run again, and
bpf_tramp_image_put()already relies on it to free the image. It doesn't wait for tasks that sleep in the trampoline, the next commit takes care of those.Quoted 5 lines
Only progs that were linked to a trampoline can be called this way, so
bpf_trampoline_add_prog()marks them and other progs are still freed as before.Fixes e21aa341785c ("bpf: Fix fexit trampoline.")Is this the right Fixes: target?
The race being closed here (a task preempted inside the trampoline image before __bpf_prog_enter*() takes its RCU read lock, while the prog is freed after only a regular RCU or RCU tasks trace grace period) appears to predate
e21aa341785c.In
fec56f5890d9("bpf: Introduce BPF trampoline")bpf_trampoline_update()switched images with no tasks RCU wait, and__bpf_prog_put_noref()freed the prog with a plaincall_rcu(&prog->aux->rcu, __bpf_prog_put_rcu).Just before
e21aa341785c, thesynchronize_rcu_mult(call_rcu_tasks, call_rcu_tasks_trace)inbpf_trampoline_update()ran beforearch_prepare_bpf_trampoline()rather than after the new image was installed, and the total == 0unregister_fentry()path had no wait at all, so a task preempted in the old image could already reach a freed prog.
Quoted 3 lines
Would Fixes:
fec56f5890d9("bpf: Introduce BPF trampoline") be more accurate, so that stable trees withoute21aa341785calso pick this up? The follow-up49ca60187380in this series uses the same tag.
Yeah, that seems right. It shouldn't make much of a difference for stable because both have been introduced in the same 5.10.y. I can either switch this if there's a v6 or a maintainer can change when applying.
PATCH v5 2/3 bpf: Skip detached progs in trampoline images that are still in use
In reply to Florent Revest (Anthropic)bpf_tramp_image_put() makes sure a trampoline image is not freed while a task may still be running in it, but nothing similar is done for the progs called by that image. Detach drops the last prog reference right away and the prog is freed after grace periods, on the basis that a task still in the traced function skips the fexit progs once the nop at ip_after_call is patched to a jump.
That leaves out a task sleeping in a sleepable prog that runs before the detached one, which no grace period waits for:
CPU 0 CPU 1
in image I, sleeping in prog S
detach P from I's trampoline
-> new image, bpf_tramp_image_put(I)
bpf_prog_put(P), last ref grace periods, P freed
back from S
__bpf_prog_enter(P)
call P->bpf_func
If S and P are fexit progs the task is already past the patched jump, and fentry only images don't have one. On x86 this is an int3 in poisoned bpf_prog_pack memory:
Oops: int3: 0000 [#1] SMP NOPTI CPU: 18 UID: 0 PID: 94573 Comm: x169 Not tainted 6.18.44 #1 PREEMPT(lazy) RIP: 0010:0xffffffffc0601d8d Call Trace: <TASK> ? bpf_trampoline_6442515411+0x1a4/0x21b bpf_lsm_bprm_committed_creds+0x5/0x10 security_bprm_committed_creds+0x5f/0x70 begin_new_exec+0x2d6/0x410 ...
We hit this in production when progs attached through trampolines got detached while their hooks were busy, and it was independently found with a fuzzer and KASAN.
Have the JITs emit a patchable nop in front of each prog call sequence and record it in the image. When a prog is detached, patch its nop to a jump over the call sequence. Progs that stay attached keep running for the tasks that are in the image, and ip_after_call isn't needed anymore.
The task can be in any image that isn't freed yet, not only in the current one. It sleeps in image I1 that calls S, P and Q, then P is detached and the trampoline moves to image I2, then Q is detached and its call is still in I1. So the trampoline keeps a list of its images until they are freed, and detaching a prog patches its nop in all of them. Images hold a reference on the trampoline for that long.
On riscv and loongarch a jump of any range takes several instructions, and a task preempted in the middle of them could resume into half of the new sequence. The nop is a single instruction there, patched to a near branch through arch_bpf_trampoline_skip().
With the extra nops, BPF_MAX_TRAMP_LINKS progs no longer fit in a page on x86 and arm64 (and already didn't on powerpc), so lower the limit there like s390 does.
--- arch/arm64/net/bpf_jit_comp.c | 37 +++++++++------ arch/loongarch/net/bpf_jit.c | 60 +++++++++++++++-------- arch/powerpc/net/bpf_jit_comp.c | 45 +++++++++--------- arch/riscv/net/bpf_jit_comp64.c | 56 +++++++++++++++------- arch/s390/net/bpf_jit_comp.c | 46 +++++++++++------- arch/x86/net/bpf_jit_comp.c | 26 ++++++---- include/linux/bpf.h | 44 +++++++++++++++-- kernel/bpf/trampoline.c | 84 ++++++++++++++++++++++++++------- 8 files changed, 277 insertions(+), 121 deletions(-)
arch/arm64/net/bpf_jit_comp.c +22 -15
@@ -2418,10 +2418,11 @@ bool bpf_jit_supports_subprog_tailcalls(void)return true; } -static void invoke_bpf_prog (struct jit_ctx *ctx,struct bpf_tramp_node *node, -int bargs_off,int retval_off,int run_ctx_off, -bool save_ret) +static void invoke_bpf_prog (struct jit_ctx *ctx,struct bpf_tramp_image *im, +struct bpf_tramp_node *node,int bargs_off, +int retval_off,int run_ctx_off,bool save_ret) { +void *skip;__le32 *branch;u64 enter_prog;u64 exit_prog;
@@ -2431,6 +2432,10 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node, enter_prog = (u64 )bpf_trampoline_enter (p); exit_prog = (u64 )bpf_trampoline_exit (p); +/* nop, patched to skip this prog when it is detached */ + skip = ctx->ro_image + ctx->idx; +emit (A64_NOP , ctx); +if (node->cookie ==0 ) {/* if cookie is zero, one instruction is enough to store it */ emit (A64_STR64I (A64_ZR ,A64_SP , run_ctx_off + cookie_off), ctx);
@@ -2483,11 +2488,13 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_node *node,emit (A64_ADD_I (1 ,A64_R (2 ),A64_SP , run_ctx_off), ctx);emit_call (exit_prog, ctx); + +bpf_tramp_image_add_skip (im, p, skip, ctx->ro_image + ctx->idx); } -static void invoke_bpf_mod_ret (struct jit_ctx *ctx,struct bpf_tramp_nodes *tn, -int bargs_off,int retval_off,int run_ctx_off, -__le32 **branches) +static void invoke_bpf_mod_ret (struct jit_ctx *ctx,struct bpf_tramp_image *im, +struct bpf_tramp_nodes *tn,int bargs_off, +int retval_off,int run_ctx_off,__le32 **branches) {int i;
@@ -2496,7 +2503,7 @@ static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn,*/ emit (A64_STR64I (A64_ZR ,A64_SP , retval_off), ctx);for (i =0 ; i < tn->nr_nodes; i++) { -invoke_bpf_prog (ctx, tn->nodes[i], bargs_off, retval_off, +invoke_bpf_prog (ctx, im, tn->nodes[i], bargs_off, retval_off, run_ctx_off, true);/* if (*(u64 *)(sp + retval_off) != 0) * goto do_fexit;
@@ -2882,7 +2889,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,store_func_meta (ctx, meta, func_meta_off); cookie_bargs_off--; } -invoke_bpf_prog (ctx, fentry->nodes[i], bargs_off, +invoke_bpf_prog (ctx, im, fentry->nodes[i], bargs_off, retval_off, run_ctx_off, flags &BPF_TRAMP_F_RET_FENTRY_RET ); }
@@ -2893,7 +2900,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,if (!branches)return -ENOMEM ; -invoke_bpf_mod_ret (ctx, fmod_ret, bargs_off, retval_off, +invoke_bpf_mod_ret (ctx, im, fmod_ret, bargs_off, retval_off, run_ctx_off, branches); }
@@ -2906,9 +2913,6 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,emit (A64_RET (A64_R (10 )), ctx);/* store return value */ emit (A64_STR64I (A64_R (0 ),A64_SP , retval_off), ctx); -/* reserve a nop for bpf_tramp_image_put */ - im->ip_after_call = ctx->ro_image + ctx->idx; -emit (A64_NOP , ctx); }/* update the branches saved in invoke_bpf_mod_ret with cbnz */
@@ -2930,12 +2934,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,store_func_meta (ctx, meta, func_meta_off); cookie_bargs_off--; } -invoke_bpf_prog (ctx, fexit->nodes[i], bargs_off, retval_off, +invoke_bpf_prog (ctx, im, fexit->nodes[i], bargs_off, retval_off, run_ctx_off, false); }if (flags &BPF_TRAMP_F_CALL_ORIG ) { - im->ip_epilogue = ctx->ro_image + ctx->idx;/* for the first pass, assume the worst case */ if (!ctx->image) ctx->idx +=4 ;
@@ -2994,7 +2997,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, .image = NULL, .idx = 0, }; -struct bpf_tramp_image im; +struct bpf_tramp_image im = {};struct arg_aux aaux;int ret;
@@ -3281,6 +3284,10 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,* longer reachable, since bpf_tramp_image_put() function already * uses percpu_ref and task-based rcu to do the sync, no need to call * the sync version here, see bpf_tramp_image_put() for details. +* +* 3. when a detached prog is patched out of a trampoline, a CPU that +* still executes the old nop calls the prog before it went through +* a quiescent state, and the prog is freed after grace periods. */ ret =aarch64_insn_patch_text_nosync (ip, new_insn); out:
arch/loongarch/net/bpf_jit.c +40 -20
128 lines not on this page · raw message
arch/powerpc/net/bpf_jit_comp.c +23 -22
114 lines not on this page · raw message
arch/riscv/net/bpf_jit_comp64.c +39 -17
123 lines not on this page · raw message
arch/s390/net/bpf_jit_comp.c +30 -16
121 lines not on this page · raw message
arch/x86/net/bpf_jit_comp.c +16 -10
@@ -3217,16 +3217,21 @@ static void restore_regs(const struct btf_func_model *m, u8 **prog, }static int invoke_bpf_prog (const struct btf_func_model *m,u8 **pprog, +struct bpf_tramp_image *im,struct bpf_tramp_node *node,int stack_size,int run_ctx_off,bool save_ret,void *image,void *rw_image) {u8 *prog = *pprog; -u8 *jmp_insn; +u8 *jmp_insn, *skip;int ctx_cookie_off = offsetof(struct bpf_tramp_run_ctx , bpf_cookie);struct bpf_prog *p = node->link->prog;u64 cookie = node->cookie; +/* nop, patched to skip this prog when it is detached */ + skip = image + (prog - (u8 *)rw_image); +emit_nops (&prog,X86_PATCH_SIZE ); +/* mov rdi, cookie */ emit_mov_imm64 (&prog,BPF_REG_1 , (long ) cookie >>32 , (u32 ) (long ) cookie);
@@ -3301,6 +3306,8 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,if (emit_rsb_call (&prog,bpf_trampoline_exit (p), image + (prog - (u8 *)rw_image)))return -EINVAL ; +bpf_tramp_image_add_skip (im, p, skip, image + (prog - (u8 *)rw_image)); + *pprog = prog;return 0 ; }
@@ -3332,6 +3339,7 @@ static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond) } static int invoke_bpf(const struct btf_func_model *m, u8 **pprog, + struct bpf_tramp_image *im, struct bpf_tramp_nodes *tl, int stack_size, int run_ctx_off, int func_meta_off, bool save_ret, void *image, void *rw_image, u64 func_meta,
@@ -3346,7 +3354,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog, func_meta | (cur_cookie << BPF_TRAMP_COOKIE_INDEX_SHIFT)); cur_cookie--; } -if (invoke_bpf_prog (m, &prog, tl->nodes[i], stack_size, +if (invoke_bpf_prog (m, &prog, im, tl->nodes[i], stack_size, run_ctx_off, save_ret, image, rw_image))return -EINVAL ; }
@@ -3355,6 +3363,7 @@ static int invoke_bpf(const struct btf_func_model *m, u8 **pprog, }static int invoke_bpf_mod_ret (const struct btf_func_model *m,u8 **pprog, +struct bpf_tramp_image *im,struct bpf_tramp_nodes *tl,int stack_size,int run_ctx_off,u8 **branches,void *image,void *rw_image)
@@ -3368,7 +3377,7 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,emit_mov_imm32 (&prog, false,BPF_REG_0 ,0 );emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 ,-8 );for (i =0 ; i < tl->nr_nodes; i++) { -if (invoke_bpf_prog (m, &prog, tl->nodes[i], stack_size, run_ctx_off, true, +if (invoke_bpf_prog (m, &prog, im, tl->nodes[i], stack_size, run_ctx_off, true, image, rw_image))return -EINVAL ;
@@ -3640,7 +3649,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im }if (fentry->nr_nodes) { -if (invoke_bpf (m, &prog, fentry, regs_off, run_ctx_off, func_meta_off, +if (invoke_bpf (m, &prog, im, fentry, regs_off, run_ctx_off, func_meta_off, flags &BPF_TRAMP_F_RET_FENTRY_RET , image, rw_image, func_meta, cookie_off))return -EINVAL ;
@@ -3652,7 +3661,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_imif (!branches)return -ENOMEM ; -if (invoke_bpf_mod_ret (m, &prog, fmod_ret, regs_off, +if (invoke_bpf_mod_ret (m, &prog, im, fmod_ret, regs_off, run_ctx_off, branches, image, rw_image)) { ret = -EINVAL ; goto cleanup;
@@ -3682,8 +3691,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im }/* remember return value in a stack for bpf prog to access */ emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 ,-8 ); - im->ip_after_call = image + (prog - (u8 *)rw_image); -emit_nops (&prog,X86_PATCH_SIZE ); }if (fmod_ret->nr_nodes) {
@@ -3708,7 +3715,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_imemit_store_stack_imm64 (&prog,BPF_REG_0 , -func_meta_off, func_meta);if (fexit->nr_nodes) { -if (invoke_bpf (m, &prog, fexit, regs_off, run_ctx_off, func_meta_off, +if (invoke_bpf (m, &prog, im, fexit, regs_off, run_ctx_off, func_meta_off, false, image, rw_image, func_meta, cookie_off)) { ret = -EINVAL ; goto cleanup;
@@ -3723,7 +3730,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im* restored to R0. */ if (flags &BPF_TRAMP_F_CALL_ORIG ) { - im->ip_epilogue = image + (prog - (u8 *)rw_image);/* arg1: mov rdi, im */ emit_mov_imm64 (&prog,BPF_REG_1 , (long ) im >>32 , (u32 ) (long ) im);if (emit_rsb_call (&prog, __bpf_tramp_exit, image + (prog - (u8 *)rw_image))) {
@@ -3811,7 +3817,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iint arch_bpf_trampoline_size (const struct btf_func_model *m,u32 flags,struct bpf_tramp_nodes *tnodes,void *func_addr) { -struct bpf_tramp_image im; +struct bpf_tramp_image im = {};void *image;int ret;
include/linux/bpf.h +40 -4
82 lines not on this page · raw message
kernel/bpf/trampoline.c +67 -17
176 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf v5 2/3] bpf: Skip detached progs in trampoline images that are still in use
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
bpf_tramp_image_put()makes sure a trampoline image is not freed while a task may still be running in it, but nothing similar is done for the progs called by that image. Detach drops the last prog reference right away and the prog is freed after grace periods, on the basis that a task still in the traced function skips the fexit progs once the nop atip_after_callis patched to a jump.That leaves out a task sleeping in a sleepable prog that runs before the detached one, which no grace period waits for:
CPU 0 CPU 1
in image I, sleeping in prog S
detach P from I's trampoline
-> new image,bpf_tramp_image_put(I)
bpf_prog_put(P), last ref
Quoted 6 lines
[ ... ]Reported-by Sechang Lim <[email protected]>Suggested-by Alexei Starovoitov <[email protected]>Assisted-by Claude:unspecifiedSigned-off-by Florent Revest (Anthropic) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v5 3/3 selftests/bpf: Detach a trampoline prog while a task sleeps before it
In reply to Florent Revest (Anthropic)Add a test for the use-after-free fixed by the previous commit. A sleepable prog on bpf_fentry_test1() blocks a task on a userfaultfd page, like bpf_mod_race does, while the two progs that run after it in the same trampoline are detached and freed one after the other, which the test knows from their .bss maps going away. After the first detach the task is in an image that isn't the trampoline's current one anymore. The task is then released and must not call into the freed progs. This is done with fentry progs, with fexit progs, where the task is already past the original function, and with a task sleeping in a fentry prog while fexit progs are detached, where the original function must still be called. bpf_mod_race's userfaultfd helper moves to testing_helpers.c so that both tests can use it.
Without the fix, on a kernel with KASAN:
BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_enter_recur+0xed/0x1e0 Read of size 8 at addr ffa0000000144040 by task test_progs/171 CPU: 6 UID: 0 PID: 171 Comm: test_progs Tainted: G OE 7.2.0+ #1 PREEMPT(full) Call Trace: <TASK> __bpf_prog_enter_recur+0xed/0x1e0 bpf_trampoline_6442545468+0x72/0xe3 bpf_fentry_test1+0x9/0x20 bpf_prog_test_run_tracing+0x183/0x3e0 __sys_bpf+0xd3f/0x38f0 ...
--- .../selftests/bpf/prog_tests/bpf_mod_race.c | 34 +--- .../bpf/prog_tests/tramp_prog_detach.c | 188 ++++++++++++++++++ .../selftests/bpf/progs/tramp_prog_detach.c | 56 ++++++ tools/testing/selftests/bpf/testing_helpers.c | 28 +++ tools/testing/selftests/bpf/testing_helpers.h | 2 + 5 files changed, 275 insertions(+), 33 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c create mode 100644 tools/testing/selftests/bpf/progs/tramp_prog_detach.c
tools/testing/selftests/bpf/prog_tests/bpf_mod_race.c +1 -33
@@ -55,38 +55,6 @@ static void *load_module_thread(void *p)return p; } -static int sys_userfaultfd (int flags) -{ -return syscall (__NR_userfaultfd, flags); -} - -static int test_setup_uffd (void *fault_addr) -{ -struct uffdio_register uffd_register = {}; -struct uffdio_api uffd_api = {}; -int uffd; - - uffd =sys_userfaultfd (O_CLOEXEC ); -if (uffd <0 ) -return -errno; - - uffd_api.api =UFFD_API ; - uffd_api.features =0 ; -if (ioctl (uffd,UFFDIO_API , &uffd_api)) { -close (uffd); -return -1 ; - } - - uffd_register.range.start = (unsigned long )fault_addr; - uffd_register.range.len =getpagesize (); - uffd_register.mode =UFFDIO_REGISTER_MODE_MISSING ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffd_register)) { -close (uffd); -return -1 ; - } -return uffd; -} -static void test_bpf_mod_race_config (const struct test_config *config) {void *fault_addr, *skel_fail;
@@ -117,7 +85,7 @@ static void test_bpf_mod_race_config(const struct test_config *config)if (!ASSERT_OK (bpf_mod_race__attach (skel),"bpf_mod_kfunc_race__attach" )) goto end_destroy; - uffd =test_setup_uffd (fault_addr); + uffd =uffd_block_page (fault_addr);if (!ASSERT_GE (uffd,0 ,"userfaultfd open + register address" )) goto end_destroy;
tools/testing/selftests/bpf/prog_tests/tramp_prog_detach.c new file +188 -0
188 lines not on this page · raw message
tools/testing/selftests/bpf/progs/tramp_prog_detach.c new file +56 -0
56 lines not on this page · raw message
tools/testing/selftests/bpf/testing_helpers.c +28 -0
@@ -13,6 +13,7 @@#include "test_progs.h" #include "testing_helpers.h" #include <linux/membarrier.h> +#include <linux/userfaultfd.h> int parse_num_list (const char *s,bool **num_set,int *num_set_len) {
@@ -459,6 +460,33 @@ int kern_sync_rcu(void)return syscall (__NR_membarrier,MEMBARRIER_CMD_SHARED ,0 ,0 ); } +int uffd_block_page (void *fault_addr) +{ +struct uffdio_register uffd_register = {}; +struct uffdio_api uffd_api = {}; +int uffd; + + uffd =syscall (__NR_userfaultfd,O_CLOEXEC ); +if (uffd <0 ) +return -errno; + + uffd_api.api =UFFD_API ; + uffd_api.features =0 ; +if (ioctl (uffd,UFFDIO_API , &uffd_api)) { +close (uffd); +return -1 ; + } + + uffd_register.range.start = (unsigned long )fault_addr; + uffd_register.range.len =getpagesize (); + uffd_register.mode =UFFDIO_REGISTER_MODE_MISSING ; +if (ioctl (uffd,UFFDIO_REGISTER , &uffd_register)) { +close (uffd); +return -1 ; + } +return uffd; +} +int get_xlated_program (int fd_prog,struct bpf_insn **buf,__u32 *cnt) {__u32 buf_element_size =sizeof (struct bpf_insn );
tools/testing/selftests/bpf/testing_helpers.h +2 -0
@@ -36,6 +36,8 @@ __u64 read_perf_max_sample_freq(void);int load_bpf_testmod (bool verbose);int unload_bpf_testmod (bool verbose);int kern_sync_rcu (void ); +/* returns a userfaultfd that makes accesses to fault_addr's page block */ +int uffd_block_page (void *fault_addr);int finit_module (int fd,const char *param_values,int flags);int delete_module (const char *name,int flags);int load_module (const char *path,bool verbose);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf v5 3/3] selftests/bpf: Detach a trampoline prog while a task sleeps before it
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 27 lines
Add a test for the use-after-free fixed by the previous commit. A sleepable prog on
bpf_fentry_test1()blocks a task on a userfaultfd page, likebpf_mod_racedoes, while the two progs that run after it in the same trampoline are detached and freed one after the other, which the test knows from their .bss maps going away. After the first detach the task is in an image that isn't the trampoline's current one anymore. The task is then released and must not call into the freed progs. This is done with fentry progs, with fexit progs, where the task is already past the original function, and with a task sleeping in a fentry prog while fexit progs are detached, where the original function must still be called. bpf_mod_race's userfaultfd helper moves totesting_helpers.cso that both tests can use it.Without the fix, on a kernel with KASAN:
BUG: KASAN: vmalloc-out-of-bounds in __bpf_prog_enter_recur+0xed/0x1e0 Read of size 8 at addr ffa0000000144040 by task test_progs/171 CPU: 6 UID: 0 PID: 171 Comm: test_progs Tainted: G OE 7.2.0+ #1 PREEMPT(full) Call Trace: <TASK> __bpf_prog_enter_recur+0xed/0x1e0 bpf_trampoline_6442545468+0x72/0xe3 bpf_fentry_test1+0x9/0x20 bpf_prog_test_run_tracing+0x183/0x3e0 __sys_bpf+0xd3f/0x38f0 ...
Quoted 2 lines
Assisted-by Claude:unspecifiedSigned-off-by Florent Revest (Anthropic) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Sat, 26 Sep 2026 13:55:57 +0000 you wrote:
A task running in a trampoline image can call a prog that was detached and freed in the meantime, when it slept in a sleepable prog before reaching the detached one or was preempted right before calling it. Patch 1 handles the preempted case with an RCU tasks grace period, patch 2 handles the sleeping case by patching detached progs out of the images that still call them and patch 3 adds a selftest for the sleeping case.
[...]
Here is the summary with links:
- [bpf,v5,1/3] bpf: Wait for an RCU tasks grace period before freeing trampoline progs
https://git.kernel.org/bpf/bpf/c/0fb5751a6e41 - [bpf,v5,2/3] bpf: Skip detached progs in trampoline images that are still in use https://git.kernel.org/bpf/bpf/c/17637e1a581a - [bpf,v5,3/3] selftests/bpf: Detach a trampoline prog while a task sleeps before it https://git.kernel.org/bpf/bpf/c/d5288c519bbd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html