Skip to content

Commit 2a65da5

Browse files
Andre-ARMgregkh
authored andcommitted
ARM: 9159/1: decompressor: Avoid UNPREDICTABLE NOP encoding
[ Upstream commit a92882a ] In the decompressor's head.S we need to start with an instruction that is some kind of NOP, but also mimics as the PE/COFF header, when the kernel is linked as an UEFI application. The clever solution here is "tstne r0, #0x4d000", which in the worst case just clobbers the condition flags, and bears the magic "MZ" signature in the lowest 16 bits. However the encoding used (0x13105a4d) is actually not valid, since bits [15:12] are supposed to be 0 (written as "(0)" in the ARM ARM). Violating this is UNPREDICTABLE, and *can* trigger an UNDEFINED exception. Common Cortex cores seem to ignore those bits, but QEMU chooses to trap, so the code goes fishing because of a missing exception handler at this point. We are just saved by the fact that commonly (with -kernel or when running from U-Boot) the "Z" bit is set, so the instruction is never executed. See [0] for more details. To make things more robust and avoid UNPREDICTABLE behaviour in the kernel code, lets replace this with a "two-instruction NOP": The first instruction is an exclusive OR, the effect of which the second instruction reverts. This does not leave any trace, neither in a register nor in the condition flags. Also it's a perfectly valid encoding. Kudos to Peter Maydell for coming up with this gem. [0] https://lore.kernel.org/qemu-devel/YTPIdbUCmwagL5%2FD@os.inf.tu-dresden.de/T/ Link: https://lore.kernel.org/linux-arm-kernel/20210908162617.104962-1-andre.przywara@arm.com/T/ Fixes: 81a0bc3 ("ARM: add UEFI stub support") Signed-off-by: Andre Przywara <andre.przywara@arm.com> Reported-by: Adam Lackorzynski <adam@l4re.org> Suggested-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 47dd693 commit 2a65da5

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

‎arch/arm/boot/compressed/efi-header.S‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@
99
#include <linux/sizes.h>
1010

1111
.macro __nop
12-
#ifdef CONFIG_EFI_STUB
13-
@ This is almost but not quite a NOP, since it does clobber the
14-
@ condition flags. But it is the best we can do for EFI, since
15-
@ PE/COFF expects the magic string "MZ" at offset 0, while the
16-
@ ARM/Linux boot protocol expects an executable instruction
17-
@ there.
18-
.inst MZ_MAGIC | (0x1310 << 16) @ tstne r0, #0x4d000
19-
#else
2012
AR_CLASS( mov r0, r0 )
2113
M_CLASS( nop.w )
14+
.endm
15+
16+
.macro __initial_nops
17+
#ifdef CONFIG_EFI_STUB
18+
@ This is a two-instruction NOP, which happens to bear the
19+
@ PE/COFF signature "MZ" in the first two bytes, so the kernel
20+
@ is accepted as an EFI binary. Booting via the UEFI stub
21+
@ will not execute those instructions, but the ARM/Linux
22+
@ boot protocol does, so we need some NOPs here.
23+
.inst MZ_MAGIC | (0xe225 << 16) @ eor r5, r5, 0x4d000
24+
eor r5, r5, 0x4d000 @ undo previous insn
25+
#else
26+
__nop
27+
__nop
2228
#endif
2329
.endm
2430

‎arch/arm/boot/compressed/head.S‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ start:
190190
* were patching the initial instructions of the kernel, i.e
191191
* had started to exploit this "patch area".
192192
*/
193-
.rept 7
193+
__initial_nops
194+
.rept 5
194195
__nop
195196
.endr
196197
#ifndef CONFIG_THUMB2_KERNEL

0 commit comments

Comments
 (0)