Currently bpf_throw() is a C function. It walks the stack to the frame of the main program and calls its exception callback, which resets the stack to that frame and returns from the program. To restore the callee-saved registers of the frames it discards, the unwinder needs their values at the throw site. By the time C code runs, the compiler may have clobbered them. Move the body of bpf_throw() into bpf_throw_unwind(), which takes the callee-saved registers of the throw site, R6-R9, along with the cookie: - An architecture can provide a native entry for the kfunc, which captures the registers and then calls bpf_throw_unwind(). bpf_arch_throw_entry() returns that entry. - The verifier redirects a call to bpf_throw() to the entry when it specializes the kfunc. The JIT still finds the function model of the call through the specialized descriptor. - Without an entry, bpf_throw() calls bpf_throw_unwind() with NULL, as before. Nothing uses the registers yet. bpf_throw_unwind() never returns. It transfers to a JITed callback, which returns to the kernel code that called the program. On arm64 with a shadow call stack, the function's prologue would push a return address that is never popped. The caller of the program would then pop it instead of its own return address, and trap when it returns. So: - Mark the function __noscs, which keeps a static shadow call stack out of it. - A dynamic shadow call stack is patched in at boot, in place of the pointer authentication instructions of each function. Under CONFIG_DYNAMIC_SCS, build the function without pointer authentication but with branch target identification, as drivers/misc/lkdtm/cfi.c does for the same reason. The function loses nothing by this, since it never returns through its return address. For the same reason, mark bpf_throw_unwind() notrace, and add it and bpf_throw() to the functions that an fexit program cannot attach to: a function that does not return never leaves the trampoline. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf.h | 1 + include/linux/bpf_throw.h | 52 +++++++++++++++++++++++++++++++++++++++ kernel/bpf/core.c | 9 +++++++ kernel/bpf/helpers.c | 17 ++++++++++--- kernel/bpf/verifier.c | 7 ++++++ 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 include/linux/bpf_throw.h diff --git a/include/linux/bpf.h b/include/linux/bpf.h index da42dbd32fd4..b01820e67795 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/include/linux/bpf_throw.h b/include/linux/bpf_throw.h new file mode 100644 index 000000000000..57d974458750 --- /dev/null +++ b/include/linux/bpf_throw.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * The interface between the architecture entries of bpf_throw() and the + * generic unwinder. + */ +#ifndef _LINUX_BPF_THROW_H +#define _LINUX_BPF_THROW_H + +/* The callee-saved registers the unwinder tracks: R6..R9. */ +#define BPF_THROW_NR_CALLEE_SAVED 4 +/* The size of struct bpf_throw_regs, for the entries. */ +#define BPF_THROW_REGS_SIZE (8 * BPF_THROW_NR_CALLEE_SAVED) + +#ifndef __ASSEMBLER__ +#include +#include + +/* + * The callee-saved registers captured at a throw site, indexed like + * bpf_prog_aux::callee_reg_off. + */ +struct bpf_throw_regs { + u64 saved[BPF_THROW_NR_CALLEE_SAVED]; +}; +static_assert(sizeof(struct bpf_throw_regs) == BPF_THROW_REGS_SIZE); + +/* + * bpf_throw_unwind() never returns: it transfers to a JITed callback, which + * returns to the kernel code that called the program. With a shadow call + * stack the return address its prologue pushed would never be popped, and the + * program's caller would pop it in place of its own. __noscs keeps a static + * shadow call stack out of the function. A dynamic one is patched in at boot + * in place of the pointer authentication of every function that has it, see + * UNWIND_PATCH_PAC_INTO_SCS, so the function has to do without that as well, + * which costs it nothing, as it never returns through it; the branch target + * identification stays. + */ +#if defined(CONFIG_DYNAMIC_SCS) && defined(CONFIG_ARM64_BTI_KERNEL) +#define __bpf_unwind_entry __noscs __attribute__((__target__("branch-protection=bti"))) +#elif defined(CONFIG_DYNAMIC_SCS) +#define __bpf_unwind_entry __noscs __attribute__((__target__("branch-protection=none"))) +#else +#define __bpf_unwind_entry __noscs +#endif + +void bpf_throw_unwind(struct bpf_throw_regs *regs, u64 cookie); +/* The native entry of bpf_throw(), on an architecture that has one. */ +void bpf_throw_entry(void); +void *bpf_arch_throw_entry(void); +#endif /* __ASSEMBLER__ */ + +#endif /* _LINUX_BPF_THROW_H */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index f88a1c041a7a..7a25d1cb2c30 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -3930,6 +3930,15 @@ void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, { } +/* + * The native entry of bpf_throw(), which captures the BPF register file of + * the throw site, or NULL when the architecture calls the kfunc directly. + */ +void * __weak bpf_arch_throw_entry(void) +{ + return NULL; +} + bool __weak bpf_jit_supports_timed_may_goto(void) { return false; diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 937a0d7d6839..de31fdd18411 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3460,7 +3460,13 @@ static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp) return false; } -__bpf_kfunc void bpf_throw(u64 cookie) +/* + * Unwind the stack to the exception callback of the main program. @regs holds + * the callee-saved registers of the throw site, or is NULL when the + * architecture has no native entry and the kfunc was called directly. + */ +__bpf_unwind_entry +notrace void bpf_throw_unwind(struct bpf_throw_regs *regs, u64 cookie) { struct bpf_throw_ctx ctx = {}; @@ -3472,8 +3478,8 @@ __bpf_kfunc void bpf_throw(u64 cookie) WARN_ON_ONCE(!ctx.cnt); /* * Prevent KASAN false positives for CONFIG_KASAN_STACK by unpoisoning - * deeper stack depths than ctx.sp as we do not return from bpf_throw, - * which skips compiler generated instrumentation to do the same. Some + * deeper stack depths than ctx.sp as we do not return from here, which + * skips compiler generated instrumentation to do the same. Some * architectures cannot recover sp while unwinding, so fall back to bp. */ kasan_unpoison_task_stack_below((void *)(long)(ctx.sp ?: ctx.bp)); @@ -3481,6 +3487,11 @@ __bpf_kfunc void bpf_throw(u64 cookie) WARN(1, "A call to BPF exception callback should never return\n"); } +__bpf_kfunc void bpf_throw(u64 cookie) +{ + bpf_throw_unwind(NULL, cookie); +} + __bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__const_map, unsigned int flags) { struct bpf_async_kern *async = (struct bpf_async_kern *)wq; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 20b1b9ff574c..c5b4025ee222 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -24434,6 +24434,8 @@ BTF_ID(func, do_exit) BTF_ID(func, do_group_exit) BTF_ID(func, kthread_complete_and_exit) BTF_ID(func, make_task_dead) +BTF_ID(func, bpf_throw) +BTF_ID(func, bpf_throw_unwind) BTF_SET_END(noreturn_deny) static bool can_be_sleepable(struct bpf_prog *prog) @@ -24793,6 +24795,11 @@ static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc } else if (func_id == special_kfunc_list[KF_bpf_arena_free_pages]) { if (env->insn_aux_data[insn_idx].non_sleepable) addr = (unsigned long)bpf_arena_free_pages_non_sleepable; + } else if (func_id == special_kfunc_list[KF_bpf_throw]) { + void *entry = bpf_arch_throw_entry(); + + if (entry) + addr = (unsigned long)entry; } desc->addr = addr; return 0; -- 2.53.0