diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6346d0123077fc44585c94a037703f12af439a5c..f34f0989e453902d3585756202c0184d9fe0ddec 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1044,7 +1044,15 @@ __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn) static inline unsigned long __gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn) { - return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE; + /* + * The index was checked originally in search_memslots. To avoid + * that a malicious guest builds a Spectre gadget out of e.g. page + * table walks, do not let the processor speculate loads outside + * the guest's registered memslots. + */ + unsigned long offset = gfn - slot->base_gfn; + offset = array_index_nospec(offset, slot->npages); + return slot->userspace_addr + offset * PAGE_SIZE; } static inline int memslot_id(struct kvm *kvm, gfn_t gfn)