kvm: fix potentially corrupt mmio cache
Subsystems (4)
PATCH 1/2 kvm: fix potentially corrupt mmio cache
vcpu exits and memslot mutations can run concurrently as long as the vcpu does not aquire the slots mutex. Thus it is theoretically possible for memslots to change underneath a vcpu that is handling an exit.
If we increment the memslot generation number again after synchronize_srcu_expedited(), vcpus can safely cache memslot generation without maintaining a single rcu_dereference through an entire vm exit. And much of the x86/kvm code does not maintain a single rcu_dereference of the current memslots during each exit.
We can prevent the following case:
vcpu (CPU 0) | thread (CPU 1)
--------------------------------------------+--------------------------
1 vm exit |
2 decide to cache something based on |
old memslots |
3 | change memslots
4 | increment generation
5 tag cache with new memslot generation |
... |
<action based on cache occurs even |
though the caching decision was based |
on the old memslots> |
... |
<action *continues* to occur until next |
memslot generation change, which may |
be never> |By incrementing the generation again after synchronizing kvm->srcu readers, we guarantee the generation cached in (5) will very soon become invalid.
--- virt/kvm/kvm_main.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
virt/kvm/kvm_main.c +11 -7
@@ -95,8 +95,6 @@ static int hardware_enable_all(void);static void hardware_disable_all (void );static void kvm_io_bus_destroy (struct kvm_io_bus *bus); -static void update_memslots (struct kvm_memslots *slots, -struct kvm_memory_slot *new,u64 last_generation);static void kvm_release_pfn_dirty (pfn_t pfn); static void mark_page_dirty_in_slot(struct kvm *kvm,
@@ -685,8 +683,7 @@ static void sort_memslots(struct kvm_memslots *slots) }static void update_memslots (struct kvm_memslots *slots, -struct kvm_memory_slot *new, -u64 last_generation) +struct kvm_memory_slot *new) {if (new) {int id = new->id;
@@ -697,8 +694,6 @@ static void update_memslots(struct kvm_memslots *slots,if (new->npages != npages)sort_memslots (slots); } - - slots->generation = last_generation +1 ; }static int check_memory_region_flags (struct kvm_userspace_memory_region *mem)
@@ -720,10 +715,19 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, {struct kvm_memslots *old_memslots = kvm->memslots; -update_memslots (slots, new, kvm->memslots->generation); + slots->generation = old_memslots->generation +1 ; + +update_memslots (slots, new);rcu_assign_pointer (kvm->memslots, slots);synchronize_srcu_expedited (&kvm->srcu); +/* +* Increment the new memslot generation a second time. This prevents +* vm exits that race with memslot updates from caching a memslot +* generation that will (potentially) be valid forever. +*/ + slots->generation++; +kvm_arch_memslots_updated (kvm);return old_memslots;
Signature · 1 line
2.1.0.rc2.206.gedb03e5
PATCH 2/2 kvm: x86: fix stale mmio cache bug
The following events can lead to an incorrect KVM_EXIT_MMIO bubbling up to userspace:
(1) Guest accesses gpa X without a memory slot. The gfn is cached in struct kvm_vcpu_arch (mmio_gfn). On Intel EPT-enabled hosts, KVM sets the SPTE write-execute-noread so that future accesses cause EPT_MISCONFIGs.
(2) Host userspace creates a memory slot via KVM_SET_USER_MEMORY_REGION covering the page just accessed.
(3) Guest attempts to read or write to gpa X again. On Intel, this generates an EPT_MISCONFIG. The memory slot generation number that was incremented in (2) would normally take care of this but we fast path mmio faults through quickly_check_mmio_pf(), which only checks the per-vcpu mmio cache. Since we hit the cache, KVM passes a KVM_EXIT_MMIO up to userspace.
This patch fixes the issue by using the memslot generation number to validate the mmio cache.
[ xiaoguangrong: adjust the code to make it simpler for stable-tree fix. ]
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 4 ++-- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/x86.h | 21 ++++++++++++++++----- 4 files changed, 21 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -479,6 +479,7 @@ struct kvm_vcpu_arch {u64 mmio_gva;unsigned access;gfn_t mmio_gfn; +unsigned int mmio_gen;struct kvm_pmu pmu;
arch/x86/kvm/mmu.c +2 -2
@@ -234,7 +234,7 @@ static unsigned int get_mmio_spte_generation(u64 spte)return gen; } -static unsigned int kvm_current_mmio_generation (struct kvm *kvm) +unsigned int kvm_current_mmio_generation (struct kvm *kvm) {/* * Init kvm generation close to MMIO_MAX_GEN to easily test the
@@ -3163,7 +3163,7 @@ static void mmu_sync_roots(struct kvm_vcpu *vcpu)if (!VALID_PAGE (vcpu->arch.mmu.root_hpa))return ; -vcpu_clear_mmio_info (vcpu, ~0ul ); +vcpu_clear_mmio_info (vcpu,MMIO_GVA_ANY );kvm_mmu_audit (vcpu,AUDIT_PRE_SYNC );if (vcpu->arch.mmu.root_level ==PT64_ROOT_LEVEL ) {hpa_t root = vcpu->arch.mmu.root_hpa;
arch/x86/kvm/mmu.h +2 -0
@@ -76,6 +76,8 @@ enum { };int handle_mmio_page_fault_common (struct kvm_vcpu *vcpu,u64 addr,bool direct); +unsigned int kvm_current_mmio_generation (struct kvm *kvm); +void kvm_init_shadow_mmu (struct kvm_vcpu *vcpu,struct kvm_mmu *context);void kvm_init_shadow_ept_mmu (struct kvm_vcpu *vcpu,struct kvm_mmu *context,bool execonly);
arch/x86/kvm/x86.h +16 -5
@@ -3,6 +3,7 @@#include <linux/kvm_host.h> #include "kvm_cache_regs.h" +#include "mmu.h" static inline void kvm_clear_exception_queue (struct kvm_vcpu *vcpu) {
@@ -78,15 +79,23 @@ static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu, vcpu->arch.mmio_gva = gva &PAGE_MASK ; vcpu->arch.access = access; vcpu->arch.mmio_gfn = gfn; + vcpu->arch.mmio_gen =kvm_current_mmio_generation (vcpu->kvm); +} + +static inline bool vcpu_match_mmio_gen (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.mmio_gen ==kvm_current_mmio_generation (vcpu->kvm); }/* -* Clear the mmio cache info for the given gva, -* specially, if gva is ~0ul, we clear all mmio cache info. +* Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we +* clear all mmio cache info. */ +#define MMIO_GVA_ANY (~(gva_t)0) +static inline void vcpu_clear_mmio_info (struct kvm_vcpu *vcpu,gva_t gva) { -if (gva != (~0ul ) && vcpu->arch.mmio_gva != (gva &PAGE_MASK )) +if (gva !=MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva &PAGE_MASK ))return ; vcpu->arch.mmio_gva =0 ;
@@ -94,7 +103,8 @@ static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)static inline bool vcpu_match_mmio_gva (struct kvm_vcpu *vcpu,unsigned long gva) { -if (vcpu->arch.mmio_gva && vcpu->arch.mmio_gva == (gva &PAGE_MASK )) +if (vcpu_match_mmio_gen (vcpu) && vcpu->arch.mmio_gva && + vcpu->arch.mmio_gva == (gva &PAGE_MASK ))return true;return false;
@@ -102,7 +112,8 @@ static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)static inline bool vcpu_match_mmio_gpa (struct kvm_vcpu *vcpu,gpa_t gpa) { -if (vcpu->arch.mmio_gfn && vcpu->arch.mmio_gfn == gpa >>PAGE_SHIFT ) +if (vcpu_match_mmio_gen (vcpu) && vcpu->arch.mmio_gfn && + vcpu->arch.mmio_gfn == gpa >>PAGE_SHIFT )return true;return false;
Signature · 1 line
2.1.0.rc2.206.gedb03e5
Re: [PATCH 2/2] kvm: x86: fix stale mmio cache bug
Quoted 21 lines · David Matlack
The following events can lead to an incorrect
KVM_EXIT_MMIObubbling up to userspace:(1) Guest accesses gpa X without a memory slot. The gfn is cached in struct
kvm_vcpu_arch(mmio_gfn). On Intel EPT-enabled hosts, KVM sets the SPTE write-execute-noread so that future accesses causeEPT_MISCONFIGs.(2) Host userspace creates a memory slot via
KVM_SET_USER_MEMORY_REGIONcovering the page just accessed.(3) Guest attempts to read or write to gpa X again. On Intel, this generates an
EPT_MISCONFIG. The memory slot generation number that was incremented in (2) would normally take care of this but we fast path mmio faults throughquickly_check_mmio_pf(), which only checks the per-vcpu mmio cache. Since we hit the cache, KVM passes aKVM_EXIT_MMIOup to userspace.This patch fixes the issue by using the memslot generation number to validate the mmio cache.
Quoted 6 lines
[ xiaoguangrong: adjust the code to make it simpler for stable-tree fix. ]
Signed-off-by David Matlack <[email protected]>Signed-off-by Xiao Guangrong <[email protected]>---
Paolo,
It seems like this patch ("[PATCH 2/2] kvm: x86: fix stale mmio cache") is ready to go. Is there anything blocking it from being merged?
(It should be fine to merge this on its own, independent of the fix discussed in "[PATCH 1/2] KVM: fix cache stale memslot info with correct mmio generation number", https://lkml.org/lkml/2014/8/14/62.)
Quoted 115 lines
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 4 ++-- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/x86.h | 21 ++++++++++++++++----- 4 files changed, 21 insertions(+), 7 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -479,6 +479,7 @@ struct kvm_vcpu_arch {u64 mmio_gva;unsigned access;gfn_t mmio_gfn; +unsigned int mmio_gen;struct kvm_pmu pmu;arch/x86/kvm/mmu.c +2
-2@@ -234,7 +234,7 @@ static unsigned int get_mmio_spte_generation(u64 spte)return gen; } -static unsigned int kvm_current_mmio_generation (struct kvm *kvm) +unsigned int kvm_current_mmio_generation (struct kvm *kvm) {/* * Init kvm generation close to MMIO_MAX_GEN to easily test the @@ -3163,7 +3163,7 @@ static void mmu_sync_roots(struct kvm_vcpu *vcpu)if (!VALID_PAGE (vcpu->arch.mmu.root_hpa))return ; -vcpu_clear_mmio_info (vcpu, ~0ul ); +vcpu_clear_mmio_info (vcpu,MMIO_GVA_ANY );kvm_mmu_audit (vcpu,AUDIT_PRE_SYNC );if (vcpu->arch.mmu.root_level ==PT64_ROOT_LEVEL ) {hpa_t root = vcpu->arch.mmu.root_hpa;arch/x86/kvm/mmu.h +2
-0@@ -76,6 +76,8 @@ enum { };int handle_mmio_page_fault_common (struct kvm_vcpu *vcpu,u64 addr,bool direct); +unsigned int kvm_current_mmio_generation (struct kvm *kvm); +void kvm_init_shadow_mmu (struct kvm_vcpu *vcpu,struct kvm_mmu *context);void kvm_init_shadow_ept_mmu (struct kvm_vcpu *vcpu,struct kvm_mmu *context,bool execonly);arch/x86/kvm/x86.h +16
-6@@ -3,6 +3,7 @@#include <linux/kvm_host.h> #include "kvm_cache_regs.h" +#include "mmu.h" static inline void kvm_clear_exception_queue (struct kvm_vcpu *vcpu) {@@ -78,15 +79,23 @@ static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu, vcpu->arch.mmio_gva = gva &PAGE_MASK ; vcpu->arch.access = access; vcpu->arch.mmio_gfn = gfn; + vcpu->arch.mmio_gen =kvm_current_mmio_generation (vcpu->kvm); +} + +static inline bool vcpu_match_mmio_gen (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.mmio_gen ==kvm_current_mmio_generation (vcpu->kvm); }/* -* Clear the mmio cache info for the given gva, -* specially, if gva is ~0ul, we clear all mmio cache info. +* Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we +* clear all mmio cache info. */ +#define MMIO_GVA_ANY (~(gva_t)0) +static inline void vcpu_clear_mmio_info (struct kvm_vcpu *vcpu,gva_t gva) { -if (gva != (~0ul ) && vcpu->arch.mmio_gva != (gva &PAGE_MASK )) +if (gva !=MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva &PAGE_MASK ))return ; vcpu->arch.mmio_gva =0 ;@@ -94,7 +103,8 @@ static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)static inline bool vcpu_match_mmio_gva (struct kvm_vcpu *vcpu,unsigned long gva) { -if (vcpu->arch.mmio_gva && vcpu->arch.mmio_gva == (gva &PAGE_MASK )) +if (vcpu_match_mmio_gen (vcpu) && vcpu->arch.mmio_gva && + vcpu->arch.mmio_gva == (gva &PAGE_MASK ))return true;return false;@@ -102,7 +112,8 @@ static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)static inline bool vcpu_match_mmio_gpa (struct kvm_vcpu *vcpu,gpa_t gpa) { -if (vcpu->arch.mmio_gfn && vcpu->arch.mmio_gfn == gpa >>PAGE_SHIFT ) +if (vcpu_match_mmio_gen (vcpu) && vcpu->arch.mmio_gfn && + vcpu->arch.mmio_gfn == gpa >>PAGE_SHIFT )return true;return false; --2.1.0.rc2.206.gedb03e5
Paolo Bonzini
maintainerRe: [PATCH 2/2] kvm: x86: fix stale mmio cache bug
Il 28/08/2014 23:10, David Matlack ha scritto:
Quoted 7 lines
Paolo,
It seems like this patch ("[PATCH 2/2] kvm: x86: fix stale mmio cache") is ready to go. Is there anything blocking it from being merged?(It should be fine to merge this on its own, independent of the fix discussed in "[PATCH 1/2] KVM: fix cache stale memslot info with correct mmio generation number", https://lkml.org/lkml/2014/8/14/62.)
I'll post the full series today. Sorry, I've been swamped a bit.
Paolo
Re: [PATCH 2/2] kvm: x86: fix stale mmio cache bug
Quoted 10 lines · Paolo Bonzini
Il 28/08/2014 23:10, David Matlack ha scritto:
Quoted 7 lines
Paolo,
It seems like this patch ("[PATCH 2/2] kvm: x86: fix stale mmio cache") is ready to go. Is there anything blocking it from being merged?(It should be fine to merge this on its own, independent of the fix discussed in "[PATCH 1/2] KVM: fix cache stale memslot info with correct mmio generation number", https://lkml.org/lkml/2014/8/14/62.)
I'll post the full series today. Sorry, I've been swamped a bit.
Thanks, I'll start testing it. Hope things quiet down for you soon :)
Quoted 2 lines
Paolo