aarch64_insn_adrp_get_offset() and aarch64_insn_adrp_set_offset() pass the offset as an s32, while the imm21 of ADRP reaches +/-4GB. The getter shifts the immediate into 32 bits, so a page 2GB or more away loses its sign: page -524289 comes back as +2147479552 and page 524288 as -2147483648. The setter has no range check, so an offset beyond the reach wraps into the immediate. Pass the offset as an s64, sign extend the immediate, and reject an offset that is not page aligned or out of reach with AARCH64_BREAK_FAULT, like the encoders do. The alternatives keep their offsets in s64 as well. An alternative whose ADRP would be out of reach then gets a BRK patched in instead of an ADRP to the wrong page. The BPF JIT does not use the two helpers. Their users are the alternatives, the module PLT comparison and the static call trampolines, whose ADRPs stay within 2GB, 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 | 4 ++-- arch/arm64/kernel/alternative.c | 2 +- arch/arm64/lib/insn.c | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/insn.h b/arch/arm64/include/asm/insn.h index b25dfd958f94..302ee42d2b3c 100644 --- a/arch/arm64/include/asm/insn.h +++ b/arch/arm64/include/asm/insn.h @@ -733,8 +733,8 @@ u32 aarch64_insn_gen_mrs(enum aarch64_insn_register result, s32 aarch64_get_branch_offset(u32 insn); u32 aarch64_set_branch_offset(u32 insn, s32 offset); -s32 aarch64_insn_adrp_get_offset(u32 insn); -u32 aarch64_insn_adrp_set_offset(u32 insn, s32 offset); +s64 aarch64_insn_adrp_get_offset(u32 insn); +u32 aarch64_insn_adrp_set_offset(u32 insn, s64 offset); bool aarch32_insn_is_wide(u32 insn); diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternative.c index f5ec7e7c1d3f..2a1be7010e03 100644 --- a/arch/arm64/kernel/alternative.c +++ b/arch/arm64/kernel/alternative.c @@ -78,7 +78,7 @@ static __always_inline u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, insn = aarch64_set_branch_offset(insn, offset); } } else if (aarch64_insn_is_adrp(insn)) { - s32 orig_offset, new_offset; + s64 orig_offset, new_offset; unsigned long target; /* diff --git a/arch/arm64/lib/insn.c b/arch/arm64/lib/insn.c index 1b4525c50504..57a5c662411e 100644 --- a/arch/arm64/lib/insn.c +++ b/arch/arm64/lib/insn.c @@ -1529,17 +1529,25 @@ u32 aarch64_set_branch_offset(u32 insn, s32 offset) BUG(); } -s32 aarch64_insn_adrp_get_offset(u32 insn) +s64 aarch64_insn_adrp_get_offset(u32 insn) { BUG_ON(!aarch64_insn_is_adrp(insn)); - return aarch64_insn_decode_immediate(AARCH64_INSN_IMM_ADR, insn) << 12; + return sign_extend64(aarch64_insn_decode_immediate(AARCH64_INSN_IMM_ADR, + insn), 20) << 12; } -u32 aarch64_insn_adrp_set_offset(u32 insn, s32 offset) +u32 aarch64_insn_adrp_set_offset(u32 insn, s64 offset) { BUG_ON(!aarch64_insn_is_adrp(insn)); + + if ((offset & (SZ_4K - 1)) || offset < -(s64)SZ_4G || + offset >= (s64)SZ_4G) { + pr_err("%s: invalid offset %lld\n", __func__, offset); + return AARCH64_BREAK_FAULT; + } + return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_ADR, insn, - offset >> 12); + offset >> 12); } /* -- 2.43.0