Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1117200rwo; Wed, 2 Aug 2023 08:59:21 -0700 (PDT) X-Google-Smtp-Source: APBJJlE1Tv9Qj64o+MjoCgdSjoWf1gzdNUa15uTaULwU7eWwJ3SwAfkLNaET58Y8j1hE6YAIwBB+ X-Received: by 2002:aa7:d1cc:0:b0:522:18b6:c023 with SMTP id g12-20020aa7d1cc000000b0052218b6c023mr5395615edp.18.1690991961533; Wed, 02 Aug 2023 08:59:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690991961; cv=none; d=google.com; s=arc-20160816; b=uvQx7yeRfaPNETbH8YV9RDAIEGNjaI0OTNPUWT0MRZQtFanC3jYHiySPg7F1ew/VYR ULXneUkHWaZomUThOOdsbnlL37CHyLNrqSreXPZRAHtkpck1knLBKhdSpfIZHv53xUxB C3IGQIu8UVpt3/vh6un4cM+D55Vn24+wuJddKoj9ft/9ApTQ3H6k8m6/fi+gjnlJolke SuoArggm0bzixt00BdLaHTk41ZczXxEaRtZRRsoJwLaStchNEIZy1+lTNyN11gGxSuzO C8VUNXriwaRhTTiHXY7GDGVAwoNuVvuU05v+MNlxDeAT8vH7u9iVvf94+c3hYlLt2aOg qn8w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vOBo24w4Q4DKzHC3R7yJXEfT/vzI0l5F34i3nh+9YhM=; fh=UoPnrMfWVKGOqsML3Cwqzr4dYAyNRBpPmY/ITnBmPi0=; b=Kd/dV/B9Ks6xsbpjSDD8TOC1U/YCuc+1nEkEmHpGz5cjGPb61IVJhRGfoxrbXAXn5k vRejZlJ3b+IVei7MKIIM53JtQZMd9cpBWoItZjS98tL+wT5GvimOfQ/lZow3wO3Bdgdc 1P2adXFVtve3U8YEMHyy+6JSpY4iV74GIo44qtGtniUrUg4a5nkWxjnw2zQc09cEFm3k j9XJyp1Pz35lf2dBYbFwarBAhL+ogiN+Ab0Nl9Wb4rSNwNERAG2KXcq7JBqcLUuju1wC Ek4OfRR7Yv7VcVDQcm1lWRou4N+b7hV4cREtbmoThzwfjq6YuoiIfrh8fuKeLDVPbtSh YUzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=u38RyJmz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x9-20020aa7d389000000b00522bbb9c8ffsi6377538edq.195.2023.08.02.08.58.56; Wed, 02 Aug 2023 08:59:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=u38RyJmz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234503AbjHBPtC (ORCPT + 99 others); Wed, 2 Aug 2023 11:49:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233910AbjHBPs6 (ORCPT ); Wed, 2 Aug 2023 11:48:58 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF7A31981; Wed, 2 Aug 2023 08:48:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3CFED619F7; Wed, 2 Aug 2023 15:48:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF6B9C433CB; Wed, 2 Aug 2023 15:48:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690991336; bh=XjLX3hjHoLnvob8g9oghOOvEmelRZfiMIypqVk9Wd+A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u38RyJmzJee/nzoBUihV/cDotiVXiSdm1JMs5LjYn19hSGMMQ8We5OncH5Eo3VVIa +LiNfb55P6ne305nfZWPFdTmG1rUmoPKYNl9JWRHM0R6P15gPrIHbHRncKzPLJr+al D0w6UUJn4DNvvULYGPYRqO7+NHPrkR8QIuAjnJLVXdj7nXs5vxqDWk9Ogtdv2+i8nP xUhl0JhmUcvbTa+o7SO7Aw184bxJZLvDSzkhZB56JHt7XbB12yBYz+Yf7Wh/MI/tnA d1agXkYDfIe6frcnbU3iSJy3YB+HxrDJnsJqIZvN9ZI+114nrCCAx2/tJhfMaGv6OP 6vn1R4f1PbUQg== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Subject: [PATCH v8 01/23] x86/decompressor: Don't rely on upper 32 bits of GPRs being preserved Date: Wed, 2 Aug 2023 17:48:09 +0200 Message-Id: <20230802154831.2147855-2-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230802154831.2147855-1-ardb@kernel.org> References: <20230802154831.2147855-1-ardb@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Developer-Signature: v=1; a=openpgp-sha256; l=3520; i=ardb@kernel.org; h=from:subject; bh=XjLX3hjHoLnvob8g9oghOOvEmelRZfiMIypqVk9Wd+A=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIeVU1abfZgX9Oyd8bHC+sv/2jXX9E57sl910kWuWxhtT6 8VHLyVId5SyMIhxMMiKKbIIzP77bufpiVK1zrNkYeawMoEMYeDiFICJ/MlhZFjFt3GB3st5kYt9 jQSO24hU7F3ZMpn1/91Fe3ceXHArmKGU4X+MCl/9h+ylp10eTniyYO6qwyGC29x+9nYsyeHjeSp dEsUGAA== X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The 4-to-5 level mode switch trampoline disables long mode and paging in order to be able to flick the LA57 bit. According to section 3.4.1.1 of the x86 architecture manual [0], 64-bit GPRs might not retain the upper 32 bits of their contents across such a mode switch. Given that RBP, RBX and RSI are live at this point, preserve them on the stack, along with the return address that might be above 4G as well. [0] Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture "Because the upper 32 bits of 64-bit general-purpose registers are undefined in 32-bit modes, the upper 32 bits of any general-purpose register are not preserved when switching from 64-bit mode to a 32-bit mode (to protected mode or compatibility mode). Software must not depend on these bits to maintain a value after a 64-bit to 32-bit mode switch." Fixes: 194a9749c73d650c ("x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G") Signed-off-by: Ard Biesheuvel --- arch/x86/boot/compressed/head_64.S | 30 +++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 03c4328a88cbd5d0..f732426d3b483139 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -459,11 +459,25 @@ SYM_CODE_START(startup_64) /* Save the trampoline address in RCX */ movq %rax, %rcx + /* Set up 32-bit addressable stack */ + leaq TRAMPOLINE_32BIT_STACK_END(%rcx), %rsp + /* - * Load the address of trampoline_return() into RDI. - * It will be used by the trampoline to return to the main code. + * Preserve live 64-bit registers on the stack: this is necessary + * because the architecture does not guarantee that GPRs will retain + * their full 64-bit values across a 32-bit mode switch. + */ + pushq %rbp + pushq %rbx + pushq %rsi + + /* + * Push the 64-bit address of trampoline_return() onto the new stack. + * It will be used by the trampoline to return to the main code. Due to + * the 32-bit mode switch, it cannot be kept it in a register either. */ leaq trampoline_return(%rip), %rdi + pushq %rdi /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ pushq $__KERNEL32_CS @@ -471,6 +485,11 @@ SYM_CODE_START(startup_64) pushq %rax lretq trampoline_return: + /* Restore live 64-bit registers */ + popq %rsi + popq %rbx + popq %rbp + /* Restore the stack, the 32-bit trampoline uses its own stack */ leaq rva(boot_stack_end)(%rbx), %rsp @@ -582,7 +601,7 @@ SYM_FUNC_END(.Lrelocated) /* * This is the 32-bit trampoline that will be copied over to low memory. * - * RDI contains the return address (might be above 4G). + * Return address is at the top of the stack (might be above 4G). * ECX contains the base address of the trampoline memory. * Non zero RDX means trampoline needs to enable 5-level paging. */ @@ -592,9 +611,6 @@ SYM_CODE_START(trampoline_32bit_src) movl %eax, %ds movl %eax, %ss - /* Set up new stack */ - leal TRAMPOLINE_32BIT_STACK_END(%ecx), %esp - /* Disable paging */ movl %cr0, %eax btrl $X86_CR0_PG_BIT, %eax @@ -671,7 +687,7 @@ SYM_CODE_END(trampoline_32bit_src) .code64 SYM_FUNC_START_LOCAL_NOALIGN(.Lpaging_enabled) /* Return from the trampoline */ - jmp *%rdi + retq SYM_FUNC_END(.Lpaging_enabled) /* -- 2.39.2