KVM: x86/mmu: avoid NULL-pointer dereference on page freeing bugs
authorPaolo Bonzini <pbonzini@redhat.com>
Wed, 9 Feb 2022 00:08:33 +0000 (19:08 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 25 Feb 2022 13:20:16 +0000 (08:20 -0500)
WARN and bail if KVM attempts to free a root that isn't backed by a shadow
page.  KVM allocates a bare page for "special" roots, e.g. when using PAE
paging or shadowing 2/3/4-level page tables with 4/5-level, and so root_hpa
will be valid but won't be backed by a shadow page.  It's all too easy to
blindly call mmu_free_root_page() on root_hpa, be nice and WARN instead of
crashing KVM and possibly the kernel.

Reviewed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/mmu/mmu.c

index 0620480b99e02bf66e4dc46d0cb421a72011c165..28c4d658d2ac7336c52656047275565386340716 100644 (file)
@@ -3206,6 +3206,8 @@ static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
                return;
 
        sp = to_shadow_page(*root_hpa & PT64_BASE_ADDR_MASK);
+       if (WARN_ON(!sp))
+               return;
 
        if (is_tdp_mmu_page(sp))
                kvm_tdp_mmu_put_root(kvm, sp, false);