From patchwork Mon Mar 13 12:58:09 2023 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: 68825 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1169034wrd; Mon, 13 Mar 2023 06:00:12 -0700 (PDT) X-Google-Smtp-Source: AK7set89qaFJufi4n92aWCx9kJAylLgY/yEcr3IOxh5UOG6Q/gVA+fOIwENJjMLkYk1kwGJ8tccZ X-Received: by 2002:a17:90b:128f:b0:23b:53d1:21b5 with SMTP id fw15-20020a17090b128f00b0023b53d121b5mr4949883pjb.46.1678712411752; Mon, 13 Mar 2023 06:00:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678712411; cv=none; d=google.com; s=arc-20160816; b=aCjYNz4Xajl+RoZK7FnRzwJp4hS3zJ+DRmZEU7LbeN0FRNZ7gbsTdJGUaXSly1O1Uv Cn8nABnIx79rDiWnu9QIBMN63GtwVmBzbgyX+AERefkgDTTIVMdCt7L4XoAHXv30wMks caLL8HyTE8PZmMTs5MAnmxbtLk5Wff0py02xmIJJkfcMimVu6ApAVkTMY5jZv/6U0lFg qeFJfLJAcZZgNd7d9eKqff30YBqPKe6Pjd8TeXRkfWbPJTo1EvGTMbNjSdHz6hJnXhf6 mD1rYf6MZ3Ka24f9etRAh7uk60dlHG+2ChYzEMWvh/fFizKA2X47R6ybdazYzS8pCQCc q+xQ== 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=ezAgmmWZUX9qfEA4FY9WaiK/oZ2j4GAaTy3tsHaDIYU=; b=mxLcD4AekfDfWPo6jjac84h5u6+z+qoYK6ySQcNiW6k7aMh9QxrM1Tj0WkMa2Uu36F mNVnkFw2I6DHIMrR87M0m/cI0atHVnuts/EIFrrp8BI6U8q4SFoVguB+Z+rDEQ5S6aUi hpq7kcPWzpQSLfR2ZAYPiuBt9De7MjW5+LCk1CI8DH1pgGHhGCh356UFIHor2uEf7CAY d0Wm+4e4ycwVnEf/ZK8d+hI7O3Xg5O90TWQTIZtiMWoQIdhyyMSG7IyyrbUGe8HKnMlf u9H4KkavlJ1QMuWYgUaKD+bwqiDTA0bdL/HVZL+Wl205osybKPv+h3NfwAFkIGnpRn6o nwkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=3oxTCoK6; dkim=neutral (no key) header.i=@linutronix.de header.b=SZUz8pcg; 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 j1-20020a17090a3e0100b0023b3da3ffc2si5351448pjc.138.2023.03.13.05.59.56; Mon, 13 Mar 2023 06:00:11 -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=@linutronix.de header.s=2020 header.b=3oxTCoK6; dkim=neutral (no key) header.i=@linutronix.de header.b=SZUz8pcg; 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 S230360AbjCMM60 (ORCPT + 99 others); Mon, 13 Mar 2023 08:58:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230175AbjCMM6Q (ORCPT ); Mon, 13 Mar 2023 08:58:16 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DD8011140; Mon, 13 Mar 2023 05:58:12 -0700 (PDT) Date: Mon, 13 Mar 2023 12:58:09 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1678712290; 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=ezAgmmWZUX9qfEA4FY9WaiK/oZ2j4GAaTy3tsHaDIYU=; b=3oxTCoK6+Nfj77n/bBQc0TbTDYgoDDS/HTJHq9YlLDLvALjhP3qwdAm4IfP4wNbP9ojy0Q 4qHiXZv5+cGKCKx0tEei8Zwpwh01HshRPinecAY45MLxDv+5YIm64eg0FzEBP3WT5dcxLi SNOttLik4R7IbKPHjYQTRJ+0cMs3UaGCFkyu3t5zrZOGbMJmH0geYPCtQscARI0nMxj8+A JGSZP3UTqH5/0g8X2rDsEddiapVI5f3jMPBAD/6Xv5jtQ4d9tfB7rc6oLscBh+J/Oq6Czl bDIb/rJciKMSRaDYODDofjyexMDxmHrdzRgXbOGKH6bJcELKypjhcktWmmd2tw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1678712290; 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=ezAgmmWZUX9qfEA4FY9WaiK/oZ2j4GAaTy3tsHaDIYU=; b=SZUz8pcgF1u3DCDE48U+Wanovq7MZm26kOCOlLA/dZC0s0pQhQ0FUl27pqjmQKKD3wt6j6 ReWUS3JYHHvo7iAg== From: "tip-bot2 for Dionna Glaze" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] virt/coco/sev-guest: Add throttling awareness Cc: Dionna Glaze , "Borislav Petkov (AMD)" , Tom Lendacky , , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230214164638.1189804-2-dionnaglaze@google.com> References: <20230214164638.1189804-2-dionnaglaze@google.com> MIME-Version: 1.0 Message-ID: <167871228947.5837.4976586787776962885.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?1757826479878801726?= X-GMAIL-MSGID: =?utf-8?q?1760257546282136380?= The following commit has been merged into the x86/urgent branch of tip: Commit-ID: 72f7754dcf31c87c92c0c353dcf747814cc5ce10 Gitweb: https://git.kernel.org/tip/72f7754dcf31c87c92c0c353dcf747814cc5ce10 Author: Dionna Glaze AuthorDate: Thu, 16 Feb 2023 11:08:02 +01:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 13 Mar 2023 13:29:27 +01:00 virt/coco/sev-guest: Add throttling awareness A potentially malicious SEV guest can constantly hammer the hypervisor using this driver to send down requests and thus prevent or at least considerably hinder other guests from issuing requests to the secure processor which is a shared platform resource. Therefore, the host is permitted and encouraged to throttle such guest requests. Add the capability to handle the case when the hypervisor throttles excessive numbers of requests issued by the guest. Otherwise, the VM platform communication key will be disabled, preventing the guest from attesting itself. Realistically speaking, a well-behaved guest should not even care about throttling. During its lifetime, it would end up issuing a handful of requests which the hardware can easily handle. This is more to address the case of a malicious guest. Such guest should get throttled and if its VMPCK gets disabled, then that's its own wrongdoing and perhaps that guest even deserves it. To the implementation: the hypervisor signals with SNP_GUEST_REQ_ERR_BUSY that the guest requests should be throttled. That error code is returned in the upper 32-bit half of exitinfo2 and this is part of the GHCB spec v2. So the guest is given a throttling period of 1 minute in which it retries the request every 2 seconds. This is a good default but if it turns out to not pan out in practice, it can be tweaked later. For safety, since the encryption algorithm in GHCBv2 is AES_GCM, control must remain in the kernel to complete the request with the current sequence number. Returning without finishing the request allows the guest to make another request but with different message contents. This is IV reuse, and breaks cryptographic protections. [ bp: - Rewrite commit message and do a simplified version. - The stable tags are supposed to denote that a cleanup should go upfront before backporting this so that any future fixes to this can preserve the sanity of the backporter(s). ] Fixes: d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs") Signed-off-by: Dionna Glaze Co-developed-by: Borislav Petkov (AMD) Signed-off-by: Borislav Petkov (AMD) Reviewed-by: Tom Lendacky Cc: # d6fd48eff750 ("virt/coco/sev-guest: Check SEV_SNP attribute at probe time") Cc: # 970ab823743f (" virt/coco/sev-guest: Simplify extended guest request handling") Cc: # c5a338274bdb ("virt/coco/sev-guest: Remove the disable_vmpck label in handle_guest_request()") Cc: # 0fdb6cc7c89c ("virt/coco/sev-guest: Carve out the request issuing logic into a helper") Cc: # d25bae7dc7b0 ("virt/coco/sev-guest: Do some code style cleanups") Cc: # fa4ae42cc60a ("virt/coco/sev-guest: Convert the sw_exit_info_2 checking to a switch-case") Link: https://lore.kernel.org/r/20230214164638.1189804-2-dionnaglaze@google.com --- arch/x86/include/asm/sev-common.h | 3 ++- arch/x86/kernel/sev.c | 4 ++++ drivers/virt/coco/sev-guest/sev-guest.c | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index b8357d6..b63be69 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -128,8 +128,9 @@ struct snp_psc_desc { struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY]; } __packed; -/* Guest message request error code */ +/* Guest message request error codes */ #define SNP_GUEST_REQ_INVALID_LEN BIT_ULL(32) +#define SNP_GUEST_REQ_ERR_BUSY BIT_ULL(33) #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index d67884f..3f664ab 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2214,6 +2214,10 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned case 0: break; + case SNP_GUEST_REQ_ERR_BUSY: + ret = -EAGAIN; + break; + case SNP_GUEST_REQ_INVALID_LEN: /* Number of expected pages are returned in RBX */ if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) { diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c index 81a53c3..46f1a8d 100644 --- a/drivers/virt/coco/sev-guest/sev-guest.c +++ b/drivers/virt/coco/sev-guest/sev-guest.c @@ -31,6 +31,9 @@ #define AAD_LEN 48 #define MSG_HDR_VER 1 +#define SNP_REQ_MAX_RETRY_DURATION (60*HZ) +#define SNP_REQ_RETRY_DELAY (2*HZ) + struct snp_guest_crypto { struct crypto_aead *tfm; u8 *iv, *authtag; @@ -320,7 +323,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8 static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, __u64 *fw_err) { - unsigned long err, override_err = 0; + unsigned long err = 0xff, override_err = 0; + unsigned long req_start = jiffies; unsigned int override_npages = 0; int rc; @@ -360,6 +364,19 @@ retry_request: * user as an ioctl() return code. */ goto retry_request; + + /* + * The host may return SNP_GUEST_REQ_ERR_EBUSY if the request has been + * throttled. Retry in the driver to avoid returning and reusing the + * message sequence number on a different message. + */ + case -EAGAIN: + if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) { + rc = -ETIMEDOUT; + break; + } + schedule_timeout_killable(SNP_REQ_RETRY_DELAY); + goto retry_request; } /*