Make cfi_get_offset(), SYM_TYPED_FUNC_START and the BPF JIT use CONFIG_ARM64_FUNCTION_PREFIX_NOPS instead of hardcoding four bytes. kCFI kernels with CALL_OPS need three NOPs, or five with ThinLTO and BTI.
CONFIG_ARM64_FUNCTION_PREFIX_NOPS is 0 on every CFI kernel today, so no functional change. Reviewed-by: Ben Cressey <[email protected]> Reviewed-by: Florent Revest (Anthropic) <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Assisted-by: LLM Signed-off-by: Jose Fernandez (Anthropic) <[email protected]> --- arch/arm64/include/asm/cfi.h | 11 +++++++++++ arch/arm64/include/asm/linkage.h | 11 +++++++++++ arch/arm64/net/bpf_jit_comp.c | 8 ++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/cfi.h b/arch/arm64/include/asm/cfi.h index ab90f0351b7a..f3c04a2a6a95 100644 --- a/arch/arm64/include/asm/cfi.h +++ b/arch/arm64/include/asm/cfi.h @@ -2,6 +2,17 @@ #ifndef _ASM_ARM64_CFI_H #define _ASM_ARM64_CFI_H +#include <asm/insn-def.h> + #define __bpfcall +#ifdef CONFIG_CFI +static inline int cfi_get_offset(void) +{ + /* The 4-byte type hash sits before the prefix NOPs. */ + return 4 + CONFIG_ARM64_FUNCTION_PREFIX_NOPS * AARCH64_INSN_SIZE; +} +#define cfi_get_offset cfi_get_offset +#endif /* CONFIG_CFI */ + #endif /* _ASM_ARM64_CFI_H */ diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h index d1f7a16729d2..14bc3c67c38b 100644 --- a/arch/arm64/include/asm/linkage.h +++ b/arch/arm64/include/asm/linkage.h @@ -39,6 +39,17 @@ SYM_START(name, SYM_L_WEAK, SYM_A_NONE) \ bti c ; +/* + * The compiler emits CONFIG_ARM64_FUNCTION_PREFIX_NOPS NOPs between a C + * function's kCFI type hash and its entry point. Callers check the hash + * at that offset. + */ +#define __CFI_TYPE(name) \ + .4byte __kcfi_typeid_##name ASM_NL \ + .rept CONFIG_ARM64_FUNCTION_PREFIX_NOPS ASM_NL \ + nop ASM_NL \ + .endr + #define SYM_TYPED_FUNC_START(name) \ SYM_TYPED_START(name, SYM_L_GLOBAL, SYM_A_ALIGN) \ bti c ; diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index aaa34c739004..42cb2d446680 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -185,8 +185,12 @@ static inline void emit_bti(u32 insn, struct jit_ctx *ctx) static inline void emit_kcfi(u32 hash, struct jit_ctx *ctx) { - if (IS_ENABLED(CONFIG_CFI)) - emit_u32_data(hash, ctx); + if (!IS_ENABLED(CONFIG_CFI)) + return; + + emit_u32_data(hash, ctx); + for (int i = 0; i < CONFIG_ARM64_FUNCTION_PREFIX_NOPS; i++) + emit(A64_NOP, ctx); } /* -- 2.54.0
