Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4477139pxu; Tue, 20 Oct 2020 19:24:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw10ti1J9Jxk0+yH2ujJdOF+fV+yQtSg7OhAGgTJV60W9EwiCpc64E2tMaIrb1/RbnUFbkD X-Received: by 2002:a17:906:1e95:: with SMTP id e21mr1047609ejj.355.1603247041830; Tue, 20 Oct 2020 19:24:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603247041; cv=none; d=google.com; s=arc-20160816; b=Mw/Ap4VNZncXpKdjWOK7ShA0m7TaC/S87KbvGu4+Sfx+rIqs7h3zk3PGlDtc5DZSyE 5CVLrAovCdatt6iFoQr8fRcUO7sPsKeW07/m8sjnba6KwT0V+D9uztPRFZrlrI08GjGc h6c1E++LPc1ZligxspCxGXpTzASbbrXPaFSVvzrfyG6jIHrK/u1kYuC1trEZNrT9qt1q F3LHaZfq4elX0LLBiRC185qgr0ohPhMsQLtvXXTzIpOGeVF8A4SP6xLNc+Kwj5AFmX6N DfoX2ReJaJ0+g1VTpYfoDCRYcz9wEd/5Y7x8LScRHxIj5oEAev08qIMaFP3XquK69TWR 7pfg== 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=he3MptdV9jt3KokEEXdVX9mdsPZmV6rWydU/ncZPoZM=; b=StfJe/pPoolzunkh/AzaIaug5JxGYuQE9XrPP1B9F+jHWV5GW4gQ5pWMUBbTxYZhRz vrypA7cFEchVupDfF2l2UV/CEyG39ZX1tyj8OLBmCv+JckWfv6KPy0w6vu8Z0yMfgUeO UbZwzvn2ht5KfzYGvM/QE77Npq03wA+6XkVDFf8VLo4w3HoWDIj8j9WUiqWsBEDz5h8q Xep1F5RwMQOV5NUmuaYcHGoSLCdJiqPbsLOObYlAOYSg+s+mV8zKXwpHzfR7H6+k1FAy F2GCRlf5RPt+QIboxGrsIYY1b0+toif180q4Pmxyn0N/Lb6sbywqB6s4xom94i1jeRVC zvSQ== 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 w23si460067edu.226.2020.10.20.19.23.40; Tue, 20 Oct 2020 19:24:01 -0700 (PDT) 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 S2394315AbgJTMTL (ORCPT + 99 others); Tue, 20 Oct 2020 08:19:11 -0400 Received: from 8bytes.org ([81.169.241.247]:34898 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2394292AbgJTMTI (ORCPT ); Tue, 20 Oct 2020 08:19:08 -0400 Received: from cap.home.8bytes.org (p549add56.dip0.t-ipconnect.de [84.154.221.86]) (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 326512A9; Tue, 20 Oct 2020 14:19:07 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Kees Cook , Arvind Sankar , Martin Radev , Tom Lendacky , linux-kernel@vger.kernel.org Subject: [PATCH v2 4/5] x86/head/64: Check SEV encryption before switching to kernel page-table Date: Tue, 20 Oct 2020 14:18:55 +0200 Message-Id: <20201020121856.19427-5-joro@8bytes.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201020121856.19427-1-joro@8bytes.org> References: <20201020121856.19427-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 When SEV is enabled the kernel requests the C-Bit position again from the hypervisor to built its own page-table. Since the hypervisor is an untrusted source the C-bit position needs to be verified before the kernel page-table is used. Call the sev_verify_cbit() function before writing the CR3. Signed-off-by: Joerg Roedel --- arch/x86/kernel/head_64.S | 14 +++++++++++++- arch/x86/mm/mem_encrypt.c | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 7eb2a1c87969..c6f4562359a5 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -161,7 +161,18 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup early boot stage 4-/5-level pagetables. */ addq phys_base(%rip), %rax - movq %rax, %cr3 + + /* + * For SEV guests: Verify that the C-bit is correct. A malicious + * hypervisor could lie about the C-bit position to perform a ROP + * attack on the guest by writing to the unencrypted stack and wait for + * the next RET instruction. + */ + movq %rax, %rdi + call sev_verify_cbit + + /* Switch to new page-table */ + movq %rdi, %cr3 /* Ensure I am executing from virtual addresses */ movq $1f, %rax @@ -279,6 +290,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) SYM_CODE_END(secondary_startup_64) #include "verify_cpu.S" +#include "sev_verify_cbit.S" #ifdef CONFIG_HOTPLUG_CPU /* diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index ebb7edc8bc0a..bd9b62af2e3d 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -39,6 +39,7 @@ */ u64 sme_me_mask __section(.data) = 0; u64 sev_status __section(.data) = 0; +u64 sev_check_data __section(.data) = 0; EXPORT_SYMBOL(sme_me_mask); DEFINE_STATIC_KEY_FALSE(sev_enable_key); EXPORT_SYMBOL_GPL(sev_enable_key); -- 2.28.0