From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CAA6BC433EF for ; Thu, 9 Sep 2021 10:50:32 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8F95760F4A for ; Thu, 9 Sep 2021 10:50:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8F95760F4A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zESt7H1XoUkubD6mSGk1MyPst/n6lZPvdp+4c9YGo5I=; b=mTY7Lhd1Er3xBl e9xkP3/wbjVwb6lpYvZNm2q4xkoR6U+w0tBYdTQhZk1ckspHcdmMc05JnU22MlwltvDMfSWq0PTUb Kc/EWm8JT0Nvgjkd1YVx7DNREgJdjRMG+yX1DHEGAYkm8guVjoXJ3MfAxaG21p9voDktUCADHYFnE 9nc9AuES7N8FzbVi9RO8ovWS0BfhnJAwT3k7Jsc6ORt62tHls23oj4o7BuxxWNUOKYZNfepf9ZwG3 mOwiFOV3458x+pQmwgNleF2Zgc/zlRvX1xxYyT1D54pf062gnri7E5Ztl6SR3HzYulZm591rxN1yb sSMHH5yAU11Ck3BOoHQA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mOHbN-008xbU-U0; Thu, 09 Sep 2021 10:48:18 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mOHbJ-008xai-AY for linux-arm-kernel@lists.infradead.org; Thu, 09 Sep 2021 10:48:15 +0000 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8F4B261132 for ; Thu, 9 Sep 2021 10:48:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631184492; bh=YP6JEFf8tb5qau/oInbN0Na3QT6+ETb0P24PaLLXNVo=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=qOga4lDDPK8Ne4ReZzZgIygwhWyepIGGQfsHwxHpWC1ir06Bi2gZRpOXuuNLPlbPv 9jAIoEciEK1egcJlw4W8HHEmEgpT4QQUO68q9RoqheYNxf37eMIvdFUVMztv0c5H0R 8igGcyNgx3w1UM0KvoiyU/jmLg/y73uowkK6XrvUF0ZvHMajlIa7MPIKwEZP2yt94q 2UKBx4VIbHsSUqYT0nUtJVXFkB6KlSVpIqKbLHEuxVka9lLbrKl/AmRaM4XP2h5H97 J2TmGn7To8o/4jpcw9l6AZdk9xRzwASC/DG12CGIEp98pRgx0CNQnypN+EPb49ZFTm cRHmU0+yrLsJQ== Received: by mail-oi1-f181.google.com with SMTP id w144so1902515oie.13 for ; Thu, 09 Sep 2021 03:48:12 -0700 (PDT) X-Gm-Message-State: AOAM531WoVvWK76dGXMCeKQVX/322wWx9nm8RdwtgrlzN8DN1rib641V RjhkuCveHdTukO3799emwnFuBZFvWPVFSMjgZ1Y= X-Google-Smtp-Source: ABdhPJwsEovDSctU6v/tY7SGA7LnUBloQ95GnvdzAB8vZ2/uBR3e4mcCKbHcHzDIh2vR2YM4jIIcfys1ie9pyRm2crE= X-Received: by 2002:a05:6808:15a2:: with SMTP id t34mr1459667oiw.47.1631184491895; Thu, 09 Sep 2021 03:48:11 -0700 (PDT) MIME-Version: 1.0 References: <20210908162617.104962-1-andre.przywara@arm.com> In-Reply-To: <20210908162617.104962-1-andre.przywara@arm.com> From: Ard Biesheuvel Date: Thu, 9 Sep 2021 12:48:00 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] ARM: decompressor: Avoid UNPREDICTABLE NOP encoding To: Andre Przywara Cc: Russell King , linux-efi , Linux ARM , Linux Kernel Mailing List , Linus Walleij , Catalin Marinas , Adam Lackorzynski , Peter Maydell X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210909_034813_445413_35023CB6 X-CRM114-Status: GOOD ( 30.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, 8 Sept 2021 at 18:26, Andre Przywara wrote: > > 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/ > > Signed-off-by: Andre Przywara > Reported-by: Adam Lackorzynski > Suggested-by: Peter Maydell Thanks a lot for fixing this! I had no idea the encoding was invalid. Reviewed-by: Ard Biesheuvel > --- > arch/arm/boot/compressed/efi-header.S | 22 ++++++++++++++-------- > arch/arm/boot/compressed/head.S | 3 ++- > 2 files changed, 16 insertions(+), 9 deletions(-) > > diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S > index c0e7a745103e..230030c13085 100644 > --- a/arch/arm/boot/compressed/efi-header.S > +++ b/arch/arm/boot/compressed/efi-header.S > @@ -9,16 +9,22 @@ > #include > > .macro __nop > -#ifdef CONFIG_EFI_STUB > - @ This is almost but not quite a NOP, since it does clobber the > - @ condition flags. But it is the best we can do for EFI, since > - @ PE/COFF expects the magic string "MZ" at offset 0, while the > - @ ARM/Linux boot protocol expects an executable instruction > - @ there. > - .inst MZ_MAGIC | (0x1310 << 16) @ tstne r0, #0x4d000 > -#else > AR_CLASS( mov r0, r0 ) > M_CLASS( nop.w ) > + .endm > + > + .macro __initial_nops > +#ifdef CONFIG_EFI_STUB > + @ This is a two-instruction NOP, which happens to bear the > + @ PE/COFF signature "MZ" in the first two bytes, so the kernel > + @ is accepted as an EFI binary. Booting via the UEFI stub > + @ will not execute those instructions, but the ARM/Linux > + @ boot protocol does, so we need some NOPs here. > + .inst MZ_MAGIC | (0xe225 << 16) @ eor r5, r5, 0x4d000 > + eor r5, r5, 0x4d000 @ undo previous insn > +#else > + __nop > + __nop > #endif > .endm > > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S > index b1cb1972361b..bf79f2f78d23 100644 > --- a/arch/arm/boot/compressed/head.S > +++ b/arch/arm/boot/compressed/head.S > @@ -203,7 +203,8 @@ start: > * were patching the initial instructions of the kernel, i.e > * had started to exploit this "patch area". > */ > - .rept 7 > + __initial_nops > + .rept 5 > __nop > .endr > #ifndef CONFIG_THUMB2_KERNEL > -- > 2.17.1 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel