[RFC PATCH 18/27] x86/bpf: Make BPF JIT aware of CET handling
From: K Prateek Nayak
Date: Sun Nov 29 2026 - 17:35:38 EST
The retpolies that BPF JIT uses are already aware of shadow stack
handling.
Emit a incsspq %ecx for the trampoline that return to parent directly
when a function return hook is active and BPF runs the function body as
part of the trampoline.
For the unwinding case, use incsspq %rcx again with %rcx set to
(unwinding depth + 1) when transferring the control back to a
caller in chain for do_exception callback. The "+ 1" is for the extra
frame that call to "bpf_exception_cb" adds.
Since unwinding and call to bpf_exception_cb is done from the bpf
core, use the 4th argument to pass the unwinding depth.
Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/net/bpf_jit_comp.c | 18 +++++++++++++++++-
kernel/bpf/helpers.c | 3 ++-
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 6bca87457e87..9d6be7de7521 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -560,6 +560,12 @@ static void emit_prologue(u8 **pprog, u8 *ip, u32 stack_depth, bool ebpf_from_cb
}
/* Exception callback receives FP as third parameter */
if (is_exception_cb) {
+ /*
+ * The number of slots to unwind to reach the
+ * return address is in the fourth parameter.
+ */
+ if (cpu_feature_enabled(X86_FEATURE_KERN_SHSTK))
+ EMIT5(0xF3, 0x48, 0x0F, 0xAE, 0xE9); /* incsspq rcx */
EMIT3(0x48, 0x89, 0xF4); /* mov rsp, rsi */
EMIT3(0x48, 0x89, 0xD5); /* mov rbp, rdx */
/* The main frame must have exception_boundary as true, so we
@@ -1530,7 +1536,7 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)
/* jump over faulting load and clear dest register */
if (reg != DONT_CLEAR)
*(unsigned long *)((void *)regs + reg) = 0;
- regs->ip += insn_len;
+ regs_set_return_ip(regs, regs->ip + insn_len);
return true;
}
@@ -3756,6 +3762,16 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
if (flags & BPF_TRAMP_F_SKIP_FRAME) {
/* skip our return address and return to parent */
EMIT4(0x48, 0x83, 0xC4, 8); /* add rsp, 8 */
+
+ /*
+ * Drop the same return address from the shadow stack. The
+ * traced function is returning here, so every caller-saved
+ * register except RAX is dead.
+ */
+ if (cpu_feature_enabled(X86_FEATURE_KERN_SHSTK)) {
+ EMIT1_off32(0xB9, 1); /* mov ecx, 1 */
+ EMIT5(0xF3, 0x48, 0x0F, 0xAE, 0xE9); /* incsspq rcx */
+ }
}
emit_return(&prog, image + (prog - (u8 *)rw_image));
/* Make sure the trampoline generation logic doesn't overflow */
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 712dca5a2c5b..4d1cdd507563 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -3402,7 +3402,8 @@ __bpf_kfunc void bpf_throw(u64 cookie)
* architectures cannot recover sp while unwinding, so fall back to bp.
*/
kasan_unpoison_task_stack_below((void *)(long)(ctx.sp ?: ctx.bp));
- ctx.aux->bpf_exception_cb(cookie, ctx.sp + ctx.aux->stack_arg_sp_adjust, ctx.bp, 0, 0);
+ ctx.aux->bpf_exception_cb(cookie, ctx.sp + ctx.aux->stack_arg_sp_adjust,
+ ctx.bp, ctx.cnt + 1, 0);
WARN(1, "A call to BPF exception callback should never return\n");
}
--
2.43.0