Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3658865pxb; Mon, 24 Jan 2022 14:39:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJyoLcwi8hURvrbARupDZ7C1M/0VjQGTNAssAvAv7Kk3j2D3hDu76fanbrpG2cYoLViyP1c5 X-Received: by 2002:a17:90a:ec02:: with SMTP id l2mr477138pjy.61.1643063991051; Mon, 24 Jan 2022 14:39:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643063991; cv=none; d=google.com; s=arc-20160816; b=f0MROhHrJAzXJFeNdmaaOP8HgYrdOpPeIWiR+rGAfdgvtDZJRk7AXhQ4dTWwbi4cD6 xIXQFNEwOPt8m4RzBt/qGus7YdGngXnmrhcUBD3Gbgr9u0q4t9CcG5Y9SqaqR+lL9fcC yCKbcQdHm5Q6lo+nqXFw0xtw6MxHIJQO1xT9q7bHmzhF/8W9aPVvqpO/TD6kFLq1asPq MliTWH+b2lRi7eY0HLPfxruuUAMxz5AcGgPWmFSLz+G+PM8oBG/yCDAGf0R4lztM639o H+kOqmfz81BomWy/M0mmWKS4YFMV78eVaNRYvcxMGMu83fq/8cH0QNeP+0qoMRcy+LUM luRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ap9b8yx2fG1VF/MjcGcxaQm/f8IzSE+sECxzr09d4u0=; b=m7ugi7bk0Oznzb3RhagzvtXM9U4m3Pywo5fajAUYxnD3597P5I2sZigve5/2UEDXtd U7iEHKxaM+Jq94I9IjlU3Ys8EY3Snw0a+iY7G7eFsUTwjMFEO5zyM0R7rJZBXEfzVoU3 KjmWw4sYnuMthjdF84PCJlBoNMiQ41n37drdGNb+KVcIEUaR5nYGfLCpR/ueIQmKv+kO AxH0iGuwUzc8eQK3jpNCzx6kl9vCkEBNUuK+4w70nEnJ1j13ROmWxMQ5G9P0inKApQt2 3FwxaxWPyIrvkrX+7trjR3t8DE187KniKrIbeDFPcmT+qiZ/WZiwTovoU7RJiVfvll6b 7MDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WF4taM1p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n16si16805397plh.462.2022.01.24.14.39.38; Mon, 24 Jan 2022 14:39:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WF4taM1p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1588768AbiAXWdx (ORCPT + 99 others); Mon, 24 Jan 2022 17:33:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1456532AbiAXVjb (ORCPT ); Mon, 24 Jan 2022 16:39:31 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B0F8C061341; Mon, 24 Jan 2022 12:24:35 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 39E786090A; Mon, 24 Jan 2022 20:24:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 18C96C340E5; Mon, 24 Jan 2022 20:24:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643055874; bh=l3akZgYVfwMEfwcCOQk0BID8yWFagZs9UcWLxnR9ZfA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WF4taM1pWA/dvUaOBojgGibAYV/0cUf0QITTIoOXD710kR+7E7GvPuxGzk+9Xw+zo nvHkL/6HFSm05b4dpC4SbhguwSPHjzvxkQA891cbbUoyvdNhv/IRZwrCLEh+Q7VVP1 DWg4tgKFmdskuQR/aeN04MnwDdxQP1n6x0FIS5f4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andre Przywara , Adam Lackorzynski , Peter Maydell , Ard Biesheuvel , Linus Walleij , "Russell King (Oracle)" , Sasha Levin Subject: [PATCH 5.15 261/846] ARM: 9159/1: decompressor: Avoid UNPREDICTABLE NOP encoding Date: Mon, 24 Jan 2022 19:36:18 +0100 Message-Id: <20220124184109.923209775@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andre Przywara [ Upstream commit a92882a4d270fbcc021ee6848de5e48b7f0d27f3 ] 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: 81a0bc39ea19 ("ARM: add UEFI stub support") Signed-off-by: Andre Przywara Reported-by: Adam Lackorzynski Suggested-by: Peter Maydell Reviewed-by: Ard Biesheuvel Reviewed-by: Linus Walleij Signed-off-by: Russell King (Oracle) Signed-off-by: Sasha Levin --- 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 c0e7a745103e2..230030c130853 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 b1cb1972361b8..bf79f2f78d232 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.34.1