Skip to contentSkip to conversation

KVM: VMX: do not use uninitialized gfn_to_hva_cache

PATCH In review Paolo Bonzini 2 participants 2 messages Nov 22, 2021 – Nov 23, 2021

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: VMX: do not use uninitialized gfn_to_hva_cache

An uninitialized gfn_to_hva_cache has ghc->len == 0, which causes the accessors to croak very loudly. While a BUG_ON is definitely _too_ loud and a bug on its own, there is indeed an issue of using the caches in such a way that they could not have been initialized, because ghc->gpa == 0 might match and thus kvm_gfn_to_hva_cache_init would not be called.

For the vmcs12_cache, the solution is simply to invoke kvm_gfn_to_hva_cache_init unconditionally: we already know that the cache does not match the current VMCS pointer. For the shadow_vmcs12_cache, there is no similar condition that checks the VMCS link pointer, so invalidate the cache on VMXON.

Fixes cee66664dcd6 ("KVM: nVMX: Use a gfn_to_hva_cache for vmptrld")
Cc David Woodhouse <[email protected]>
Reported-by [email protected]
---
 arch/x86/kvm/vmx/nested.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +2 -2
index 1e2f66951566..315fa456d368 100644
@@ -4857,6 +4857,7 @@ static int enter_vmx_operation(struct kvm_vcpu *vcpu)
 	if (!vmx->nested.cached_vmcs12)
 		goto out_cached_vmcs12;
 
+	vmx->nested.shadow_vmcs12_cache.gpa = INVALID_GPA;
 	vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
 	if (!vmx->nested.cached_shadow_vmcs12)
 		goto out_cached_shadow_vmcs12;
@@ -5289,8 +5290,7 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)
 		struct gfn_to_hva_cache *ghc = &vmx->nested.vmcs12_cache;
 		struct vmcs_hdr hdr;
 
-		if (ghc->gpa != vmptr &&
-		    kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr, VMCS12_SIZE)) {
+		if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr, VMCS12_SIZE)) {
 			/*
 			 * Reads from an unbacked page return all 1s,
 			 * which means that the 32 bits located at the
Signature · 1 line
2.27.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Paolo Bonzini

An uninitialized gfn_to_hva_cache has ghc->len == 0, which causes the accessors to croak very loudly. While a BUG_ON is definitely _too_ loud and a bug on its own, there is indeed an issue of using the caches in such a way that they could not have been initialized, because ghc->gpa == 0 might match and thus kvm_gfn_to_hva_cache_init would not be called.

Hm, in real usage, I thought an initialized-to-zeroes cache would have been considered invalid regardless of the gpa, because ghc->generation wouldn't have matched slots->generation.

But in the syzbot case perhaps it hadn't actually added any memslots yet, so slots->generation == 0 and that doesn't help. Although.... would it even have got that far if there weren't any memslots at all?

Quoted 7 lines

For the vmcs12_cache, the solution is simply to invoke kvm_gfn_to_hva_cache_init unconditionally: we already know that the cache does not match the current VMCS pointer. For the shadow_vmcs12_cache, there is no similar condition that checks the VMCS link pointer, so invalidate the cache on VMXON.

Quoted 5 lines
Fixes cee66664dcd6 ("KVM: nVMX: Use a gfn_to_hva_cache for vmptrld")
Cc David Woodhouse <[email protected]>
Reported-by [email protected]

Thanks, I was just staring at the report when I realised you'd already fixed it.

Quoted 33 lines
---
 arch/x86/kvm/vmx/nested.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -0
index 1e2f66951566..315fa456d368 100644
@@ -4857,6 +4857,7 @@ static int enter_vmx_operation(struct kvm_vcpu
 *vcpu)
 	if (!vmx->nested.cached_vmcs12)
 		goto out_cached_vmcs12;
 
+	vmx->nested.shadow_vmcs12_cache.gpa = INVALID_GPA;
 	vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE,
 GFP_KERNEL_ACCOUNT);
arch/x86/kvm/vmx/nested.c +1 -2
 	if (!vmx->nested.cached_shadow_vmcs12)
 		goto out_cached_shadow_vmcs12;
@@ -5289,8 +5290,7 @@ static int handle_vmptrld(struct kvm_vcpu
 *vcpu)
 		struct gfn_to_hva_cache *ghc = &vmx-
 >nested.vmcs12_cache;
 		struct vmcs_hdr hdr;
 
-		if (ghc->gpa != vmptr &&
-		    kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr,
 VMCS12_SIZE)) {
+		if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr,

VMCS12_SIZE)) {

arch/x86/kvm/vmx/nested.c +0 -0
 			/*
 			 * Reads from an unbacked page return all 1s,
 			 * which means that the 32 bits located at the