bpf: Unwinding with unwind_goto and unwind_exit
v4 cover bpf: Unwinding with unwind_goto and unwind_exit
From: Alexei Starovoitov <[email protected]>
1. What is the problem?
A rust panic or a C++ exception has to run Drop impls or destructors of every frame it passes through. bpf_throw() discards the frames, so the verifier doesn't let a frame that owns a lock or a reference throw. In rust every index and every unwrap() can panic.
2. Is it worth solving?
Without it rust-bpf [1] drops all landing pads and a panic is bpf_throw(). A prog that can panic with a guard held doesn't load.
3. Is there a simpler way?
[2] keeps the normal path free of unwind code. llvm emits a table, libbpf passes it to the kernel, bpf_unwind() walks the stack and rewrites return addresses, and the verifier models hidden edges from calls to pads. That's 1400 lines in the kernel, with new uapi and two kfuncs, and 440 lines in libbpf.
Here the edge from a call to its pad is an insn, and so is the other way to leave a frame [3]:
ret exit invoke foo unwind pad call foo; unwind_goto pad resume r0 = 0; unwind_exit
Pads are ordinary code. The verifier learns one bit: how the callee left. The normal path is free of unwind code too: JITs emit nothing for unwind_goto, and exit is what it was.
200 lines in patch 1 and 100 lines in each JIT.
4. Will the api and the layers last?
Two new insns: one more kind of BPF_JCOND and one more kind of BPF_EXIT. No kfuncs, no BTF, no changes in libbpf and other loaders. The source level entry point is _Unwind_RaiseException() of the Itanium ABI. It's what the runtimes of rust and C++ call on other architectures. unwind_exit returns r0 like exit does. Today it's 0. That's where an exception object would go.
5. Is it good taste?
Every frame returns through its own epilogue. The only question is where it returns to. That's one bit, and it's a bit of control flow. v1 kept it in a register.
It's not specific to rust. A C++ prog with throw, catch and destructors compiles, loads and runs. It's a selftest in patch 5.
The llvm side is a new PR [3]. It replaces [4].
v3 -> v4:
- CI stayed red on x86-64. Call depth accounting was not the reason. CI builds x86-64 with KASAN, where the JIT wraps a load in pushes, a call and pops. The patterns of two __jited tests took that call for the call of the subprog. Now they start after the loads.
- When the callee never exits, what follows unwind_goto is seen only speculatively. It was removed as dead code, and other code moved to where the cpu predicts the return. Now 'goto pc-1' is there (sashiko).
- unwind_exit of an async or an exception callback is exit, like in the main prog. It was rejected. A sync callback still cannot unwind.
- arm64: no autia1716 and pacia1716. The address of the pad isn't signed. The epilogue authenticates LR as it is, and the offset of the pad is added to it afterwards.
- BPF_UNWIND_EXIT is enum instead of #define.
- unwind_attach and unwind_cxx skip when JIT is disabled, and don't define ENOTSUPP (bpf-ci).
v2 -> v3:
- bpf_unwind_target() is back in the JITs. In core.c it broke the build on MIPS, where struct exception_table_entry has no fixup (sashiko, bpf-ci).
- CI was red on x86-64: two __jited tests didn't expect call depth accounting in front of a call (bpf-ci).
- A global subprog that returns 16 bytes couldn't unwind: the verifier checks r0 and r2 at unwind_exit, and llvm set r0 only. Fixed in llvm. Patch 6 is Kumar's test.
- freplace: unwind_exit of an extension was exit, so the exception was lost (Eduard). Now the extension returns to the pad of the caller of what it replaced. An extension that can unwind replaces only a subprog that can. fexit cannot attach to such an extension.
- unwind_exit makes the cpu mispredict the return. Without CAP_PERFMON the verifier looks at what follows unwind_goto as a speculative path (bpf-ci).
- unwind_goto was 'call pc+0' in the xlated dump: BPF_JCOND is the same opcode as BPF_CALL_ARGS.
- selftests: 'the callee never returns' looks at xlated insns, and 'the way the callee left is a part of the state' has BPF_F_TEST_STATE_FREQ. Neither tested what it said (bpf-ci). New tests for async and exception callbacks, speculation and freplace. Every check that the series adds was removed in turn to see that a test fails.
- llvm: a pad doesn't set r0 when it only writes w0. Yonghong's callx case from [4] is a lit test.
- Rebased.
v1 -> v2:
- unwind_goto and unwind_exit instead of 'if r3 != 0 goto pad' and 'r3 = 1; exit' (Kumar).
- JIT support is not an optimization anymore. There are no shape tests, no look at jump_target, which was stale (sashiko), no exit that looks at r3 and no fallback to a branch.
- r3 is an argument again and nothing else. In v1 a subprog that didn't write it passed it through, and fentry didn't restore it (bpf-ci).
- Async and exception callbacks cannot unwind (sashiko).
- No BTF decl_tag, which was matched by prefix (bpf-ci). It works without BTF.
- x86 private stack is supported.
- Tail call and ld_abs are allowed.
- selftests: no #ifdef by arch, which broke the build on the rest of them. The messages are checked. fentry is attached and the target is run. Skips say why (bpf-ci).
- clang defines __BPF_FEATURE_UNWIND.
v3: https://lore.kernel.org/bpf/[email protected]/
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/
Tested on x86-64 in kvm with KASAN, like CI does: test_progs, test_progs-no_alu32, test_progs-cpuv4 and test_verifier, all of them in full. The new tests also with retbleed=stuff. C, C++ and rust progs built with [3] were run on x86-64 and arm64, with and without bpf_jit_harden=2. arm64 is qemu, with and without pointer authentication, and a small loader instead of test_progs. v3 passed test_progs on arm64 in CI.
[1] https://github.com/4ast/rust-bpf
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://github.com/llvm/llvm-project/pull/233649
[4] https://github.com/llvm/llvm-project/pull/233140
Alexei Starovoitov (5):
bpf: Add unwind_goto and unwind_exit insns
bpf, x86: Support unwind_goto and unwind_exit bpf, arm64: Support unwind_goto and unwind_exit selftests/bpf: Add tests for unwind_goto and unwind_exit selftests/bpf: Add a C++ prog with exceptions
Kumar Kartikeya Dwivedi (1):
selftests/bpf: Test unwinding from R0:R2 subprog
arch/arm64/net/bpf_jit_comp.c | 97 +- arch/x86/net/bpf_jit_comp.c | 101 +- include/linux/bpf.h | 1 + include/linux/bpf_verifier.h | 3 + include/linux/filter.h | 20 + include/uapi/linux/bpf.h | 13 + kernel/bpf/const_fold.c | 2 +- kernel/bpf/core.c | 6 + kernel/bpf/disasm.c | 7 +- kernel/bpf/fixups.c | 18 + kernel/bpf/states.c | 3 + kernel/bpf/syscall.c | 6 + kernel/bpf/verifier.c | 109 +- tools/include/uapi/linux/bpf.h | 13 + tools/testing/selftests/bpf/Makefile.skel | 11 + .../selftests/bpf/prog_tests/unwind_attach.c | 112 ++ .../selftests/bpf/prog_tests/unwind_cxx.c | 99 ++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/unwind_attach.c | 43 + .../selftests/bpf/progs/unwind_cxx.cpp | 150 ++ .../selftests/bpf/progs/verifier_unwind.c | 1211 +++++++++++++++++ tools/testing/selftests/bpf/test_loader.c | 3 +- 22 files changed, 2021 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/unwind_attach.c create mode 100644 tools/testing/selftests/bpf/prog_tests/unwind_cxx.c create mode 100644 tools/testing/selftests/bpf/progs/unwind_attach.c create mode 100644 tools/testing/selftests/bpf/progs/unwind_cxx.cpp create mode 100644 tools/testing/selftests/bpf/progs/verifier_unwind.c
base-commit: f25f6319e248034f3df83352e450ab3172eece43
Signature · 1 line
2.55.0
PATCH v4 1/6 bpf: Add unwind_goto and unwind_exit insns
From: Alexei Starovoitov <[email protected]>
Rust Drop, C++ destructors and __attribute__((cleanup)) in C have to run when a panic or an exception passes through a frame.
v1 [1] did it with a flag in r3 and 'if r3 != 0 goto pad' after the call. Three bugs that the bots found there had the same cause: r3 is an ordinary register, and JITs guessed the meaning from the shape of the code. Kumar suggested new insns instead [2]:
unwind_goto pad BPF_JMP | BPF_JCOND, src_reg = BPF_UNWIND_GOTO unwind_exit BPF_JMP | BPF_EXIT, src_reg = BPF_UNWIND_EXIT
'call foo; unwind_goto pad' goes to the pad when foo left with unwind_exit and falls through when it left with exit. No register says which one it was. In everything else unwind_exit is exit.
llvm lowers unwinding to them [3]:
ret exit invoke foo unwind pad call foo; unwind_goto pad resume r0 = 0; unwind_exit call _Unwind_RaiseException r0 = 0; unwind_exit invoke _Unwind_RaiseException goto pad
In a function that returns 16 bytes it's 'r0 = 0; r2 = 0; unwind_exit'.
Landing pads are ordinary blocks, so the verifier checks them like any other code. A pad that forgets to drop a lock or a reference is a leak on that path. There are no tables, no stack walk, no kfuncs, no BTF and nothing to do for libbpf and other loaders.
In the verifier:
- unwind_goto has to follow a call or callx of a subprog and cannot be a jump target. For cfg, liveness and the rest it's one more conditional jump, like may_goto.
- How the callee left is one bit of the verifier state. It lives from the exit of the callee to the next insn. states_equal() looks at it, since the registers can be the same either way.
- unwind_exit is rejected when unwind_goto doesn't follow the call.
- A global subprog that has unwind_exit can leave either way. Both are explored after the call, like it's done for might_throw.
- A sync callback cannot unwind. The helper that calls it doesn't expect
that: bpf_loop() would go on.
- unwind_exit of the main prog is exit, unless it's an extension. So it is in async and exception callbacks. The kernel calls them, and there is no frame to unwind to.
- An extension leaves either way, like the subprog that it replaced. freplace is a jump, so the extension returns to the caller of the subprog. An extension that has unwind_exit can replace only a subprog that has it too. Otherwise there is no unwind_goto after the calls.
- Of tracing progs only fentry can attach to a subprog or an extension that has unwind_exit. It would return to the trampoline that called it.
- unwind_exit changes its return address, so the cpu mispredicts the return and runs what follows unwind_goto speculatively. Without CAP_PERFMON the verifier looks at that path too.
- unwind_goto goes away together with a pad that turned out to be dead code. It stays when what follows it is dead code. The cpu predicts that the callee returns there, so 'goto pc-1' takes the place of the dead code. Otherwise its removal would move other code there.
JITs don't emit a branch:
unwind_goto:
extable[n++] = { ret_addr - 1 -> pad } unwind_exit:
my_ret_addr = bpf_unwind_target(my_ret_addr) epilogue
So exit and the normal path after the call cost nothing. Every frame fixes only its own return address. There is no stack walk. The lookup is what the page fault handler does for bpf progs.
The interpreter cannot do that, so the prog has to be JITed, and JITs opt in with bpf_jit_supports_unwind(). x86-64 and arm64 are in the next patches.
BPF_JCOND is the same opcode as BPF_CALL_ARGS of the interpreter, and BPF_UNWIND_GOTO is the same number as BPF_PSEUDO_CALL. may_goto is gone by the time a prog is dumped, unwind_goto is not. bpf_insn_prepare_dump() tells them apart: a prog with unwind_goto is always JITed.
Compared to v1 there is no BTF decl_tag and no 'r3 = 0' before every exit. Tail call and ld_abs are fine in a subprog that can unwind, since they leave it like exit does.
unwind_exit doesn't set r0. An implicit definition of r0 would need special cases in backtracking, in liveness and in the check of the return value of the main prog. A plain 'r0 = 0' in front of it needs none.
Rust
rust-bpf [4] builds with that llvm as it is. The programs are built with -C panic=unwind. The panic handler prints the message and calls _Unwind_RaiseException().exc_cleanup.rs panics three frames deep, with preemption disabled in one frame and rcu_read_lock held in another. All Drop impls run and the prog returns 0.scx_simple.rs is 2195 insns with 48 unwind_goto, 20 of them after callx, and 16 unwind_exit. With r3 it was 2216 insns. scx_cosmos.rs is 3694 insns with 68 and 18. It was 3716.
C++
Exceptions work too. This compiles, loads and runs:
struct rcu_guard {
rcu_guard() { bpf_rcu_read_lock(); }
~rcu_guard() { bpf_rcu_read_unlock(); }
};
static __noinline int inner(int len)
{
rcu_guard guard; if (len > 100)
throw 1;
return len;}
SEC("tc") int prog(struct __sk_buff *skb)
{
try {
return inner(skb->len);
} catch (...) {
return 42;
}
}It returns 64 for a packet of 64 bytes and 42 for a packet of 200 bytes. The destructor runs in both cases, otherwise the verifier would reject the prog. 'throw' is a jump to the pad with the destructor. 'catch' is unwind_goto:
inner:
w6 = w1
call bpf_rcu_read_lock
if w6 s< 101 goto ret
r1 = exc_obj ll
*(u32 *)(r1 + 0) = 1
goto pad
ret:
call bpf_rcu_read_unlock
w0 = w6
exit
pad:
call bpf_rcu_read_unlock
r0 = 0
unwind_exitprog:
w6 = *(u32 *)(r1 + 0)
w1 = w6
call inner
unwind_goto catch
out:
w0 = w6
exit
catch:
w6 = 42
goto outIt needs -fexceptions -funwind-tables and four one line __cxa_*() functions, where __cxa_throw() calls _Unwind_RaiseException(). Every pad is entered whatever was thrown, so llvm takes catch (...) only. It's a selftest in patch 5.
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://github.com/llvm/llvm-project/pull/233649
[4] https://github.com/4ast/rust-bpf
--- include/linux/bpf.h | 1 + include/linux/bpf_verifier.h | 3 + include/linux/filter.h | 20 ++++++ include/uapi/linux/bpf.h | 13 ++++ kernel/bpf/const_fold.c | 2 +- kernel/bpf/core.c | 6 ++ kernel/bpf/disasm.c | 7 ++- kernel/bpf/fixups.c | 18 ++++++ kernel/bpf/states.c | 3 + kernel/bpf/syscall.c | 6 ++ kernel/bpf/verifier.c | 109 ++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 13 ++++ 12 files changed, 197 insertions(+), 4 deletions(-)
include/linux/bpf.h +1 -0
@@ -1897,6 +1897,7 @@ struct bpf_prog_aux {bool changes_pkt_data;bool has_prologue;bool might_sleep; +bool can_unwind;bool kprobe_write_ctx;/* has insns that work with the arena base, see bpf_prog_has_arena_insns() */ bool uses_arena_insns;
include/linux/bpf_verifier.h +3 -0
@@ -532,6 +532,8 @@ struct bpf_verifier_state {bool speculative;bool in_sleepable; +/* The callee left with unwind_exit. Next insn is unwind_goto. */ +bool unwound;/* * First and last insn idx of this verifier state.
@@ -957,6 +959,7 @@ struct bpf_subprog_info {s16 fastcall_stack_off; bool has_tail_call: 1; bool might_throw: 1; + bool can_unwind: 1; /* has unwind_exit */ bool tail_call_reachable: 1; bool has_ld_abs: 1; bool is_cb: 1;
include/linux/filter.h +20 -0
@@ -254,6 +254,25 @@ static inline bool insn_is_mov_percpu_addr(const struct bpf_insn *insn) .off = 0, \ .imm = 1 }) +static inline bool bpf_is_unwind_goto (const struct bpf_insn *insn) +{ +return insn->code == (BPF_JMP |BPF_JCOND ) && insn->src_reg ==BPF_UNWIND_GOTO ; +} + +static inline bool bpf_is_unwind_exit (const struct bpf_insn *insn) +{ +return insn->code == (BPF_JMP |BPF_EXIT ) && insn->src_reg ==BPF_UNWIND_EXIT ; +} + +/* +* The caller of the main prog doesn't tell unwind_exit from exit. An extension +* is different: its caller is the caller of the subprog that it replaced. +*/ +static inline bool bpf_prog_can_unwind (const struct bpf_prog *prog) +{ +return bpf_is_subprog (prog) || prog->type ==BPF_PROG_TYPE_EXT ; +} +static inline bool insn_is_zext (const struct bpf_insn *insn) {return insn->code == (BPF_ALU |BPF_MOV |BPF_X ) && insn->imm ==1 ;
@@ -1257,6 +1276,7 @@ bool bpf_jit_supports_percpu_insn(void);bool bpf_jit_supports_kfunc_call (void );bool bpf_jit_supports_callx (void );bool bpf_jit_supports_kfunc_ret_reg_pair (void ); +bool bpf_jit_supports_unwind (void );bool bpf_jit_supports_stack_args (void );bool bpf_jit_supports_arena_args (void );bool bpf_jit_supports_far_kfunc_call (void );
include/uapi/linux/bpf.h +13 -0
@@ -56,6 +56,19 @@enum bpf_cond_pseudo_jmp {BPF_MAY_GOTO =0 , +/* +* Follows a call of a bpf subprog. Taken when the subprog left with +* BPF_EXIT that has src_reg == BPF_UNWIND_EXIT. +*/ +BPF_UNWIND_GOTO =1 , +}; + +enum bpf_pseudo_exit { +/* +* When bpf_exit->src_reg == BPF_UNWIND_EXIT, it's the same as bpf_exit, +* except that BPF_UNWIND_GOTO after the call is taken. +*/ +BPF_UNWIND_EXIT =1 , };/* Register numbers */
kernel/bpf/const_fold.c +1 -1
@@ -357,7 +357,7 @@ int bpf_prune_dead_branches(struct bpf_verifier_env *env)if (!bpf_insn_is_cond_jump (insn->code))continue ; -if (bpf_is_may_goto_insn (insn)) +if (BPF_OP (insn->code) ==BPF_JCOND )continue ;if (!(aux->const_reg_mask &BIT (insn->dst_reg)))
kernel/bpf/core.c +6 -0
@@ -3399,6 +3399,12 @@ bool __weak bpf_jit_supports_kfunc_ret_reg_pair(void)return false; } +/* unwind_goto and unwind_exit */ +bool __weak bpf_jit_supports_unwind (void ) +{ +return false; +} +/* * How this arch places a by-value kfunc argument, or NULL for one that has * not opted in and so only takes an argument of a single eightbyte, which
kernel/bpf/disasm.c +6 -1
@@ -375,11 +375,16 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs, insn->src_reg == BPF_MAY_GOTO) {verbose (cbs->private_data,"(%02x) may_goto pc%+d" , insn->code, insn->off); + } else if (insn->code == (BPF_JMP | BPF_JCOND) && + insn->src_reg == BPF_UNWIND_GOTO) { +verbose (cbs->private_data,"(%02x) unwind_goto pc%+d" , + insn->code, insn->off); } else if (insn->code == (BPF_JMP32 | BPF_JA)) {verbose (cbs->private_data,"(%02x) gotol pc%+d" , insn->code, insn->imm); } else if (insn->code == (BPF_JMP | BPF_EXIT)) { -verbose (cbs->private_data,"(%02x) exit" , insn->code); +verbose (cbs->private_data,"(%02x) %s" , insn->code, + insn->src_reg ==BPF_UNWIND_EXIT ?"unwind_exit" :"exit" ); } else if (BPF_SRC(insn->code) == BPF_X) { verbose(cbs->private_data, "(%02x) if %c%d %s %c%d goto pc%+d",
kernel/bpf/fixups.c +18 -0
@@ -658,6 +658,7 @@ void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) {struct bpf_insn_aux_data *aux_data = env->insn_aux_data;struct bpf_insn ja =BPF_JMP_IMM (BPF_JA ,0 ,0 ,0 ); +struct bpf_insn trap =BPF_JMP_IMM (BPF_JA ,0 ,0 ,-1 );struct bpf_insn *insn = env->prog->insnsi;const int insn_cnt = env->prog->len;int i;
@@ -666,6 +667,20 @@ void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)if (!bpf_insn_is_cond_jump (insn->code))continue ; +if (bpf_is_unwind_goto (insn) && aux_data[i +1 + insn->off].seen) { +/* +* The callee never exits. Its unwind_exit needs +* unwind_goto all the same, and the cpu predicts that it +* returns to what follows. Don't let the removal of dead +* code put something else there. +*/ +if (!aux_data[i +1 ].seen) { +memcpy (insn +1 , &trap,sizeof (trap)); + aux_data[i +1 ].seen = env->pass_cnt; + } +continue ; + } +if (!aux_data[i +1 ].seen) ja.off = insn->off;else if (!aux_data[i +1 + insn->off].seen)
@@ -1467,12 +1482,15 @@ static int jit_subprogs(struct bpf_verifier_env *env)if (BPF_CLASS (insn->code) ==BPF_STX &&BPF_MODE (insn->code) ==BPF_PROBE_ATOMIC ) num_exentries++; +if (bpf_is_unwind_goto (insn)) + num_exentries++; } func[i]->aux->num_exentries = num_exentries; func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; func[i]->aux->exception_cb = env->subprog_info[i].is_exception_cb; func[i]->aux->changes_pkt_data = env->subprog_info[i].changes_pkt_data; func[i]->aux->might_sleep = env->subprog_info[i].might_sleep; + func[i]->aux->can_unwind = env->subprog_info[i].can_unwind; func[i]->aux->token = prog->aux->token;if (!i) func[i]->aux->exception_boundary = env->seen_exception;
kernel/bpf/states.c +3 -0
@@ -1046,6 +1046,9 @@ static bool states_equal(struct bpf_verifier_env *env,if (old->in_sleepable != cur->in_sleepable)return false; +if (old->unwound != cur->unwound) +return false; +if (!refsafe (old, cur, &env->idmap_scratch))return false;
kernel/bpf/syscall.c +6 -0
@@ -5087,6 +5087,12 @@ static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, insns[i].imm = BPF_FUNC_tail_call;/* fall-through */ } +/* +* BPF_JCOND is the same opcode as BPF_CALL_ARGS. The latter is for +* the interpreter, and a prog with unwind_goto is always JITed. +*/ +if (prog->jited &&bpf_is_unwind_goto (&insns[i])) +continue ;if (code == (BPF_JMP |BPF_CALL ) || code == (BPF_JMP |BPF_CALL_ARGS )) {/* Restore the legacy xlated dump layout. */
kernel/bpf/verifier.c +107 -2
175 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +13 -0
@@ -56,6 +56,19 @@enum bpf_cond_pseudo_jmp {BPF_MAY_GOTO =0 , +/* +* Follows a call of a bpf subprog. Taken when the subprog left with +* BPF_EXIT that has src_reg == BPF_UNWIND_EXIT. +*/ +BPF_UNWIND_GOTO =1 , +}; + +enum bpf_pseudo_exit { +/* +* When bpf_exit->src_reg == BPF_UNWIND_EXIT, it's the same as bpf_exit, +* except that BPF_UNWIND_GOTO after the call is taken. +*/ +BPF_UNWIND_EXIT =1 , };/* Register numbers */
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v4 bpf-next 1/6] bpf: Add unwind_goto and unwind_exit insns
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] bpf: stale insn_aux_data and unvisited unwind_goto handling in…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] bpf: stale insn_aux_data and unvisited unwind_goto handling in bpf_opt_hard_wire_dead_code_branches()
- [Medium] bpf: missing jit_requested and offload checks in check_unwind_insns()
--
commit d6e6c3108f32eea3c296a97e0da89132803476f1
Author: Alexei Starovoitov <[email protected]>
bpf: Add unwind_goto and unwind_exit insns
This patch introduces the BPF_UNWIND_GOTO and BPF_UNWIND_EXIT instructions to support stack frame unwinding in BPF programs without extra register overhead or stack walking. It adds verifier tracking, dead-code branch fixups, and disassembler and dump support for the new instructions.
Quoted 20 lines
kernel/bpf/fixups.c +4
-0@@ -658,6 +658,7 @@ void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) {struct bpf_insn_aux_data *aux_data = env->insn_aux_data;struct bpf_insn ja =BPF_JMP_IMM (BPF_JA ,0 ,0 ,0 ); +struct bpf_insn trap =BPF_JMP_IMM (BPF_JA ,0 ,0 ,-1 );struct bpf_insn *insn = env->prog->insnsi;const int insn_cnt = env->prog->len;int i;@@ -666,6 +667,20 @@ void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)if (!bpf_insn_is_cond_jump (insn->code))continue ; +if (bpf_is_unwind_goto (insn) && aux_data[i +1 + insn->off].seen) { +/* +* The callee never exits. Its unwind_exit needs
kernel/bpf/fixups.c void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) · line 682
Quoted 10 lines
kernel/bpf/fixups.c +10
-0+* unwind_goto all the same, and the cpu predicts that it +* returns to what follows. Don't let the removal of dead +* code put something else there. +*/ +if (!aux_data[i +1 ].seen) { +memcpy (insn +1 , &trap,sizeof (trap)); + aux_data[i +1 ].seen = env->pass_cnt; + } +continue ; + }
[Severity: High]
When bpf_opt_hard_wire_dead_code_branches() overwrites insn + 1 with trap and marks aux_data[i + 1].seen = env->pass_cnt, could stale flags in aux_data[i + 1] corrupt the trap instruction or trigger a verifier bug?
Before do_check_main() and do_check_subprogs() run, bpf_compute_live_registers() sets insn_aux[i + 1].zext_dst = true on the static CFG if the original instruction at i + 1 defines a 32-bit subregister whose upper half is live out:
kernel/bpf/liveness.c:bpf_compute_live_registers() {
...
for (i = 0; i < insn_cnt; ++i) {
int def32 = bpf_insn_def32(env->prog, &insns[i]);
u32 out = state[i].out;
u32 in = state[i].in;
insn_aux[i].live_regs_before = mask_lo(in) | mask_hi(in);
insn_aux[i].zext_dst = def32 >= 0 && (mask_hi(out) & BIT(def32));
}
}In addition, when !env->bypass_spec_v1 is true, check_cond_jmp_op() calls sanitize_speculative_path(env, NULL, *insn_idx + 1, *insn_idx) for unwind_goto. That speculative walk leaves aux_data[i + 1].seen as 0 via sanitize_mark_insn_seen(), while do_check_insn() can set zext_dst, needs_zext, or arena_scalar on aux_data[i + 1].
Unlike keep_funcs_with_addr_taken() and sanitize_dead_code(), which clear zext_dst = false when overwriting an unvisited instruction with trap, bpf_opt_hard_wire_dead_code_branches() leaves aux_data[i + 1] flags intact.
Later, bpf_do_misc_fixups() will rewrite the BPF_JA trap instruction if needs_zext or arena_scalar is set on aux_data[i + 1]:
kernel/bpf/fixups.c:bpf_do_misc_fixups() {
...
if (env->insn_aux_data[i + delta].needs_zext)
/* Convert BPF_CLASS(insn->code) == BPF_ALU64 to 32-bit ALU */
insn->code = BPF_ALU | BPF_OP(insn->code) | BPF_SRC(insn->code);
if (env->insn_aux_data[i + delta].arena_scalar) {
cnt = arena_scalar_access(insn, insn_buf);
...
}And on architectures where bpf_jit_needs_zext() is true, bpf_opt_subreg_zext_lo32_rnd_hi32() sees load_reg == -1 on the BPF_JA trap instruction while aux[adj_idx].zext_dst is set, hitting verifier_bug_if():
kernel/bpf/fixups.c:bpf_opt_subreg_zext_lo32_rnd_hi32() {
...
if (verifier_bug_if(load_reg == -1, env,
"zext_dst is set, but no reg is defined"))
return -EFAULT;
}Also, what happens if the call and unwind_goto at insn i are themselves in dead code (!aux_data[i].seen), but jump to a shared landing pad that was reached from another call (aux_data[i + 1 + insn->off].seen)? Because aux_data[i].seen is not checked here, insn + 1 is overwritten with trap and marked seen, so bpf_opt_remove_dead_code() removes the dead call and unwind_goto while leaving an orphaned goto pc-1 in the program (and any later live conditional jump with untaken target i + 1 can no longer see !aux_data[i + 1].seen).
Should this check aux_data[i].seen before handling unwind_goto, and clear zext_dst, needs_zext, and arena_scalar on aux_data[i + 1] when writing trap?
[ ... ]
Quoted 21 lines
kernel/bpf/syscall.c +6
-0@@ -5087,6 +5087,12 @@ static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, insns[i].imm = BPF_FUNC_tail_call;/* fall-through */ } +/* +* BPF_JCOND is the same opcode as BPF_CALL_ARGS. The latter is for +* the interpreter, and a prog with unwind_goto is always JITed. +*/ +if (prog->jited &&bpf_is_unwind_goto (&insns[i])) +continue ;if (code == (BPF_JMP |BPF_CALL ) || code == (BPF_JMP |BPF_CALL_ARGS )) {/* Restore the legacy xlated dump layout. */ kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 33 lines
kernel/bpf/verifier.c +29
-0@@ -3430,6 +3431,46 @@ static int next_callx_callee(struct bpf_verifier_env *env, int caller, int from)return bit < end ? bit - caller * cnt :-1 ; } +/* +* 'call foo; unwind_goto pad' goes to the pad when foo left with unwind_exit. +* JITs don't emit a branch for it. unwind_exit finds the pad by its return +* address. So unwind_goto has to follow the call and nothing may jump to it. +*/ +static int check_unwind_insns (struct bpf_verifier_env *env) +{ +struct bpf_insn *insn = env->prog->insnsi; +bool seen = false; +int i; + +for (i =0 ; i < env->prog->len; i++, insn++) { +if (bpf_is_unwind_exit (insn)) { +bpf_find_containing_subprog (env, i)->can_unwind = true; + seen = true; + } +if (!bpf_is_unwind_goto (insn)) +continue ; + seen = true; +if (!i || !(bpf_pseudo_call (insn -1 ) ||bpf_is_callx (insn -1 ))) { +verbose (env,"unwind_goto at insn %d doesn't follow a call of a subprog\n" , + i); +return -EINVAL ; + } +if (bpf_is_jump_target (env, i)) { +verbose (env,"jump to unwind_goto at insn %d\n" , i); +return -EINVAL ; + } + }
kernel/bpf/verifier.c static int next_callx_callee(struct bpf_verifier_env *env, int caller, int from) · line 3472
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+if (!seen) +return 0 ; +if (!bpf_jit_supports_unwind ()) { +verbose (env,"JIT does not support unwinding\n" ); +return -ENOTSUPP ; + } + env->prog->jit_required =1 ; + env->prog->aux->can_unwind = env->subprog_info[0 ].can_unwind; +return 0 ; +}
[Severity: Medium]
Should check_unwind_insns() also reject programs when !env->prog->jit_requested or bpf_prog_is_offloaded(env->prog->aux)?
When CONFIG_BPF_JIT_ALWAYS_ON is enabled and a program with unwind_goto is loaded with prog_ifindex set to an offload device (such as netdevsim), bpf_fixup_call_args() skips bpf_jit_subprogs() because !bpf_prog_is_offloaded(env->prog->aux) is false, and the
#ifndef CONFIG_BPF_JIT_ALWAYS_ON check on prog->jit_required is compiled out:
kernel/bpf/fixups.c:bpf_fixup_call_args() {
...
if (env->prog->jit_requested &&
!bpf_prog_is_offloaded(env->prog->aux)) {
err = bpf_jit_subprogs(env);
if (err == 0)
return 0;
if (err == -EFAULT || err == -EINTR)
return err;
}
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
if (prog->jit_required) {
verbose(env, "program requires BPF JIT compiler but it is not available\n");
return -EINVAL;
}
...
}__bpf_prog_select_runtime() then takes the bpf_prog_offload_compile() branch without checking jit_needed, leaving prog->jited as 0:
kernel/bpf/core.c:__bpf_prog_select_runtime() {
...
if (!bpf_prog_is_offloaded(fp->aux)) {
...
if (!fp->jited && jit_needed) {
*err = -ENOTSUPP;
return fp;
}
} else {
*err = bpf_prog_offload_compile(fp);
if (*err)
return fp;
}
}Because prog->jited is 0 on that offloaded program, bpf_insn_prepare_dump() skips the prog->jited && bpf_is_unwind_goto(&insns[i]) check and falls through to the BPF_JMP | BPF_CALL_ARGS branch, where insns[i].src_reg == BPF_PSEUDO_CALL (both equal to 1) causes prepare_dump_pseudo_call() to corrupt unwind_goto into BPF_JMP | BPF_CALL in xlated program dumps.
Similarly, when CONFIG_BPF_JIT_ALWAYS_ON is disabled and bpf_jit_enable is 0, checking !env->prog->jit_requested in check_unwind_insns() (like require_callx_jit() does) would reject unwinding before passes such as bpf_compute_subprog_ret_regs() that gate on env->prog->jit_requested.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v4 bpf-next 1/6] bpf: Add unwind_goto and unwind_exit insns
Quoted 9 lines · On Tue, Oct 20, 2026 at 11:57 AM [email protected]…
kernel/bpf/fixups.c void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) · line 680
Quoted 4 lines
kernel/bpf/fixups.c +4
-0+if (!aux_data[i +1 ].seen) { +memcpy (insn +1 , &trap,sizeof (trap)); + aux_data[i +1 ].seen = env->pass_cnt; + }[Severity: High]
Whenbpf_opt_hard_wire_dead_code_branches()overwrites insn + 1 with trap and marks aux_data[i + 1].seen =env->pass_cnt, could stale flags in aux_data[i + 1] corrupt the trap instruction or trigger a verifier bug?
No. That verifier_bug_if() needs bpf_jit_needs_zext(), and it's false on x86-64 and arm64, the only JITs with bpf_jit_supports_unwind(). needs_zext and arena_scalar need an arena, and arena needs CAP_PERFMON. Then bypass_spec_v1 is true, there is no speculative walk of i + 1, and they cannot be set on an insn that wasn't seen.
Quoted 11 lines
Also, what happens if the call and
unwind_gotoat insn i are themselves in dead code (!aux_data[i].seen), but jump to a shared landing pad that was reached from another call (aux_data[i + 1 + insn->off].seen)? Because aux_data[i].seen is not checked here, insn + 1 is overwritten with trap and marked seen, sobpf_opt_remove_dead_code()removes the dead call andunwind_gotowhile leaving an orphaned goto pc-1 in the program (and any later live conditional jump with untaken target i + 1 can no longer see !aux_data[i + 1].seen).Should this check aux_data[i].seen before handling
unwind_goto, and clearzext_dst,needs_zext, andarena_scalaron aux_data[i + 1] when writing trap?
Not a bug. It's one extra 'goto pc-1' that nothing can reach. A live cond jump to it is never taken, otherwise i + 1 would have been seen.
Quoted 8 lines
kernel/bpf/verifier.c static int next_callx_callee(struct bpf_verifier_env *env, int caller, int from) · line 3468
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+if (!bpf_jit_supports_unwind ()) { +verbose (env,"JIT does not support unwinding\n" ); +return -ENOTSUPP ; + }[Severity: Medium]
Shouldcheck_unwind_insns()also reject programs when !env->prog->jit_requested orbpf_prog_is_offloaded(env->prog->aux)?
yeah. Only netdevsim gets there, nfp rejects the opcode in nfp_verify_insn(), and nothing runs such a prog, so it's just a wrong xlated dump. Will reject offloaded progs in check_unwind_insns() in v5.
Quoted 4 lines
Similarly, when
CONFIG_BPF_JIT_ALWAYS_ONis disabled andbpf_jit_enableis 0, checking !env->prog->jit_requested incheck_unwind_insns()(likerequire_callx_jit()does) would reject unwinding before passes such asbpf_compute_subprog_ret_regs()that gate onenv->prog->jit_requested.
Not a bug. jit_required is set, so bpf_fixup_call_args() rejects the prog at the end of verification. It's only a matter of when.
PATCH v4 2/6 bpf, x86: Support unwind_goto and unwind_exit
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
bpf x86
call inner call inner
unwind_goto pad
w0 = w6 mov eax, ebx
r0 = 0 xor eax, eax
unwind_exit push rax
push rsi
mov rdi, [rbp + 8]
call bpf_unwind_target
mov [rbp + 8], rax
pop rsi
pop rax
leave
retunwind_goto is an extable entry and no code. unwind_exit looks its return address up there and returns to the pad. r0 and r2 are what a subprog returns, so rax and rsi are saved around the lookup.
When there is no entry, the kernel called the subprog as an async or an exception callback, and unwind_exit is exit.
The caller of the main prog doesn't tell exit from unwind_exit, so there it's exit. An extension is different. freplace is a jump, so its return address is the one of the subprog that it replaced.
With private stack r9 is saved around a call, and 'pop r9' has to be done on the way to the pad too. In such progs unwind_goto is:
push r9
call foo
pop r9 <- exit returns here
jmp 1f
pop r9 <- unwind_exit returns here
jmp pad
1:
--- arch/x86/net/bpf_jit_comp.c | 101 +++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-)
arch/x86/net/bpf_jit_comp.c +100 -1
@@ -15,6 +15,7 @@#include <linux/memory.h> #include <linux/sort.h> #include <linux/execmem.h> +#include <linux/extable.h> #include <asm/extable.h> #include <asm/ftrace.h> #include <asm/set_memory.h>
@@ -2511,6 +2512,68 @@ static void adjust_cmp_imm(u32 *op, s32 *imm) } } +/* +* There is no branch for unwind_goto, only an extable entry +* { .insn = ret_addr - 1, .fixup = target - ret_addr } +* where ret_addr is the return address of the call in front of it. unwind_exit +* replaces the return address of its frame with bpf_unwind_target() of it. +* ret_addr - 1, because what follows the call can have an entry of its own. +* No insn starts there, so a fault cannot hit the entry. +*/ +static int add_unwind_exentry (struct bpf_prog *prog,u8 *image,u8 *rw_image,int *excnt, +int ret_addr,int target) +{ +struct exception_table_entry *ex; +s64 delta; + +if (!prog->aux->extable) +return 0 ; +if (*excnt >= prog->aux->num_exentries) { +pr_err ("unwind extable bug\n" ); +return -EFAULT ; + } + ex = &prog->aux->extable[(*excnt)++]; + delta = image + ret_addr -1 - (u8 *)&ex->insn; +if (!is_simm32 (delta)) { +pr_err ("extable->insn doesn't fit into 32-bit\n" ); +return -EFAULT ; + } +/* switch ex to rw buffer for writes */ + ex = (void *)rw_image + ((void *)ex - (void *)image); + ex->insn = delta; + ex->fixup = target - ret_addr; + ex->data =EX_TYPE_BPF ; +return 0 ; +} + +static u64 bpf_unwind_target (u64 ret_addr) +{ +const struct exception_table_entry *e =search_bpf_extables (ret_addr -1 ); + +/* +* The verifier made sure that unwind_goto follows a call in a prog. When +* the kernel called an async or an exception callback, it's exit. +*/ +return e ? ret_addr + e->fixup : ret_addr; +} + +/* unwind_exit: fix the return address of this frame. r0 and r2 are returned. */ +static int emit_unwind_ret_addr (u8 **pprog,u8 *ip) +{ +u8 *prog = *pprog, *start = prog; + +EMIT2 (0x50 ,0x56 );/* push rax; push rsi */ +EMIT4 (0x48 ,0x8B ,0x7D ,0x08 );/* mov rdi, [rbp + 8] */ + ip += prog - start; + ip +=x86_call_depth_emit_accounting (&prog, bpf_unwind_target, ip); +if (emit_call (&prog, bpf_unwind_target, ip)) +return -EINVAL ; +EMIT4 (0x48 ,0x89 ,0x45 ,0x08 );/* mov [rbp + 8], rax */ +EMIT2 (0x5E ,0x58 );/* pop rsi; pop rax */ + *pprog = prog; +return 0 ; +} +static int do_jit (struct bpf_verifier_env *env,struct bpf_prog *bpf_prog,int *addrs,u8 *image,u8 *rw_image,int oldproglen,struct jit_context *ctx,bool jmp_padding) {
@@ -3493,6 +3556,34 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *pop_r9 (&prog);break ; +/* unwind_goto. The verifier expanded may_goto. */ +case BPF_JMP |BPF_JCOND : +if (!priv_frame_ptr) { + err =add_unwind_exentry (bpf_prog, image, rw_image, &excnt, + addrs[i -1 ], addrs[i + insn->off]); +if (err) +return err; +break ; + } +/* +* 'pop r9' after the call has to be done on the way to +* the pad too: +* call foo +* pop r9 <- exit returns here +* jmp 1f +* pop r9 <- unwind_exit returns here +* jmp pad +* 1: +*/ + err =add_unwind_exentry (bpf_prog, image, rw_image, &excnt, + addrs[i -1 ] -2 , addrs[i -1 ] +2 ); +if (err) +return err; +EMIT2 (0xEB ,7 ); +pop_r9 (&prog); +EMIT1_off32 (0xE9 , addrs[i + insn->off] - (addrs[i -1 ] +9 )); +break ; +case BPF_JMP |BPF_TAIL_CALL :if (imm32) emit_bpf_tail_call_direct(bpf_prog,
@@ -3774,13 +3865,16 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *break ;case BPF_JMP |BPF_EXIT : +if (src_reg ==BPF_UNWIND_EXIT &&bpf_prog_can_unwind (bpf_prog) && +emit_unwind_ret_addr (&prog, ip)) +return -EINVAL ;if (seen_exit) { jmp_offset = ctx->cleanup_addr - addrs[i]; goto emit_jmp; } seen_exit = true;/* Update cleanup_addr */ - ctx->cleanup_addr = proglen; + ctx->cleanup_addr = proglen + (prog - temp);if (bpf_prog_was_classic (bpf_prog) && !ns_capable_noaudit (&init_user_ns,CAP_SYS_ADMIN )) {if (emit_spectre_bhb_barrier (&prog, ip, bpf_prog))
@@ -5027,6 +5121,11 @@ bool bpf_jit_supports_kfunc_ret_reg_pair(void)return true; } +bool bpf_jit_supports_unwind (void ) +{ +return true; +} +const struct bpf_jit_arg_abi *bpf_jit_arg_abi (void ) {return &x86_arg_abi;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v4 bpf-next 2/6] bpf, x86: Support unwind_goto and unwind_exit
TL;DR: An exception callback's unwind_exit may return into an unwind pad the verifier never checked for that path (via freplace or tail call), possibly yielding unsafe register state; JIT should…
Quoted 1 line
bpf, x86: Support
unwind_gotoandunwind_exit
TL;DR: An exception callback's unwind_exit may return into an unwind pad the verifier never checked for that path (via freplace or tail call), possibly yielding unsafe register state; JIT should skip it there.
Quoted 3 lines
arch/x86/net/bpf_jit_comp.c +0
-0
[ ... ]
Quoted 14 lines
arch/x86/net/bpf_jit_comp.c +14
-0+static u64 bpf_unwind_target (u64 ret_addr) +{ +const struct exception_table_entry *e =search_bpf_extables (ret_addr -1 ); + +/* +* The verifier made sure that unwind_goto follows a call in a prog. When +* the kernel called an async or an exception callback, it's exit. +*/ +return e ? ret_addr + e->fixup : ret_addr; +} + +/* unwind_exit: fix the return address of this frame. r0 and r2 are returned. */ +static int emit_unwind_ret_addr (u8 **pprog,u8 *ip) +{
[ ... ]
Quoted 1 line
arch/x86/net/bpf_jit_comp.c +1
-0+EMIT4 (0x48 ,0x8B ,0x7D ,0x08 );/* mov rdi, [rbp + 8] */
[ ... ]
arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 3871
Quoted 8 lines
arch/x86/net/bpf_jit_comp.c +3
-0@@ -3774,13 +3865,16 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *break ;case BPF_JMP |BPF_EXIT : +if (src_reg ==BPF_UNWIND_EXIT &&bpf_prog_can_unwind (bpf_prog) && +emit_unwind_ret_addr (&prog, ip)) +return -EINVAL ;if (seen_exit) {
Can an exception callback's unwind_exit land in a pad that the verifier never explored for that path?
An exception callback is a subprog (func_idx != 0), so bpf_prog_can_unwind() is true for it and do_jit() emits emit_unwind_ret_addr() for its unwind_exit. The verifier treats unwind_exit in an exception callback as a plain exit, and prepare_func_exit() only handles curframe > 0. The comment in bpf_unwind_target() relies on the same assumption:
/*
* The verifier made sure that unwind_goto follows a call in a prog. When
* the kernel called an async or an exception callback, it's exit. */
The exception callback does not run in a frame of its own, though. bpf_throw() calls ctx.aux->bpf_exception_cb(cookie, ctx.sp, ctx.bp, 0, 0) with the frame of the first non-subprog prog on the stack, and emit_prologue() does "mov rbp, rdx" for is_exception_cb. So "mov rdi, [rbp + 8]" reads the return address of the main prog's frame, and that address is not always in the kernel:
- Extension: freplace is a jmp from the replaced subprog's nop5 (bpf_arch_text_poke(..., BPF_MOD_JUMP, ..., prog->bpf_func)), so [rbp + 8] of the extension's frame is the return address of "call b" in the target prog.
- Tail call from a subprog: emit_bpf_tail_call_indirect() does not pop rbp and jumps past push rbp / mov rbp, rsp, so the tail-called main prog runs in the subprog's frame and [rbp + 8] is the return address of "call S" in the caller.
If an unwind_goto follows that call, search_bpf_extables() finds its entry and the exception callback returns into the pad.
One example with extensions: target prog P has
call a; unwind_goto pad; ...; call b; unwind_goto pad
where global a() can unwind and global b() cannot. The pad is seen via a(), so bpf_opt_hard_wire_dead_code_branches() keeps the unwind_goto after "call b" and the JIT emits an extable entry for it.
Extension E replaces b(). E's main has no unwind_exit, so prog->aux->can_unwind (which is only subprog_info[0].can_unwind) is false and the "Extension program can unwind, while original does not" check passes. E calls bpf_throw(), and E's exception callback ends with "r0 = 0; unwind_exit".
At runtime the callback returns into P's pad with the registers and references that follow "call b". The verifier only checked the pad with the state that follows "call a" unwinding, e.g. r6 as a map value pointer there, while P could set r6 to an arbitrary scalar before "call b".
The tail call route does not need an extension. A subprog S with unwind_exit (for example one that returns a pointer in r0 on its unwind path) tail-calls T, T throws, and T's exception callback unwind_exit sends it into P's pad after "call S" with r0 taken from the callback.
Should do_jit() skip emit_unwind_ret_addr() when bpf_prog->aux->exception_cb is set, so the JIT matches the verifier's "exception callback unwind_exit is exit" model? The alternative is for the verifier to account for the exception callback's can_unwind.
The arm64 patch in this series, c1df16f0ed1d ("bpf, arm64: Support unwind_goto and unwind_exit"), has the same structure, and none of the later commits in the series change this.
arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 3874
Quoted 3 lines
arch/x86/net/bpf_jit_comp.c +0
-0jmp_offset = ctx->cleanup_addr - addrs[i]; goto emit_jmp; }
[ ... ]
---
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/39474515830
[email protected]
AI reviewRe: [PATCH v4 bpf-next 2/6] bpf, x86: Support unwind_goto and unwind_exit
In reply to Alexei StarovoitovThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf, x86: emit_unwind_ret_addr() in exception_cb unwinds caller frame --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf, x86: emit_unwind_ret_addr() in exception_cb unwinds caller frame --
commit c3cf97c5b42c930ee82a85d0896d91eed8f4f5f6
Author: Alexei Starovoitov <[email protected]>
bpf, x86: Support unwind_goto and unwind_exit
This commit adds x86 JIT support for the BPF unwind_goto and unwind_exit instructions using exception table entries and return address fixups.
Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +0
-0
[ ... ]
Quoted 27 lines
arch/x86/net/bpf_jit_comp.c +27
-0+static u64 bpf_unwind_target (u64 ret_addr) +{ +const struct exception_table_entry *e =search_bpf_extables (ret_addr -1 ); + +/* +* The verifier made sure that unwind_goto follows a call in a prog. When +* the kernel called an async or an exception callback, it's exit. +*/ +return e ? ret_addr + e->fixup : ret_addr; +} + +/* unwind_exit: fix the return address of this frame. r0 and r2 are returned. */ +static int emit_unwind_ret_addr (u8 **pprog,u8 *ip) +{ +u8 *prog = *pprog, *start = prog; + +EMIT2 (0x50 ,0x56 );/* push rax; push rsi */ +EMIT4 (0x48 ,0x8B ,0x7D ,0x08 );/* mov rdi, [rbp + 8] */ + ip += prog - start; + ip +=x86_call_depth_emit_accounting (&prog, bpf_unwind_target, ip); +if (emit_call (&prog, bpf_unwind_target, ip)) +return -EINVAL ; +EMIT4 (0x48 ,0x89 ,0x45 ,0x08 );/* mov [rbp + 8], rax */ +EMIT2 (0x5E ,0x58 );/* pop rsi; pop rax */ + *pprog = prog; +return 0 ; +}
[ ... ]
arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 3870
Quoted 7 lines
arch/x86/net/bpf_jit_comp.c +3
-0@@ -3774,13 +3865,16 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *break ;case BPF_JMP |BPF_EXIT : +if (src_reg ==BPF_UNWIND_EXIT &&bpf_prog_can_unwind (bpf_prog) && +emit_unwind_ret_addr (&prog, ip)) +return -EINVAL ;
[Severity: High]
Can emitting emit_unwind_ret_addr() inside an exception callback subprogram cause unwind_exit to unwind the exception boundary's caller frame into an unverified landing pad?
In include/linux/filter.h, bpf_prog_can_unwind() returns true for any subprogram:
include/linux/filter.h:bpf_prog_can_unwind() {
return bpf_is_subprog(prog) || prog->type == BPF_PROG_TYPE_EXT;
}Because an exception callback is a subprogram (bpf_prog->aux->func_idx != 0 with bpf_prog->aux->exception_cb set), bpf_prog_can_unwind(bpf_prog) is true in do_jit(), while the verifier treats BPF_UNWIND_EXIT in an exception callback as a normal frame-0 exit in process_bpf_exit_full() and ignores the exception callback when setting prog->aux->can_unwind in check_unwind_insns().
The comment in bpf_unwind_target() assumes that when the kernel invokes an exception callback, the return address at [rbp + 8] belongs to the kernel caller and will not match any BPF extable entry. However, in arch/x86/net/bpf_jit_comp.c, emit_prologue() resets rbp to the third argument (ctx.bp of the exception boundary frame found by bpf_stack_walker()):
arch/x86/net/bpf_jit_comp.c:emit_prologue() {
if (is_exception_cb) {
EMIT3(0x48, 0x89, 0xF4); /* mov rsp, rsi */
EMIT3(0x48, 0x89, 0xD5); /* mov rbp, rdx */
...
EMIT3(0x48, 0x89, 0xEC); /* mov rsp, rbp */
}As a result, [rbp + 8] inside the exception callback is the return address of the exception boundary frame (func[0]), not bpf_throw().
If the exception boundary frame was entered via a tail call from a subprogram that was called before an unwind_goto instruction, or if an extension program (BPF_PROG_TYPE_EXT) with can_unwind == false on func[0] replaces a global subprogram followed by an unwind_goto instruction and then calls bpf_throw(), [rbp + 8] points to the instruction after the subprogram call in the caller. When the exception callback executes BPF_UNWIND_EXIT, bpf_unwind_target() finds the caller's unwind_goto extable entry and redirects [rbp + 8] to the landing pad with register and stack state that the verifier never checked for that path.
Should bpf_prog_can_unwind() (or the check in do_jit()) exclude prog->aux->exception_cb?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v4 bpf-next 2/6] bpf, x86: Support unwind_goto and unwind_exit
In reply to Alexei StarovoitovHi Alexei,
Quoted 40 lines · Alexei Starovoitov
From: Alexei Starovoitov <[email protected]>
bpf x86
call inner call inner
unwind_gotopad
w0 = w6 mov eax, ebxr0 = 0 xor eax, eax
unwind_exitpush raxpush rsi mov rdi, [rbp + 8] call bpf_unwind_target mov [rbp + 8], rax pop rsi pop rax leave ret
unwind_gotois an extable entry and no code.unwind_exitlooks its return address up there and returns to the pad. r0 and r2 are what a subprog returns, so rax and rsi are saved around the lookup.When there is no entry, the kernel called the subprog as an async or an exception callback, and
unwind_exitis exit.The caller of the main prog doesn't tell exit from
unwind_exit, so there it's exit. An extension is different. freplace is a jump, so its return address is the one of the subprog that it replaced.With private stack r9 is saved around a call, and 'pop r9' has to be done on the way to the pad too. In such progs
unwind_gotois:push r9
call foo
pop r9 <- exit returns here
jmp 1f
pop r9 <-unwind_exitreturns here
jmp pad
1:
Quoted 1 line
Signed-off-by Alexei Starovoitov <[email protected]>
After this change landed in -next as commit cff94a3d78d8 ("bpf, x86:
Support unwind_goto and unwind_exit"), I am seeing the following objtool warning when building with LLVM 23.1.4 and a recent tip of tree version of LLVM 24 with the attached configuration (which can become an error with CONFIG_OBJTOOL_WERROR):
vmlinux.o: warning: objtool: do_jit+0x1330: relocation to !ENDBR: .text+0xc7fab
Something like the below diff appears to inhibit this but I am not sure if it is too big of a hammer or if there is a better solution, hence just the report and deferring to you, but maybe it is a good starting point.
arch/x86/net/bpf_jit_comp.c +1 -0
@@ -641,6 +641,7 @@ static int emit_patch(u8 **pprog, void *func, void *ip, u8 opcode)static int emit_call (u8 **pprog,void *func,void *ip) { +OPTIMIZER_HIDE_VAR (func);return emit_patch (pprog, func, ip,0xE8 ); }
Signature · 2 lines
Cheers, Nathan
Re: [PATCH v4 bpf-next 2/6] bpf, x86: Support unwind_goto and unwind_exit
Quoted 18 lines · Nathan Chancellor
After this change landed in -next as commit
cff94a3d78d8("bpf, x86:
Supportunwind_gotoandunwind_exit"), I am seeing the following objtool warning when building with LLVM 23.1.4 and a recent tip of tree version of LLVM 24 with the attached configuration (which can become an error withCONFIG_OBJTOOL_WERROR):vmlinux.o: warning: objtool:
do_jit+0x1330: relocation to !ENDBR: .text+0xc7fabSomething like the below diff appears to inhibit this but I am not sure if it is too big of a hammer or if there is a better solution, hence just the report and deferring to you, but maybe it is a good starting point.
arch/x86/net/bpf_jit_comp.c +0
-0
arch/x86/net/bpf_jit_comp.c static int emit_patch(u8 **pprog, void *func, void *ip, u8 opcode) · line 646
Quoted 6 lines
arch/x86/net/bpf_jit_comp.c +1
-0@@ -641,6 +641,7 @@ static int emit_patch(u8 **pprog, void *func, void *ip, u8 opcode)static int emit_call (u8 **pprog,void *func,void *ip) { +OPTIMIZER_HIDE_VAR (func);return emit_patch (pprog, func, ip,0xE8 ); }
Thanks for the report.
Sent a fix. Pls test it in your setup.
PATCH v4 3/6 bpf, arm64: Support unwind_goto and unwind_exit
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
bpf arm64
call inner bl inner
mov x8, x0
unwind_goto pad
w0 = w6 mov w8, w19
Like on x86 unwind_goto is an extable entry and no code. unwind_exit returns to the pad past 'mov x8, x0'. That's fine: r0 is x8, and the epilogue of the callee copies it to x0 and leaves x8 as it is.
With pointer authentication LR in the frame record is signed. unwind_exit doesn't sign the address of the pad. It looks up the offset of the pad from its return address. Then it's the epilogue as it is, and the offset is added to LR after autiasp:
stp x8, x1, [sp, #-16]!
ldr x0, [x29, #8]
bl bpf_unwind_fixup
mov x10, x0
ldp x8, x1, [sp], #16
...
ldp x29, x30, [sp], #16
mov x0, x8
autiasp
add x30, x30, x10
ret
So LR is authenticated like in exit, and the frame record stays as it is. bpf_unwind_fixup() strips the signature for the lookup.
RET is not subject to BTI.
r0 and r2 are what a subprog returns, so x8 and x1 are saved around the lookup.
Like on x86 the offset is 0 when the kernel called the subprog as a callback, and unwind_exit of the main prog is exit, unless it's an extension.
The private stack pointer is in a callee saved register. Unlike x86 there is nothing to undo after the call.
Tested in qemu with -cpu max and -cpu cortex-a72, i.e. with and without pointer authentication.
--- arch/arm64/net/bpf_jit_comp.c | 97 +++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-)
arch/arm64/net/bpf_jit_comp.c +94 -3
@@ -11,6 +11,7 @@#include <linux/bitfield.h> #include <linux/bpf.h> #include <linux/cfi.h> +#include <linux/extable.h> #include <linux/filter.h> #include <linux/memory.h> #include <linux/printk.h>
@@ -1044,8 +1045,9 @@ static void __maybe_unused build_bhb_mitigation(struct jit_ctx *ctx) } } -static void build_epilogue (struct jit_ctx *ctx,bool was_classic) +static void build_epilogue (struct jit_ctx *ctx,bool was_classic,bool unwind) { +const u8 tmp = bpf2a64[TMP_REG_1 ];const u8 r0 = bpf2a64[BPF_REG_0 ];const u8 ptr = bpf2a64[TCCNT_PTR ];
@@ -1073,6 +1075,10 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)if (IS_ENABLED (CONFIG_ARM64_PTR_AUTH_KERNEL ))emit (A64_AUTIASP , ctx); +/* tmp is the offset of the pad, see emit_unwind_exit() */ +if (unwind) +emit (A64_ADD (1 ,A64_LR ,A64_LR , tmp), ctx); +emit (A64_RET (A64_LR ), ctx); }
@@ -1221,6 +1227,76 @@ static int add_exception_handler(const struct bpf_insn *insn,return 0 ; } +/* +* There is no branch for unwind_goto, only an extable entry +* { .insn = ret_addr - 1, .fixup = pad - ret_addr } +* where ret_addr is the return address of the call in front of it. unwind_exit +* adds .fixup to its LR. +* ret_addr - 1, because what follows the call can have an entry of its own. +* No insn starts there, so a fault cannot hit the entry. +* 'bl foo; mov x8, x0' is in front of unwind_goto. The pad doesn't miss the mov: +* r0 is x8 and the epilogue of the callee copies it to x0 and leaves x8 as it is. +*/ +static int add_unwind_exentry (struct jit_ctx *ctx,int pad) +{ +int ret_addr = ctx->idx -1 ; +struct exception_table_entry *ex; +off_t ins_offset; + +if (!ctx->image) +/* First pass */ +return 0 ; + +if (!ctx->prog->aux->extable || +WARN_ON_ONCE (ctx->exentry_idx >= ctx->prog->aux->num_exentries)) +return -EINVAL ; + + ex = &ctx->prog->aux->extable[ctx->exentry_idx]; + ins_offset = (long )&ctx->ro_image[ret_addr] -1 - (long )&ex->insn; +if (WARN_ON_ONCE (ins_offset >=0 || ins_offset <INT_MIN )) +return -ERANGE ; + +/* switch ex to rw buffer for writing */ + ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image); + ex->insn = ins_offset; + ex->fixup = (ctx->offset[pad] - ret_addr) *AARCH64_INSN_SIZE ; + ex->type =EX_TYPE_BPF ; + + ctx->exentry_idx++; +return 0 ; +} + +static long bpf_unwind_fixup (u64 lr) +{ +const struct exception_table_entry *e; + + e =search_bpf_extables (ptrauth_strip_kernel_insn_pac (lr) -1 ); +/* +* The verifier made sure that unwind_goto follows a call in a prog. When +* the kernel called an async or an exception callback, it's exit. +*/ +return e ? e->fixup :0 ; +} + +/* +* unwind_exit: return to the pad. LR in the frame record is signed. Instead of +* signing the address of the pad, let the epilogue authenticate LR as it is and +* add the offset of the pad to it. r0 and r2 are returned. +*/ +static void emit_unwind_exit (struct jit_ctx *ctx) +{ +const u8 tmp = bpf2a64[TMP_REG_1 ]; +const u8 r0 = bpf2a64[BPF_REG_0 ]; +const u8 r2 = bpf2a64[BPF_REG_2 ]; + +emit (A64_PUSH (r0, r2,A64_SP ), ctx); +emit (A64_LDR64I (A64_R (0 ),A64_FP ,8 ), ctx); +emit_call ((u64 )bpf_unwind_fixup, ctx); +emit (A64_MOV (1 , tmp,A64_R (0 )), ctx); +emit (A64_POP (r0, r2,A64_SP ), ctx); +build_epilogue (ctx, false, true); +} +static const struct bpf_jit_arg_abi arm64_arg_abi = { .nr_arg_regs =8 , .even_reg_align = true,
@@ -1806,8 +1882,18 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insnif (emit_bpf_tail_call (ctx))return -EFAULT ;break ; +/* unwind_goto. The verifier expanded may_goto. */ +case BPF_JMP |BPF_JCOND : + ret =add_unwind_exentry (ctx, i +1 + off); +if (ret) +return ret; +break ;/* function return */ case BPF_JMP |BPF_EXIT : +if (bpf_is_unwind_exit (insn) &&bpf_prog_can_unwind (ctx->prog)) { +emit_unwind_exit (ctx); +break ; + }/* Optimization: when last instruction is EXIT, simply fallthrough to epilogue. */ if (i == ctx->prog->len -1 )
@@ -2312,7 +2398,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr goto out_off; ctx.epilogue_offset = ctx.idx; -build_epilogue (&ctx, was_classic); +build_epilogue (&ctx, was_classic, false);build_plt (&ctx); extable_align = __alignof__(struct exception_table_entry );
@@ -2369,7 +2455,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr WARN_ON_ONCE(ctx.idx != ctx.epilogue_offset)) goto out_free_hdr; -build_epilogue (&ctx, was_classic); +build_epilogue (&ctx, was_classic, false);build_plt (&ctx);/* Extra pass to validate JITed code. */
@@ -2466,6 +2552,11 @@ bool bpf_jit_supports_kfunc_ret_reg_pair(void)return true; } +bool bpf_jit_supports_unwind (void ) +{ +return true; +} +const struct bpf_jit_arg_abi *bpf_jit_arg_abi (void ) {return &arm64_arg_abi;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v4 bpf-next 3/6] bpf, arm64: Support unwind_goto and unwind_exit
TL;DR: In arm64 JIT, unwind_exit in an exception callback may return into an unverified unwind_goto pad (via extension or tail call), possibly allowing type confusion and out-of-bounds kernel memory…
Quoted 1 line
bpf, arm64: Support
unwind_gotoandunwind_exit
TL;DR: In arm64 JIT, unwind_exit in an exception callback may return into an unverified unwind_goto pad (via extension or tail call), possibly allowing type confusion and out-of-bounds kernel memory access.
Quoted 4 lines
arch/arm64/net/bpf_jit_comp.c +0
-0
[ ... ]
Quoted 1 line
arch/arm64/net/bpf_jit_comp.c +0
-0@@ -1221,6 +1227,76 @@ static int add_exception_handler(const struct bpf_insn *insn,
[ ... ]
Quoted 11 lines
arch/arm64/net/bpf_jit_comp.c +11
-0+static long bpf_unwind_fixup (u64 lr) +{ +const struct exception_table_entry *e; + + e =search_bpf_extables (ptrauth_strip_kernel_insn_pac (lr) -1 ); +/* +* The verifier made sure that unwind_goto follows a call in a prog. When +* the kernel called an async or an exception callback, it's exit. +*/ +return e ? e->fixup :0 ; +}
[ ... ]
arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn · line 1890
Quoted 10 lines
arch/arm64/net/bpf_jit_comp.c +4
-0@@ -1806,8 +1882,18 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn/* function return */ case BPF_JMP |BPF_EXIT : +if (bpf_is_unwind_exit (insn) &&bpf_prog_can_unwind (ctx->prog)) { +emit_unwind_exit (ctx); +break ; + }/* Optimization: when last instruction is EXIT, simply fallthrough to epilogue. */ if (i == ctx->prog->len -1 )
Can this unwind_exit land in a pad that the verifier never checked, when it is emitted for an exception callback?
The verifier treats unwind_exit in an exception callback as a plain exit (process_bpf_exit_full() -> check_return_code()), so prepare_func_exit() never checks for an unwind_goto at that call site. The JIT doesn't follow that rule. An exception callback is func[i] with func_idx != 0, so bpf_prog_can_unwind() returns true and emit_unwind_exit() is emitted. The comment in bpf_unwind_fixup() ("When the kernel called an async or an exception callback, it's exit") then relies on search_bpf_extables() failing.
An exception callback has no frame record of its own. build_prologue() does "mov x29, x2", and x2 is ctx.bp from bpf_stack_walker(), which is the FP of the first func_idx == 0 frame. So the "ldr x0, [x29, #8]" in emit_unwind_exit() reads the return address of the main prog that threw, not an address in bpf_throw(). That address is in BPF code in at least two supported setups:
1. Extension. Prog P calls global foo() as "call foo; unwind_goto pad". Extension E replaces foo(). freplace is a B from foo's patch site (bpf_arch_text_poke(), BPF_MOD_JUMP), so E's frame record LR is the return address in P after "bl foo". E calls bpf_throw() and has __exception_cb(cb), and cb ends with "r0 = 0; unwind_exit". Extensions with exception callbacks are used in progs/exceptions_ext.c.
2. Tail call. P's subprog s tail-calls Q. On arm64 Q runs on s's frame record (emit_bpf_tail_call() branches past the FP/LR push), so Q's exception callback reads the return address in P after "bl s".
In both cases search_bpf_extables() finds the entry that add_unwind_exentry() created for P's unwind_goto, and the callback's epilogue adds .fixup to LR and returns into P's pad.
Is that path checked anywhere? In case 1, check_unwind_insns() sets prog->aux->can_unwind from subprog_info[0] only, so an unwind_exit in E's exception callback doesn't count. The bpf_check_attach_target() check "Extension program can unwind, while original does not" then lets E replace a foo() that cannot unwind, and check_func_call() never pushes the unwound branch for that call. In case 2, check_helper_call() models a taken tail call as a normal return only.
The unwind_goto and its extable entry survive whenever the pad is reached from another path, for example another jump or another call whose callee does unwind, because bpf_opt_hard_wire_dead_code_branches() keeps it:
if (bpf_is_unwind_goto(insn) && aux_data[i + 1 + insn->off].seen)
continue;At runtime the pad then runs with r6-r9 and the stack as they were at the "call foo" or "call s" site, plus r0 from the callback, while the verifier only checked the pad with the states from its other predecessors. Could that mismatch allow type confusion, for example a register that is a scalar at the call site but a map value pointer on the verified path into the pad, and from there an out-of-bounds kernel memory access?
The commit message statement "the offset is 0 when the kernel called the subprog as a callback" and the comment in bpf_unwind_fixup() don't appear to hold for exception callbacks.
Would it work to treat unwind_exit as exit whenctx->prog->aux->exception_cb is set, for example:
if (bpf_is_unwind_exit(insn) && bpf_prog_can_unwind(ctx->prog) &&
!ctx->prog->aux->exception_cb)or else to make the verifier and the attach check account for unwind_exit in exception callbacks?
The x86 JIT in 9fa54d36ee99 ("bpf, x86: Support unwind_goto and unwind_exit") uses the same condition,
"src_reg == BPF_UNWIND_EXIT && bpf_prog_can_unwind(bpf_prog)". No fix for this was found in the rest of the series (c1df16f0ed1d..72e419bb0b07), which only adds selftests, and those cover the exception callback case only for a non-extension main prog.
---
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/39474515830
PATCH v4 4/6 selftests/bpf: Add tests for unwind_goto and unwind_exit
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
The tests spell the two insns out as raw insns, so they don't depend on llvm version.
verifier_unwind:
- A static subprog, a global one and a callee of callx leave either way at run time, through one and two frames. The verifier follows a static subprog, so the tests derive the argument from skb->len or read it from a map.
- r0 of unwind_exit, r6-r9 and the stack are there in the pad. r1-r5 are not readable.
- A pad is checked like any other code: it has to drop rcu_read_lock.
- How the callee left is a part of the verifier state: two states that differ only in that are not pruned.
- The pad of a call of a global subprog is always checked.
- A callee that never returns, where unwind_goto stays and 'goto pc-1' follows it, and one that never unwinds, where it goes away with the pad.
- What follows unwind_goto runs speculatively: with CAP_BPF only there is nospec in front of an insn that isn't safe with r0 of unwind_exit.
- unwind_exit without unwind_goto after the call.
- unwind_goto that doesn't follow a call of a subprog or is a jump target.
- Reserved fields.
- A sync callback cannot unwind. What it calls can.
- unwind_exit of an async and of an exception callback is exit.
- Tail call and ld_abs in a subprog that can unwind.
- unwind_exit of the main prog is exit.
- What x86-64 and arm64 JITs emit. With KASAN x86-64 JIT wraps a load in pushes, a call and pops, so the patterns start after the loads.
- Private stack. On x86-64 r9 has to be restored on the way to the pad.
The last one has to look at the return value of a prog that uses private stack. raw_tp is such a prog. Its test_run doesn't take a packet, so let test_loader run it like it runs syscall progs.
unwind_attach, with the caller run after every attach:
- fentry on a subprog that unwinds. fexit is refused.
- freplace of a subprog that unwinds by an extension that returns and by one that unwinds. The caller goes on after the call or in the pad.
- An extension that unwinds cannot replace a subprog that doesn't.
Every check that the series adds was removed in turn to see that a test fails.
--- .../selftests/bpf/prog_tests/unwind_attach.c | 112 ++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/unwind_attach.c | 43 + .../selftests/bpf/progs/verifier_unwind.c | 1211 +++++++++++++++++ tools/testing/selftests/bpf/test_loader.c | 3 +- 5 files changed, 1370 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/unwind_attach.c create mode 100644 tools/testing/selftests/bpf/progs/unwind_attach.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_unwind.c
tools/testing/selftests/bpf/prog_tests/unwind_attach.c new file +112 -0
@@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "verifier_unwind.skel.h" +#include "unwind_attach.skel.h" + +static char log_buf[16 *1024 ]; + +static int run (int fd) +{ +char data[64 ] = {}; +LIBBPF_OPTS (bpf_test_run_opts, opts, + .data_in = data, + .data_size_in = sizeof(data), + .repeat = 1, + ); + +ASSERT_OK (bpf_prog_test_run_opts (fd, &opts),"test_run" ); +return opts.retval; +} + +/* +* msg: why it cannot be loaded. Otherwise retval is what the target returns +* with the prog attached. +*/ +static void attach (int tgt_fd,const char *func,const char *name,const char *msg,int retval) +{ +struct bpf_link *link =NULL ; +struct unwind_attach *skel; +struct bpf_program *prog; +int err; + + skel =unwind_attach__open (); +if (!ASSERT_OK_PTR (skel,"unwind_attach__open" )) +return ; + + prog =bpf_object__find_program_by_name (skel->obj, name); +if (!ASSERT_OK_PTR (prog, name)) + goto out; +bpf_program__set_autoload (prog, true); + log_buf[0 ] =0 ; +bpf_program__set_log_buf (prog, log_buf,sizeof (log_buf)); + err =bpf_program__set_attach_target (prog, tgt_fd, func); +if (!ASSERT_OK (err,"set_attach_target" )) + goto out; + + err =unwind_attach__load (skel); +if (msg) { +ASSERT_ERR (err, name); +ASSERT_HAS_SUBSTR (log_buf, msg, name); + goto out; + } +if (!ASSERT_OK (err, name)) + goto out; + + link =bpf_program__attach (prog); +if (!ASSERT_OK_PTR (link,"attach" )) + goto out; +ASSERT_EQ (run (tgt_fd), retval,"retval" ); +if (prog == skel->progs.fentry_unwind) +ASSERT_EQ (skel->bss->fentry_cnt,1 ,"fentry_cnt" ); +out: +bpf_link__destroy (link); +unwind_attach__destroy (skel); +} + +void test_unwind_attach (void ) +{ +struct verifier_unwind *tgt; +struct bpf_program *prog; +int tgt_fd, plain_fd, err; + +if (!is_jit_enabled ()) { +printf ("%s:SKIP:JIT is disabled\n" , __func__); +test__skip (); +return ; + } + tgt =verifier_unwind__open (); +if (!ASSERT_OK_PTR (tgt,"verifier_unwind__open" )) +return ; +bpf_object__for_each_program (prog, tgt->obj) +bpf_program__set_autoload (prog, prog == tgt->progs.calls_global_unwinds || + prog == tgt->progs.calls_global_never_unwinds); + err =verifier_unwind__load (tgt); +if (err == -ENOTSUPP ) { +printf ("%s:SKIP:JIT does not support unwinding\n" , __func__); +test__skip (); + goto out; + } +if (!ASSERT_OK (err,"verifier_unwind__load" )) + goto out; +/* +* It calls global_unwinds(), which unwinds with r0 = 5, and returns r0 + 10 +* from the pad. +*/ + tgt_fd =bpf_program__fd (tgt->progs.calls_global_unwinds); + plain_fd =bpf_program__fd (tgt->progs.calls_global_never_unwinds); +ASSERT_EQ (run (tgt_fd),15 ,"retval" ); + +attach (tgt_fd,"global_unwinds" ,"fentry_unwind" ,NULL ,15 ); +/* unwind_exit would return to the trampoline */ +attach (tgt_fd,"global_unwinds" ,"fexit_unwind" , +"Only fentry can attach to global_unwinds" ,0 ); +/* An extension leaves either way, like what it replaced */ +attach (tgt_fd,"global_unwinds" ,"freplace_returns" ,NULL ,9 ); +attach (tgt_fd,"global_unwinds" ,"freplace_unwinds" ,NULL ,13 ); +/* There is no unwind_goto after the call */ +attach (plain_fd,"global_never_unwinds" ,"freplace_unwinds" , +"Extension program can unwind, while original does not" ,0 ); +ASSERT_EQ (run (tgt_fd),15 ,"retval" ); +out: +verifier_unwind__destroy (tgt); +}
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -130,6 +130,7 @@#include "verifier_typedef.skel.h" #include "verifier_uninit.skel.h" #include "verifier_unpriv.skel.h" +#include "verifier_unwind.skel.h" #include "verifier_unpriv_perf.skel.h" #include "verifier_value_adj_spill.skel.h" #include "verifier_value.skel.h"
@@ -320,6 +321,7 @@ void test_verifier_tailcall_jit(void) { RUN(verifier_tailcall_jit); }void test_verifier_typedef (void ) {RUN (verifier_typedef); }void test_verifier_uninit (void ) {RUN (verifier_uninit); }void test_verifier_unpriv (void ) {RUN (verifier_unpriv); } +void test_verifier_unwind (void ) {RUN (verifier_unwind); }void test_verifier_unpriv_perf (void ) {RUN (verifier_unpriv_perf); }void test_verifier_value_adj_spill (void ) {RUN (verifier_value_adj_spill); }void test_verifier_value (void ) {RUN (verifier_value); }
tools/testing/selftests/bpf/progs/unwind_attach.c new file +43 -0
43 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_unwind.c new file +1211 -0
1,211 lines not on this page · raw message
tools/testing/selftests/bpf/test_loader.c +2 -1
@@ -1546,7 +1546,8 @@ void run_subtest(struct test_loader *tester, } err =do_prog_test_run (bpf_program__fd (tprog), &retval, -bpf_program__type (tprog) ==BPF_PROG_TYPE_SYSCALL ? true : false, +bpf_program__type (tprog) ==BPF_PROG_TYPE_SYSCALL || +bpf_program__type (tprog) ==BPF_PROG_TYPE_RAW_TRACEPOINT , spec->linear_sz);if (!err && retval != subspec->retval && subspec->retval !=POINTER_VALUE ) {PRINT_FAIL ("Unexpected retval: %d != %d\n" , retval, subspec->retval);
Signature · 1 line
2.55.0
PATCH v4 5/6 selftests/bpf: Add a C++ prog with exceptions
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
static __noinline int inner(int len)
{
rcu_guard guard; if (len > 100)
throw 1;
return len;}
static __noinline int middle(int len)
{
preempt_guard guard;return inner(len) + 1;
}
SEC("tc") int catches(struct __sk_buff *skb)
{
try {
return middle(skb->len);
} catch (...) {
return 42;
}
}The exception passes through two frames. The destructors call bpf_rcu_read_unlock() and bpf_preempt_enable(). The verifier insists on both, and the test checks that they ran in that order.
In the second prog nobody catches. The destructors run and the prog returns 0.
All that is left of the C++ runtime is four one line functions. __cxa_throw() calls _Unwind_RaiseException(), which llvm lowers.
progs/*.cpp are built like progs/*.rs: by clang only and without skeletons. vmlinux.h and bpf_helpers.h are not C++, so the prog declares what it needs.
clang that lowers unwinding [1] defines __BPF_FEATURE_UNWIND. Without it the object has no progs and the test is skipped.
[1] https://github.com/llvm/llvm-project/pull/233649
--- tools/testing/selftests/bpf/Makefile.skel | 11 ++ .../selftests/bpf/prog_tests/unwind_cxx.c | 77 ++++++++++++ .../selftests/bpf/progs/unwind_cxx.cpp | 115 ++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/unwind_cxx.c create mode 100644 tools/testing/selftests/bpf/progs/unwind_cxx.cpp
tools/testing/selftests/bpf/Makefile.skel +11 -0
@@ -153,4 +153,15 @@ $(RUST_OBJS): $(RDIR)/%.bpf.o: progs/%.rs $(RUST_CORE) | $(RDIR) endif endif +# Programs in C++. No skeletons: gcc doesn't build them. +ifeq ($(BPF_CC),$(CLANG)) +CXX_OBJS := $(patsubst progs/%.cpp,$(RDIR)/%.bpf.o,$(wildcard progs/*.cpp)) +BPF_OBJS += $(CXX_OBJS) + +$(CXX_OBJS): $(RDIR)/%.bpf.o: progs/%.cpp | $(RDIR) + $(call msg,$(BPF_CC_MSG),$(BINARY),$@) + $(Q)$(BPF_CC) -x c++ -g -Wall -Werror -fexceptions -funwind-tables -fno-rtti \ + $(BPF_CC_FLAGS) -c $< -o $@ $(call skip_on_fail,BPF) +endif + endif # BPF_CC
tools/testing/selftests/bpf/prog_tests/unwind_cxx.c new file +77 -0
77 lines not on this page · raw message
tools/testing/selftests/bpf/progs/unwind_cxx.cpp new file +115 -0
115 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v4 6/6 selftests/bpf: Test unwinding from R0:R2 subprog
In reply to Alexei StarovoitovFrom: Kumar Kartikeya Dwivedi <[email protected]>
Global subprograms returning a 16-byte scalar use R0:R2. The verifier therefore checks both registers at every ordinary and unwind exit.
LLVM lowers a resume to "r0 = 0; unwind_exit". If cleanup code does not happen to initialize R2, the verifier rejects the unwind path with:
R2 !read_ok
Extend unwind_cxx with a global unsigned __int128-returning subprogram whose cleanup only releases its RCU read lock. Load it separately so the existing cases continue to run and the failure is attributed to this case. Check both the normal aggregate return and caught exception. The test passes once LLVM defines R2 before emitting unwind_exit.
--- .../selftests/bpf/prog_tests/unwind_cxx.c | 24 ++++++++++++- .../selftests/bpf/progs/unwind_cxx.cpp | 35 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/unwind_cxx.c +23 -1
@@ -38,7 +38,8 @@ void test_unwind_cxx(void) {const char *file ="./unwind_cxx.bpf.o" ;struct bpf_object *obj; -int err; +struct bpf_program *prog; +int err = -EINVAL ;if (access (file,R_OK )) {printf ("%s:SKIP:gcc doesn't build %s\n" , __func__, file);
@@ -58,6 +59,10 @@ void test_unwind_cxx(void) obj =bpf_object__open_file (file,NULL );if (!ASSERT_OK_PTR (obj,"open" ))return ; + prog =bpf_object__find_program_by_name (obj,"catches_aggregate" ); +if (!ASSERT_OK_PTR (prog,"catches_aggregate" )) + goto out; +bpf_program__set_autoload (prog, false); err =bpf_object__load (obj);if (err == -ENOTSUPP ) {printf ("%s:SKIP:JIT does not support unwinding\n" , __func__);
@@ -74,4 +79,21 @@ void test_unwind_cxx(void)run (obj,"does_not_catch" ,200 ,0 ,12 ); out:bpf_object__close (obj); +if (err) +return ; + +if (!test__start_subtest ("aggregate_return" )) +return ; + obj =bpf_object__open_file (file,NULL ); +if (!ASSERT_OK_PTR (obj,"open_aggregate" )) +return ; +bpf_object__for_each_program (prog, obj) +bpf_program__set_autoload (prog, +strcmp (bpf_program__name (prog),"catches_aggregate" ) ==0 ); +if (!ASSERT_OK (bpf_object__load (obj),"load_aggregate" )) + goto out_aggregate; +run (obj,"catches_aggregate" ,64 ,71 ,0 ); +run (obj,"catches_aggregate" ,200 ,42 ,0 ); +out_aggregate: +bpf_object__close (obj); }
tools/testing/selftests/bpf/progs/unwind_cxx.cpp +35 -0
@@ -67,6 +67,17 @@ struct rcu_guard { } }; +struct plain_rcu_guard { + __always_inline plain_rcu_guard() + { + bpf_rcu_read_lock(); + } + __always_inline ~plain_rcu_guard() + { + bpf_rcu_read_unlock(); + } +}; + struct preempt_guard { __always_inline preempt_guard() {
@@ -95,6 +106,18 @@ static __noinline int middle(int len) return inner(len) + 1; } +using u128 = unsigned __int128; + +/* Global subprogs returning 16 bytes use R0:R2, including unwind exits. */ +extern "C" __noinline u128 aggregate_return(int len) +{ + plain_rcu_guard guard; + + if (len > 100) + throw 1; + return (static_cast<u128>(len) << 64) | 7; +} + extern "C" SEC("tc") int catches(struct __sk_buff *skb) { try {
@@ -110,6 +133,18 @@ extern "C" SEC("tc") int does_not_catch(struct __sk_buff *skb) return middle(skb->len); } +extern "C" SEC("tc") int catches_aggregate(struct __sk_buff *skb) +{ + try { + u128 value = aggregate_return(skb->len); + + return static_cast<unsigned long>(value) + + static_cast<unsigned long>(value >> 64); + } catch (...) { + return 42; + } +} + #endif char _license[] SEC("license") = "GPL";
Signature · 1 line
2.55.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Tue, 20 Oct 2026 18:13:14 +0000 you wrote:
From: Alexei Starovoitov <[email protected]>
1. What is the problem?
A rust panic or a C++ exception has to run Drop impls or destructors of every frame it passes through.
bpf_throw()discards the frames, so the verifier doesn't let a frame that owns a lock or a reference throw. In rust every index and everyunwrap()can panic.[...]
Here is the summary with links:
- [v4,bpf-next,1/6] bpf: Add unwind_goto and unwind_exit insns
https://git.kernel.org/bpf/bpf-next/c/a2d274771321 - [v4,bpf-next,2/6] bpf, x86: Support unwind_goto and unwind_exit https://git.kernel.org/bpf/bpf-next/c/cff94a3d78d8 - [v4,bpf-next,3/6] bpf, arm64: Support unwind_goto and unwind_exit https://git.kernel.org/bpf/bpf-next/c/fc85015a5e86 - [v4,bpf-next,4/6] selftests/bpf: Add tests for unwind_goto and unwind_exit https://git.kernel.org/bpf/bpf-next/c/86cec0897108 - [v4,bpf-next,5/6] selftests/bpf: Add a C++ prog with exceptions https://git.kernel.org/bpf/bpf-next/c/0935acd1a8a7 - [v4,bpf-next,6/6] selftests/bpf: Test unwinding from R0:R2 subprog https://git.kernel.org/bpf/bpf-next/c/55c34496ee99
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html