arm64/insn: Avoid undefined behaviour in branch offset decode
cover arm64/insn: Avoid undefined behaviour in branch offset decode
I hit a UBSAN failure while alternatives patching decoded a branch with a negative offset.
The branch offset helper sign-extends the decoded immediate by shifting it left into bit 31 and back again. Doing the left shift in the signed domain trips UBSAN on negative offsets, so switch the intermediate to uint32_t and cast back to int32_t only for the final right shift.
(XEN) alternatives: Patching with alt table 00000a00005e7298 -> 00000a00005e8894 (XEN) ================================================================================ (XEN) UBSAN: Undefined behaviour in arch/arm/arm64/insn.c:232:15
(XEN) left shift of 66621055 by 6 places cannot be represented in type 'int' (XEN) Xen WARN at common/ubsan/ubsan.c:176
(XEN) ----[ Xen-4.21.0 arm64 debug=y ubsan=y Not tainted ]----
(XEN) CPU: 0 (XEN) PC: 00000a00003432e0 ubsan.c#ubsan_epilogue+0x14/0xec (XEN) LR: 00000a0000344a58 (XEN) SP: 00000a000061fbd0 (XEN) CPSR: 00000000400002c9 MODE:64-bit EL2h (Hypervisor, handler)
Edgar E. Iglesias (1):
arm64/insn: Avoid undefined behaviour in branch offset decode
xen/arch/arm/arm64/insn.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/1 arm64/insn: Avoid undefined behaviour in branch offset decode
Branch offset decoding sign-extends the immediate by shifting it left into bit 31 and back. Perform the left shift in uint32_t and cast to int32_t only for the final right shift to avoid UBSAN failures on negative offsets.
--- xen/arch/arm/arm64/insn.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/insn.c +4 -4
@@ -225,22 +225,22 @@ u32 __kprobes aarch64_insn_gen_nop(void)*/ int32_t aarch64_get_branch_offset (uint32_t insn) { -int32_t imm; +uint32_t imm;if (aarch64_insn_is_b (insn) ||aarch64_insn_is_bl (insn)) { imm =aarch64_insn_decode_immediate (AARCH64_INSN_IMM_26 , insn); -return (imm <<6 ) >>4 ; +return (int32_t )(imm <<6 ) >>4 ; }if (aarch64_insn_is_cbz (insn) ||aarch64_insn_is_cbnz (insn) ||aarch64_insn_is_bcond (insn)) { imm =aarch64_insn_decode_immediate (AARCH64_INSN_IMM_19 , insn); -return (imm <<13 ) >>11 ; +return (int32_t )(imm <<13 ) >>11 ; }if (aarch64_insn_is_tbz (insn) ||aarch64_insn_is_tbnz (insn)) { imm =aarch64_insn_decode_immediate (AARCH64_INSN_IMM_14 , insn); -return (imm <<18 ) >>16 ; +return (int32_t )(imm <<18 ) >>16 ; }/* Unhandled instruction */
Signature · 1 line
2.43.0
Quoted 6 lines · Edgar E. Iglesias
Branch offset decoding sign-extends the immediate by shifting it left into bit 31 and back. Perform the left shift in
uint32_tand cast toint32_tonly for the final right shift to avoid UBSAN failures on negative offsets.Fixes 6dbf3f0e3074 ("xen/arm: arm64: Add helpers to decode and encode branch instructions")Signed-off-by Edgar E. Iglesias <[email protected]>
Quoted 38 lines
--- xen/arch/arm/arm64/insn.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)xen/arch/arm/arm64/insn.c +4
-4@@ -225,22 +225,22 @@ u32 __kprobes aarch64_insn_gen_nop(void)*/ int32_t aarch64_get_branch_offset (uint32_t insn) { -int32_t imm; +uint32_t imm;if (aarch64_insn_is_b (insn) ||aarch64_insn_is_bl (insn)) { imm =aarch64_insn_decode_immediate (AARCH64_INSN_IMM_26 , insn); -return (imm <<6 ) >>4 ; +return (int32_t )(imm <<6 ) >>4 ; }if (aarch64_insn_is_cbz (insn) ||aarch64_insn_is_cbnz (insn) ||aarch64_insn_is_bcond (insn)) { imm =aarch64_insn_decode_immediate (AARCH64_INSN_IMM_19 , insn); -return (imm <<13 ) >>11 ; +return (int32_t )(imm <<13 ) >>11 ; }if (aarch64_insn_is_tbz (insn) ||aarch64_insn_is_tbnz (insn)) { imm =aarch64_insn_decode_immediate (AARCH64_INSN_IMM_14 , insn); -return (imm <<18 ) >>16 ; +return (int32_t )(imm <<18 ) >>16 ; }/* Unhandled instruction */ Signature · 1 line
2.43.0