History

KVM: x86/speculation: Disable Fill buffer clear within guests

The enumeration of MD_CLEAR in CPUID(EAX=7,ECX=0).EDX{bit 10} is not an accurate indicator on all CPUs of whether the VERW instruction will overwrite fill buffers. FB_CLEAR enumeration in IA32_ARCH_CAPABILITIES{bit 17} covers the case of CPUs that are not vulnerable to MDS/TAA, indicating that microcode does overwrite fill buffers. Guests running in VMM environments may not be aware of all the capabilities/vulnerabilities of the host CPU. Specifically, a guest may apply MDS/TAA mitigations when a virtual CPU is enumerated as vulnerable to MDS/TAA even when the physical CPU is not. On CPUs that enumerate FB_CLEAR_CTRL the VMM may set FB_CLEAR_DIS to skip overwriting of fill buffers by the VERW instruction. This is done by setting FB_CLEAR_DIS during VMENTER and resetting on VMEXIT. For guests that enumerate FB_CLEAR (explicitly asking for fill buffer clear capability) the VMM will not use FB_CLEAR_DIS. Irrespective of guest state, host overwrites CPU buffers before VMENTER to protect itself from an MMIO capable guest, as part of mitigation for MMIO Stale Data vulnerabilities.

Pawan Gupta committed 4 years ago

Commit 027bbb81 parent a992b8a5 files · +86 −0Browse files

Unified · Split

5 files
  1. arch/x86/include/asm/msr-index.h +6 −0
  2. arch/x86/kvm/vmx/vmx.c +69 −0
  3. arch/x86/kvm/vmx/vmx.h +2 −0
  4. arch/x86/kvm/x86.c +3 −0
  5. tools/arch/x86/include/asm/msr-index.h +6 −0
arch/x86/include/asm/msr-index.h+6−0
@@ -133,6 +133,11 @@ · 132 unmodified lines
133 * VERW clears CPU fill buffer133 * VERW clears CPU fill buffer
134 * even on MDS_NO CPUs.134 * even on MDS_NO CPUs.
135 */135 */
136#define ARCH_CAP_FB_CLEAR_CTRL BIT(18) /*
137 * MSR_IA32_MCU_OPT_CTRL[FB_CLEAR_DIS]
138 * bit available to control VERW
139 * behavior.
140 */
136 141
137#define MSR_IA32_FLUSH_CMD 0x0000010b142#define MSR_IA32_FLUSH_CMD 0x0000010b
138#define L1D_FLUSH BIT(0) /*143#define L1D_FLUSH BIT(0) /*
@@ -150,6 +155,7 @@ · 11 unmodified lines
150#define MSR_IA32_MCU_OPT_CTRL 0x00000123155#define MSR_IA32_MCU_OPT_CTRL 0x00000123
151#define RNGDS_MITG_DIS BIT(0) /* SRBDS support */156#define RNGDS_MITG_DIS BIT(0) /* SRBDS support */
152#define RTM_ALLOW BIT(1) /* TSX development mode */157#define RTM_ALLOW BIT(1) /* TSX development mode */
158#define FB_CLEAR_DIS BIT(3) /* CPU Fill buffer clear disable */
153 159
154#define MSR_IA32_SYSENTER_CS 0x00000174160#define MSR_IA32_SYSENTER_CS 0x00000174
155#define MSR_IA32_SYSENTER_ESP 0x00000175161#define MSR_IA32_SYSENTER_ESP 0x00000175
arch/x86/kvm/vmx/vmx.c+69−0
@@ -229,6 +229,9 @@ static const struct { · 228 unmodified lines
229#define L1D_CACHE_ORDER 4229#define L1D_CACHE_ORDER 4
230static void *vmx_l1d_flush_pages;230static void *vmx_l1d_flush_pages;
231 231
232/* Control for disabling CPU Fill buffer clear */
233static bool __read_mostly vmx_fb_clear_ctrl_available;
234
232static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)235static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
233{236{
234 struct page *page;237 struct page *page;
@@ -360,6 +363,60 @@ static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) · 125 unmodified lines
360 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);363 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
361}364}
362 365
366static void vmx_setup_fb_clear_ctrl(void)
367{
368 u64 msr;
369
370 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES) &&
371 !boot_cpu_has_bug(X86_BUG_MDS) &&
372 !boot_cpu_has_bug(X86_BUG_TAA)) {
373 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
374 if (msr & ARCH_CAP_FB_CLEAR_CTRL)
375 vmx_fb_clear_ctrl_available = true;
376 }
377}
378
379static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx)
380{
381 u64 msr;
382
383 if (!vmx->disable_fb_clear)
384 return;
385
386 rdmsrl(MSR_IA32_MCU_OPT_CTRL, msr);
387 msr |= FB_CLEAR_DIS;
388 wrmsrl(MSR_IA32_MCU_OPT_CTRL, msr);
389 /* Cache the MSR value to avoid reading it later */
390 vmx->msr_ia32_mcu_opt_ctrl = msr;
391}
392
393static __always_inline void vmx_enable_fb_clear(struct vcpu_vmx *vmx)
394{
395 if (!vmx->disable_fb_clear)
396 return;
397
398 vmx->msr_ia32_mcu_opt_ctrl &= ~FB_CLEAR_DIS;
399 wrmsrl(MSR_IA32_MCU_OPT_CTRL, vmx->msr_ia32_mcu_opt_ctrl);
400}
401
402static void vmx_update_fb_clear_dis(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx)
403{
404 vmx->disable_fb_clear = vmx_fb_clear_ctrl_available;
405
406 /*
407 * If guest will not execute VERW, there is no need to set FB_CLEAR_DIS
408 * at VMEntry. Skip the MSR read/write when a guest has no use case to
409 * execute VERW.
410 */
411 if ((vcpu->arch.arch_capabilities & ARCH_CAP_FB_CLEAR) ||
412 ((vcpu->arch.arch_capabilities & ARCH_CAP_MDS_NO) &&
413 (vcpu->arch.arch_capabilities & ARCH_CAP_TAA_NO) &&
414 (vcpu->arch.arch_capabilities & ARCH_CAP_PSDP_NO) &&
415 (vcpu->arch.arch_capabilities & ARCH_CAP_FBSDP_NO) &&
416 (vcpu->arch.arch_capabilities & ARCH_CAP_SBDR_SSDP_NO)))
417 vmx->disable_fb_clear = false;
418}
419
363static const struct kernel_param_ops vmentry_l1d_flush_ops = {420static const struct kernel_param_ops vmentry_l1d_flush_ops = {
364 .set = vmentry_l1d_flush_set,421 .set = vmentry_l1d_flush_set,
365 .get = vmentry_l1d_flush_get,422 .get = vmentry_l1d_flush_get,
@@ -2252,6 +2309,10 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · 1886 unmodified lines
2252 ret = kvm_set_msr_common(vcpu, msr_info);2309 ret = kvm_set_msr_common(vcpu, msr_info);
2253 }2310 }
2254 2311
2312 /* FB_CLEAR may have changed, also update the FB_CLEAR_DIS behavior */
2313 if (msr_index == MSR_IA32_ARCH_CAPABILITIES)
2314 vmx_update_fb_clear_dis(vcpu, vmx);
2315
2255 return ret;2316 return ret;
2256}2317}
2257 2318
@@ -4553,6 +4614,8 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · 2295 unmodified lines
4553 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);4614 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4554 4615
4555 vpid_sync_context(vmx->vpid);4616 vpid_sync_context(vmx->vpid);
4617
4618 vmx_update_fb_clear_dis(vcpu, vmx);
4556}4619}
4557 4620
4558static void vmx_enable_irq_window(struct kvm_vcpu *vcpu)4621static void vmx_enable_irq_window(struct kvm_vcpu *vcpu)
@@ -6777,6 +6840,8 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, · 2218 unmodified lines
6777 kvm_arch_has_assigned_device(vcpu->kvm))6840 kvm_arch_has_assigned_device(vcpu->kvm))
6778 mds_clear_cpu_buffers();6841 mds_clear_cpu_buffers();
6779 6842
6843 vmx_disable_fb_clear(vmx);
6844
6780 if (vcpu->arch.cr2 != native_read_cr2())6845 if (vcpu->arch.cr2 != native_read_cr2())
6781 native_write_cr2(vcpu->arch.cr2);6846 native_write_cr2(vcpu->arch.cr2);
6782 6847
@@ -6785,6 +6850,8 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, · 2 unmodified lines
6785 6850
6786 vcpu->arch.cr2 = native_read_cr2();6851 vcpu->arch.cr2 = native_read_cr2();
6787 6852
6853 vmx_enable_fb_clear(vmx);
6854
6788 guest_state_exit_irqoff();6855 guest_state_exit_irqoff();
6789}6856}
6790 6857
@@ -8185,6 +8252,8 @@ static int __init vmx_init(void) · 1394 unmodified lines
8185 return r;8252 return r;
8186 }8253 }
8187 8254
8255 vmx_setup_fb_clear_ctrl();
8256
8188 for_each_possible_cpu(cpu) {8257 for_each_possible_cpu(cpu) {
8189 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));8258 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
8190 8259
arch/x86/kvm/vmx/vmx.h+2−0
@@ -348,6 +348,8 @@ struct vcpu_vmx { · 347 unmodified lines
348 u64 msr_ia32_feature_control_valid_bits;348 u64 msr_ia32_feature_control_valid_bits;
349 /* SGX Launch Control public key hash */349 /* SGX Launch Control public key hash */
350 u64 msr_ia32_sgxlepubkeyhash[4];350 u64 msr_ia32_sgxlepubkeyhash[4];
351 u64 msr_ia32_mcu_opt_ctrl;
352 bool disable_fb_clear;
351 353
352 struct pt_desc pt_desc;354 struct pt_desc pt_desc;
353 struct lbr_desc lbr_desc;355 struct lbr_desc lbr_desc;
arch/x86/kvm/x86.c+3−0
@@ -1587,6 +1587,9 @@ static u64 kvm_get_arch_capabilities(void) · 1586 unmodified lines
1587 */1587 */
1588 }1588 }
1589 1589
1590 /* Guests don't need to know "Fill buffer clear control" exists */
1591 data &= ~ARCH_CAP_FB_CLEAR_CTRL;
1592
1590 return data;1593 return data;
1591}1594}
1592 1595
tools/arch/x86/include/asm/msr-index.h+6−0
@@ -133,6 +133,11 @@ · 132 unmodified lines
133 * VERW clears CPU fill buffer133 * VERW clears CPU fill buffer
134 * even on MDS_NO CPUs.134 * even on MDS_NO CPUs.
135 */135 */
136#define ARCH_CAP_FB_CLEAR_CTRL BIT(18) /*
137 * MSR_IA32_MCU_OPT_CTRL[FB_CLEAR_DIS]
138 * bit available to control VERW
139 * behavior.
140 */
136 141
137#define MSR_IA32_FLUSH_CMD 0x0000010b142#define MSR_IA32_FLUSH_CMD 0x0000010b
138#define L1D_FLUSH BIT(0) /*143#define L1D_FLUSH BIT(0) /*
@@ -150,6 +155,7 @@ · 11 unmodified lines
150#define MSR_IA32_MCU_OPT_CTRL 0x00000123155#define MSR_IA32_MCU_OPT_CTRL 0x00000123
151#define RNGDS_MITG_DIS BIT(0) /* SRBDS support */156#define RNGDS_MITG_DIS BIT(0) /* SRBDS support */
152#define RTM_ALLOW BIT(1) /* TSX development mode */157#define RTM_ALLOW BIT(1) /* TSX development mode */
158#define FB_CLEAR_DIS BIT(3) /* CPU Fill buffer clear disable */
153 159
154#define MSR_IA32_SYSENTER_CS 0x00000174160#define MSR_IA32_SYSENTER_CS 0x00000174
155#define MSR_IA32_SYSENTER_ESP 0x00000175161#define MSR_IA32_SYSENTER_ESP 0x00000175

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t