Skip to contentSkip to conversation

virt: sev: Prevent IV reuse in SNP guest driver

PATCH Superseded Peter Gonda 3 participants 13 messages Nov 3, 2022 – Nov 16, 2022 KVM

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 virt: sev: Prevent IV reuse in SNP guest driver

The ASP and an SNP guest use a series of AES-GCM keys called VMPCKs to communicate securely with each other. The IV to this scheme is a sequence number that both the ASP and the guest track. Currently this sequence number in a guest request must exactly match the sequence number tracked by the ASP. This means that if the guest sees an error from the host during a request it can only retry that exact request or disable the VMPCK to prevent an IV reuse. AES-GCM cannot tolerate IV reuse see:
https://csrc.nist.gov/csrc/media/projects/block-cipher-techniques/documents/bcm/comments/800-38-series-drafts/gcm/joux_comments.pdf

To handle userspace querying the cert_data length handle_guest_request()
now: saves the number of pages required by the host, retries the request without requesting the extended data, then returns the number of pages required.

Fixes fce96cf044308 ("virt: Add SEV-SNP guest driver")
Cc Dionna Glaze <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
Cc Haowen Bai <[email protected]>
Cc Yang Yingliang <[email protected]>
Cc Marc Orr <[email protected]>
Cc David Rientjes <[email protected]>
Cc Ashish Kalra <[email protected]>

---
Tested by placing each of the guest requests: attestation quote, extended attestation quote, and get key. Then tested the extended attestation quote certificate length querying.

V4
* As suggested by Dionna moved the extended request retry logic into the driver.
* Due to big change in patch dropped any reviewed-by tags.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 70 +++++++++++++++++++------
 1 file changed, 53 insertions(+), 17 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +53 -17
index f422f9c58ba79..7dd6337ebdd5b 100644
@@ -41,7 +41,7 @@ struct snp_guest_dev {
 	struct device *dev;
 	struct miscdevice misc;
 
-	void *certs_data;
+	u8 (*certs_data)[SEV_FW_BLOB_MAX_SIZE];
 	struct snp_guest_crypto *crypto;
 	struct snp_guest_msg *request, *response;
 	struct snp_secrets_page_layout *layout;
@@ -67,8 +67,27 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
 	return true;
 }
 
+/*
+ * If we receive an error from the host or ASP we have two options. We can
+ * either retry the exact same encrypted request or we can discontinue using the
+ * VMPCK.
+ *
+ * This is because in the current encryption scheme GHCB v2 uses AES-GCM to
+ * encrypt the requests. The IV for this scheme is the sequence number. GCM
+ * cannot tolerate IV reuse.
+ *
+ * The ASP FW v1.51 only increments the sequence numbers on a successful
+ * guest<->ASP back and forth and only accepts messages at its exact sequence
+ * number.
+ *
+ * So if we were to reuse the sequence number the encryption scheme is
+ * vulnerable. If we encrypt the sequence number for a fresh IV the ASP will
+ * reject our request.
+ */
 static void snp_disable_vmpck(struct snp_guest_dev *snp_dev)
 {
+	dev_alert(snp_dev->dev, "Disabling vmpck_id: %d to prevent IV reuse.\n",
+		  vmpck_id);
 	memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN);
 	snp_dev->vmpck = NULL;
 }
@@ -323,32 +342,49 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 
 	/* Call firmware to process the request */
 	rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+	/*
+	 * If the extended guest request fails due to having to small of a
+	 * certificate data buffer retry the same guest request without the
+	 * extended data request.
+	 */
+	if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+	    err == SNP_GUEST_REQ_INVALID_LEN) {
+		const unsigned int certs_npages = snp_dev->input.data_npages;
+
+		exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+		rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+		err = SNP_GUEST_REQ_INVALID_LEN;
+		snp_dev->input.data_npages = certs_npages;
+	}
+
 	if (fw_err)
 		*fw_err = err;
 
-	if (rc)
-		return rc;
+	if (rc) {
+		dev_alert(snp_dev->dev,
+			  "Detected error from ASP request. rc: %d, fw_err: %llu\n",
+			  rc, *fw_err);
+		goto disable_vmpck;
+	}
 
-	/*
-	 * The verify_and_dec_payload() will fail only if the hypervisor is
-	 * actively modifying the message header or corrupting the encrypted payload.
-	 * This hints that hypervisor is acting in a bad faith. Disable the VMPCK so that
-	 * the key cannot be used for any communication. The key is disabled to ensure
-	 * that AES-GCM does not use the same IV while encrypting the request payload.
-	 */
 	rc = verify_and_dec_payload(snp_dev, resp_buf, resp_sz);
 	if (rc) {
 		dev_alert(snp_dev->dev,
-			  "Detected unexpected decode failure, disabling the vmpck_id %d\n",
-			  vmpck_id);
-		snp_disable_vmpck(snp_dev);
-		return rc;
+			  "Detected unexpected decode failure from ASP. rc: %d\n",
+			  rc);
+		goto disable_vmpck;
 	}
 
 	/* Increment to new message sequence after payload decryption was successful. */
 	snp_inc_msg_seqno(snp_dev);
 
 	return 0;
+
+disable_vmpck:
+	snp_disable_vmpck(snp_dev);
+	return rc;
 }
 
 static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg)
@@ -676,7 +712,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	if (!snp_dev->response)
 		goto e_free_request;
 
-	snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE);
+	snp_dev->certs_data = alloc_shared_pages(dev, sizeof(*snp_dev->certs_data));
 	if (!snp_dev->certs_data)
 		goto e_free_response;
 
@@ -703,7 +739,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	return 0;
 
 e_free_cert_data:
-	free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE);
+	free_shared_pages(snp_dev->certs_data, sizeof(*snp_dev->certs_data));
 e_free_response:
 	free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg));
 e_free_request:
@@ -717,7 +753,7 @@ static int __exit sev_guest_remove(struct platform_device *pdev)
 {
 	struct snp_guest_dev *snp_dev = platform_get_drvdata(pdev);
 
-	free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE);
+	free_shared_pages(snp_dev->certs_data, sizeof(*snp_dev->certs_data));
 	free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg));
 	free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg));
 	deinit_crypto(snp_dev->crypto);
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
Reviewed-by on v4 1/1
#1 lore raw
Quoted 15 lines · Peter Gonda

The ASP and an SNP guest use a series of AES-GCM keys called VMPCKs to communicate securely with each other. The IV to this scheme is a sequence number that both the ASP and the guest track. Currently this sequence number in a guest request must exactly match the sequence number tracked by the ASP. This means that if the guest sees an error from the host during a request it can only retry that exact request or disable the VMPCK to prevent an IV reuse. AES-GCM cannot tolerate IV reuse see:
https://csrc.nist.gov/csrc/media/projects/block-cipher-techniques/documents/bcm/comments/800-38-series-drafts/gcm/joux_comments.pdf

To handle userspace querying the cert_data length handle_guest_request()
now: saves the number of pages required by the host, retries the request without requesting the extended data, then returns the number of pages required.

Quoted 14 lines
Fixes fce96cf044308 ("virt: Add SEV-SNP guest driver")
Cc Dionna Glaze <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
Cc Haowen Bai <[email protected]>
Cc Yang Yingliang <[email protected]>
Cc Marc Orr <[email protected]>
Cc David Rientjes <[email protected]>
Cc Ashish Kalra <[email protected]>
Quoted 11 lines

---
Tested by placing each of the guest requests: attestation quote, extended attestation quote, and get key. Then tested the extended attestation quote certificate length querying.

V4
* As suggested by Dionna moved the extended request retry logic into the driver.
* Due to big change in patch dropped any reviewed-by tags.

---

#2 lore raw
In reply to Peter Gonda
Quoted 1 line · Peter Gonda

The ASP and an SNP guest use a series of AES-GCM keys called VMPCKs to

ASP?

That must be the AMD Secure Processor or so but pls write it out.

Quoted 8 lines

communicate securely with each other. The IV to this scheme is a sequence number that both the ASP and the guest track. Currently this sequence number in a guest request must exactly match the sequence number tracked by the ASP. This means that if the guest sees an error from the host during a request it can only retry that exact request or disable the VMPCK to prevent an IV reuse. AES-GCM cannot tolerate IV reuse see:
https://csrc.nist.gov/csrc/media/projects/block-cipher-techniques/documents/bcm/comments/800-38-series-drafts/gcm/joux_comments.pdf

Right, how stable will that link be?

IOW, perhaps quote the paper name and authors so that people can find it on their own.

Quoted 2 lines

To handle userspace querying the cert_data length handle_guest_request()
now: saves the number of pages required by the host, retries the request

This needs to sound like this:

"In order to address this, save the number of pages ..."

IOW, as the docs say:

"Describe your changes in imperative mood, e.g. "make xyzzy do frotz" instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behaviour."

Quoted 4 lines

without requesting the extended data, then returns the number of pages required.

Fixes fce96cf044308 ("virt: Add SEV-SNP guest driver")

I'm guessing this needs to go to stable?

Quoted 41 lines
Cc Dionna Glaze <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
Cc Haowen Bai <[email protected]>
Cc Yang Yingliang <[email protected]>
Cc Marc Orr <[email protected]>
Cc David Rientjes <[email protected]>
Cc Ashish Kalra <[email protected]>

---
Tested by placing each of the guest requests: attestation quote, extended attestation quote, and get key. Then tested the extended attestation quote certificate length querying.

V4
* As suggested by Dionna moved the extended request retry logic into the driver.
* Due to big change in patch dropped any reviewed-by tags.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 70 +++++++++++++++++++------
 1 file changed, 53 insertions(+), 17 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
index f422f9c58ba79..7dd6337ebdd5b 100644
@@ -41,7 +41,7 @@ struct snp_guest_dev {
 	struct device *dev;
 	struct miscdevice misc;
 
-	void *certs_data;
+	u8 (*certs_data)[SEV_FW_BLOB_MAX_SIZE];
 	struct snp_guest_crypto *crypto;
 	struct snp_guest_msg *request, *response;
 	struct snp_secrets_page_layout *layout;

drivers/virt/coco/sev-guest/sev-guest.c static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) · line 71

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +2 -0
@@ -67,8 +67,27 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
 	return true;
 }
 
+/*
+ * If we receive an error from the host or ASP we have two options. We can

Please use passive voice in your commit message: no "we" or "I", etc, and describe your changes in imperative mood.

Bottom line is: personal pronouns are ambiguous in text, especially with so many parties/companies/etc developing the kernel so let's avoid them please.

Quoted 23 lines
drivers/virt/coco/sev-guest/sev-guest.c +17 -0
+ * either retry the exact same encrypted request or we can discontinue using the
+ * VMPCK.
+ *
+ * This is because in the current encryption scheme GHCB v2 uses AES-GCM to
+ * encrypt the requests. The IV for this scheme is the sequence number. GCM
+ * cannot tolerate IV reuse.
+ *
+ * The ASP FW v1.51 only increments the sequence numbers on a successful
+ * guest<->ASP back and forth and only accepts messages at its exact sequence
+ * number.
+ *
+ * So if we were to reuse the sequence number the encryption scheme is
+ * vulnerable. If we encrypt the sequence number for a fresh IV the ASP will
+ * reject our request.
+ */
 static void snp_disable_vmpck(struct snp_guest_dev *snp_dev)
 {
+	dev_alert(snp_dev->dev, "Disabling vmpck_id: %d to prevent IV reuse.\n",
+		  vmpck_id);
 	memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN);
 	snp_dev->vmpck = NULL;
 }

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 347

Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +3 -0
@@ -323,32 +342,49 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 
 	/* Call firmware to process the request */
 	rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+	/*
+	 * If the extended guest request fails due to having to small of a

"... too small... "

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 358

Quoted 11 lines
drivers/virt/coco/sev-guest/sev-guest.c +11 -0
+	 * certificate data buffer retry the same guest request without the
+	 * extended data request.
+	 */
+	if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+	    err == SNP_GUEST_REQ_INVALID_LEN) {
+		const unsigned int certs_npages = snp_dev->input.data_npages;
+
+		exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+		rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+		err = SNP_GUEST_REQ_INVALID_LEN;

Huh, why are we overwriting err here?

Quoted 46 lines
drivers/virt/coco/sev-guest/sev-guest.c +16 -13
+		snp_dev->input.data_npages = certs_npages;
+	}
+
 	if (fw_err)
 		*fw_err = err;
 
-	if (rc)
-		return rc;
+	if (rc) {
+		dev_alert(snp_dev->dev,
+			  "Detected error from ASP request. rc: %d, fw_err: %llu\n",
+			  rc, *fw_err);
+		goto disable_vmpck;
+	}
 
-	/*
-	 * The verify_and_dec_payload() will fail only if the hypervisor is
-	 * actively modifying the message header or corrupting the encrypted payload.
-	 * This hints that hypervisor is acting in a bad faith. Disable the VMPCK so that
-	 * the key cannot be used for any communication. The key is disabled to ensure
-	 * that AES-GCM does not use the same IV while encrypting the request payload.
-	 */
 	rc = verify_and_dec_payload(snp_dev, resp_buf, resp_sz);
 	if (rc) {
 		dev_alert(snp_dev->dev,
-			  "Detected unexpected decode failure, disabling the vmpck_id %d\n",
-			  vmpck_id);
-		snp_disable_vmpck(snp_dev);
-		return rc;
+			  "Detected unexpected decode failure from ASP. rc: %d\n",
+			  rc);
+		goto disable_vmpck;
 	}
 
 	/* Increment to new message sequence after payload decryption was successful. */
 	snp_inc_msg_seqno(snp_dev);
 
 	return 0;
+
+disable_vmpck:
+	snp_disable_vmpck(snp_dev);
+	return rc;
 }
 
 static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg)

drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 715

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
@@ -676,7 +712,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	if (!snp_dev->response)
 		goto e_free_request;
 
-	snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE);
+	snp_dev->certs_data = alloc_shared_pages(dev, sizeof(*snp_dev->certs_data));

What's that change for?

I went searching for that ->certs_data only ot realize that it is an array of size of SEV_FW_BLOB_MAX_SIZE elems.

Thx.

Signature · 6 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)
#3 lore raw
Quoted 27 lines · Borislav Petkov
Quoted 1 line · Peter Gonda

The ASP and an SNP guest use a series of AES-GCM keys called VMPCKs to

ASP?

That must be the AMD Secure Processor or so but pls write it out.

Quoted 8 lines

communicate securely with each other. The IV to this scheme is a sequence number that both the ASP and the guest track. Currently this sequence number in a guest request must exactly match the sequence number tracked by the ASP. This means that if the guest sees an error from the host during a request it can only retry that exact request or disable the VMPCK to prevent an IV reuse. AES-GCM cannot tolerate IV reuse see:
https://csrc.nist.gov/csrc/media/projects/block-cipher-techniques/documents/bcm/comments/800-38-series-drafts/gcm/joux_comments.pdf

Right, how stable will that link be?

IOW, perhaps quote the paper name and authors so that people can find it on their own.

Quoted 2 lines

To handle userspace querying the cert_data length handle_guest_request()
now: saves the number of pages required by the host, retries the request

This needs to sound like this:

Quoted 8 lines

"In order to address this, save the number of pages ..."

IOW, as the docs say:

"Describe your changes in imperative mood, e.g. "make xyzzy do frotz" instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behaviour."

Thanks for detailed review. Working on cleaning up the text for a V5.

Quoted 157 lines
Quoted 5 lines

without requesting the extended data, then returns the number of pages required.

Fixes fce96cf044308 ("virt: Add SEV-SNP guest driver")

I'm guessing this needs to go to stable?

Quoted 41 lines
Cc Dionna Glaze <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
Cc Haowen Bai <[email protected]>
Cc Yang Yingliang <[email protected]>
Cc Marc Orr <[email protected]>
Cc David Rientjes <[email protected]>
Cc Ashish Kalra <[email protected]>

---
Tested by placing each of the guest requests: attestation quote, extended attestation quote, and get key. Then tested the extended attestation quote certificate length querying.

V4
* As suggested by Dionna moved the extended request retry logic into the driver.
* Due to big change in patch dropped any reviewed-by tags.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 70 +++++++++++++++++++------
 1 file changed, 53 insertions(+), 17 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
index f422f9c58ba79..7dd6337ebdd5b 100644
@@ -41,7 +41,7 @@ struct snp_guest_dev {
      struct device *dev;
      struct miscdevice misc;
 
-     void *certs_data;
+     u8 (*certs_data)[SEV_FW_BLOB_MAX_SIZE];
      struct snp_guest_crypto *crypto;
      struct snp_guest_msg *request, *response;
      struct snp_secrets_page_layout *layout;

drivers/virt/coco/sev-guest/sev-guest.c static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) · line 71

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +2 -0
@@ -67,8 +67,27 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
      return true;
 }
 
+/*
+ * If we receive an error from the host or ASP we have two options. We can

Please use passive voice in your commit message: no "we" or "I", etc, and describe your changes in imperative mood.

Bottom line is: personal pronouns are ambiguous in text, especially with so many parties/companies/etc developing the kernel so let's avoid them please.

Quoted 23 lines
drivers/virt/coco/sev-guest/sev-guest.c +17 -0
+ * either retry the exact same encrypted request or we can discontinue using the
+ * VMPCK.
+ *
+ * This is because in the current encryption scheme GHCB v2 uses AES-GCM to
+ * encrypt the requests. The IV for this scheme is the sequence number. GCM
+ * cannot tolerate IV reuse.
+ *
+ * The ASP FW v1.51 only increments the sequence numbers on a successful
+ * guest<->ASP back and forth and only accepts messages at its exact sequence
+ * number.
+ *
+ * So if we were to reuse the sequence number the encryption scheme is
+ * vulnerable. If we encrypt the sequence number for a fresh IV the ASP will
+ * reject our request.
+ */
 static void snp_disable_vmpck(struct snp_guest_dev *snp_dev)
 {
+     dev_alert(snp_dev->dev, "Disabling vmpck_id: %d to prevent IV reuse.\n",
+               vmpck_id);
      memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN);
      snp_dev->vmpck = NULL;
 }

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 347

Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +3 -0
@@ -323,32 +342,49 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 
      /* Call firmware to process the request */
      rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+     /*
+      * If the extended guest request fails due to having to small of a

"... too small... "

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 358

Quoted 11 lines
drivers/virt/coco/sev-guest/sev-guest.c +11 -0
+      * certificate data buffer retry the same guest request without the
+      * extended data request.
+      */
+     if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+         err == SNP_GUEST_REQ_INVALID_LEN) {
+             const unsigned int certs_npages = snp_dev->input.data_npages;
+
+             exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+             rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+             err = SNP_GUEST_REQ_INVALID_LEN;

Huh, why are we overwriting err here?

Quoted 49 lines
drivers/virt/coco/sev-guest/sev-guest.c +16 -13
+             snp_dev->input.data_npages = certs_npages;
+     }
+
      if (fw_err)
              *fw_err = err;
 
-     if (rc)
-             return rc;
+     if (rc) {
+             dev_alert(snp_dev->dev,
+                       "Detected error from ASP request. rc: %d, fw_err: %llu\n",
+                       rc, *fw_err);
+             goto disable_vmpck;
+     }
 
-     /*
-      * The verify_and_dec_payload() will fail only if the hypervisor is
-      * actively modifying the message header or corrupting the encrypted payload.
-      * This hints that hypervisor is acting in a bad faith. Disable the VMPCK so that
-      * the key cannot be used for any communication. The key is disabled to ensure
-      * that AES-GCM does not use the same IV while encrypting the request payload.
-      */
      rc = verify_and_dec_payload(snp_dev, resp_buf, resp_sz);
      if (rc) {
              dev_alert(snp_dev->dev,
-                       "Detected unexpected decode failure, disabling the vmpck_id %d\n",
-                       vmpck_id);
-             snp_disable_vmpck(snp_dev);
-             return rc;
+                       "Detected unexpected decode failure from ASP. rc: %d\n",
+                       rc);
+             goto disable_vmpck;
      }
 
      /* Increment to new message sequence after payload decryption was successful. */
      snp_inc_msg_seqno(snp_dev);
 
      return 0;
+
+disable_vmpck:
+     snp_disable_vmpck(snp_dev);
+     return rc;
 }
 
 static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg)
@@ -676,7 +712,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
      if (!snp_dev->response)
              goto e_free_request;
 
Quoted 7 lines

drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 715

Quoted 2 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
-     snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE);
+     snp_dev->certs_data = alloc_shared_pages(dev, sizeof(*snp_dev->certs_data));

What's that change for?

I went searching for that ->certs_data only ot realize that it is an array of size of SEV_FW_BLOB_MAX_SIZE elems.

Do you want this change reverted? I liked the extra readability of the sizeof(*snp_dev->certs_data) but its unnecessary for this change.

Quoted 10 lines

Thx.

--
Regards/Gruss,
Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman (HRB 36809, AG Nürnberg)

#4 lore raw
Quoted 1 line · Peter Gonda

Thanks for detailed review. Working on cleaning up the text for a V5.

Yeah, and I have some questions in my reply which you haven't addressed...

Quoted 9 lines
Quoted 9 lines

drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 715

Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
@@ -676,7 +712,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
      if (!snp_dev->response)
              goto e_free_request;
 
-     snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE);
+     snp_dev->certs_data = alloc_shared_pages(dev, sizeof(*snp_dev->certs_data));

What's that change for?

Quoted 5 lines
Quoted 2 lines

I went searching for that ->certs_data only ot realize that it is an array of size of SEV_FW_BLOB_MAX_SIZE elems.

Do you want this change reverted? I liked the extra readability of the sizeof(*snp_dev->certs_data) but its unnecessary for this change.

Really?

I think using a define which is a SIZE define is better. Especially if you look at

	sizeof(*snp_dev->certs_data)

and wonder what type ->certs_data is.

And it's not like it'll go out of sync since it is an array of size, well, SEV_FW_BLOB_MAX_SIZE.

:-)

Thx.

Signature · 6 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)
#5 lore raw
Quoted 6 lines · Borislav Petkov
Quoted 1 line · Peter Gonda

Thanks for detailed review. Working on cleaning up the text for a V5.

Yeah, and I have some questions in my reply which you haven't addressed...

Ah I was just applying answers to those in the comments/commit description.

I have replied back to your first post.

Quoted 21 lines
Quoted 10 lines
Quoted 10 lines

drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 715

Quoted 7 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
@@ -676,7 +712,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
      if (!snp_dev->response)
              goto e_free_request;
 
-     snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE);
+     snp_dev->certs_data = alloc_shared_pages(dev, sizeof(*snp_dev->certs_data));

What's that change for?

Quoted 5 lines
Quoted 2 lines

I went searching for that ->certs_data only ot realize that it is an array of size of SEV_FW_BLOB_MAX_SIZE elems.

Do you want this change reverted? I liked the extra readability of the sizeof(*snp_dev->certs_data) but its unnecessary for this change.

Really?

I think using a define which is a SIZE define is better. Especially if you look at

Quoted 7 lines
        sizeof(*snp_dev->certs_data)

and wonder what type ->certs_data is.

And it's not like it'll go out of sync since it is an array of size, well, SEV_FW_BLOB_MAX_SIZE.

OK! I'll revert this part of the change.

#6 lore raw
In reply to Borislav Petkov
Quoted 7 lines · Borislav Petkov
Quoted 1 line · Peter Gonda

The ASP and an SNP guest use a series of AES-GCM keys called VMPCKs to

ASP?

That must be the AMD Secure Processor or so but pls write it out.

Yes I'll update to write out AMD Secure Processor (ASP). So that the acronym is clear through in each comment block.

Quoted 10 lines
Quoted 9 lines

communicate securely with each other. The IV to this scheme is a sequence number that both the ASP and the guest track. Currently this sequence number in a guest request must exactly match the sequence number tracked by the ASP. This means that if the guest sees an error from the host during a request it can only retry that exact request or disable the VMPCK to prevent an IV reuse. AES-GCM cannot tolerate IV reuse see:
https://csrc.nist.gov/csrc/media/projects/block-cipher-techniques/documents/bcm/comments/800-38-series-drafts/gcm/joux_comments.pdf

Quoted 4 lines

Right, how stable will that link be?

IOW, perhaps quote the paper name and authors so that people can find it on their own.

I will update to the title + authors.

Quoted 6 lines
Quoted 3 lines

To handle userspace querying the cert_data length handle_guest_request()
now: saves the number of pages required by the host, retries the request

This needs to sound like this:

Quoted 8 lines

"In order to address this, save the number of pages ..."

IOW, as the docs say:

"Describe your changes in imperative mood, e.g. "make xyzzy do frotz" instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behaviour."

Thanks I have updated the comments and description to this style for the next revision.

Quoted 7 lines
Quoted 5 lines

without requesting the extended data, then returns the number of pages required.

Fixes fce96cf044308 ("virt: Add SEV-SNP guest driver")

I'm guessing this needs to go to stable?

Yes this should go to stable.

Quoted 51 lines
Quoted 42 lines
Cc Dionna Glaze <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
Cc Haowen Bai <[email protected]>
Cc Yang Yingliang <[email protected]>
Cc Marc Orr <[email protected]>
Cc David Rientjes <[email protected]>
Cc Ashish Kalra <[email protected]>

---
Tested by placing each of the guest requests: attestation quote, extended attestation quote, and get key. Then tested the extended attestation quote certificate length querying.

V4
* As suggested by Dionna moved the extended request retry logic into the driver.
* Due to big change in patch dropped any reviewed-by tags.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 70 +++++++++++++++++++------
 1 file changed, 53 insertions(+), 17 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
index f422f9c58ba79..7dd6337ebdd5b 100644
@@ -41,7 +41,7 @@ struct snp_guest_dev {
      struct device *dev;
      struct miscdevice misc;
 
-     void *certs_data;
+     u8 (*certs_data)[SEV_FW_BLOB_MAX_SIZE];
      struct snp_guest_crypto *crypto;
      struct snp_guest_msg *request, *response;
      struct snp_secrets_page_layout *layout;

drivers/virt/coco/sev-guest/sev-guest.c static bool is_vmpck_empty(struct snp_guest_dev *snp_dev) · line 71

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +2 -0
@@ -67,8 +67,27 @@ static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
      return true;
 }
 
+/*
+ * If we receive an error from the host or ASP we have two options. We can

Please use passive voice in your commit message: no "we" or "I", etc, and describe your changes in imperative mood.

Quoted 3 lines

Bottom line is: personal pronouns are ambiguous in text, especially with so many parties/companies/etc developing the kernel so let's avoid them please.

I have removed the pronouns for the next revision.

Quoted 33 lines
Quoted 24 lines
drivers/virt/coco/sev-guest/sev-guest.c +17 -0
+ * either retry the exact same encrypted request or we can discontinue using the
+ * VMPCK.
+ *
+ * This is because in the current encryption scheme GHCB v2 uses AES-GCM to
+ * encrypt the requests. The IV for this scheme is the sequence number. GCM
+ * cannot tolerate IV reuse.
+ *
+ * The ASP FW v1.51 only increments the sequence numbers on a successful
+ * guest<->ASP back and forth and only accepts messages at its exact sequence
+ * number.
+ *
+ * So if we were to reuse the sequence number the encryption scheme is
+ * vulnerable. If we encrypt the sequence number for a fresh IV the ASP will
+ * reject our request.
+ */
 static void snp_disable_vmpck(struct snp_guest_dev *snp_dev)
 {
+     dev_alert(snp_dev->dev, "Disabling vmpck_id: %d to prevent IV reuse.\n",
+               vmpck_id);
      memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN);
      snp_dev->vmpck = NULL;
 }

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 347

Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +3 -0
@@ -323,32 +342,49 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 
      /* Call firmware to process the request */
      rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+     /*
+      * If the extended guest request fails due to having to small of a

"... too small... "

Quoted 13 lines

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 358

Quoted 11 lines
drivers/virt/coco/sev-guest/sev-guest.c +11 -0
+      * certificate data buffer retry the same guest request without the
+      * extended data request.
+      */
+     if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+         err == SNP_GUEST_REQ_INVALID_LEN) {
+             const unsigned int certs_npages = snp_dev->input.data_npages;
+
+             exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+             rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+             err = SNP_GUEST_REQ_INVALID_LEN;

Huh, why are we overwriting err here?

I have added a comment for the next revision.

We are overwriting err here so that userspace is alerted that they supplied a buffer too small.

Quoted 67 lines
Quoted 47 lines
drivers/virt/coco/sev-guest/sev-guest.c +16 -13
+             snp_dev->input.data_npages = certs_npages;
+     }
+
      if (fw_err)
              *fw_err = err;
 
-     if (rc)
-             return rc;
+     if (rc) {
+             dev_alert(snp_dev->dev,
+                       "Detected error from ASP request. rc: %d, fw_err: %llu\n",
+                       rc, *fw_err);
+             goto disable_vmpck;
+     }
 
-     /*
-      * The verify_and_dec_payload() will fail only if the hypervisor is
-      * actively modifying the message header or corrupting the encrypted payload.
-      * This hints that hypervisor is acting in a bad faith. Disable the VMPCK so that
-      * the key cannot be used for any communication. The key is disabled to ensure
-      * that AES-GCM does not use the same IV while encrypting the request payload.
-      */
      rc = verify_and_dec_payload(snp_dev, resp_buf, resp_sz);
      if (rc) {
              dev_alert(snp_dev->dev,
-                       "Detected unexpected decode failure, disabling the vmpck_id %d\n",
-                       vmpck_id);
-             snp_disable_vmpck(snp_dev);
-             return rc;
+                       "Detected unexpected decode failure from ASP. rc: %d\n",
+                       rc);
+             goto disable_vmpck;
      }
 
      /* Increment to new message sequence after payload decryption was successful. */
      snp_inc_msg_seqno(snp_dev);
 
      return 0;
+
+disable_vmpck:
+     snp_disable_vmpck(snp_dev);
+     return rc;
 }
 
 static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg)

drivers/virt/coco/sev-guest/sev-guest.c static int __init sev_guest_probe(struct platform_device *pdev) · line 715

Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
@@ -676,7 +712,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
      if (!snp_dev->response)
              goto e_free_request;
 
-     snp_dev->certs_data = alloc_shared_pages(dev, SEV_FW_BLOB_MAX_SIZE);
+     snp_dev->certs_data = alloc_shared_pages(dev, sizeof(*snp_dev->certs_data));

What's that change for?

I went searching for that ->certs_data only ot realize that it is an array of size of SEV_FW_BLOB_MAX_SIZE elems.

Thx.

--
Regards/Gruss,
Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman (HRB 36809, AG Nürnberg)

#7 lore raw
Quoted 14 lines · Peter Gonda
Quoted 5 lines
Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +5 -0
+      * certificate data buffer retry the same guest request without the
+      * extended data request.
+      */
+     if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+         err == SNP_GUEST_REQ_INVALID_LEN) {
Quoted 8 lines

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 358

Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +6 -0
+             const unsigned int certs_npages = snp_dev->input.data_npages;
+
+             exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+             rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+             err = SNP_GUEST_REQ_INVALID_LEN;

Huh, why are we overwriting err here?

Quoted 4 lines

I have added a comment for the next revision.

We are overwriting err here so that userspace is alerted that they supplied a buffer too small.

Sure but you're not checking rc either. What if that reissue fails for whatever other reason? -EIO for example...

Signature · 6 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)
#8 lore raw
Quoted 16 lines · Borislav Petkov
Quoted 14 lines · Peter Gonda
Quoted 13 lines

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 358

Quoted 11 lines
drivers/virt/coco/sev-guest/sev-guest.c +11 -0
+      * certificate data buffer retry the same guest request without the
+      * extended data request.
+      */
+     if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+         err == SNP_GUEST_REQ_INVALID_LEN) {
+             const unsigned int certs_npages = snp_dev->input.data_npages;
+
+             exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+             rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+             err = SNP_GUEST_REQ_INVALID_LEN;

Huh, why are we overwriting err here?

Quoted 7 lines
Quoted 4 lines

I have added a comment for the next revision.

We are overwriting err here so that userspace is alerted that they supplied a buffer too small.

Sure but you're not checking rc either. What if that reissue fails for whatever other reason? -EIO for example...

If we get any error here we have to wipe the VMPCK here so I thought this always override @err was OK.

I can update this to only override @err if after the secondary SVM_VMGEXIT_GUEST_REQUEST rc and err are OK. Thoughts?

Quoted 8 lines

--
Regards/Gruss,
Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman (HRB 36809, AG Nürnberg)

#9 lore raw
Quoted 19 lines · Peter Gonda
Quoted 18 lines · Borislav Petkov
Quoted 16 lines · Peter Gonda
Quoted 5 lines
Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +5 -0
+      * certificate data buffer retry the same guest request without the
+      * extended data request.
+      */
+     if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+         err == SNP_GUEST_REQ_INVALID_LEN) {
Quoted 8 lines

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 358

Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +6 -0
+             const unsigned int certs_npages = snp_dev->input.data_npages;
+
+             exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+             rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+             err = SNP_GUEST_REQ_INVALID_LEN;

Huh, why are we overwriting err here?

I have added a comment for the next revision.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

We are overwriting err here so that userspace is alerted that they supplied a buffer too small.

Sure but you're not checking rc either. What if that reissue fails for whatever other reason? -EIO for example...

If we get any error here we have to wipe the VMPCK here so I thought

More accurate to say that you will wipe the VMPCK, since the value of rc is checked a bit further down in the code and the -EIO (or other non-zero) will be result in a call to snp_disable_vmpck() and rc being propagated back to the user as an ioctl() return code.

Might be worth a comment above that second snp_issue_guest_request() explaining that.

Quoted 4 lines

this always override @err was OK.

I can update this to only override @err if after the secondary SVM_VMGEXIT_GUEST_REQUEST rc and err are OK. Thoughts?

I think it's ok to set it no matter what, but I don't have a strong opinion either way.

Thanks,
Tom

Quoted 9 lines
Quoted 9 lines

--
Regards/Gruss,
Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman (HRB 36809, AG Nürnberg)

#10 lore raw
Quoted 29 lines · Tom Lendacky
Quoted 19 lines · Peter Gonda
Quoted 18 lines · Borislav Petkov
Quoted 16 lines · Peter Gonda
Quoted 5 lines
Quoted 5 lines
drivers/virt/coco/sev-guest/sev-guest.c +5 -0
+      * certificate data buffer retry the same guest request without the
+      * extended data request.
+      */
+     if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
+         err == SNP_GUEST_REQ_INVALID_LEN) {
Quoted 8 lines

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 358

Quoted 6 lines
drivers/virt/coco/sev-guest/sev-guest.c +6 -0
+             const unsigned int certs_npages = snp_dev->input.data_npages;
+
+             exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+             rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
+
+             err = SNP_GUEST_REQ_INVALID_LEN;

Huh, why are we overwriting err here?

I have added a comment for the next revision.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

We are overwriting err here so that userspace is alerted that they supplied a buffer too small.

Sure but you're not checking rc either. What if that reissue fails for whatever other reason? -EIO for example...

If we get any error here we have to wipe the VMPCK here so I thought

Quoted 7 lines

More accurate to say that you will wipe the VMPCK, since the value of rc is checked a bit further down in the code and the -EIO (or other non-zero) will be result in a call to snp_disable_vmpck() and rc being propagated back to the user as an ioctl() return code.

Might be worth a comment above that second snp_issue_guest_request() explaining that.

I'll add a comment above the second snp_issue_guest_request(), good idea thanks.

I think another comment above the first snp_issue_guest_request() could help too. Saying once we call this function we either need to increment the sequence number or wipe the VMPCK to ensure the encryption scheme is safe.

Quoted 21 lines
Quoted 5 lines

this always override @err was OK.

I can update this to only override @err if after the secondary SVM_VMGEXIT_GUEST_REQUEST rc and err are OK. Thoughts?

I think it's ok to set it no matter what, but I don't have a strong opinion either way.

Thanks,
Tom

Quoted 9 lines
Quoted 9 lines

--
Regards/Gruss,
Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman (HRB 36809, AG Nürnberg)

#11 lore raw
Quoted 4 lines · Peter Gonda

I think another comment above the first snp_issue_guest_request() could help too. Saying once we call this function we either need to increment the sequence number or wipe the VMPCK to ensure the encryption scheme is safe.

And make that explicit pls:

/*
* If the extended guest request fails due to having to small of a
* certificate data buffer retry the same guest request without the
* extended data request...

... in order to not have to reuse the IV.

I have to admit, the flow in that function is still not optimal but I haven't stared at it long enough to have a better idea...

Thx.

Signature · 6 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
(HRB 36809, AG Nürnberg)
#12 lore raw
Quoted 14 lines · Borislav Petkov
Quoted 4 lines · Peter Gonda

I think another comment above the first snp_issue_guest_request() could help too. Saying once we call this function we either need to increment the sequence number or wipe the VMPCK to ensure the encryption scheme is safe.

And make that explicit pls:

/*
* If the extended guest request fails due to having to small of a
* certificate data buffer retry the same guest request without the
* extended data request...

Quoted 5 lines

... in order to not have to reuse the IV.

I have to admit, the flow in that function is still not optimal but I haven't stared at it long enough to have a better idea...

Thanks for all the feedback Tom and Boris. I've sent out a V5. I hope I've gotten the grammar correct in these comments.