From: Borislav Petkov <bp@alien8.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Borislav Petkov <bp@alien8.de>,
Dionna Glaze <dionnaglaze@google.com>,
Joerg Roedel <jroedel@suse.de>,
Michael Roth <michael.roth@amd.com>,
Nikunj A Dadhania <nikunj@amd.com>,
Peter Gonda <pgonda@google.com>,
Tom Lendacky <Thomas.Lendacky@amd.com>,
linux-coco@lists.linux.dev, x86@kernel.org
Subject: [PATCH -v3 09/11] virt/coco/sev-guest: Add throttling awareness
Date: Tue, 7 Mar 2023 20:24:47 +0100 [thread overview]
Message-ID: <20230307192449.24732-10-bp@alien8.de> (raw)
In-Reply-To: <20230307192449.24732-1-bp@alien8.de>
From: Dionna Glaze <dionnaglaze@google.com>
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. ]
Fixes: d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs")
Signed-off-by: Dionna Glaze <dionnaglaze@google.com>
Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
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 b8357d6ecd47..b63be696b776 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 d67884fb38c1..3f664ab277c4 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 81a53c31ff46..46f1a8d558b0 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 @@ static int __handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code,
* 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;
}
/*
--
2.35.1
next prev parent reply other threads:[~2023-03-07 19:25 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-07 19:24 [PATCH -v3 00/11] SEV: Cleanup sev-guest a bit and add throttling Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 01/11] crypto: ccp - Name -1 return value as SEV_RET_NO_FW_CALL Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 02/11] virt/coco/sev-guest: Check SEV_SNP attribute at probe time Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 03/11] virt/coco/sev-guest: Simplify extended guest request handling Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 04/11] virt/coco/sev-guest: Remove the disable_vmpck label in handle_guest_request() Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 05/11] virt/coco/sev-guest: Carve out the request issuing logic into a helper Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 06/11] virt/coco/sev-guest: Do some code style cleanups Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 07/11] virt/coco/sev-guest: Convert the sw_exit_info_2 checking to a switch-case Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 08/11] crypto: ccp: Get rid of __sev_platform_init_locked()'s local function pointer Borislav Petkov
2023-03-07 19:24 ` Borislav Petkov [this message]
2023-03-07 20:27 ` [PATCH -v3 09/11] virt/coco/sev-guest: Add throttling awareness Dionna Amalie Glaze
2023-03-07 19:24 ` [PATCH -v3 10/11] virt/coco/sev-guest: Double-buffer messages Borislav Petkov
2023-03-07 19:24 ` [PATCH -v3 11/11] x86/sev: Change snp_guest_issue_request()'s fw_err argument Borislav Petkov
2023-03-08 20:33 ` [PATCH -v3 00/11] SEV: Cleanup sev-guest a bit and add throttling Tom Lendacky
2023-03-09 12:12 ` Borislav Petkov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230307192449.24732-10-bp@alien8.de \
--to=bp@alien8.de \
--cc=Thomas.Lendacky@amd.com \
--cc=dionnaglaze@google.com \
--cc=jroedel@suse.de \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=michael.roth@amd.com \
--cc=nikunj@amd.com \
--cc=pgonda@google.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).