Skip to content

Commit b6467ab

Browse files
Sean Christophersonbonzini
authored andcommitted
KVM: Check validity of resolved slot when searching memslots
Check that the resolved slot (somewhat confusingly named 'start') is a valid/allocated slot before doing the final comparison to see if the specified gfn resides in the associated slot. The resolved slot can be invalid if the binary search loop terminated because the search index was incremented beyond the number of used slots. This bug has existed since the binary search algorithm was introduced, but went unnoticed because KVM statically allocated memory for the max number of slots, i.e. the access would only be truly out-of-bounds if all possible slots were allocated and the specified gfn was less than the base of the lowest memslot. Commit 3694725 ("KVM: Dynamically size memslot array based on number of used slots") eliminated the "all possible slots allocated" condition and made the bug embarrasingly easy to hit. Fixes: 9c1a5d3 ("kvm: optimize GFN to memslot lookup with large slots amount") Reported-by: [email protected] Cc: [email protected] Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Reviewed-by: Cornelia Huck <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent fb56baa commit b6467ab

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

include/linux/kvm_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ search_memslots(struct kvm_memslots *slots, gfn_t gfn)
10481048
start = slot + 1;
10491049
}
10501050

1051-
if (gfn >= memslots[start].base_gfn &&
1051+
if (start < slots->used_slots && gfn >= memslots[start].base_gfn &&
10521052
gfn < memslots[start].base_gfn + memslots[start].npages) {
10531053
atomic_set(&slots->lru_slot, start);
10541054
return &memslots[start];

0 commit comments

Comments
 (0)