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]>
Assisted-by: LLM
Signed-off-by: Jose Fernandez (Anthropic) <[email protected]>
---
 arch/arm64/include/asm/cfi.h     |  8 ++++++++
 arch/arm64/include/asm/linkage.h | 11 +++++++++++
 arch/arm64/net/bpf_jit_comp.c    |  8 ++++++--
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/include/asm/cfi.h b/arch/arm64/include/asm/cfi.h
index ab90f0351b7ae..03f02d4614c23 100644
--- a/arch/arm64/include/asm/cfi.h
+++ b/arch/arm64/include/asm/cfi.h
@@ -4,4 +4,12 @@
 
 #define __bpfcall
 
+#ifdef CONFIG_CFI
+static inline int cfi_get_offset(void)
+{
+       return 4 + 4 * CONFIG_ARM64_FUNCTION_PREFIX_NOPS;
+}
+#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 d1f7a16729d25..14bc3c67c38b3 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 c18e005a41dbe..7ad14d9847138 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.52.0


Reply via email to