Skip to content

Commit f4064ef

Browse files
sean-jcgregkh
authored andcommitted
KVM: x86/mmu: Get root level from walkers when retrieving MMIO SPTE
commit 39b4d43 upstream. Get the so called "root" level from the low level shadow page table walkers instead of manually attempting to calculate it higher up the stack, e.g. in get_mmio_spte(). When KVM is using PAE shadow paging, the starting level of the walk, from the callers perspective, is not the CR3 root but rather the PDPTR "root". Checking for reserved bits from the CR3 root causes get_mmio_spte() to consume uninitialized stack data due to indexing into sptes[] for a level that was not filled by get_walk(). This can result in false positives and/or negatives depending on what garbage happens to be on the stack. Opportunistically nuke a few extra newlines. Fixes: 95fb5b0 ("kvm: x86/mmu: Support MMIO in the TDP MMU") Reported-by: Richard Herbert <rherbert@sympatico.ca> Cc: Ben Gardon <bgardon@google.com> Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20201218003139.2167891-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent afd6216 commit f4064ef

3 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎arch/x86/kvm/mmu/mmu.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,16 +3485,16 @@ static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
34853485
* Return the level of the lowest level SPTE added to sptes.
34863486
* That SPTE may be non-present.
34873487
*/
3488-
static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes)
3488+
static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, int *root_level)
34893489
{
34903490
struct kvm_shadow_walk_iterator iterator;
34913491
int leaf = -1;
34923492
u64 spte;
34933493

3494-
34953494
walk_shadow_page_lockless_begin(vcpu);
34963495

3497-
for (shadow_walk_init(&iterator, vcpu, addr);
3496+
for (shadow_walk_init(&iterator, vcpu, addr),
3497+
*root_level = iterator.level;
34983498
shadow_walk_okay(&iterator);
34993499
__shadow_walk_next(&iterator, spte)) {
35003500
leaf = iterator.level;
@@ -3504,7 +3504,6 @@ static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes)
35043504

35053505
if (!is_shadow_present_pte(spte))
35063506
break;
3507-
35083507
}
35093508

35103509
walk_shadow_page_lockless_end(vcpu);
@@ -3517,9 +3516,7 @@ static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
35173516
{
35183517
u64 sptes[PT64_ROOT_MAX_LEVEL];
35193518
struct rsvd_bits_validate *rsvd_check;
3520-
int root = vcpu->arch.mmu->shadow_root_level;
3521-
int leaf;
3522-
int level;
3519+
int root, leaf, level;
35233520
bool reserved = false;
35243521

35253522
if (!VALID_PAGE(vcpu->arch.mmu->root_hpa)) {
@@ -3528,9 +3525,9 @@ static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
35283525
}
35293526

35303527
if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa))
3531-
leaf = kvm_tdp_mmu_get_walk(vcpu, addr, sptes);
3528+
leaf = kvm_tdp_mmu_get_walk(vcpu, addr, sptes, &root);
35323529
else
3533-
leaf = get_walk(vcpu, addr, sptes);
3530+
leaf = get_walk(vcpu, addr, sptes, &root);
35343531

35353532
if (unlikely(leaf < 0)) {
35363533
*sptep = 0ull;

‎arch/x86/kvm/mmu/tdp_mmu.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,13 +1148,16 @@ bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,
11481148
* Return the level of the lowest level SPTE added to sptes.
11491149
* That SPTE may be non-present.
11501150
*/
1151-
int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes)
1151+
int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,
1152+
int *root_level)
11521153
{
11531154
struct tdp_iter iter;
11541155
struct kvm_mmu *mmu = vcpu->arch.mmu;
11551156
gfn_t gfn = addr >> PAGE_SHIFT;
11561157
int leaf = -1;
11571158

1159+
*root_level = vcpu->arch.mmu->shadow_root_level;
1160+
11581161
tdp_mmu_for_each_pte(iter, mmu, gfn, gfn + 1) {
11591162
leaf = iter.level;
11601163
sptes[leaf - 1] = iter.old_spte;

‎arch/x86/kvm/mmu/tdp_mmu.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ void kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,
4444
bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,
4545
struct kvm_memory_slot *slot, gfn_t gfn);
4646

47-
int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes);
47+
int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,
48+
int *root_level);
49+
4850
#endif /* __KVM_X86_MMU_TDP_MMU_H */

0 commit comments

Comments
 (0)