aarch64_set_branch_offset() puts the offset into the immediate of the branch without a range or alignment check, so an offset beyond the reach of the branch wraps: 128MB in a B becomes -128MB, 1MB + 8 in a CBZ becomes -1MB + 8. Check the offset against the reach of the branch, 128MB for B and BL, 1MB for CBZ, CBNZ and B.cond and 32KB for TBZ and TBNZ, and return AARCH64_BREAK_FAULT when it does not fit, like the encoders do. Take the offset as an s64, and keep it in s64 in the alternatives, so the distance reaches the check untruncated. An alternative whose branch would be out of reach then gets a BRK patched in instead of a wrapped branch. The BPF JIT does not use the helper. Its only user, get_alt_insn(), moves the branches of an alternative out of its replacement sequence within the kernel image or a module, well within reach, so no kernel code is affected. Found by checking the encoders against Arm's A64 ISA XML (2026-09). Signed-off-by: Daniel Borkmann --- arch/arm64/include/asm/insn.h | 2 +- arch/arm64/kernel/alternative.c | 2 +- arch/arm64/lib/insn.c | 28 ++++++++++++++++++++-------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/insn.h b/arch/arm64/include/asm/insn.h index 302ee42d2b3c..55e6de8e7ad4 100644 --- a/arch/arm64/include/asm/insn.h +++ b/arch/arm64/include/asm/insn.h @@ -731,7 +731,7 @@ u32 aarch64_insn_gen_mrs(enum aarch64_insn_register result, enum aarch64_insn_system_register sysreg); s32 aarch64_get_branch_offset(u32 insn); -u32 aarch64_set_branch_offset(u32 insn, s32 offset); +u32 aarch64_set_branch_offset(u32 insn, s64 offset); s64 aarch64_insn_adrp_get_offset(u32 insn); u32 aarch64_insn_adrp_set_offset(u32 insn, s64 offset); diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternative.c index 2a1be7010e03..beb91a266fda 100644 --- a/arch/arm64/kernel/alternative.c +++ b/arch/arm64/kernel/alternative.c @@ -63,7 +63,7 @@ static __always_inline u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, insn = le32_to_cpu(*altinsnptr); if (aarch64_insn_is_branch_imm(insn)) { - s32 offset = aarch64_get_branch_offset(insn); + s64 offset = aarch64_get_branch_offset(insn); unsigned long target; target = (unsigned long)altinsnptr + offset; diff --git a/arch/arm64/lib/insn.c b/arch/arm64/lib/insn.c index 57a5c662411e..e4bc21c0c2cc 100644 --- a/arch/arm64/lib/insn.c +++ b/arch/arm64/lib/insn.c @@ -1506,24 +1506,36 @@ s32 aarch64_get_branch_offset(u32 insn) BUG(); } +static u32 branch_imm_set(enum aarch64_insn_imm_type type, s64 range, + u32 insn, s64 offset) +{ + if ((offset & 0x3) || offset < -range || offset >= range) { + pr_err("%s: invalid offset %lld\n", __func__, offset); + return AARCH64_BREAK_FAULT; + } + + return aarch64_insn_encode_immediate(type, insn, offset >> 2); +} + /* * Encode the displacement of a branch in the imm field and return the - * updated instruction. + * updated instruction, or AARCH64_BREAK_FAULT for a displacement out of + * the reach of the branch. */ -u32 aarch64_set_branch_offset(u32 insn, s32 offset) +u32 aarch64_set_branch_offset(u32 insn, s64 offset) { if (aarch64_insn_is_b(insn) || aarch64_insn_is_bl(insn)) - return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_26, insn, - offset >> 2); + return branch_imm_set(AARCH64_INSN_IMM_26, SZ_128M, insn, + offset); if (aarch64_insn_is_cbz(insn) || aarch64_insn_is_cbnz(insn) || aarch64_insn_is_bcond(insn)) - return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn, - offset >> 2); + return branch_imm_set(AARCH64_INSN_IMM_19, SZ_1M, insn, + offset); if (aarch64_insn_is_tbz(insn) || aarch64_insn_is_tbnz(insn)) - return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_14, insn, - offset >> 2); + return branch_imm_set(AARCH64_INSN_IMM_14, SZ_32K, insn, + offset); /* Unhandled instruction */ BUG(); -- 2.43.0