From patchwork Thu Nov 24 08:12:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 25353 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3258480wrr; Thu, 24 Nov 2022 00:13:53 -0800 (PST) X-Google-Smtp-Source: AA0mqf6QX0Cld8+uh0zLMRjUmey+UMuHq46TVZFxHqhqmDXP1VQU4NPh0trZDdmEpaRYyldbUL03 X-Received: by 2002:a17:90a:d3ce:b0:218:7a66:64b9 with SMTP id d14-20020a17090ad3ce00b002187a6664b9mr32021759pjw.85.1669277633225; Thu, 24 Nov 2022 00:13:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669277633; cv=none; d=google.com; s=arc-20160816; b=QurdwLISqYZTPTh4I3uBReISMxd5DFjKnvsr80TRg1XW2udkXfb24bWaqbqikL3RpT nvffb9qPSIgnE7+IpANPItEzFOnG9tSqUAHEuWg8AteDkp0j3B4A4b0rWWU8UlnNSTV/ R8Dzqy/e3/eayZ1WWNtUXMXpXwshvmk8ZIEThf16LOMBSlPEBie9SxL29yIyi6mt9E8B gzHo3XMO+NomJDxof2uS+iaKkJA1npcWjgPrK5uSAORpD61uaHybyYD0PIUI3r5fxudB gJ6ypSfWAjHAMXF5Ef3t07g1HR9ytWcszguDdLhgi9DA47XPRQNxZcxzmWDw2MDBvME2 dKsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=4AUUIgELc/8f64Y6NlAUQy2yAq4EBAXXDe6jsRvwdmU=; b=odE2a5QneWnyd73M3UVgUqxu3vQU10S1nnaAeR84yvtLoWNqqDUDkYtAmg4NyhhiSf VQvv9SkuJ7bwW0Rp4sHc41wpY3Up12gO+Gqfd6d9SLuPIWedGdEUNKfhVqOh5df6zvqH 4LoRLqYlmDW3Rd5clYCIf5btN3oBcExGURX5sLvzWsFRJUfQ6QGW0Vi/96h4ZkFKKtCm Yw4OxbhJx2K5cz5AFFZHaVEyAwiT3/zUWHlHY83RAwPHuJppkrpauB50LCfXo23Zmk+w mY7zkoG/7BUHPRAvR7gURWlR16dz6OUCcSk4sRE+FIgkzVHFk2n2IlkKyc8Jo+HkbIlC vUnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=mus2ZsKl; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pj4-20020a17090b4f4400b002189516ee01si783617pjb.100.2022.11.24.00.13.40; Thu, 24 Nov 2022 00:13:53 -0800 (PST) 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=@linutronix.de header.s=2020 header.b=mus2ZsKl; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229914AbiKXINO (ORCPT + 99 others); Thu, 24 Nov 2022 03:13:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229773AbiKXIMT (ORCPT ); Thu, 24 Nov 2022 03:12:19 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34805ECCF8; Thu, 24 Nov 2022 00:12:04 -0800 (PST) Date: Thu, 24 Nov 2022 08:12:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1669277522; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4AUUIgELc/8f64Y6NlAUQy2yAq4EBAXXDe6jsRvwdmU=; b=mus2ZsKlDclZ47SSZq6RJ7TMIdhJD76U9S4SjQ1j2hpJFse8EHdoNIDViQ1qEpXllFHuc9 pwKCkOyie7twhxfuSW8xS0i7qJGAc0diTuLEMMe6LoPSDCnE+GsD+coXJssR+x1GBCro4p Loi9yc1VnfY1GhO0VMDX3sgksvS4OIXl1cpmnUZJjt1koWZupe69GCITyHqGvG+dKzJwFe d0S5bJEfUmuofiUXGsQ0JRRTnLBnpAmcDCLeSW5CRf3copbtesJwMUjQqmDwQa5o1s4DQB 9x1Qzkv7avJn9Tpgd7qQE/Few7lKKEAyzQ4kV+MacvbNQInRog7avZOjf8M6kg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1669277522; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4AUUIgELc/8f64Y6NlAUQy2yAq4EBAXXDe6jsRvwdmU=; b=phhn7DLzMruvp6QC7X3Shc7ZAMQyv5ff4EECVSOBN/XtA1SJJ39NZYGJRXFcBJhXttbpQt dRjS2OHlM4/RjaCg== From: "tip-bot2 for Ard Biesheuvel" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] x86/boot/compressed: Move startup32_check_sev_cbit() out of head_64.S Cc: Ard Biesheuvel , Borislav Petkov , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20221122161017.2426828-15-ardb@kernel.org> References: <20221122161017.2426828-15-ardb@kernel.org> MIME-Version: 1.0 Message-ID: <166927752150.4906.4076564626502028600.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750364463599544289?= X-GMAIL-MSGID: =?utf-8?q?1750364463599544289?= The following commit has been merged into the x86/boot branch of tip: Commit-ID: 9d7eaae6a071ff1f718e0aa5e610bb712f8cc632 Gitweb: https://git.kernel.org/tip/9d7eaae6a071ff1f718e0aa5e610bb712f8cc632 Author: Ard Biesheuvel AuthorDate: Tue, 22 Nov 2022 17:10:14 +01:00 Committer: Borislav Petkov CommitterDate: Thu, 24 Nov 2022 08:57:41 +01:00 x86/boot/compressed: Move startup32_check_sev_cbit() out of head_64.S Now that the startup32_check_sev_cbit() routine can execute from anywhere and behaves like an ordinary function, it can be moved where it belongs. Signed-off-by: Ard Biesheuvel Signed-off-by: Borislav Petkov Link: https://lore.kernel.org/r/20221122161017.2426828-15-ardb@kernel.org --- arch/x86/boot/compressed/head_64.S | 71 +------------------------- arch/x86/boot/compressed/mem_encrypt.S | 68 ++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 71 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index 30ba541..db577fb 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -711,77 +711,6 @@ SYM_DATA_START(boot_idt) SYM_DATA_END_LABEL(boot_idt, SYM_L_GLOBAL, boot_idt_end) /* - * Check for the correct C-bit position when the startup_32 boot-path is used. - * - * The check makes use of the fact that all memory is encrypted when paging is - * disabled. The function creates 64 bits of random data using the RDRAND - * instruction. RDRAND is mandatory for SEV guests, so always available. If the - * hypervisor violates that the kernel will crash right here. - * - * The 64 bits of random data are stored to a memory location and at the same - * time kept in the %eax and %ebx registers. Since encryption is always active - * when paging is off the random data will be stored encrypted in main memory. - * - * Then paging is enabled. When the C-bit position is correct all memory is - * still mapped encrypted and comparing the register values with memory will - * succeed. An incorrect C-bit position will map all memory unencrypted, so that - * the compare will use the encrypted random data and fail. - */ -#ifdef CONFIG_AMD_MEM_ENCRYPT - .text -SYM_FUNC_START(startup32_check_sev_cbit) - pushl %ebx - pushl %ebp - - call 0f -0: popl %ebp - - /* Check for non-zero sev_status */ - movl (sev_status - 0b)(%ebp), %eax - testl %eax, %eax - jz 4f - - /* - * Get two 32-bit random values - Don't bail out if RDRAND fails - * because it is better to prevent forward progress if no random value - * can be gathered. - */ -1: rdrand %eax - jnc 1b -2: rdrand %ebx - jnc 2b - - /* Store to memory and keep it in the registers */ - leal (sev_check_data - 0b)(%ebp), %ebp - movl %eax, 0(%ebp) - movl %ebx, 4(%ebp) - - /* Enable paging to see if encryption is active */ - movl %cr0, %edx /* Backup %cr0 in %edx */ - movl $(X86_CR0_PG | X86_CR0_PE), %ecx /* Enable Paging and Protected mode */ - movl %ecx, %cr0 - - cmpl %eax, 0(%ebp) - jne 3f - cmpl %ebx, 4(%ebp) - jne 3f - - movl %edx, %cr0 /* Restore previous %cr0 */ - - jmp 4f - -3: /* Check failed - hlt the machine */ - hlt - jmp 3b - -4: - popl %ebp - popl %ebx - RET -SYM_FUNC_END(startup32_check_sev_cbit) -#endif - -/* * Stack and heap for uncompression */ .bss diff --git a/arch/x86/boot/compressed/mem_encrypt.S b/arch/x86/boot/compressed/mem_encrypt.S index 6747e5e..14cf04a 100644 --- a/arch/x86/boot/compressed/mem_encrypt.S +++ b/arch/x86/boot/compressed/mem_encrypt.S @@ -243,6 +243,74 @@ SYM_FUNC_START(startup32_load_idt) RET SYM_FUNC_END(startup32_load_idt) +/* + * Check for the correct C-bit position when the startup_32 boot-path is used. + * + * The check makes use of the fact that all memory is encrypted when paging is + * disabled. The function creates 64 bits of random data using the RDRAND + * instruction. RDRAND is mandatory for SEV guests, so always available. If the + * hypervisor violates that the kernel will crash right here. + * + * The 64 bits of random data are stored to a memory location and at the same + * time kept in the %eax and %ebx registers. Since encryption is always active + * when paging is off the random data will be stored encrypted in main memory. + * + * Then paging is enabled. When the C-bit position is correct all memory is + * still mapped encrypted and comparing the register values with memory will + * succeed. An incorrect C-bit position will map all memory unencrypted, so that + * the compare will use the encrypted random data and fail. + */ +SYM_FUNC_START(startup32_check_sev_cbit) + pushl %ebx + pushl %ebp + + call 0f +0: popl %ebp + + /* Check for non-zero sev_status */ + movl (sev_status - 0b)(%ebp), %eax + testl %eax, %eax + jz 4f + + /* + * Get two 32-bit random values - Don't bail out if RDRAND fails + * because it is better to prevent forward progress if no random value + * can be gathered. + */ +1: rdrand %eax + jnc 1b +2: rdrand %ebx + jnc 2b + + /* Store to memory and keep it in the registers */ + leal (sev_check_data - 0b)(%ebp), %ebp + movl %eax, 0(%ebp) + movl %ebx, 4(%ebp) + + /* Enable paging to see if encryption is active */ + movl %cr0, %edx /* Backup %cr0 in %edx */ + movl $(X86_CR0_PG | X86_CR0_PE), %ecx /* Enable Paging and Protected mode */ + movl %ecx, %cr0 + + cmpl %eax, 0(%ebp) + jne 3f + cmpl %ebx, 4(%ebp) + jne 3f + + movl %edx, %cr0 /* Restore previous %cr0 */ + + jmp 4f + +3: /* Check failed - hlt the machine */ + hlt + jmp 3b + +4: + popl %ebp + popl %ebx + RET +SYM_FUNC_END(startup32_check_sev_cbit) + .code64 #include "../../kernel/sev_verify_cbit.S"