* [PATCH v2] KVM: SEV: Return INVALID_INPUT on SNP req/resp buffer access failure
@ 2026-09-23 20:18 Jacky Li
0 siblings, 0 replies; only message in thread
From: Jacky Li @ 2026-09-23 20:18 UTC (permalink / raw)
To: kvm
Cc: Sean Christopherson, Paolo Bonzini, Tom Lendacky, Michael Roth,
Ashish Kalra, Jacob Xu, Supraja Sridhara, linux-coco,
linux-kernel, Jacky Li
Currently, snp_handle_(ext_)guest_req() returns -EIO when
kvm_{read/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()).
Additionally, tickle the response buffer before issuing the PSP command
so that an invalid resp_gpa fails upfront with GHCB_ERR_INVALID_INPUT.
Do not return an error to the guest if the post-firmware write fails, as
the firmware has already incremented the VMPCK sequence number. Exiting
to userspace avoids creating an ambiguous failure where the guest has no
way to know whether to advance its VMPCK sequence number.
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")
Signed-off-by: Jacky Li <jackyli@google.com>
---
v2:
- "Tickle" resp_gpa before issuing the PSP command. [Sean]
- Keep post-command write failure as -EIO; returning INVALID_INPUT
creates an ambiguous ABI on whether VMPCK was consumed. [Tom, Sean]
v1: https://lore.kernel.org/all/20260910-snp-invalid-input-v1-1-fb2e03da614b@google.com
---
arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++------
1 file changed, 26 insertions(+), 6 deletions(-)
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 5705723f1f41..37a2a2677d3a 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -4221,6 +4221,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))
@@ -4228,8 +4229,15 @@ 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;
+ }
+
+ 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);
@@ -4244,6 +4252,14 @@ static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_
if (ret && !fw_err)
return ret;
+ /*
+ * Exit to userspace if writing the response fails. The buffer was already
+ * tickled, so failure here indicates the mapping changed in flight.
+ *
+ * Do not return an error to the guest because firmware already incremented
+ * the VMPCK sequence number. Exiting to userspace avoids creating an
+ * ambiguous failure ABI for the guest.
+ */
if (kvm_write_guest(kvm, resp_gpa, sev->guest_resp_buf, PAGE_SIZE))
return -EIO;
@@ -4295,8 +4311,10 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r
return -EINVAL;
if (kvm_read_guest(kvm, req_gpa + offsetof(struct snp_guest_msg_hdr, msg_type),
- &msg_type, 1))
- return -EIO;
+ &msg_type, 1)) {
+ svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+ return 1;
+ }
/*
* As per GHCB spec, requests of type MSG_REPORT_REQ also allow for
@@ -4335,8 +4353,10 @@ static int snp_handle_ext_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t r
* As per GHCB spec (see "SNP Extended Guest Request"), the
* certificate table is terminated by 24-bytes of zeroes.
*/
- if (data_npages && kvm_clear_guest(kvm, data_gpa, 24))
- return -EIO;
+ if (data_npages && kvm_clear_guest(kvm, data_gpa, 24)) {
+ svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+ return 1;
+ }
}
return snp_handle_guest_req(svm, req_gpa, resp_gpa);
---
base-commit: 50d05c7c76c96b90462f24debacca971d2e86713
change-id: 20260910-snp-invalid-input-5dbb6a408e7f
Best regards,
--
Jacky Li <jackyli@google.com>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-09-23 20:18 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-23 20:18 [PATCH v2] KVM: SEV: Return INVALID_INPUT on SNP req/resp buffer access failure Jacky Li
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®