From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Reima Ishii <ishiir@g.ecc.u-tokyo.ac.jp>
Subject: [PATCH 2/5] KVM: x86/mmu: Harden new PGD against roots without shadow pages
Date: Fri, 21 Jul 2023 18:23:47 -0700 [thread overview]
Message-ID: <20230722012350.2371049-3-seanjc@google.com> (raw)
In-Reply-To: <20230722012350.2371049-1-seanjc@google.com>
Harden kvm_mmu_new_pgd() against NULL pointer dereference bugs by sanity
checking that the target root has an associated shadow page prior to
dereferencing said shadow page. The code in question is guaranteed to
only see roots with shadow pages as fast_pgd_switch() explicitly frees the
current root if it doesn't have a shadow page, i.e. is a PAE root, and
that in turn prevents valid roots from being cached, but that's all very
subtle.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/mmu/mmu.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 1eadfcde30be..dd8cc46551b2 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -4560,9 +4560,19 @@ static void nonpaging_init_context(struct kvm_mmu *context)
static inline bool is_root_usable(struct kvm_mmu_root_info *root, gpa_t pgd,
union kvm_mmu_page_role role)
{
- return (role.direct || pgd == root->pgd) &&
- VALID_PAGE(root->hpa) &&
- role.word == root_to_sp(root->hpa)->role.word;
+ struct kvm_mmu_page *sp;
+
+ if (!VALID_PAGE(root->hpa))
+ return false;
+
+ if (!role.direct && pgd != root->pgd)
+ return false;
+
+ sp = root_to_sp(root->hpa);
+ if (WARN_ON_ONCE(!sp))
+ return false;
+
+ return role.word == sp->role.word;
}
/*
@@ -4682,9 +4692,12 @@ void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd)
* If this is a direct root page, it doesn't have a write flooding
* count. Otherwise, clear the write flooding count.
*/
- if (!new_role.direct)
- __clear_sp_write_flooding_count(
- root_to_sp(vcpu->arch.mmu->root.hpa));
+ if (!new_role.direct) {
+ struct kvm_mmu_page *sp = root_to_sp(vcpu->arch.mmu->root.hpa);
+
+ if (!WARN_ON_ONCE(!sp))
+ __clear_sp_write_flooding_count(sp);
+ }
}
EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);
--
2.41.0.487.g6d72f3e995-goog
next prev parent reply other threads:[~2023-07-22 1:24 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-22 1:23 [PATCH 0/5] KVM: x86/mmu: Don't synthesize triple fault on bad root Sean Christopherson
2023-07-22 1:23 ` [PATCH 1/5] KVM: x86/mmu: Add helper to convert root hpa to shadow page Sean Christopherson
2023-07-22 1:23 ` Sean Christopherson [this message]
2023-07-22 1:23 ` [PATCH 3/5] KVM: x86/mmu: Harden TDP MMU iteration against root w/o " Sean Christopherson
2023-07-25 10:39 ` Yu Zhang
2023-07-25 15:56 ` Sean Christopherson
2023-07-26 3:55 ` Yu Zhang
2023-07-22 1:23 ` [PATCH 4/5] KVM: x86/mmu: Disallow guest from using !visible slots for page tables Sean Christopherson
2023-07-22 1:23 ` [PATCH 5/5] KVM: x86/mmu: Use dummy root, backed by zero page, for !visible guest roots Sean Christopherson
2023-07-25 11:36 ` Yu Zhang
2023-07-25 15:53 ` Sean Christopherson
2023-07-26 9:20 ` Yu Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230722012350.2371049-3-seanjc@google.com \
--to=seanjc@google.com \
--cc=ishiir@g.ecc.u-tokyo.ac.jp \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®