From: Sean Christopherson <seanjc@google.com>
To: Dave Hansen <dave.hansen@intel.com>
Cc: Xiaoyao Li <xiaoyao.li@intel.com>, Chao Gao <chao.gao@intel.com>,
kvm@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
linux-kernel@vger.kernel.org, Jim Mattson <jmattson@google.com>
Subject: Re: [PATCH] KVM: x86: Track supported ARCH_CAPABILITIES in kvm_caps
Date: Mon, 22 May 2023 09:37:50 -0700 [thread overview]
Message-ID: <ZGuaXp2f5VhfEyUI@google.com> (raw)
In-Reply-To: <4ffbb2c3-8ed1-d419-16ca-b311867537be@intel.com>
On Mon, May 22, 2023, Dave Hansen wrote:
> On 5/18/23 10:33, Sean Christopherson wrote:
> >
> > 2. I'm pretty sure conditioning mmio_stale_data_clear on kvm_arch_has_assigned_device()
> > is a bug. AIUI, the vulnerability applies to _any_ MMIO accesses. Assigning
> > a device is necessary to let the device DMA into the guest, but it's not
> > necessary to let the guest access MMIO addresses, that's done purely via
> > memslots.
>
> Just to make sure we're all on the same page: KVM needs mitigations when
> real, hardware MMIO is exposed to the guest. None of this has anything
> to do with virtio or what guests _normally_ see as devices or MMIO. Right?
Yes. I try to always call MMIO that is handled by a synthetic/virtual/emulated
device "emulated MMIO", specifically to differentiate between the two cases.
> But direct device assignment does that "real hardware MMIO" for sure
> because it's mapping parts of the PCI address space (which is all MMIO)
> into the guest. That's what the kvm_arch_has_assigned_device() check
> was going for.
>
> But I think you're also saying that, in the end, memory gets exposed to
> the guest by KVM userspace setting up a memslot. KVM userspace _could_
> have mapped a piece of MMIO and could just pass that down to a guest
> without kvm_arch_has_assigned_device() being involved. That makes the
> kvm_arch_has_assigned_device() useless.
Yep.
> In other words, all guests with kvm_arch_has_assigned_device() need
> mitigation.
Yes, assuming the guest wants to actually use the device :-)
> But there are potentially situations where the guest can see real hardware MMIO
> and yet also be !kvm_arch_has_assigned_device().
Yes. There may or may not be _legitimate_ scenarios for exposing host MMIO to the
guest without an assigned device, but as far as the mitigation is concerned, being
legitimate or not doesn't matter, all that matters is that userspace can expose
host MMIO to the guest irrespective of VFIO.
FWIW, I think this would be a minimal fix without having to apply the mitigation
blindly. My only concern is that there might be gaps in the kvm_is_mmio_pfn()
heuristic, but if that's the case then KVM likely has other issues, e.g. would
potentially map MMIO with the wrong memtype.
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 2865c3cb3501..ac3c535ae3b9 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1274,6 +1274,7 @@ struct kvm_arch {
bool apic_access_memslot_enabled;
bool apic_access_memslot_inhibited;
+ bool vm_has_passthrough_mmio;
/* Protects apicv_inhibit_reasons */
struct rw_semaphore apicv_update_lock;
diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c
index cf2c6426a6fc..83d235488e56 100644
--- a/arch/x86/kvm/mmu/spte.c
+++ b/arch/x86/kvm/mmu/spte.c
@@ -189,6 +189,10 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
if (level > PG_LEVEL_4K)
spte |= PT_PAGE_SIZE_MASK;
+ if (static_branch_unlikely(&mmio_stale_data_clear) &&
+ !vcpu->kvm->arch.vm_has_passthrough_mmio && kvm_is_mmio_pfn(pfn))
+ vcpu->kvm->arch.vm_has_passthrough_mmio = true;
+
if (shadow_memtype_mask)
spte |= static_call(kvm_x86_get_mt_mask)(vcpu, gfn,
kvm_is_mmio_pfn(pfn));
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 44fb619803b8..9c66ba35af92 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7159,7 +7159,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
else if (static_branch_unlikely(&mds_user_clear))
mds_clear_cpu_buffers();
else if (static_branch_unlikely(&mmio_stale_data_clear) &&
- kvm_arch_has_assigned_device(vcpu->kvm))
+ to_kvm_vmx(vcpu->kvm)->vm_has_passthrough_mmio)
mds_clear_cpu_buffers();
vmx_disable_fb_clear(vmx);
next prev parent reply other threads:[~2023-05-22 16:37 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-06 3:04 [PATCH] KVM: x86: Track supported ARCH_CAPABILITIES in kvm_caps Chao Gao
2023-05-18 9:32 ` Xiaoyao Li
2023-05-18 17:33 ` Sean Christopherson
2023-05-19 8:40 ` Chao Gao
2023-05-19 15:25 ` Sean Christopherson
2023-05-20 1:02 ` Pawan Gupta
2023-05-22 17:43 ` Sean Christopherson
2023-05-22 19:31 ` Xiaoyao Li
2023-05-22 21:23 ` Pawan Gupta
2023-05-23 1:00 ` Xiaoyao Li
2023-05-23 3:34 ` Pawan Gupta
2023-05-25 15:42 ` Xiaoyao Li
2023-05-25 20:33 ` Pawan Gupta
2023-05-22 20:54 ` Pawan Gupta
2023-05-23 4:47 ` Pawan Gupta
2023-05-22 14:23 ` Dave Hansen
2023-05-22 16:37 ` Sean Christopherson [this message]
2023-05-29 3:35 ` Chao Gao
2023-06-06 16:54 ` Sean Christopherson
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=ZGuaXp2f5VhfEyUI@google.com \
--to=seanjc@google.com \
--cc=bp@alien8.de \
--cc=chao.gao@intel.com \
--cc=dave.hansen@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
--cc=xiaoyao.li@intel.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