Skip to contentSkip to conversation

L1TF KVM 0

PATCH In review Paolo Bonzini 13 participants 38 messages May 29, 2018 – Jun 11, 2018 5 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover L1TF KVM 0

Here is the first version of the L1 terminal fault KVM mitigation patches, adding a TLB flush on vmentry.

Thanks,

Paolo

author maintainer
#1 lore raw

PATCH 1/2 L1TF KVM 1

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

"vmexit_l1d_flush=2" is simply doing an L1 cache flush on every vmexit. "vmexit_l1d_flush=1" is trying to avoid so on vmexits that are "confined" in the kind of code that they execute. The idea is based on Intel's patches, but the final list of "confined" vmexits isn't quite.

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

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. This part of the work is not ready yet.

For now I'm leaving the default to "vmexit_l1d_flush=2", in case we need to push out the patches in an emergency embargo break, but I don't think it's the best setting. The cost 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).

---
 arch/x86/include/asm/kvm_host.h |  7 ++++-
 arch/x86/kvm/mmu.c              |  1 +
 arch/x86/kvm/svm.c              |  3 +-
 arch/x86/kvm/vmx.c              | 62 ++++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              | 54 +++++++++++++++++++++++++++++++++--
 5 files changed, 122 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -1
index c25775fad4ed..ae4bab8b1f8a 100644
@@ -711,6 +711,9 @@ struct kvm_vcpu_arch {
 
 	/* be preempted when it's in kernel-mode(cpl=0) */
 	bool preempted_in_kernel;
+	
+	/* for L1 terminal fault vulnerability */
+	bool vcpu_unconfined;
 };
 
 struct kvm_lpage_info {
@@ -879,6 +882,7 @@ struct kvm_vcpu_stat {
 	u64 signal_exits;
 	u64 irq_window_exits;
 	u64 nmi_window_exits;
+	u64 l1d_flush;
 	u64 halt_exits;
 	u64 halt_successful_poll;
 	u64 halt_attempted_poll;
@@ -937,7 +941,7 @@ struct kvm_x86_ops {
 	void (*vcpu_free)(struct kvm_vcpu *vcpu);
 	void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
 
-	void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
+	void (*prepare_guest_switch)(struct kvm_vcpu *vcpu, bool *need_l1d_flush);
 	void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
 	void (*vcpu_put)(struct kvm_vcpu *vcpu);
 
@@ -1446,6 +1450,7 @@ bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
 
 void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
 		     struct kvm_lapic_irq *irq);
+void kvm_l1d_flush(void);
 
 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
 {
arch/x86/kvm/mmu.c +1 -0
index 8494dbae41b9..3b1140b156b2 100644
@@ -3836,6 +3836,7 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
 {
 	int r = 1;
 
+	vcpu->arch.vcpu_unconfined = true;
 	switch (vcpu->arch.apf.host_apf_reason) {
 	default:
 		trace_kvm_page_fault(fault_address, error_code);
arch/x86/kvm/svm.c +2 -1
index 1fc05e428aba..849edcd31aad 100644
@@ -5404,8 +5404,9 @@ static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
 		svm->asid_generation--;
 }
 
-static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
+static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu, bool *need_l1d_flush)
 {
+	*need_l1d_flush = false;
 }
 
 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx.c +61 -1

97 lines not on this page · raw message

arch/x86/kvm/x86.c +52 -2

121 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#2 lore raw

PATCH 2/2 L1TF KVM 2

In reply to Paolo Bonzini

Intel's new microcode adds a new feature bit in CPUID[7,0].EDX[28]. If it is active, the displacement flush is unnecessary. Tested on a Coffee Lake machine.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 arch/x86/include/asm/msr-index.h   | 3 +++
 arch/x86/kvm/x86.c                 | 4 ++++
 3 files changed, 8 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 578793e97431..aebf89c4175d 100644
@@ -333,6 +333,7 @@
 #define X86_FEATURE_PCONFIG		(18*32+18) /* Intel PCONFIG */
 #define X86_FEATURE_SPEC_CTRL		(18*32+26) /* "" Speculation Control (IBRS + IBPB) */
 #define X86_FEATURE_INTEL_STIBP		(18*32+27) /* "" Single Thread Indirect Branch Predictors */
+#define X86_FEATURE_FLUSH_L1D		(18*32+28) /* IA32_FLUSH_L1D MSR */
 #define X86_FEATURE_ARCH_CAPABILITIES	(18*32+29) /* IA32_ARCH_CAPABILITIES MSR (Intel) */
 
 /*
arch/x86/include/asm/msr-index.h +3 -0
index 53d5b1b9255e..f43bd9f23053 100644
@@ -65,6 +65,9 @@
 
 #define MSR_MTRRcap			0x000000fe
 
+#define MSR_IA32_FLUSH_L1D             0x10b
+#define MSR_IA32_FLUSH_L1D_VALUE       0x00000001
+
 #define MSR_IA32_ARCH_CAPABILITIES	0x0000010a
 #define ARCH_CAP_RDCL_NO		(1 << 0)   /* Not susceptible to Meltdown */
 #define ARCH_CAP_IBRS_ALL		(1 << 1)   /* Enhanced IBRS support */
arch/x86/kvm/x86.c +4 -0
index ada9e55fc871..43738283aa2a 100644
@@ -6518,6 +6518,10 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 
 void kvm_l1d_flush(void)
 {
+	if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
+		wrmsrl(MSR_IA32_FLUSH_L1D, MSR_IA32_FLUSH_L1D_VALUE);
+		return;
+	}
 	asm volatile(
 		"movq %0, %%rax\n\t"
 		"leaq 65536(%0), %%rdx\n\t"
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Re: [PATCH 1/2] L1TF KVM 1

Quoted 5 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

This is confusing at best. Why is this vmexit_l1d_flush? You flush on VMENTER not on VMEXIT.

What you are doing is to decide whether the last exit reason requires a flush or not. But that decision happens on VMENTER.

Quoted 6 lines

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

What about interrupts?

Quoted 14 lines
(quoted diff) +10 -0
@@ -2423,6 +2428,59 @@ static void vmx_save_host_state(struct kvm_vcpu *vcpu)
 				   vmx->guest_msrs[i].mask);
 }
 
+static inline bool vmx_handling_confined(int reason)
+{
+	switch (reason) {
+	case EXIT_REASON_EXCEPTION_NMI:
+	case EXIT_REASON_HLT:
+	case EXIT_REASON_PAUSE_INSTRUCTION:
+	case EXIT_REASON_APIC_WRITE:
+	case EXIT_REASON_MSR_WRITE:
+	case EXIT_REASON_VMCALL:
+	case EXIT_REASON_CR_ACCESS:

Review static void vmx_save_host_state(struct kvm_vcpu *vcpu) · line 2450

Quoted 10 lines
(quoted diff) +10 -0
+	case EXIT_REASON_DR_ACCESS:
+	case EXIT_REASON_CPUID:
+	case EXIT_REASON_PREEMPTION_TIMER:
+	case EXIT_REASON_MSR_READ:
+	case EXIT_REASON_EOI_INDUCED:
+	case EXIT_REASON_WBINVD:
+	case EXIT_REASON_XSETBV:
+		/*
+		 * The next three set vcpu->arch.vcpu_unconfined themselves, so
+		 * we consider them confined here.

What's the logic behind that?

Review static void vmx_save_host_state(struct kvm_vcpu *vcpu) · line 2459

Quoted 9 lines
(quoted diff) +9 -0
+		 */
+	case EXIT_REASON_EPT_VIOLATION:
+	case EXIT_REASON_EPT_MISCONFIG:
+	case EXIT_REASON_IO_INSTRUCTION:
+		return true;
+	case EXIT_REASON_EXTERNAL_INTERRUPT: {
+		int cpu = raw_smp_processor_id();
+		int vector = per_cpu(last_vector, cpu);
+		return vector == LOCAL_TIMER_VECTOR || vector == RESCHEDULE_VECTOR;

That wants a comment why these two are considered safe.

The timer vector is a doubtful one. It does not necessarily cause a reschedule and it can run arbitrary softirq code on interrupt return. I wouldn't be that sure that it's safe.

Quoted 10 lines
(quoted diff) +10 -0
+	}
+	default:
+		return false;
+	}
+}
+
+static bool vmx_core_confined(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+

Review static void vmx_save_host_state(struct kvm_vcpu *vcpu) · line 2479

Quoted 10 lines
(quoted diff) +10 -0
+	return vmx_handling_confined(vmx->exit_reason);
+}
+
+static void vmx_prepare_guest_switch(struct kvm_vcpu *vcpu, bool *need_l1d_flush)
+{
+	vmx_save_host_state(vcpu);
+	if (vmexit_l1d_flush == 0 || !enable_ept)
+		*need_l1d_flush = false;
+	else if (vmexit_l1d_flush == 1)
+		*need_l1d_flush |= !vmx_core_confined(vcpu);

This inverted logic does not make the code more readable. It's obfuscation for no value.

Quoted 5 lines
(quoted diff) +3 -0
+	else
+		*need_l1d_flush = true;
+}
 

Review static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) · line 9524

Quoted 10 lines
(quoted diff) +2 -0
@@ -9457,11 +9515,13 @@ static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
 		unsigned long entry;
 		gate_desc *desc;
 		struct vcpu_vmx *vmx = to_vmx(vcpu);
+		int cpu = raw_smp_processor_id();
 #ifdef CONFIG_X86_64
 		unsigned long tmp;
 #endif
 
 		vector =  exit_intr_info & INTR_INFO_VECTOR_MASK;
+		per_cpu(last_vector, cpu) = vector;

Why aren't you doing the evaluation of the vector right here and set the unconfined bit instead of having yet another indirection and probably another cache line for that per_cpu() storage? That does not make any sense at all.

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6516

Quoted 11 lines
(quoted diff) +0 -0
 		desc = (gate_desc *)vmx->host_idt_base + vector;
 		entry = gate_offset(desc);
 
arch/x86/kvm/x86.c +2 -0
@@ -6509,10 +6512,40 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 };
 #endif
 
+
+#define L1D_CACHE_ORDER 3

This should use the cache size information and not a hard coded value I think.

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6523

Quoted 7 lines
arch/x86/kvm/x86.c +7 -0
+static void *__read_mostly empty_zero_pages;
+
+void kvm_l1d_flush(void)
+{
+	asm volatile(
+		"movq %0, %%rax\n\t"
+		"leaq 65536(%0), %%rdx\n\t"

Why 64K?

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6530

Quoted 7 lines
arch/x86/kvm/x86.c +7 -0
+		"11: \n\t"
+		"movzbl (%%rax), %%ecx\n\t"
+		"addq $4096, %%rax\n\t"
+		"cmpq %%rax, %%rdx\n\t"
+		"jne 11b\n\t"
+		"xorl %%eax, %%eax\n\t"
+		"cpuid\n\t"

What's the cpuid invocation for?

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6536

Quoted 6 lines
arch/x86/kvm/x86.c +6 -0
+		"xorl %%eax, %%eax\n\t"
+		"12:\n\t"
+		"movzwl %%ax, %%edx\n\t"
+		"addl $64, %%eax\n\t"
+		"movzbl (%%rdx, %0), %%ecx\n\t"
+		"cmpl $65536, %%eax\n\t"

And this whole magic should be documented.

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6541

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+		"jne 12b\n\t"
+		"lfence\n\t"
+		:
+		: "r" (empty_zero_pages)
+		: "rax", "rbx", "rcx", "rdx");

How is that supposed to compile on 32bit?

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6542

Quoted 1 line
arch/x86/kvm/x86.c +1 -0
+}

Aside of that do we really need that manual flush thingy? Is that ucode update going to take forever?

Thanks,

tglx

#5 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 34 lines · speck for Thomas Gleixner

Review

Quoted 7 lines · speck for Paolo Bonzini
(quoted diff) +7 -0
+static void *__read_mostly empty_zero_pages;
+
+void kvm_l1d_flush(void)
+{
+	asm volatile(
+		"movq %0, %%rax\n\t"
+		"leaq 65536(%0), %%rdx\n\t"

Why 64K?

Review

Quoted 7 lines
(quoted diff) +7 -0
+		"11: \n\t"
+		"movzbl (%%rax), %%ecx\n\t"
+		"addq $4096, %%rax\n\t"
+		"cmpq %%rax, %%rdx\n\t"
+		"jne 11b\n\t"
+		"xorl %%eax, %%eax\n\t"
+		"cpuid\n\t"

What's the cpuid invocation for?

Review

Quoted 6 lines
(quoted diff) +6 -0
+		"xorl %%eax, %%eax\n\t"
+		"12:\n\t"
+		"movzwl %%ax, %%edx\n\t"
+		"addl $64, %%eax\n\t"
+		"movzbl (%%rdx, %0), %%ecx\n\t"
+		"cmpl $65536, %%eax\n\t"

And this whole magic should be documented.

Review

Quoted 5 lines
(quoted diff) +5 -0
+		"jne 12b\n\t"
+		"lfence\n\t"
+		:
+		: "r" (empty_zero_pages)
+		: "rax", "rbx", "rcx", "rdx");

How is that supposed to compile on 32bit?

Quoted 3 lines
Quoted 1 line

+}

Aside of that do we really need that manual flush thingy? Is that ucode update going to take forever?

I already provided feedback on this software loop, but have had radio silence as a result.  The CPUID is serialisation (best as I can guess) to terminate any WC buffer which got hit, but this is going to truly suck inside a VM.  If it is for full serialisation properties, the least overhead option would be a write to %cr2, which is serialising on all affected parts.

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

~Andrew

author maintainer
#6 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

Paolo

#7 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Martin

#8 lore raw

Re: [PATCH 1/2] L1TF KVM 1

Quoted 3 lines · speck for Martin Pohlack

-----BEGIN PGP MESSAGE-----
Charset: windows-1252
Version: GnuPG v2

Sorry the remailer failed to decrypt that message. Investigating.

#9 lore raw

Re: [PATCH 1/2] L1TF KVM 1

Quoted 7 lines · speck for Thomas Gleixner
Quoted 3 lines · speck for Martin Pohlack

-----BEGIN PGP MESSAGE-----
Charset: windows-1252
Version: GnuPG v2

Sorry the remailer failed to decrypt that message. Investigating.

It was missing a decoding from quoted-printable which is required due to charset = windows-1252. Fixed and replayed.

Thanks,

tglx

#10 lore raw

PATCH 1/2 L1TF KVM 1

In reply to Paolo Bonzini

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Martin

#11 lore raw

[MODERATED] Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Isn't Tim Chen from Intel on this mailing list? Tim, could you find out please?

Quoted 2 lines

Martin

#12 lore raw

[MODERATED] Encrypted Message

From: Tim Chen <[email protected]>
To: speck for Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 19 lines · speck for Konrad Rzeszutek Wilk
Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 8 lines
Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Isn't Tim Chen from Intel on this mailing list? Tim, could you find out please?

Will do.

Tim

#13 lore raw

[MODERATED] Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

In reply to Konrad Rzeszutek Wilk
Quoted 19 lines · speck for Konrad Rzeszutek Wilk
Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 7 lines
Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Isn't Tim Chen from Intel on this mailing list? Tim, could you find out please?

I had assumed it was for the more straightforward reason that $future uarch has a 64K L1D$, at least according to "The Internet" (TM):

https://en.wikichip.org/wiki/intel/core_i3/i3-8121u

It ought to be associativity that impacts the displacement itself, not the LRU policy since you still end up with the L1D being updated.

Jon.

Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop
#14 lore raw

[MODERATED] Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 5 lines · speck for Jon Masters

I had assumed it was for the more straightforward reason that $future uarch has a 64K L1D$, at least according to "The Internet" (TM):

https://en.wikichip.org/wiki/intel/core_i3/i3-8121u

No, that 64kB is just 2x32kB due to two cores.

It's still 8-way and 32kB per core, from what I can tell.

Linus

#15 lore raw

[MODERATED] Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

In reply to Jon Masters
Quoted 28 lines · speck for Jon Masters
Quoted 19 lines · speck for Konrad Rzeszutek Wilk
Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 7 lines
Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Isn't Tim Chen from Intel on this mailing list? Tim, could you find out please?

Quoted 7 lines

I had assumed it was for the more straightforward reason that $future uarch has a 64K L1D$, at least according to "The Internet" (TM):

https://en.wikichip.org/wiki/intel/core_i3/i3-8121u

It ought to be associativity that impacts the displacement itself, not the LRU policy since you still end up with the L1D being updated.

Associativity should already be well covered as the flush array is laid out contiguously, so reading 32K should be enough assuming a perfect LRU implementation and no interference from the page table walker.

Martin

#16 lore raw

[MODERATED] Encrypted Message

In reply to Konrad Rzeszutek Wilk

From: Tim Chen <[email protected]>
To: speck for Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 19 lines · speck for Konrad Rzeszutek Wilk
Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 5 lines
Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Had some discussions with other Intel folks.

Our recommendation is not to use the software sequence for L1 clear but use wrmsrl(MSR_IA32_FLUSH_L1D, MSR_IA32_FLUSH_L1D_VALUE).
We expect that all affected systems will be receiving a ucode update to provide L1 clearing capability.

Yes, the 4k stride is for getting TLB walks out of the way and the 64kB replacement is to accommodate pseudo LRU.

Thanks.

Tim

#17 lore raw

[MODERATED] Encrypted Message

From: Tim Chen <[email protected]>
To: speck for Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 28 lines · Tim Chen
Quoted 19 lines · speck for Konrad Rzeszutek Wilk
Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 5 lines
Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Had some discussions with other Intel folks.

Quoted 7 lines

Our recommendation is not to use the software sequence for L1 clear but use wrmsrl(MSR_IA32_FLUSH_L1D, MSR_IA32_FLUSH_L1D_VALUE).
We expect that all affected systems will be receiving a ucode update to provide L1 clearing capability.

Yes, the 4k stride is for getting TLB walks out of the way and the 64kB replacement is to accommodate pseudo LRU.

I will try to see if I can get hold of the relevant documentation on pseudo LRU.

Tim

#18 lore raw

[MODERATED] Encrypted Message

From: Tim Chen <[email protected]>
To: speck for Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 34 lines · Tim Chen
Quoted 33 lines · Tim Chen
Quoted 19 lines · speck for Konrad Rzeszutek Wilk
Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 5 lines
Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Had some discussions with other Intel folks.

Our recommendation is not to use the software sequence for L1 clear but use wrmsrl(MSR_IA32_FLUSH_L1D, MSR_IA32_FLUSH_L1D_VALUE).
We expect that all affected systems will be receiving a ucode update to provide L1 clearing capability.

Quoted 6 lines
Quoted 2 lines

Yes, the 4k stride is for getting TLB walks out of the way and the 64kB replacement is to accommodate pseudo LRU.

I will try to see if I can get hold of the relevant documentation on pseudo LRU.

The HW folks mentioned that if we have nothing from the flush buffer in L1, then 32 KB would be sufficient (if we load miss for everything).

However, that's not the case. If some data from the flush buffer is already in L1, it could protect an unrelated line that's considered "near" by the LRU from getting flushed. To make sure that does not happen, we go through 64 KB of data to guarantee every line in L1 will encounter a load miss and is flushed.

Tim

#19 lore raw

[MODERATED] Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 46 lines · speck for Tim Chen

From: Tim Chen <[email protected]>
To: speck for Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 34 lines · Tim Chen
Quoted 33 lines · Tim Chen
Quoted 19 lines · speck for Konrad Rzeszutek Wilk
Quoted 18 lines · speck for Martin Pohlack

[resending as new message as the replay seems to have been lost on at least some mail paths]

Quoted 8 lines · speck for Paolo Bonzini
Quoted 4 lines · speck for Andrew Cooper

Other bits I don't understand are the 64k limit in the first place, why it gets walked over in 4k strides to begin with (I'm not aware of any prefetching which would benefit that...) and why a particularly obfuscated piece of magic is used for the 64byte strides.

That is the only part I understood, :) the 4k strides ensure that the source data is in the TLB. Why that is needed is still a mystery though.

I think the reasoning is that you first want to populate the TLB for the whole flush array, then fence, to make sure TLB walks do not interfere with the actual flushing later, either for performance reasons or for preventing leakage of partial walk results.

Quoted 5 lines
Quoted 4 lines

Not sure about the 64K, it likely is about the LRU implementation for L1 replacement not being perfect (but pseudo LRU), so you need to flush more than the L1 size (32K) in software. But I have also seen smaller recommendations for that (52K).

Had some discussions with other Intel folks.

Our recommendation is not to use the software sequence for L1 clear but use wrmsrl(MSR_IA32_FLUSH_L1D, MSR_IA32_FLUSH_L1D_VALUE).
We expect that all affected systems will be receiving a ucode update to provide L1 clearing capability.

Quoted 6 lines
Quoted 2 lines

Yes, the 4k stride is for getting TLB walks out of the way and the 64kB replacement is to accommodate pseudo LRU.

I will try to see if I can get hold of the relevant documentation on pseudo LRU.

Quoted 8 lines

The HW folks mentioned that if we have nothing from the flush buffer in L1, then 32 KB would be sufficient (if we load miss for everything).

However, that's not the case. If some data from the flush buffer is already in L1, it could protect an unrelated line that's considered "near" by the LRU from getting flushed. To make sure that does not happen, we go through 64 KB of data to guarantee every line in L1 will encounter a load miss and is flushed.

Also the recommended mitigation is really to use the MSR write instead of the magic software sequence. Perhaps it would be best to
just remove the software sequence. Updated microcode is needed in any case, it doesn't make sense to try to support partially updated systems.

-Andi

#20 lore raw

Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 4 lines · speck for Andi Kleen

Also the recommended mitigation is really to use the MSR write instead of the magic software sequence. Perhaps it would be best to
just remove the software sequence. Updated microcode is needed in any case, it doesn't make sense to try to support partially updated systems.

ACK. That makes sense.

Thanks,

tglx

#21 lore raw

[MODERATED] Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 7 lines · speck for Thomas Gleixner
Quoted 4 lines · speck for Andi Kleen

Also the recommended mitigation is really to use the MSR write instead of the magic software sequence. Perhaps it would be best to
just remove the software sequence. Updated microcode is needed in any case, it doesn't make sense to try to support partially updated systems.

ACK. That makes sense.

In that case do we need plumbing to expose the L1 cache flush MSR to the guest, for nested virt support?

Signature · 1 line
Josh
author maintainer
#22 lore raw

[MODERATED] Re: Is: Tim, Q to you. Was:Re: [PATCH 1/2] L1TF KVM 1

Quoted 9 lines · speck for Josh Poimboeuf
Quoted 6 lines · speck for Thomas Gleixner
Quoted 4 lines · speck for Andi Kleen

Also the recommended mitigation is really to use the MSR write instead of the magic software sequence. Perhaps it would be best to
just remove the software sequence. Updated microcode is needed in any case, it doesn't make sense to try to support partially updated systems.

ACK. That makes sense.

In that case do we need plumbing to expose the L1 cache flush MSR to the guest, for nested virt support?

No, because all L2->L1 exits go through L0 first. So nested virt systems are not vulnerable.

Is there a sanctioned way (in CPUID or elsewhere) to say the system does not have the bug?

Paolo (back on track after moving to FPU land for a few days)

#23 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

In reply to Thomas Gleixner
Quoted 10 lines · speck for Thomas Gleixner

Review

Quoted 4 lines
(quoted diff) +4 -0
+	case EXIT_REASON_EXTERNAL_INTERRUPT: {
+		int cpu = raw_smp_processor_id();
+		int vector = per_cpu(last_vector, cpu);
+		return vector == LOCAL_TIMER_VECTOR || vector == RESCHEDULE_VECTOR;

That wants a comment why these two are considered safe.

The timer vector is a doubtful one. It does not necessarily cause a reschedule and it can run arbitrary softirq code on interrupt return. I wouldn't be that sure that it's safe.

reschedule can also cause softirq to run.

And there's just no way we can guarantee softirq doesn't do something sensitive, like IPsec processing or whatever.

author maintainer
#24 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

In reply to Thomas Gleixner
Quoted 9 lines · speck for Thomas Gleixner
Quoted 5 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

This is confusing at best. Why is this vmexit_l1d_flush? You flush on VMENTER not on VMEXIT.

Yes, that makes sense.

Quoted 8 lines
Quoted 6 lines

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

What about interrupts?

Will fix the commit message and rework the patch to set vcpu_unconfined at vmexit time.

Quoted 11 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+		/*
+		 * The next three set vcpu->arch.vcpu_unconfined themselves, so
+		 * we consider them confined here.

What's the logic behind that?

Review

Quoted 5 lines
(quoted diff) +5 -0
+		 */
+	case EXIT_REASON_EPT_VIOLATION:
+	case EXIT_REASON_EPT_MISCONFIG:
+	case EXIT_REASON_IO_INSTRUCTION:
+		return true;

EPT misconfig and I/O instruction are very frequent, and most of the time they run just a small fast path. EPT violation can be put together with the "always slow" ones.

Quoted 10 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	case EXIT_REASON_EXTERNAL_INTERRUPT: {
+		int cpu = raw_smp_processor_id();
+		int vector = per_cpu(last_vector, cpu);
+		return vector == LOCAL_TIMER_VECTOR || vector == RESCHEDULE_VECTOR;

That wants a comment why these two are considered safe.

The timer vector is a doubtful one. It does not necessarily cause a reschedule and it can run arbitrary softirq code on interrupt return. I wouldn't be that sure that it's safe.

It's also the most frequent one. :( But I see your and Peter's point, I'll drop it and consider all external interrupts to be unconfined.

May there could be some kind of "ran softirq" generation count.

Quoted 9 lines
Quoted 9 lines
(quoted diff) +1 -0
@@ -9457,11 +9515,13 @@ static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
 		unsigned long entry;
 		gate_desc *desc;
 		struct vcpu_vmx *vmx = to_vmx(vcpu);
+		int cpu = raw_smp_processor_id();
 #ifdef CONFIG_X86_64
 		unsigned long tmp;
 #endif
 
Quoted 7 lines

Review static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) · line 9524

Quoted 2 lines
(quoted diff) +1 -0
 		vector =  exit_intr_info & INTR_INFO_VECTOR_MASK;
+		per_cpu(last_vector, cpu) = vector;

Why aren't you doing the evaluation of the vector right here and set the unconfined bit instead of having yet another indirection and probably another cache line for that per_cpu() storage? That does not make any sense at all.

Because that's how the patches I got from Intel did it, and I kind of liked keeping all the logic in one function. But it's going to go away, it's not safe.

Quoted 9 lines
Quoted 9 lines
(quoted diff) +0 -0
 		desc = (gate_desc *)vmx->host_idt_base + vector;
 		entry = gate_offset(desc);
 
arch/x86/kvm/x86.c +0 -0
@@ -6509,10 +6512,40 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 };
 #endif
 
Quoted 4 lines

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6516

Quoted 2 lines
arch/x86/kvm/x86.c +2 -0
+
+#define L1D_CACHE_ORDER 3

This should use the cache size information and not a hard coded value I think.

Plus it seems wrong. Order 3 is 32K, not 64K, isn't it? :/

Quoted 4 lines

And this whole magic should be documented.

Aside of that do we really need that manual flush thingy? Is that ucode update going to take forever?

As Andrew said, this was also copied from Intel's patch, assuming they knew what they were doing. I'll just drop it and just use the microcode MSR.

Paolo

#25 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

In reply to Thomas Gleixner
Quoted 10 lines · speck for Thomas Gleixner

Review

Quoted 5 lines · speck for Paolo Bonzini
(quoted diff) +5 -0
+void kvm_l1d_flush(void)
+{
+	asm volatile(
+		"movq %0, %%rax\n\t"
+		"leaq 65536(%0), %%rdx\n\t"

Why 64K?

Quoted 7 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+		"11: \n\t"
+		"movzbl (%%rax), %%ecx\n\t"
+		"addq $4096, %%rax\n\t"
+		"cmpq %%rax, %%rdx\n\t"
+		"jne 11b\n\t"
+		"xorl %%eax, %%eax\n\t"
+		"cpuid\n\t"

My guess is they're saying that the maximum L1D$ size is 64K so they want to stride it 1 4K page at a time to get the prefetchers going ahead of the next loop...this in theory will make the following loop "faster".

Quoted 7 lines

What's the cpuid invocation for?

Review

Quoted 5 lines
(quoted diff) +5 -0
+		"xorl %%eax, %%eax\n\t"
+		"12:\n\t"
+		"movzwl %%ax, %%edx\n\t"
+		"addl $64, %%eax\n\t"
+		"movzbl (%%rdx, %0), %%ecx\n\t">> +		"cmpl $65536, %%eax\n\t"

...which then tries to do 64 bytes (Intel cache line) at a time.

They use the CPUID as a serializing instruction to ensure the store has been observed, others have commented on that.

Jon.

Signature · 1 line
Computer Architect | Sent from my Fedora powered laptop

A message that is not in the archive (sent off-list or not yet received): [email protected]

#27 lore raw

[MODERATED] Re: [PATCH 2/2] L1TF KVM 2

Quoted 45 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 2/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

Intel's new microcode adds a new feature bit in CPUID[7,0].EDX[28]. If it is active, the displacement flush is unnecessary. Tested on a Coffee Lake machine.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 arch/x86/include/asm/msr-index.h   | 3 +++
 arch/x86/kvm/x86.c                 | 4 ++++
 3 files changed, 8 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 578793e97431..aebf89c4175d 100644
@@ -333,6 +333,7 @@
 #define X86_FEATURE_PCONFIG		(18*32+18) /* Intel PCONFIG */
 #define X86_FEATURE_SPEC_CTRL		(18*32+26) /* "" Speculation Control (IBRS + IBPB) */
 #define X86_FEATURE_INTEL_STIBP		(18*32+27) /* "" Single Thread Indirect Branch Predictors */
+#define X86_FEATURE_FLUSH_L1D		(18*32+28) /* IA32_FLUSH_L1D MSR */
 #define X86_FEATURE_ARCH_CAPABILITIES	(18*32+29) /* IA32_ARCH_CAPABILITIES MSR (Intel) */
 
 /*
arch/x86/include/asm/msr-index.h +3 -0
index 53d5b1b9255e..f43bd9f23053 100644
@@ -65,6 +65,9 @@
 
 #define MSR_MTRRcap			0x000000fe
 
+#define MSR_IA32_FLUSH_L1D             0x10b
+#define MSR_IA32_FLUSH_L1D_VALUE       0x00000001
+
 #define MSR_IA32_ARCH_CAPABILITIES	0x0000010a
 #define ARCH_CAP_RDCL_NO		(1 << 0)   /* Not susceptible to Meltdown */
 #define ARCH_CAP_IBRS_ALL		(1 << 1)   /* Enhanced IBRS support */
arch/x86/kvm/x86.c +0 -0
index ada9e55fc871..43738283aa2a 100644

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 6524

Quoted 7 lines
arch/x86/kvm/x86.c +4 -0
@@ -6518,6 +6518,10 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 
 void kvm_l1d_flush(void)
 {
+	if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
+		wrmsrl(MSR_IA32_FLUSH_L1D, MSR_IA32_FLUSH_L1D_VALUE);
+		return;
+	}

What is this supposed to achieve?  Sure, most of the cache content has disappeared, but some of the most interesting parts are still left available to the guest.

In Xen, I've come to the conclusion that the only viable option here is for a guest load-only MSR entry.  Without this, the GPRs at the most recent vmentry are still available in the cache (as there is no way for the hypervisor to rationally zero them), which results in a guest kenrel => guest user leak if a vmentry occurs late in the guest kernels return-to-userspace path.

A guest kernel which is implementing the PTE mitigation is immune to this attack, but the hypervisor does not know a priori whether this is the case or not.

~Andrew

#28 lore raw

Re: [PATCH 2/2] L1TF KVM 2

Quoted 11 lines · speck for Andrew Cooper

On 29/05/2018 20:42, speck for Paolo Bonzini wrote:
In Xen, I've come to the conclusion that the only viable option here is for a guest load-only MSR entry.  Without this, the GPRs at the most recent vmentry are still available in the cache (as there is no way for the hypervisor to rationally zero them), which results in a guest kenrel => guest user leak if a vmentry occurs late in the guest kernels return-to-userspace path.

A guest kernel which is implementing the PTE mitigation is immune to this attack, but the hypervisor does not know a priori whether this is the case or not.

And why would you care about some outdated guest kernel, which is vulnerable against lots of other stuff as well?

Thanks,

tglx

#29 lore raw

[MODERATED] Re: [PATCH 2/2] L1TF KVM 2

Quoted 9 lines · speck for Thomas Gleixner
Quoted 8 lines · speck for Andrew Cooper

On 29/05/2018 20:42, speck for Paolo Bonzini wrote:
In Xen, I've come to the conclusion that the only viable option here is for a guest load-only MSR entry.  Without this, the GPRs at the most recent vmentry are still available in the cache (as there is no way for the hypervisor to rationally zero them), which results in a guest kenrel => guest user leak if a vmentry occurs late in the guest kernels return-to-userspace path.

Quoted 5 lines
Quoted 3 lines

A guest kernel which is implementing the PTE mitigation is immune to this attack, but the hypervisor does not know a priori whether this is the case or not.

And why would you care about some outdated guest kernel, which is vulnerable against lots of other stuff as well?

That's a very good point.  It need not matter for the guest kernel => user leak.

However, to avoid leaking other hypervisor data into guest context, you need to account for the possibility of interrupts/exceptions late in the vmentry path, which includes an NMI hitting on the instruction boundary before vmresume.  Failure to account for this case will, most easily, leak hypervisor GPRs into guest context.

Unlike MSR_SPEC_CTRL (which can be written "shortly before the iret/vmentry"), issuing the flush before restoring GPRs is ineffective at preventing leakage, and a write to MSR_FLUSH_CMD cannot be performed after restoring GPRs, other than with a MSR guest load list entry.

~Andrew

#30 lore raw

Re: [PATCH 2/2] L1TF KVM 2

Quoted 16 lines · speck for Andrew Cooper
Quoted 9 lines · speck for Thomas Gleixner
Quoted 8 lines · speck for Andrew Cooper

On 29/05/2018 20:42, speck for Paolo Bonzini wrote:
In Xen, I've come to the conclusion that the only viable option here is for a guest load-only MSR entry.  Without this, the GPRs at the most recent vmentry are still available in the cache (as there is no way for the hypervisor to rationally zero them), which results in a guest kenrel => guest user leak if a vmentry occurs late in the guest kernels return-to-userspace path.

Quoted 5 lines
Quoted 3 lines

A guest kernel which is implementing the PTE mitigation is immune to this attack, but the hypervisor does not know a priori whether this is the case or not.

And why would you care about some outdated guest kernel, which is vulnerable against lots of other stuff as well?

Quoted 8 lines

That's a very good point.  It need not matter for the guest kernel => user leak.

However, to avoid leaking other hypervisor data into guest context, you need to account for the possibility of interrupts/exceptions late in the vmentry path, which includes an NMI hitting on the instruction boundary before vmresume.  Failure to account for this case will, most easily, leak hypervisor GPRs into guest context.

Right, but you really have to make a judgement whether this leak is useful and can be orchestrated by the attacker in the guest. If it's just the theoretical cornercase with no practical attack surface then you really can leave that as an exercise for ivory tower people.

Thanks,

tglx

#31 lore raw

[MODERATED] Re: [PATCH 2/2] L1TF KVM 2

Quoted 4 lines

Right, but you really have to make a judgement whether this leak is useful and can be orchestrated by the attacker in the guest. If it's just the theoretical cornercase with no practical attack surface then you really can leave that as an exercise for ivory tower people.

We care about user data and kernel secrets like keys.

NMIs don't have either.

(except for profile NMIs, but they only have the data of what you just interrupted)

Same for machine checks etc.

In fact most hard interrupts are likely uninteresting (except those that copy user data), although soft interrupts may not be.

-Andi

#32 lore raw

[MODERATED] Re: [PATCH 2/2] L1TF KVM 2

Quoted 8 lines · speck for Andi Kleen
Quoted 4 lines

Right, but you really have to make a judgement whether this leak is useful and can be orchestrated by the attacker in the guest. If it's just the theoretical cornercase with no practical attack surface then you really can leave that as an exercise for ivory tower people.

We care about user data and kernel secrets like keys.

NMIs don't have either.

Quoted 7 lines

(except for profile NMIs, but they only have the data of what you just interrupted)

Same for machine checks etc.

In fact most hard interrupts are likely uninteresting (except those that copy user data), although soft interrupts may not be.

Until we have instructions for how to turn off next-page prefetch (Haswell and later, I believe), *any* memory access is liable to pull in unrelated data from adjacent pages.  Accesses into the directmap are liable to pull in data from a completely unrelated context, which in Xen's case has a reasonable chance of being data from another VM, and in Linux's case, data from another process.

Even with regular prefetching, you're highly likely to pull in a cache line or two from an adjacent heap object.

Maybe I am being too pessimistic, but at this point I don't buy the argument of "architecturally, we don't touch any sensitive data, therefore it won't be the cache".  Whether said data is usefully extractable by an attacker is a different matter, but I don't feel comfortable betting peoples data isolation on the expectation that it isn't extractable.

~Andrew

A message that is not in the archive (sent off-list or not yet received): [email protected]

#34 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 10 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

"vmexit_l1d_flush=2" is simply doing an L1 cache flush on every vmexit. "vmexit_l1d_flush=1" is trying to avoid so on vmexits that are "confined" in the kind of code that they execute. The idea is based on Intel's patches, but the final list of "confined" vmexits isn't quite.

s/quite/quite fleshed out/

Quoted 13 lines

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

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. This part of the work is not ready yet.

Quoted 6 lines

For now I'm leaving the default to "vmexit_l1d_flush=2", in case we need to push out the patches in an emergency embargo break, but I don't think it's the best setting. The cost 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).

This looks very much like what Jun had. If this was based off Intel code would it make sense to give credit to him by saying something along:

"Ideas and some code based off from Jun .." ?

author maintainer
#35 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 7 lines · speck for Konrad Rzeszutek Wilk
Quoted 6 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

Quoted 6 lines
Quoted 4 lines

"vmexit_l1d_flush=2" is simply doing an L1 cache flush on every vmexit. "vmexit_l1d_flush=1" is trying to avoid so on vmexits that are "confined" in the kind of code that they execute. The idea is based on Intel's patches, but the final list of "confined" vmexits isn't quite.

s/quite/quite fleshed out/

No, not quite based on Intel's patches. :)

Quoted 20 lines
Quoted 13 lines

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

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. This part of the work is not ready yet.

Quoted 6 lines

For now I'm leaving the default to "vmexit_l1d_flush=2", in case we need to push out the patches in an emergency embargo break, but I don't think it's the best setting. The cost 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).

Quoted 4 lines

This looks very much like what Jun had. If this was based off Intel code would it make sense to give credit to him by saying something along:

"Ideas and some code based off from Jun .." ?

I was not sure who the author of Intel's code was, so I left in the references above. Really the only thing I lifted from there was the L1 flush; the "confined" moniker got stuck in my brain, but all the other code was rewritten from scratch. I'll resend a new version with review comments applied tomorrow or Wednesday.

Paolo

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#37 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Review

Quoted 5 lines · speck for Paolo Bonzini
(quoted diff) +4 -0
 	r = -ENOMEM;
+	page = alloc_pages(GFP_ATOMIC, L1D_CACHE_ORDER);
+	if (!page)
+		goto out;
+	empty_zero_pages = page_address(page);

There is also an Intel suggestion to have guard pages before and after the L1D flush buffer. As it stands, the prefetchers might pull data into the cache from pages adjacent to the allocation you have there.

You can use vmalloc(), where we get (unmapped) guard pages already. Or, you can just oversize the allocation using:

alloc_pages_exact(L1D_CACHE_ORDER * PAGE_SIZE + 2 * PAGE_SIZE)

and just point empty_zero_pages to the second page in the buffer:

	empty_zero_pages = page_address(page + 1);

I'd suggest the alloc_pages_exact() version. It will chew up fewer TLB entries.

#38 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Review

Quoted 6 lines
(quoted diff) +5 -0
 	r = -ENOMEM;
+	page = alloc_pages(GFP_ATOMIC, L1D_CACHE_ORDER);
+	if (!page)
+		goto out;
+	empty_zero_pages = page_address(page);
+

Couple more things:

PAGE_SIZE<<L1D_CACHE_ORDER is is only 32k, right? The assembly sequence clears 64k IIRC:

Review

Quoted 6 lines
(quoted diff) +6 -0
+void kvm_l1d_flush(void)
+{
+	asm volatile(
+		"movq %0, %%rax\n\t"
+		"leaq 65536(%0), %%rdx\n\t"
+		"11: \n\t"

...

So we probably want the allocation to be something like:

	l1d_clear_buf_size = PAGE_SIZE << (L1D_CACHE_ORDER+1) +
			     2 * PAGE_SIZE;
	alloc_pages_exact(l1d_clear_buf_size, GFP_ZERO|GFP_ATOMIC);

Note that we need GFP_ZERO because we could theoretically get a page that already had kernel secrets in it.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#40 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 23 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

"vmexit_l1d_flush=2" is simply doing an L1 cache flush on every vmexit. "vmexit_l1d_flush=1" is trying to avoid so on vmexits that are "confined" in the kind of code that they execute. The idea is based on Intel's patches, but the final list of "confined" vmexits isn't quite.

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

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. This part of the work is not ready yet.

Quoted 7 lines

For now I'm leaving the default to "vmexit_l1d_flush=2", in case we need to push out the patches in an emergency embargo break, but I don't think it's the best setting. The cost 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).

I think we should report the L1 flushing mitigation value (i.e., 0, 1 or
2) in the 'l1tf' vulnerabilities sysfs file.

Also, it would be clearer to name them something like

vmexit_l1d_flush=off
vmexit_l1d_flush=confined
vmexit_l1d_flush=on

etc.

Signature · 1 line
Josh
#41 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 30 lines · speck for Josh Poimboeuf
Quoted 29 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

"vmexit_l1d_flush=2" is simply doing an L1 cache flush on every vmexit. "vmexit_l1d_flush=1" is trying to avoid so on vmexits that are "confined" in the kind of code that they execute. The idea is based on Intel's patches, but the final list of "confined" vmexits isn't quite.

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

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. This part of the work is not ready yet.

For now I'm leaving the default to "vmexit_l1d_flush=2", in case we need to push out the patches in an emergency embargo break, but I don't think it's the best setting. The cost 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).

Quoted 4 lines
Quoted 1 line

I think we should report the L1 flushing mitigation value (i.e., 0, 1 or
2) in the 'l1tf' vulnerabilities sysfs file.

But this is KVM module, not the generic code (bugs.c).

Or did you have in mind some sub-registration code so that the KVM module can register its state of mind with bugs.c reporting?

Like 'L1D flush on VMENTER','IPI on VMEXIT', etc?

#42 lore raw

[MODERATED] Re: [PATCH 1/2] L1TF KVM 1

Quoted 36 lines · speck for Konrad Rzeszutek Wilk
Quoted 30 lines · speck for Josh Poimboeuf
Quoted 29 lines · speck for Paolo Bonzini

From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] kvm: x86: mitigation for L1 cache terminal fault vulnerabilities

This patch adds two mitigation modes for CVE-2018-3620, aka L1 terminal fault. The two modes are "vmexit_l1d_flush=1" and "vmexit_l1d_flush=2".

"vmexit_l1d_flush=2" is simply doing an L1 cache flush on every vmexit. "vmexit_l1d_flush=1" is trying to avoid so on vmexits that are "confined" in the kind of code that they execute. The idea is based on Intel's patches, but the final list of "confined" vmexits isn't quite.

Notably, L1 cache flushes are performed on EPT violations (which are basically KVM-level page faults), vmexits that involve the emulator, and on every KVM_RUN invocation (so each userspace exit). However, most vmexits are considered safe. I singled out the emulator because it may be a good target for other speculative execution-based threats, and the MMU because it can bring host page tables in the L1 cache.

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. This part of the work is not ready yet.

For now I'm leaving the default to "vmexit_l1d_flush=2", in case we need to push out the patches in an emergency embargo break, but I don't think it's the best setting. The cost 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).

Quoted 4 lines
Quoted 1 line

I think we should report the L1 flushing mitigation value (i.e., 0, 1 or
2) in the 'l1tf' vulnerabilities sysfs file.

Quoted 6 lines

But this is KVM module, not the generic code (bugs.c).

Or did you have in mind some sub-registration code so that the KVM module can register its state of mind with bugs.c reporting?

Like 'L1D flush on VMENTER','IPI on VMEXIT', etc?

Right, just something as simple as setting a global variable (which lives in bugs.c) would probably be good enough.

Signature · 1 line
Josh