Skip to content

Commit 042152c

Browse files
Xu Kuohaiborkmann
authored andcommitted
bpf, arm64: Sign return address for JITed code
Sign return address for JITed code when the kernel is built with pointer authentication enabled: 1. Sign LR with paciasp instruction before LR is pushed to stack. Since paciasp acts like landing pads for function entry, no need to insert bti instruction before paciasp. 2. Authenticate LR with autiasp instruction after LR is popped from stack. For BPF tail call, the stack frame constructed by the caller is reused by the callee. That is, the stack frame is constructed by the caller and destructed by the callee. Thus LR is signed and pushed to the stack in the caller's prologue, and poped from the stack and authenticated in the callee's epilogue. For BPF2BPF call, the caller and callee construct their own stack frames, and sign and authenticate their own LRs. Signed-off-by: Xu Kuohai <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://events.static.linuxfound.org/sites/events/files/slides/slides_23.pdf Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9a7ef9f commit 042152c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

arch/arm64/net/bpf_jit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@
263263
/* HINTs */
264264
#define A64_HINT(x) aarch64_insn_gen_hint(x)
265265

266+
#define A64_PACIASP A64_HINT(AARCH64_INSN_HINT_PACIASP)
267+
#define A64_AUTIASP A64_HINT(AARCH64_INSN_HINT_AUTIASP)
268+
266269
/* BTI */
267270
#define A64_BTI_C A64_HINT(AARCH64_INSN_HINT_BTIC)
268271
#define A64_BTI_J A64_HINT(AARCH64_INSN_HINT_BTIJ)

arch/arm64/net/bpf_jit_comp.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ static bool is_lsi_offset(int offset, int scale)
236236
}
237237

238238
/* Tail call offset to jump into */
239-
#if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
239+
#if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) || \
240+
IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)
240241
#define PROLOGUE_OFFSET 9
241242
#else
242243
#define PROLOGUE_OFFSET 8
@@ -278,8 +279,11 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
278279
*
279280
*/
280281

282+
/* Sign lr */
283+
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
284+
emit(A64_PACIASP, ctx);
281285
/* BTI landing pad */
282-
if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
286+
else if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
283287
emit(A64_BTI_C, ctx);
284288

285289
/* Save FP and LR registers to stay align with ARM64 AAPCS */
@@ -580,6 +584,10 @@ static void build_epilogue(struct jit_ctx *ctx)
580584
/* Set return value */
581585
emit(A64_MOV(1, A64_R(0), r0), ctx);
582586

587+
/* Authenticate lr */
588+
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
589+
emit(A64_AUTIASP, ctx);
590+
583591
emit(A64_RET(A64_LR), ctx);
584592
}
585593

0 commit comments

Comments
 (0)