Skip to contentSkip to conversation

KVM: SEV: Reject non-positive effective lengths during LAUNCH_UPDATE

PATCH Applied Sean Christopherson 2 participants 3 messages Sep 19, 2025 – Sep 24, 2025

Subsystems (4)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH v2 1/1 KVM: SEV: Reject non-positive effective lengths during LAUNCH_UPDATE

Check for an invalid length during LAUNCH_UPDATE at the start of snp_launch_update() instead of subtly relying on kvm_gmem_populate() to detect the bad state. Code that directly handles userspace input absolutely should sanitize those inputs; failure to do so is asking for bugs where KVM consumes an invalid "npages".

Keep the check in gmem, but wrap it in a WARN to flag any bad usage by the caller.

Note, this is technically an ABI change as KVM would previously allow a length of '0'. But allowing a length of '0' is nonsensical and creates pointless conundrums in KVM. E.g. an empty range is arguably neither private nor shared, but LAUNCH_UPDATE will fail if the starting gpa can't be made private. In practice, no known or well-behaved VMM passes a length of '0'.

Note #2, the PAGE_ALIGNED(params.len) check ensures that lengths between 1 and 4095 (inclusive) are also rejected, i.e. that KVM won't end up with npages=0 when doing "npages = params.len / PAGE_SIZE".

Cc Thomas Lendacky <[email protected]>
Cc Michael Roth <[email protected]>

---

v2: Check params.len right away. [Tom]

v1: https://lore.kernel.org/all/[email protected]

 arch/x86/kvm/svm/sev.c | 2 +-
 virt/kvm/guest_memfd.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +1 -1
index cce48fff2e6c..31b3e128e521 100644
@@ -2370,7 +2370,7 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	pr_debug("%s: GFN start 0x%llx length 0x%llx type %d flags %d\n", __func__,
 		 params.gfn_start, params.len, params.type, params.flags);
 
-	if (!PAGE_ALIGNED(params.len) || params.flags ||
+	if (!params.len || !PAGE_ALIGNED(params.len) || params.flags ||
 	    (params.type != KVM_SEV_SNP_PAGE_TYPE_NORMAL &&
 	     params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO &&
 	     params.type != KVM_SEV_SNP_PAGE_TYPE_UNMEASURED &&
virt/kvm/guest_memfd.c +2 -1
index 08a6bc7d25b6..1d323ca178cb 100644
@@ -716,7 +716,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
 	long i;
 
 	lockdep_assert_held(&kvm->slots_lock);
-	if (npages < 0)
+
+	if (WARN_ON_ONCE(npages <= 0))
 		return -EINVAL;
 
 	slot = gfn_to_memslot(kvm, start_gfn);

base-commit: c8fbf7ceb2ae3f64b0c377c8c21f6df577a13eb4

Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 16 lines · Sean Christopherson

Check for an invalid length during LAUNCH_UPDATE at the start of snp_launch_update() instead of subtly relying on kvm_gmem_populate() to detect the bad state. Code that directly handles userspace input absolutely should sanitize those inputs; failure to do so is asking for bugs where KVM consumes an invalid "npages".

Keep the check in gmem, but wrap it in a WARN to flag any bad usage by the caller.

Note, this is technically an ABI change as KVM would previously allow a length of '0'. But allowing a length of '0' is nonsensical and creates pointless conundrums in KVM. E.g. an empty range is arguably neither private nor shared, but LAUNCH_UPDATE will fail if the starting gpa can't be made private. In practice, no known or well-behaved VMM passes a length of '0'.

Quoted 7 lines

Note #2, the PAGE_ALIGNED(params.len) check ensures that lengths between 1 and 4095 (inclusive) are also rejected, i.e. that KVM won't end up with npages=0 when doing "npages = params.len / PAGE_SIZE".

Cc Thomas Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
Quoted 39 lines

---

v2: Check params.len right away. [Tom]

v1: https://lore.kernel.org/all/[email protected]

 arch/x86/kvm/svm/sev.c | 2 +-
 virt/kvm/guest_memfd.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +1 -1
index cce48fff2e6c..31b3e128e521 100644
@@ -2370,7 +2370,7 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	pr_debug("%s: GFN start 0x%llx length 0x%llx type %d flags %d\n", __func__,
 		 params.gfn_start, params.len, params.type, params.flags);
 
-	if (!PAGE_ALIGNED(params.len) || params.flags ||
+	if (!params.len || !PAGE_ALIGNED(params.len) || params.flags ||
 	    (params.type != KVM_SEV_SNP_PAGE_TYPE_NORMAL &&
 	     params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO &&
 	     params.type != KVM_SEV_SNP_PAGE_TYPE_UNMEASURED &&
virt/kvm/guest_memfd.c +2 -1
index 08a6bc7d25b6..1d323ca178cb 100644
@@ -716,7 +716,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
 	long i;
 
 	lockdep_assert_held(&kvm->slots_lock);
-	if (npages < 0)
+
+	if (WARN_ON_ONCE(npages <= 0))
 		return -EINVAL;
 
 	slot = gfn_to_memslot(kvm, start_gfn);

base-commit: c8fbf7ceb2ae3f64b0c377c8c21f6df577a13eb4

author maintainer Applied
#2 lore raw
In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

Check for an invalid length during LAUNCH_UPDATE at the start of snp_launch_update() instead of subtly relying on kvm_gmem_populate() to detect the bad state. Code that directly handles userspace input absolutely should sanitize those inputs; failure to do so is asking for bugs where KVM consumes an invalid "npages".

Keep the check in gmem, but wrap it in a WARN to flag any bad usage by the caller.

[...]

Applied to kvm-x86 svm, thanks!

[1/1] KVM: SEV: Reject non-positive effective lengths during LAUNCH_UPDATE

      https://github.com/kvm-x86/linux/commit/5b66e335ead6

--
https://github.com/kvm-x86/linux/tree/next