Return-Path: Received: from mail.linutronix.de (146.0.238.70:993) by crypto-ml.lab.linutronix.de with IMAP4-SSL for ; 27 Jun 2018 15:54:59 -0000 Received: from mx2.suse.de ([195.135.220.15]) by Galois.linutronix.de with esmtps (TLS1.0:DHE_RSA_CAMELLIA_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1fYCmY-0008AN-AP for speck@linutronix.de; Wed, 27 Jun 2018 17:54:58 +0200 Received: from relay1.suse.de (charybdis-ext-too.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 131E5AEDD for ; Wed, 27 Jun 2018 15:54:53 +0000 (UTC) Date: Wed, 27 Jun 2018 17:54:48 +0200 From: Borislav Petkov Subject: [MODERATED] Re: [PATCH v4 2/8] [PATCH v4 2/8] Linux Patch #2 Message-ID: <20180627155448.GF24227@zn.tnic> References: <20180623135445.576988756@localhost.localdomain> MIME-Version: 1.0 In-Reply-To: <20180623135445.576988756@localhost.localdomain> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable To: speck@linutronix.de List-ID: On Sat, Jun 23, 2018 at 09:54:16AM -0400, speck for konrad.wilk_at_oracle.com= wrote: > kvm: x86: mitigation for L1 cache terminal fault vulnerabilities >=20 > We add two mitigation modes for CVE-2018-3620, aka L1 terminal > fault. The two modes are "vmentry_l1d_flush=3D1" and "vmentry_l1d_flush=3D= 2". >=20 > "vmentry_l1d_flush=3D2" is simply doing an L1 cache flush on every VMENTER. > "vmentry_l1d_flush=3D1" is trying to avoid so many L1 cache flueshes on It goes without saying that "1" and "2" are not as good a choice as words with meaning. > VMENTER and instead only does if the reason for entering the hypervisor > is based on the type of code that is executed. The idea is based on Intel's > patches, but we treat all vmexits as safe unless they execute > specific code that is considered unsafe. There is no hardcoded list of > "safe" exit reasons; but vmexits are considered safe unless: > - They trigger the emulator, which could be a good target for > other speculative execution-based threats, > - or the MMU, which can bring host page tables in the L1 cache. > - In addition, executing userspace or another process will trigger a flush. > - external interrupts > - nested operations that require the MMU (see above). That is > vmptrld, vmptrst, vmclear,vmwrite,vmread. > - Also when handling invept,invvpid >=20 > The default is "vmentry_l1d_flush=3D1". The cost of "vmentry_l1d_flush=3D2" > is up to 2.5x more expensive vmexits on Haswell processors, and 30% on > Coffee Lake (for the latter, this is independent of whether microcode > or the generic flush code are used). >=20 > The mitigation does not in any way try to do anything about hyperthreading; > it is possible for a sibling thread to read data from the cache during a > vmexit, before the host completes the flush, or to read data from the cache > while a sibling runs. The suggestion there is to disable hyperthreading > unless you've configured your system to dedicate each core to a specific > guest. >=20 > Signed-off-by: Paolo Bonzini > Signed-off-by: Konrad Rzeszutek Wilk Looks like Paolo is the author here - this patch needs a proper From: or authorship fixed when applying. > --- > v2: Add checks for X86_BUG_L1TF > - Rework the commit description. > v3: change module parameter to S_IRUGO > move the kvm_l1d_flush closer to VMENTER > move the module parameter so it is in alphabetical order-ish. > add two extra places that are used by handle_[vmptrld, vmptrst,mclear,v= mwrite,vmread,invept,invvpid]. > Can be changed to be only specific handlers. > --- > Documentation/admin-guide/kernel-parameters.txt | 11 +++++ > arch/x86/include/asm/kvm_host.h | 8 ++++ > arch/x86/kvm/mmu.c | 1 + > arch/x86/kvm/svm.c | 1 + > arch/x86/kvm/vmx.c | 51 +++++++++++++++++++- > arch/x86/kvm/x86.c | 63 +++++++++++++++++++++= +++- > 6 files changed, 133 insertions(+), 2 deletions(-) >=20 > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt > index d59b34d4e62a..b8f7a4ab693a 100644 > --- a/Documentation/admin-guide/kernel-parameters.txt > +++ b/Documentation/admin-guide/kernel-parameters.txt > @@ -1919,6 +1919,17 @@ > kvm.enable_vmware_backdoor=3D[KVM] Support VMware backdoor PV interface. > Default is false (don't support). > =20 > + kvm.vmentry_l1d_flush=3D[KVM] Mitigation for L1 Terminal Fault CVE. Let's put the CVE number here - stuff will leave our L1s with time and having a number there will be helpful to remember in the future. > + Valid arguments: 0, 1, 2 > + > + 2 does an L1 cache flush on every VMENTER. > + 1 tries to avoid so many L1 cache flush on VMENTERs and instead > + do it only if the kind of code that is executed would lead to > + leaking host memory. > + 0 disables the mitigation > + > + Default is 1 (do L1 cache flush in specific instances) > + > kvm.mmu_audit=3D [KVM] This is a R/W parameter which allows audit > KVM MMU at runtime. > Default is 0 (off) ... > +static void vmx_prepare_guest_switch(struct kvm_vcpu *vcpu) > +{ > + vmx_save_host_state(vcpu); > + > + if (!enable_ept || static_cpu_has(X86_FEATURE_HYPERVISOR) || > + !static_cpu_has(X86_BUG_L1TF)) { > + vcpu->arch.flush_cache_req =3D false; > + return; > + } > + > + switch (vmentry_l1d_flush) { > + case 0: > + vcpu->arch.flush_cache_req =3D false; > + break; > + case 1: > + /* > + * If vmentry_l1d_flush is 1, each vmexit handler is responsible for > + * setting vcpu->arch.vcpu_unconfined. Currently this happens in the > + * following cases: > + * - vmlaunch/vmresume: we do not want the cache to be cleared by a > + * nested hypervisor *and* by KVM on bare metal, so we just do it > + * on every nested entry. Nested hypervisors do not bother clearing > + * the cache. > + * - anything that runs the emulator (the slow paths for EPT misconfig > + * or I/O instruction) > + * - anything that can cause get_user_pages (EPT violation, and again > + * the slow paths for EPT misconfig or I/O instruction) > + * - anything that can run code outside KVM (external interrupt, > + * which can run interrupt handlers or irqs; or the sched_in > + * preempt notifier) > + */ I sure hope we won't miss a case in the future... :-\ > + break; > + case 2: > + default: > + vcpu->arch.flush_cache_req =3D true; > + break; > + } > +} ... > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 0046aa70205a..4d2e4975f91d 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c ... > @@ -6533,10 +6541,49 @@ static struct notifier_block pvclock_gtod_notifier = =3D { > }; > #endif > =20 > + > +/* > + * The L1D cache is 32 KiB on Skylake, but to flush it we have to read in Skylake? I think most if not all have 32K I$... > + * 64 KiB because the replacement algorithm is not exactly LRU. > + */ > +#define L1D_CACHE_ORDER 4 > +static void *__read_mostly empty_zero_pages; > + > +void kvm_l1d_flush(void) Shouldn't this function be in vmx.c ? > +{ > + /* FIXME: could this be boot_cpu_data.x86_cache_size * 2? */ Doesn't look like it, for example here I have: model name : Intel(R) Xeon(R) CPU E7-8891 v4 @ 2.80GHz ... cache size : 61440 KB From looking at the end of init_intel_cacheinfo(), we put there the LLC size ... or so... > + int size =3D PAGE_SIZE << L1D_CACHE_ORDER; > + > + ASSERT(boot_cpu_has(X86_BUG_L1TF)); This needs to be boot_cpu_has_bug(). And then what's wrong with: if (!boot_cpu_has_bug(X86_BUG_L1TF)) return; > + > + asm volatile( > + /* First ensure the pages are in the TLB */ > + "xorl %%eax, %%eax\n\t" > + "11: \n\t" > + "movzbl (%0, %%" _ASM_AX "), %%ecx\n\t" Named asm params would make it more readable: "movzbl (%[empty_zp], %% ..." > + "addl $4096, %%eax\n\t" > + "cmpl %%eax, %1\n\t" > + "jne 11b\n\t" > + "xorl %%eax, %%eax\n\t" > + "cpuid\n\t" > + /* Now fill the cache */ > + "xorl %%eax, %%eax\n\t" > + "12:\n\t" > + "movzbl (%0, %%" _ASM_AX "), %%ecx\n\t" > + "addl $64, %%eax\n\t" > + "cmpl %%eax, %1\n\t" > + "jne 12b\n\t" > + "lfence\n\t" > + : : "r" (empty_zero_pages), "r" (size) > + : "eax", "ebx", "ecx", "edx"); > +} > +EXPORT_SYMBOL_GPL(kvm_l1d_flush); and then you won't need the symbol export either. > int kvm_arch_init(void *opaque) > { > int r; > struct kvm_x86_ops *ops =3D opaque; > + struct page *page; > =20 > if (kvm_x86_ops) { > printk(KERN_ERR "kvm: already loaded the other module\n"); > @@ -6556,10 +6603,15 @@ int kvm_arch_init(void *opaque) > } > =20 > r =3D -ENOMEM; > + page =3D alloc_pages(GFP_ATOMIC, L1D_CACHE_ORDER); GFP_ATOMIC huh? That important. And why isn't this allocation happening in vmx.c? > + if (!page) > + goto out; > + empty_zero_pages =3D page_address(page); > + > shared_msrs =3D alloc_percpu(struct kvm_shared_msrs); > if (!shared_msrs) { > printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n"); > - goto out; > + goto out_free_zero_pages; > } > =20 > r =3D kvm_mmu_module_init(); > @@ -6590,6 +6642,8 @@ int kvm_arch_init(void *opaque) > =20 > return 0; > =20 > +out_free_zero_pages: > + free_pages((unsigned long)empty_zero_pages, L1D_CACHE_ORDER); > out_free_percpu: > free_percpu(shared_msrs); > out: > @@ -6614,6 +6668,7 @@ void kvm_arch_exit(void) > #endif > kvm_x86_ops =3D NULL; > kvm_mmu_module_exit(); > + free_pages((unsigned long)empty_zero_pages, L1D_CACHE_ORDER); > free_percpu(shared_msrs); > } > =20 > @@ -7392,7 +7447,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > =20 > preempt_disable(); > =20 > + vcpu->arch.flush_cache_req =3D vcpu->arch.vcpu_unconfined; Huh, why do we need them both then? ->flush_cache_req should be enough. > kvm_x86_ops->prepare_guest_switch(vcpu); > + vcpu->arch.vcpu_unconfined =3D false; > + if (vcpu->arch.flush_cache_req) > + vcpu->stat.l1d_flush++; > =20 > /* > * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt --=20 Regards/Gruss, Boris. SUSE Linux GmbH, GF: Felix Imend=C3=B6rffer, Jane Smithard, Graham Norton, HR= B 21284 (AG N=C3=BCrnberg) --=20