Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5721443pxv; Wed, 21 Jul 2021 12:15:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxvq/g2jM9w8whXCahEKrEYxuCF63+kXCza5dRBlSSCZa74MmrJQj9natNHO54ZK/ILNAS4 X-Received: by 2002:a05:6402:1396:: with SMTP id b22mr23688070edv.380.1626894939431; Wed, 21 Jul 2021 12:15:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626894939; cv=none; d=google.com; s=arc-20160816; b=Bow2r+xjnVBq91/L66PItJG88eqeTMKf1Zsm7aLP2dyyNpE0BBsfsm22ITO7cVE79S 8UEHZhlu+EdjyQRwQci/spDAhPVMjXaoHP7GxfmzilXtVH/McmRDxwLKce6uBzSMctxB f1uOKev1VUzPPPjqGefe3Ag9a6MB9d684c9OuL3ePNoAXsCIOcPJ5ONBcg4mZE4t8nw0 zYoY88YtLoGjdwEC9mvjUzB0PslpBUS7Y5fP4es+VTPwmuHI5/tfzch/HdrK05LUnYO9 6UIbwVVD8x70S0ArR+B9A360Mz9XvGe1NavlA3UTkmIFqhzzL7moR5GZ3IUMcg3YA2kD g1ag== 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=0uzBEdpXa6Z/oRg1RzIM+0/HtzUFnFl5/Bc/dO45Z/M=; b=TSjqLjp2kJ95KL6b58r3x/70jSfezOH6r444VCpSe5CpXv8PUSFirpiS8MFjoccFGD 0OzlZ8r5dLGMdm04xTqYaXVKg5Rx152xFY3rntN6ZxtgKrlf/doVyDVUhxUQ6BVrdhsW f/jg1dp33AOOZ1eL+a6QTnWD1SQKxneY2G/KCg8EeYoO9REiJB0NVmHIjpcC0su7uWqQ 0oIFayrhEnI6F0BUOK6OnGdb6kILy7EFgqL77f2TU0IbG073m92qqp0mg+tm8MrxYfVy Qz2GehSb31hZJ4Y34yMGJDO3HwH/h7T7LtT5hUclI5cLOedV01cEa6XM5Pm5Ux/64eti 8C7w== 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 dm8si5702303edb.342.2021.07.21.12.15.15; Wed, 21 Jul 2021 12:15:39 -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 S239193AbhGUNk2 (ORCPT + 99 others); Wed, 21 Jul 2021 09:40:28 -0400 Received: from 8bytes.org ([81.169.241.247]:43236 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239204AbhGUNkH (ORCPT ); Wed, 21 Jul 2021 09:40:07 -0400 Received: from cap.home.8bytes.org (p4ff2b1ea.dip0.t-ipconnect.de [79.242.177.234]) (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 C04AC9AD; Wed, 21 Jul 2021 16:20:27 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org, Eric Biederman Cc: kexec@lists.infradead.org, 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 , Joerg Roedel , linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH 05/12] x86/sev: Use GHCB protocol version 2 if supported Date: Wed, 21 Jul 2021 16:20:08 +0200 Message-Id: <20210721142015.1401-6-joro@8bytes.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210721142015.1401-1-joro@8bytes.org> References: <20210721142015.1401-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 supports GHCB version 2 and use it if available. Signed-off-by: Joerg Roedel --- arch/x86/boot/compressed/sev.c | 10 ++++++++-- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/kernel/sev-shared.c | 17 ++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 101e08c67296..7f8416f76be7 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -119,16 +119,22 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, /* Include code for early handlers */ #include "../../kernel/sev-shared.c" +static unsigned int ghcb_protocol; + static u64 sev_get_ghcb_proto_ver(void) { - return GHCB_PROTOCOL_MAX; + return ghcb_protocol; } static bool early_setup_sev_es(void) { - if (!sev_es_negotiate_protocol(NULL)) + struct sev_ghcb_protocol_info info; + + if (!sev_es_negotiate_protocol(&info)) sev_es_terminate(GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED); + ghcb_protocol = info.vm_proto; + if (set_page_decrypted((unsigned long)&boot_ghcb_page)) return false; diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index fa5cd05d3b5b..134a7c9d91b6 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -12,8 +12,8 @@ #include #include -#define GHCB_PROTO_OUR 0x0001UL -#define GHCB_PROTOCOL_MAX 1ULL +#define GHCB_PROTOCOL_MIN 1ULL +#define GHCB_PROTOCOL_MAX 2ULL #define GHCB_DEFAULT_USAGE 0ULL #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); } diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 36eaac2773ed..40a1ca81bdb8 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -61,6 +61,7 @@ static void __noreturn sev_es_terminate(unsigned int reason) static bool sev_es_negotiate_protocol(struct sev_ghcb_protocol_info *info) { + unsigned int protocol; u64 val; /* Do the GHCB protocol version negotiation */ @@ -71,14 +72,24 @@ static bool sev_es_negotiate_protocol(struct sev_ghcb_protocol_info *info) if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP) return false; - if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTO_OUR || - GHCB_MSR_PROTO_MIN(val) > GHCB_PROTO_OUR) + /* Sanity check untrusted input */ + if (GHCB_MSR_PROTO_MIN(val) > GHCB_MSR_PROTO_MAX(val)) + return false; + + /* Use maximum supported protocol version */ + protocol = min_t(unsigned int, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX); + + /* + * Hypervisor does not support any protocol version required for this + * kernel. + */ + if (protocol < GHCB_MSR_PROTO_MIN(val)) return false; if (info) { info->hv_proto_min = GHCB_MSR_PROTO_MIN(val); info->hv_proto_max = GHCB_MSR_PROTO_MAX(val); - info->vm_proto = GHCB_PROTO_OUR; + info->vm_proto = protocol; } return true; -- 2.31.1