Skip to contentSkip to conversation

KVM: SEV: Return INVALID_INPUT on SNP req/resp buffer access failure

PATCH Superseded Jacky Li 3 participants 11 messages Sep 10 – Sep 22

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jacky Li

author
#0 lore raw

PATCH 1/1 KVM: SEV: Return INVALID_INPUT on SNP req/resp buffer access failure

Currently, snp_handle_(ext_)guest_req() returns -EIO when kvm_{read/write/clear}_guest() fails while accessing guest-provided buffers. Returning -EIO causes KVM_RUN to exit to userspace, likely killing the VM.

Fix this by returning GHCB_HV_RESP_MALFORMED_INPUT with sub-error code GHCB_ERR_INVALID_INPUT to the guest and resuming the vCPU.

Per the GHCB specification, guest-provided GPA buffers that cannot be accessed by the hypervisor (e.g. private pages) should be treated as guest input errors. Because kvm_{read/write/clear}_guest() only returns -EFAULT on failure, treating this failure as an invalid input aligns with the definition of -EFAULT ("Bad address").

Returning GHCB_ERR_INVALID_INPUT also matches existing SNP handling in KVM, which already returns this error code for unaligned or overlapping buffers. It also aligns with other hypercall implementations in KVM (e.g. Hyper-V returning INVALID_HYPERCALL_INPUT on kvm_read_guest() failures in kvm_hv_flush_tlb()).

Performing upfront validation (e.g. via kvm_mem_is_private()) is avoided because it is prone to TOCTOU races with concurrent Page State Changes.

Fixes 88caf544c930 ("KVM: SEV: Provide support for SNP_GUEST_REQUEST NAE event")
Fixes 74458e4859d8 ("KVM: SEV: Provide support for SNP_EXTENDED_GUEST_REQUEST NAE event")
---
 arch/x86/kvm/svm/sev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +10 -6
index 5705723f1f41..d07562310519 100644
@@ -4228,8 +4228,10 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 
 	guard(mutex)(&sev->guest_req_mutex);
 
-	if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE))
-		return -EIO;
+	if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE)) {
+		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+		return 1;
+	}
 
 	data.gctx_paddr = __psp_pa(sev->snp_context);
 	data.req_paddr = __psp_pa(sev->guest_req_buf);
@@ -4244,8 +4246,10 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 	if (ret && !fw_err)
 		return ret;
 
-	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE))
-		return -EIO;
+	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE)) {
+		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+		return 1;
+	}
 
 	/* No action is requested *from KVM* if there was a firmware error. */
 	svm_vmgexit_no_action(svm, SNP_GUEST_ERR(0, fw_err));
@@ -4296,7 +4300,7 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r
 
 	if (kvm_read_guest(kvm, req_gpa + offsetof(struct snp_guest_msg_hdr, msg_type),
 			   &msg_type, 1))
-		return -EIO;
+		goto request_invalid;
 
 	/*
 	 * As per GHCB spec, requests of type MSG_REPORT_REQ also allow for
@@ -4336,7 +4340,7 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r
 		 * certificate table is terminated by 24-bytes of zeroes.
 		 */
 		if (data_npages && kvm_clear_guest(kvm, data_gpa, 24))
-			return -EIO;
+			goto request_invalid;
 	}
 
 	return snp_handle_guest_req(svm, req_gpa, resp_gpa);

---
base-commit: 50d05c7c76c96b90462f24debacca971d2e86713
change-id: 20260910-snp-invalid-input-5dbb6a408e7f

Best regards,

Signature · 1 line
Jacky Li <[email protected]>
#1 lore raw
Quoted 20 lines · Jacky Li

Currently, snp_handle_(ext_)guest_req() returns -EIO when kvm_{read/write/clear}_guest() fails while accessing guest-provided buffers. Returning -EIO causes KVM_RUN to exit to userspace, likely killing the VM.

Fix this by returning GHCB_HV_RESP_MALFORMED_INPUT with sub-error code GHCB_ERR_INVALID_INPUT to the guest and resuming the vCPU.

Per the GHCB specification, guest-provided GPA buffers that cannot be accessed by the hypervisor (e.g. private pages) should be treated as guest input errors. Because kvm_{read/write/clear}_guest() only returns -EFAULT on failure, treating this failure as an invalid input aligns with the definition of -EFAULT ("Bad address").

Returning GHCB_ERR_INVALID_INPUT also matches existing SNP handling in KVM, which already returns this error code for unaligned or overlapping buffers. It also aligns with other hypercall implementations in KVM (e.g. Hyper-V returning INVALID_HYPERCALL_INPUT on kvm_read_guest() failures in kvm_hv_flush_tlb()).

Quoted 3 lines

Performing upfront validation (e.g. via kvm_mem_is_private()) is avoided because it is prone to TOCTOU races with concurrent Page State Changes.

When stating what a patch does (or doesn't) do, phrase everything as commands. Passively describing the patch, as done above, is problematic as it's not clear if the changelog is talking about what the patch itself is (not) doing, or if it's talking about the side effects of the changes. Whereas this:

Don't try to validate guest-provide ahead of time, as such checks are prone to TOCTOU races, e.g. with Page State Changes, memslot updates, etc.

is more obviously talking about the patch.

Quoted 12 lines
Fixes 88caf544c930 ("KVM: SEV: Provide support for SNP_GUEST_REQUEST NAE event")
Fixes 74458e4859d8 ("KVM: SEV: Provide support for SNP_EXTENDED_GUEST_REQUEST NAE event")
---
 arch/x86/kvm/svm/sev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +0 -0
index 5705723f1f41..d07562310519 100644

arch/x86/kvm/svm/sev.c static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_ · line 4231

Quoted 6 lines
arch/x86/kvm/svm/sev.c +1 -2
@@ -4228,8 +4228,10 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 
 	guard(mutex)(&sev->guest_req_mutex);
 
-	if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE))
-		return -EIO;
+	if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE)) {

I don't love that userspace VMM goofs will bleed into the guest, but on the other hand, KVM already uses this pattern for Hyper-V hypercalls (and worse patterns for KVM-defined PV features), and practically speaking this is better behavior than returning -EIO. So I'm good with this.

author
#2 lore raw
Quoted 9 lines · Sean Christopherson

When stating what a patch does (or doesn't) do, phrase everything as commands. Passively describing the patch, as done above, is problematic as it's not clear if the changelog is talking about what the patch itself is (not) doing, or if it's talking about the side effects of the changes. Whereas this:

Don't try to validate guest-provide ahead of time, as such checks are prone to TOCTOU races, e.g. with Page State Changes, memslot updates, etc.

is more obviously talking about the patch.

Will do, thanks! I'll use your wording in v2, with s/guest-provide/ guest-provided buffers/ and reflowed to fit two lines:

Don't try to validate guest-provided buffers ahead of time, as such checks are prone to TOCTOU races (e.g. with Page State Changes, memslot updates).

Quoted 4 lines

I don't love that userspace VMM goofs will bleed into the guest, but on the other hand, KVM already uses this pattern for Hyper-V hypercalls (and worse patterns for KVM-defined PV features), and practically speaking this is better behavior than returning -EIO. So I'm good with this.

Agreed, it's not ideal, but KVM can't distinguish a bogus guest GPA from a VMM goof for now, and killing the VM is worse for the former.

#3 lore raw
In reply to Jacky Li
Quoted 20 lines · Jacky Li

Currently, snp_handle_(ext_)guest_req() returns -EIO when kvm_{read/write/clear}_guest() fails while accessing guest-provided buffers. Returning -EIO causes KVM_RUN to exit to userspace, likely killing the VM.

Fix this by returning GHCB_HV_RESP_MALFORMED_INPUT with sub-error code GHCB_ERR_INVALID_INPUT to the guest and resuming the vCPU.

Per the GHCB specification, guest-provided GPA buffers that cannot be accessed by the hypervisor (e.g. private pages) should be treated as guest input errors. Because kvm_{read/write/clear}_guest() only returns -EFAULT on failure, treating this failure as an invalid input aligns with the definition of -EFAULT ("Bad address").

Returning GHCB_ERR_INVALID_INPUT also matches existing SNP handling in KVM, which already returns this error code for unaligned or overlapping buffers. It also aligns with other hypercall implementations in KVM (e.g. Hyper-V returning INVALID_HYPERCALL_INPUT on kvm_read_guest() failures in kvm_hv_flush_tlb()).

Quoted 3 lines

Performing upfront validation (e.g. via kvm_mem_is_private()) is avoided because it is prone to TOCTOU races with concurrent Page State Changes.

I'm ok with this approach overall, but will we run into a sequence number problem now?

If the kvm_write_guest() in snp_handle_guest_req() fails, invalid input is going to be returned, but we will have successfully called SEV_CMD_SNP_GUEST_REQUEST. The sequence number will have advanced in the firmware, but I think the guest will not think that it has and not increment the sequence number causing subsequent requests to fail. At that point the guest will need to zero out the associated VMPCK used and move to the next one (there are a max of 4). If this continues happening the guest will eventually not be able to make guest requests anymore. But, I guess, if the kvm_write_guest() is failing, we're probably already in a bad situation, so maybe it is fine.

Thanks,
Tom

Quoted 62 lines
Fixes 88caf544c930 ("KVM: SEV: Provide support for SNP_GUEST_REQUEST NAE event")
Fixes 74458e4859d8 ("KVM: SEV: Provide support for SNP_EXTENDED_GUEST_REQUEST NAE event")
---
 arch/x86/kvm/svm/sev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/sev.c +10 -7
index 5705723f1f41..d07562310519 100644
@@ -4228,8 +4228,10 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 
 	guard(mutex)(&sev->guest_req_mutex);
 
-	if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE))
-		return -EIO;
+	if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE)) {
+		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+		return 1;
+	}
 
 	data.gctx_paddr = __psp_pa(sev->snp_context);
 	data.req_paddr = __psp_pa(sev->guest_req_buf);
@@ -4244,8 +4246,10 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 	if (ret && !fw_err)
 		return ret;
 
-	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE))
-		return -EIO;
+	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE)) {
+		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+		return 1;
+	}
 
 	/* No action is requested *from KVM* if there was a firmware error. */
 	svm_vmgexit_no_action(svm, SNP_GUEST_ERR(0, fw_err));
@@ -4296,7 +4300,7 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r
 
 	if (kvm_read_guest(kvm, req_gpa + offsetof(struct snp_guest_msg_hdr, msg_type),
 			   &msg_type, 1))
-		return -EIO;
+		goto request_invalid;
 
 	/*
 	 * As per GHCB spec, requests of type MSG_REPORT_REQ also allow for
@@ -4336,7 +4340,7 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r
 		 * certificate table is terminated by 24-bytes of zeroes.
 		 */
 		if (data_npages && kvm_clear_guest(kvm, data_gpa, 24))
-			return -EIO;
+			goto request_invalid;
 	}
 
 	return snp_handle_guest_req(svm, req_gpa, resp_gpa);
 
---

base-commit: 50d05c7c76c96b90462f24debacca971d2e86713
change-id: 20260910-snp-invalid-input-5dbb6a408e7f

Best regards,

#4 lore raw
Quoted 28 lines · Tom Lendacky
Quoted 20 lines · Jacky Li

Currently, snp_handle_(ext_)guest_req() returns -EIO when kvm_{read/write/clear}_guest() fails while accessing guest-provided buffers. Returning -EIO causes KVM_RUN to exit to userspace, likely killing the VM.

Fix this by returning GHCB_HV_RESP_MALFORMED_INPUT with sub-error code GHCB_ERR_INVALID_INPUT to the guest and resuming the vCPU.

Per the GHCB specification, guest-provided GPA buffers that cannot be accessed by the hypervisor (e.g. private pages) should be treated as guest input errors. Because kvm_{read/write/clear}_guest() only returns -EFAULT on failure, treating this failure as an invalid input aligns with the definition of -EFAULT ("Bad address").

Returning GHCB_ERR_INVALID_INPUT also matches existing SNP handling in KVM, which already returns this error code for unaligned or overlapping buffers. It also aligns with other hypercall implementations in KVM (e.g. Hyper-V returning INVALID_HYPERCALL_INPUT on kvm_read_guest() failures in kvm_hv_flush_tlb()).

Quoted 3 lines

Performing upfront validation (e.g. via kvm_mem_is_private()) is avoided because it is prone to TOCTOU races with concurrent Page State Changes.

I'm ok with this approach overall, but will we run into a sequence number problem now?

Quoted 10 lines

If the kvm_write_guest() in snp_handle_guest_req() fails, invalid input is going to be returned, but we will have successfully called SEV_CMD_SNP_GUEST_REQUEST. The sequence number will have advanced in the firmware, but I think the guest will not think that it has and not increment the sequence number causing subsequent requests to fail. At that point the guest will need to zero out the associated VMPCK used and move to the next one (there are a max of 4). If this continues happening the guest will eventually not be able to make guest requests anymore. But, I guess, if the kvm_write_guest() is failing, we're probably already in a bad situation, so maybe it is fine.

Oof, "fine" is definitely not ideal though.

Quoted 10 lines

arch/x86/kvm/svm/sev.c static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_ · line 4252

Quoted 10 lines
arch/x86/kvm/svm/sev.c +4 -2
@@ -4244,8 +4246,10 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 	if (ret && !fw_err)
 		return ret;
 
-	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE))
-		return -EIO;
+	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE)) {
+		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+		return 1;
+	}

What if we keep this one as -EIO (or better, change it to -EFAULT in a separate patch?), but add a comment explaning why KVM needs to exit to userspace in this particular case? That would be a good compromise; if the guest is attempting to access non-existent memory, the initial READ will fail, i.e. we still get most of the behavior Jacky wants. The only fatal case would be where either userspace really did screw up, or the guest managed to find read-only memory (though I would probably argue that's likely also a userspace bug?).

#5 lore raw
Quoted 46 lines · Sean Christopherson
Quoted 28 lines · Tom Lendacky
Quoted 20 lines · Jacky Li

Currently, snp_handle_(ext_)guest_req() returns -EIO when kvm_{read/write/clear}_guest() fails while accessing guest-provided buffers. Returning -EIO causes KVM_RUN to exit to userspace, likely killing the VM.

Fix this by returning GHCB_HV_RESP_MALFORMED_INPUT with sub-error code GHCB_ERR_INVALID_INPUT to the guest and resuming the vCPU.

Per the GHCB specification, guest-provided GPA buffers that cannot be accessed by the hypervisor (e.g. private pages) should be treated as guest input errors. Because kvm_{read/write/clear}_guest() only returns -EFAULT on failure, treating this failure as an invalid input aligns with the definition of -EFAULT ("Bad address").

Returning GHCB_ERR_INVALID_INPUT also matches existing SNP handling in KVM, which already returns this error code for unaligned or overlapping buffers. It also aligns with other hypercall implementations in KVM (e.g. Hyper-V returning INVALID_HYPERCALL_INPUT on kvm_read_guest() failures in kvm_hv_flush_tlb()).

Quoted 3 lines

Performing upfront validation (e.g. via kvm_mem_is_private()) is avoided because it is prone to TOCTOU races with concurrent Page State Changes.

I'm ok with this approach overall, but will we run into a sequence number problem now?

Quoted 10 lines

If the kvm_write_guest() in snp_handle_guest_req() fails, invalid input is going to be returned, but we will have successfully called SEV_CMD_SNP_GUEST_REQUEST. The sequence number will have advanced in the firmware, but I think the guest will not think that it has and not increment the sequence number causing subsequent requests to fail. At that point the guest will need to zero out the associated VMPCK used and move to the next one (there are a max of 4). If this continues happening the guest will eventually not be able to make guest requests anymore. But, I guess, if the kvm_write_guest() is failing, we're probably already in a bad situation, so maybe it is fine.

Oof, "fine" is definitely not ideal though.

Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/svm/sev.c +0 -0
@@ -4244,8 +4246,10 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 	if (ret && !fw_err)
 		return ret;
 
Quoted 14 lines
Quoted 6 lines

arch/x86/kvm/svm/sev.c static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_ · line 4252

Quoted 6 lines
arch/x86/kvm/svm/sev.c +4 -2
-	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE))
-		return -EIO;
+	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE)) {
+		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+		return 1;
+	}

What if we keep this one as -EIO (or better, change it to -EFAULT in a separate patch?), but add a comment explaning why KVM needs to exit to userspace in this particular case? That would be a good compromise; if the guest is attempting to access non-existent memory, the initial READ will fail, i.e. we still get most of the behavior Jacky wants. The only fatal case would be where either userspace really did screw up, or the guest managed to find read-only memory (though I would probably argue that's likely also a userspace bug?).

That sounds good to me.

Thanks,
Tom

author
#6 lore raw
Quoted 11 lines · Tom Lendacky
Quoted 7 lines · Sean Christopherson

What if we keep this one as -EIO (or better, change it to -EFAULT in a separate patch?), but add a comment explaning why KVM needs to exit to userspace in this particular case? That would be a good compromise; if the guest is attempting to access non-existent memory, the initial READ will fail, i.e. we still get most of the behavior Jacky wants. The only fatal case would be where either userspace really did screw up, or the guest managed to find read-only memory (though I would probably argue that's likely also a userspace bug?).

That sounds good to me.

Thinking through the sequence number and VMM bug concerns, I'd still argue that returning INVALID_INPUT on kvm_write_guest() failure is preferable to exiting to userspace:

1. req_gpa and resp_gpa are two independent guest inputs, so a guest can pass a valid req_gpa and a resp_gpa that isn't backed by any memslot to sail through the initial read and fail only on the write. Exiting to userspace on write failure therefore still lets a guest kill the VM at will.

2. Comparing the two outcomes after a kvm_write_guest() failure:

- Case 1 (exit to userspace with -EFAULT/-EIO): unrecoverable. The PSP's response is lost, and userspace can't rewind the PSP's sequence number, so retrying KVM_RUN would just fail that check. There is nothing userspace can do other than kill the VM.

- Case 2 (return INVALID_INPUT to the guest): the guest disables the VMPCK and moves on to the next one, as Tom mentioned. The worst case, once all four VMPCKs are exhausted, is that the guest can no longer issue guest requests, but the VM keeps running.

Neither is ideal, but Case 2 seems to be better: a localized loss of functionality instead of the entire VM dying over a single bad request. It also leaves the decision to the guest, which can carry on with whatever doesn't depend on attestation, wind down cleanly, and shut itself down on its own terms instead of being killed abruptly.

3. More generally, when KVM can't tell a guest error apart from a VMM bug at a hypercall boundary, reporting the error to the guest seems to be the safer
default: failing the guest request degrades one service while keeping the VM alive and debuggable, whereas exiting to userspace turns a single failed request into a dead VM, and hands an untrusted guest a way to terminate it.

And in the context of SNP the guest doesn't trust the VMM anyway, so a VMM that fails to deliver a response is indistinguishable from one that refuses to, and the guest already deals with that by disabling the VMPCK on a VMGEXIT error. Letting a host problem bleed into the guest is arguably "fine" here, since the guest is designed to cope with it regardless.

#7 lore raw
Quoted 13 lines · Jacky Li
Quoted 11 lines · Tom Lendacky
Quoted 7 lines · Sean Christopherson

What if we keep this one as -EIO (or better, change it to -EFAULT in a separate patch?), but add a comment explaning why KVM needs to exit to userspace in this particular case? That would be a good compromise; if the guest is attempting to access non-existent memory, the initial READ will fail, i.e. we still get most of the behavior Jacky wants. The only fatal case would be where either userspace really did screw up, or the guest managed to find read-only memory (though I would probably argue that's likely also a userspace bug?).

That sounds good to me.

Quoted 8 lines

Thinking through the sequence number and VMM bug concerns, I'd still argue that returning INVALID_INPUT on kvm_write_guest() failure is preferable to exiting to userspace:

1. req_gpa and resp_gpa are two independent guest inputs, so a guest can pass a valid req_gpa and a resp_gpa that isn't backed by any memslot to sail through the initial read and fail only on the write. Exiting to userspace on write failure therefore still lets a guest kill the VM at will.

Drat, I missed that.

Quoted 12 lines

2. Comparing the two outcomes after a kvm_write_guest() failure:

- Case 1 (exit to userspace with -EFAULT/-EIO): unrecoverable. The PSP's response is lost, and userspace can't rewind the PSP's sequence number, so retrying KVM_RUN would just fail that check. There is nothing userspace can do other than kill the VM.

- Case 2 (return INVALID_INPUT to the guest): the guest disables the VMPCK and moves on to the next one, as Tom mentioned. The worst case, once all four VMPCKs are exhausted, is that the guest can no longer issue guest requests, but the VM keeps running.

Quoted 5 lines

Neither is ideal, but Case 2 seems to be better: a localized loss of functionality instead of the entire VM dying over a single bad request. It also leaves the decision to the guest, which can carry on with whatever doesn't depend on attestation, wind down cleanly, and shut itself down on its own terms instead of being killed abruptly.

The problem is, I don't see how the guest can know when it needs to discard the VMPCK (post-request failures) versus when the VMPCK is still "fine" (pre-request errors).

Quoted 5 lines

3. More generally, when KVM can't tell a guest error apart from a VMM bug at a hypercall boundary, reporting the error to the guest seems to be the safer
default: failing the guest request degrades one service while keeping the VM alive and debuggable, whereas exiting to userspace turns a single failed request into a dead VM, and hands an untrusted guest a way to terminate it.

Well, ideally KVM never has to make a choice.

Quoted 5 lines

And in the context of SNP the guest doesn't trust the VMM anyway, so a VMM that fails to deliver a response is indistinguishable from one that refuses to, and the guest already deals with that by disabling the VMPCK on a VMGEXIT error. Letting a host problem bleed into the guest is arguably "fine" here, since the guest is designed to cope with it regardless.

FWIW, I'm not terribly concerned about bleeding host issues into the guest, I'm more concerned about ending up with deferred fatalities and a mess of an "ABI" between the guest and KVM with respect to handling failures.

What if we "tickle" the resp_gpa before doing the request? Similar to how CPUs probe bytes early in XSAVE/XRSTOR to avoid having to unwind later on. In this case, the response is restricted to a single page, so we only need to tickle a single byte. E.g.

arch/x86/kvm/svm/sev.c +9 -4
index 2da61843a6f2..b2e74a407c2b 100644
@@ -4207,6 +4207,7 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 	struct kvm *kvm = svm->vcpu.kvm;
 	struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
 	sev_ret_code fw_err = 0;
+	u8 tickle = 0;
 	int ret;
 
 	if (!is_sev_snp_guest(&svm->vcpu))
@@ -4219,6 +4220,11 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 		return 1;
 	}
 
+	if (kvm_write_guest(kvm, resp_gpa, &tickle, sizeof(tickle))) {
+		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+		return 1;
+	}
+
 	data.gctx_paddr = __psp_pa(sev->snp_context);
 	data.req_paddr = __psp_pa(sev->guest_req_buf);
 	data.res_paddr = __psp_pa(sev->guest_resp_buf);
@@ -4232,10 +4238,9 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
 	if (ret && !fw_err)
 		return ret;
 
-	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE)) {
-		svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
-		return 1;
-	}
+	/* Comment goes here. */
+	if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE))
+		return -EIO;
 
 	/* No action is requested *from KVM* if there was a firmware error. */
 	svm_vmgexit_no_action(svm, SNP_GUEST_ERR(0, fw_err));
author
#8 lore raw
Quoted 5 lines · Sean Christopherson

The problem is, I don't see how the guest can know when it needs to discard the VMPCK (post-request failures) versus when the VMPCK is still "fine" (pre-request errors).

Quoted 9 lines

FWIW, I'm not terribly concerned about bleeding host issues into the guest, I'm more concerned about ending up with deferred fatalities and a mess of an "ABI" between the guest and KVM with respect to handling failures.

What if we "tickle" the resp_gpa before doing the request? Similar to how CPUs probe bytes early in XSAVE/XRSTOR to avoid having to unwind later on. In this case, the response is restricted to a single page, so we only need to tickle a single byte. E.g.

Agree. I see your points and why this tickle idea is a great solution. Actually, building on that idea, what do you think about using kvm_vcpu_map() in the exact place that you did kvm_write_guest(kvm, resp_gpa, &tickle, sizeof(tickle)) instead (and unmapping afterwards)? A few reasons:

1. Like kvm_write_guest(), kvm_vcpu_map() also checks the existence and writability of the memslot via __gfn_to_hva_many(..., write=true) and faults in the host page as writable, plus it pins the page across the firmware command.

2. kvm_vcpu_map() has cleaner semantics of acquiring and holding the guest response page in KVM until the response is delivered, rather than doing a dummy write to test the waters. This is somewhat consistent with the GHCB page itself, which stays mapped until the response is filled in.

3. Pinning the page prevents a malicious guest from converting resp_gpa to private between the pre-check and kvm_write_guest() after the guest_memfd in-place conversion patch series [1] is merged. (This assumes a future guest_memfd implementation, so it's less convincing than the first two points. This was also my original TOCTOU concern. And once we have either pre-check, on upstream today the guest has no way to change the memslot state before kvm_write_guest(), so I'll also drop the original TOCTOU commit message.)

[1] https://lore.kernel.org/lkml/[email protected]/

#9 lore raw
Quoted 27 lines · Jacky Li
Quoted 5 lines · Sean Christopherson

The problem is, I don't see how the guest can know when it needs to discard the VMPCK (post-request failures) versus when the VMPCK is still "fine" (pre-request errors).

Quoted 9 lines

FWIW, I'm not terribly concerned about bleeding host issues into the guest, I'm more concerned about ending up with deferred fatalities and a mess of an "ABI" between the guest and KVM with respect to handling failures.

What if we "tickle" the resp_gpa before doing the request? Similar to how CPUs probe bytes early in XSAVE/XRSTOR to avoid having to unwind later on. In this case, the response is restricted to a single page, so we only need to tickle a single byte. E.g.

Agree. I see your points and why this tickle idea is a great solution. Actually, building on that idea, what do you think about using kvm_vcpu_map() in the exact place that you did kvm_write_guest(kvm, resp_gpa, &tickle, sizeof(tickle)) instead (and unmapping afterwards)? A few reasons:

1. Like kvm_write_guest(), kvm_vcpu_map() also checks the existence and writability of the memslot via __gfn_to_hva_many(..., write=true) and faults in the host page as writable, plus it pins the page across the firmware command.

Quoted 5 lines

2. kvm_vcpu_map() has cleaner semantics of acquiring and holding the guest response page in KVM until the response is delivered, rather than doing a dummy write to test the waters. This is somewhat consistent with the GHCB page itself, which stays mapped until the response is filled in.

No, kvm_vcpu_map() actually has far worse semantics. GUP pins the *page*, it does not pin the userspace mapping. I.e. kvm_vcpu_map() guarantees that KVM won't write to a freed page, but that's literally it. E.g. if userspace mprotect()'s the page to make it read-only, KVM will write to what is supposed to be a RO page. Or if userspace completely unmaps the page, KVM will write to soon-to-be-freed memory.

We're actively trying to remove kvm_vcpu_map() to replace all usage with "struct gfn_to_pfn_cache" APIs, as the gpc functionally is tied into mmu_notifiers, i.e. guards against things like mprotect(), munmap(), etc.

So if we wanted to do something other than a uaccess, a gpc would be the way to go. But I'm not convinced that's the right choice here. There is zero danger to the host beyond what already exists with private memory in general. I.e. if guest_memfd leaves a dangling userspace mapping, then the host is hosed regardless of what this code does. Using a gpc could guarantee the write goes through or fails early, but IMO that's not worth the extra complexity involved with using a gpc, because I'm not convinced that continuing on is the best outcome if the response GPA mapping is being concurrently modified.

Quoted 3 lines

3. Pinning the page prevents a malicious guest from converting resp_gpa to private between the pre-check and kvm_write_guest() after the guest_memfd in-place conversion patch series [1] is merged.

Who cares? The guest can only hurt itself by converting resp_gpa to private.

Quoted 7 lines

(This assumes a future guest_memfd implementation, so it's less convincing than the first two points. This was also my original TOCTOU concern. And once we have either pre-check, on upstream today the guest has no way to change the memslot state before kvm_write_guest(), so I'll also drop the original TOCTOU commit message.)

[1] https://lore.kernel.org/lkml/[email protected]/

author
#10 lore raw
Quoted 6 lines · Sean Christopherson

No, kvm_vcpu_map() actually has far worse semantics. GUP pins the *page*, it does not pin the userspace mapping. I.e. kvm_vcpu_map() guarantees that KVM won't write to a freed page, but that's literally it. E.g. if userspace mprotect()'s the page to make it read-only, KVM will write to what is supposed to be a RO page. Or if userspace completely unmaps the page, KVM will write to soon-to-be-freed memory.

Sorry for the confusion. I didn't mean writing to the response page through map.hva; I only meant calling kvm_vcpu_map() to validate and pin the page, while still using kvm_write_guest() to copy the response back:

arch/x86/kvm/svm/sev.c +0 -0
@@ -4231,19 +4231,25 @@ static int snp_handle_guest_req(struct

vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
if (kvm_read_guest(kvm, req_gpa, sev->guest_req_buf, PAGE_SIZE)) return -EIO;

+ CLASS(kvm_vcpu_map_local, map)(&svm->vcpu, gpa_to_gfn(resp_gpa));
+ if (map.ret) {
+ svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+ return 1;
+ }
+
data.gctx_paddr = __psp_pa(sev->snp_context); data.req_paddr = __psp_pa(sev->guest_req_buf); data.res_paddr = __psp_pa(sev->guest_resp_buf);

/*
* Firmware failures are propagated on to guest, but any other failure
* condition along the way should be reported to userspace. E.g. if
* the PSP is dead and commands are timing out.
*/
ret = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &fw_err); if (ret && !fw_err)
return ret;

if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE)) return -EIO;

Because the actual payload write still goes through kvm_write_guest() (uaccess) rather than map.hva, a concurrent mprotect(PROT_READ) or munmap() would still cause kvm_write_guest() to fail rather than overwriting a read-only page or soon-to-be-freed memory.

Quoted 1 line

Who cares? The guest can only hurt itself by converting resp_gpa to private.

The only reason we care from the hypervisor's perspective is that a post-command -EIO/-EFAULT exit causes the VMM to abort and looks like a hypervisor infrastructure failure rather than a guest error.

That said, this only matters if a malicious guest deliberately races a conversion against its own request. Given the complexity of a gpc and that kvm_vcpu_map() is being phased out, the 1-byte tickle write is much simpler and good enough to protect normal guests from burning their VMPCK or killing the VM over a bad resp_gpa.

I'll send a v2 with the tickle approach. Thanks!