Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp86602pxf; Wed, 10 Mar 2021 00:49:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJxyV4IBepoWr/32WkOJgaYR+4I1ApsT70V6mYYW8DxtflbCErYV+XPP52nXPuqVNzTda5cN X-Received: by 2002:a17:906:9509:: with SMTP id u9mr1454682ejx.225.1615366149201; Wed, 10 Mar 2021 00:49:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615366149; cv=none; d=google.com; s=arc-20160816; b=BZfgfuJ0sm0gy5f6dVtZwfjBBdhIxZOgIkCu/PDwH/ksRFFQI7LNpl/4rSWKfVMtPm TPaJ/q2e7IcD7mjYDZHOk5IxiGMRlx9AMsqPAae5X+FcNx/87VRuLxv+Hj/sy5o/MYsS A7SMpccEOIq35AQc9mt69nhjrZwWBN6DYpXTzpth/kcXYWPFG1RqmniLDIaAqV9juEPu l/MaAxX9UR5szzrn6uVMO8Zt83CO1CF7/Kjt2sJy3QpJQlRb3PCb2+FACae0EkDDGJ6Q eh2IR+fUA9gkDGB1b10qNZCOpFLvgyglvSBDRCi9ea3PRXTbD5mIcSgvEys9w1966o5N MQqA== 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; bh=DUOpz8/G8XkWi0U3YxhYMSK3+ouSqm20PLMgjVI5rTo=; b=GFqMToESnsIQ5KFEdjNczEowP0emwlo05VVrsO6mZGf3mf8s6kC1ZJVDn6pQDmQB11 zzCKCt36VgYiUho6ARt8G8yR+gKGnnexUmfIwUfhTDlIZbNVVN8C7M8l9SsdFLVycwt0 Y41W9C4oqLbK704kzdS39eHjK4ZstQS0qfdjUEZP3bJ5V6MBoOtwAb+Fv1ik14PvEW2o DkKbsq2NPv2RCAj7QGnKsqN1EsVNn7oSBhSacU1AkZP0Bn1rAxiRa+/UnW1W4b76PJX3 CpZpIG3wAA5eJw7Znf9pWLgoji1tw+g8mM2Nu59hDzKuXxsEHNNd8nO0zv7yw/SRxfpj XmOw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jo5si11796820ejb.644.2021.03.10.00.48.46; Wed, 10 Mar 2021 00:49:09 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232519AbhCJIol (ORCPT + 99 others); Wed, 10 Mar 2021 03:44:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232323AbhCJIn6 (ORCPT ); Wed, 10 Mar 2021 03:43:58 -0500 Received: from theia.8bytes.org (8bytes.org [IPv6:2a01:238:4383:600:38bc:a715:4b6d:a889]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02C06C061764; Wed, 10 Mar 2021 00:43:58 -0800 (PST) Received: from cap.home.8bytes.org (p549adcf6.dip0.t-ipconnect.de [84.154.220.246]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by theia.8bytes.org (Postfix) with ESMTPSA id 09B8B5F0; Wed, 10 Mar 2021 09:43:55 +0100 (CET) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Sean Christopherson , Martin Radev , Arvind Sankar , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v2 6/7] x86/boot/compressed/64: Check SEV encryption in 32-bit boot-path Date: Wed, 10 Mar 2021 09:43:24 +0100 Message-Id: <20210310084325.12966-7-joro@8bytes.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210310084325.12966-1-joro@8bytes.org> References: <20210310084325.12966-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Check whether the hypervisor reported the correct C-bit when running as an SEV guest. Using a wrong C-bit position could be used to leak sensitive data from the guest to the hypervisor. Signed-off-by: Joerg Roedel --- arch/x86/boot/compressed/head_64.S | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index ee448aedb8b0..7c5c2698a96e 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -183,11 +183,21 @@ SYM_FUNC_START(startup_32) */ call get_sev_encryption_bit xorl %edx, %edx +#ifdef CONFIG_AMD_MEM_ENCRYPT testl %eax, %eax jz 1f subl $32, %eax /* Encryption bit is always above bit 31 */ bts %eax, %edx /* Set encryption mask for page tables */ + /* + * Mark SEV as active in sev_status so that startup32_check_sev_cbit() + * will do a check. The sev_status memory will be fully initialized + * with the contents of MSR_AMD_SEV_STATUS later in + * set_sev_encryption_mask(). For now it is sufficient to know that SEV + * is active. + */ + movl $1, rva(sev_status)(%ebp) 1: +#endif /* Initialize Page tables to 0 */ leal rva(pgtable)(%ebx), %edi @@ -272,6 +282,9 @@ SYM_FUNC_START(startup_32) movl %esi, %edx 1: #endif + /* Check if the C-bit position is correct when SEV is active */ + call startup32_check_sev_cbit + pushl $__KERNEL_CS pushl %eax @@ -871,6 +884,76 @@ 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) +#ifdef CONFIG_AMD_MEM_ENCRYPT + pushl %eax + pushl %ebx + pushl %ecx + pushl %edx + + /* Check for non-zero sev_status */ + movl rva(sev_status)(%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 */ + movl %eax, rva(sev_check_data)(%ebp) + movl %ebx, rva(sev_check_data+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, rva(sev_check_data)(%ebp) + jne 3f + cmpl %ebx, rva(sev_check_data+4)(%ebp) + jne 3f + + movl %edx, %cr0 /* Restore previous %cr0 */ + + jmp 4f + +3: /* Check failed - hlt the machine */ + hlt + jmp 3b + +4: + popl %edx + popl %ecx + popl %ebx + popl %eax +#endif + ret +SYM_FUNC_END(startup32_check_sev_cbit) + /* * Stack and heap for uncompression */ -- 2.30.1