Skip to contentSkip to conversation

XSA-407 followon fixes

PATCH In review Andrew Cooper 2 participants 7 messages Jul 18, 2022 – Jul 19, 2022

lore
Share & tools
lore
#0 lore raw

cover XSA-407 followon fixes

Fixes accumulated during XSA-407 which weren't security worthy in and of themselves.

Andrew Cooper (3):
x86/spec-ctrl: Consistently halt speculation using int3 x86/spec-ctrl: Make svm_vmexit_spec_ctrl conditional x86/spec-ctrl: Shrink further entry paths due to %rdx being 0

 xen/arch/x86/hvm/svm/entry.S             | 10 +++++-----
 xen/arch/x86/hvm/vmx/entry.S             |  4 +---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 14 ++++----------
 xen/arch/x86/indirect-thunk.S            |  6 ++----
 4 files changed, 12 insertions(+), 22 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/3 x86/spec-ctrl: Consistently halt speculation using int3

The RSB stuffing loop and retpoline thunks date from the very beginning, when halting speculation was a brand new field.

These days, we've largely settled on int3 for halting speculation in non-architectural paths. It's a single byte, and is fully serialising - a requirement for delivering #BP if it were to execute.

Update the thunks. Mostly for consistency across the codebase, but it does shrink every entrypath in Xen by 6 bytes which is a marginal win.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 11 +++--------
 xen/arch/x86/indirect-thunk.S            |  6 ++----
 2 files changed, 5 insertions(+), 12 deletions(-)
xen/arch/x86/include/asm/spec_ctrl_asm.h +3 -8
index 9eb4ad9ab71d..fab27ff5532b 100644
@@ -126,9 +126,8 @@
  * change. Based on Google's performance numbers, the loop is unrolled to 16
  * iterations and two calls per iteration.
  *
- * The call filling the RSB needs a nonzero displacement.  A nop would do, but
- * we use "1: pause; lfence; jmp 1b" to safely contains any ret-based
- * speculation, even if the loop is speculatively executed prematurely.
+ * The call filling the RSB needs a nonzero displacement, and int3 halts
+ * speculation.
  *
  * %rsp is preserved by using an extra GPR because a) we've got plenty spare,
  * b) the two movs are shorter to encode than `add $32*8, %rsp`, and c) can be
@@ -141,11 +140,7 @@
 
     .irp n, 1, 2                    /* Unrolled twice. */
     call .L\@_insert_rsb_entry_\n   /* Create an RSB entry. */
-
-.L\@_capture_speculation_\n:
-    pause
-    lfence
-    jmp .L\@_capture_speculation_\n /* Capture rogue speculation. */
+    int3                            /* Halt rogue speculation. */
 
 .L\@_insert_rsb_entry_\n:
     .endr
xen/arch/x86/indirect-thunk.S +2 -4
index 7cc22da0ef93..de6aef606832 100644
@@ -12,11 +12,9 @@
 #include <asm/asm_defns.h>
 
 .macro IND_THUNK_RETPOLINE reg:req
-        call 2f
+        call 1f
+        int3
 1:
-        lfence
-        jmp 1b
-2:
         mov %\reg, (%rsp)
         ret
 .endm
Signature · 1 line
2.11.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] x86/spec-ctrl: Consistently halt speculation using int3

Quoted 11 lines · Andrew Cooper

The RSB stuffing loop and retpoline thunks date from the very beginning, when halting speculation was a brand new field.

These days, we've largely settled on int3 for halting speculation in non-architectural paths. It's a single byte, and is fully serialising - a requirement for delivering #BP if it were to execute.

Update the thunks. Mostly for consistency across the codebase, but it does shrink every entrypath in Xen by 6 bytes which is a marginal win.

#3 lore raw

PATCH 2/3 x86/spec-ctrl: Make svm_vmexit_spec_ctrl conditional

In reply to Andrew Cooper

The logic was written this way out of an abundance of caution, but the reality is that AMD parts don't currently have the RAS-flushing side effect, and nor do they intend to gain it.

This removes one WRMSR from the VMExit path by default on Zen2 systems.

Fixes 614cec7d79d7 ("x86/svm: VMEntry/Exit logic for MSR_SPEC_CTRL")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

Zen3 doesn't get a speedup in general, because we use the WRMSR's to clear IBRS to avoid forcing it behind a VM's back.

---
 xen/arch/x86/hvm/svm/entry.S | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/svm/entry.S +5 -5
index 0ff4008060fa..a60d759f7108 100644
@@ -113,15 +113,15 @@ __UNLIKELY_END(nsvm_hap)
         ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_HVM
 
         .macro svm_vmexit_spec_ctrl
-            /*
-             * Write to MSR_SPEC_CTRL unconditionally, for the RAS[:32]
-             * flushing side effect.
-             */
-            mov    $MSR_SPEC_CTRL, %ecx
             movzbl CPUINFO_xen_spec_ctrl(%rsp), %eax
+            movzbl CPUINFO_last_spec_ctrl(%rsp), %edx
+            cmp    %edx, %eax
+            je     1f /* Skip write if value is correct. */
+            mov    $MSR_SPEC_CTRL, %ecx
             xor    %edx, %edx
             wrmsr
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
+1:
         .endm
         ALTERNATIVE "", svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
Signature · 1 line
2.11.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] x86/spec-ctrl: Make svm_vmexit_spec_ctrl conditional

Quoted 3 lines · Andrew Cooper

The logic was written this way out of an abundance of caution, but the reality is that AMD parts don't currently have the RAS-flushing side effect, and nor do they intend to gain it.

Nit: Looks like there's a stray (leftover from a re-write) "and" here?

Quoted 4 lines

This removes one WRMSR from the VMExit path by default on Zen2 systems.

Fixes 614cec7d79d7 ("x86/svm: VMEntry/Exit logic for MSR_SPEC_CTRL")
#5 lore raw

PATCH 3/3 x86/spec-ctrl: Shrink further entry paths due to %rdx being 0

In reply to Andrew Cooper

This is a continuation of the observation from:
e9b8d31981f1 ("x86/spec-ctrl: Rework SPEC_CTRL_ENTRY_FROM_INTR_IST") 53a570b28569 ("x86/spec-ctrl: Support IBPB-on-entry")

With %rdx known to be zero and not clobbered on the early entry path, we don't need to re-zero it every time want to write to an MSR.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/hvm/vmx/entry.S             | 4 +---
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 3 +--
 2 files changed, 2 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/vmx/entry.S +1 -3
index 5f5de45a1309..392aca42b864 100644
@@ -33,13 +33,12 @@ ENTRY(vmx_asm_vmexit_handler)
         movb $1,VCPU_vmx_launched(%rbx)
         mov  %rax,VCPU_hvm_guest_cr2(%rbx)
 
-        /* SPEC_CTRL_ENTRY_FROM_VMX    Req: b=curr %rsp=regs/cpuinfo, Clob: acd */
+        /* SPEC_CTRL_ENTRY_FROM_VMX    Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */
         ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_HVM
 
         .macro restore_spec_ctrl
             mov    $MSR_SPEC_CTRL, %ecx
             movzbl CPUINFO_xen_spec_ctrl(%rsp), %eax
-            xor    %edx, %edx
             wrmsr
         .endm
         ALTERNATIVE "", restore_spec_ctrl, X86_FEATURE_SC_MSR_HVM
@@ -49,7 +48,6 @@ ENTRY(vmx_asm_vmexit_handler)
         .macro restore_lbr
             mov $IA32_DEBUGCTLMSR_LBR, %eax
             mov $MSR_IA32_DEBUGCTLMSR, %ecx
-            xor %edx, %edx
             wrmsr
         .endm
         ALTERNATIVE "", restore_lbr, X86_FEATURE_XEN_LBR
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -2
index fab27ff5532b..61eed8510ba9 100644
@@ -176,7 +176,7 @@
 .macro DO_SPEC_CTRL_ENTRY maybexen:req
 /*
  * Requires %rsp=regs (also cpuinfo if !maybexen)
- * Requires %r14=stack_end (if maybexen)
+ * Requires %r14=stack_end (if maybexen), %rdx=0
  * Clobbers %rax, %rcx, %rdx
  *
  * PV guests can't update MSR_SPEC_CTRL behind Xen's back, so no need to read
@@ -184,7 +184,6 @@
  * while entries from Xen must leave shadowing in its current state.
  */
     mov $MSR_SPEC_CTRL, %ecx
-    xor %edx, %edx
 
     /*
      * Clear SPEC_CTRL shadowing *before* loading Xen's value.  If entering
Signature · 1 line
2.11.0
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] x86/spec-ctrl: Shrink further entry paths due to %rdx being 0

xen/arch/x86/hvm/vmx/entry.S ENTRY(vmx_asm_vmexit_handler) · line 37

Quoted 9 lines · Andrew Cooper
xen/arch/x86/hvm/vmx/entry.S +1 -1
@@ -33,13 +33,12 @@ ENTRY(vmx_asm_vmexit_handler)
         movb $1,VCPU_vmx_launched(%rbx)
         mov  %rax,VCPU_hvm_guest_cr2(%rbx)
 
-        /* SPEC_CTRL_ENTRY_FROM_VMX    Req: b=curr %rsp=regs/cpuinfo, Clob: acd */
+        /* SPEC_CTRL_ENTRY_FROM_VMX    Req: %rsp=regs/cpuinfo, %rdx=0, Clob: acd */
         ALTERNATIVE "", DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_HVM

Leaving %rdx documented as clobbered here is misleading - the scope of the comment is likely meant to extend to ...

Quoted 8 lines
xen/arch/x86/hvm/vmx/entry.S +0 -1
         .macro restore_spec_ctrl
             mov    $MSR_SPEC_CTRL, %ecx
             movzbl CPUINFO_xen_spec_ctrl(%rsp), %eax
-            xor    %edx, %edx
             wrmsr
         .endm
         ALTERNATIVE "", restore_spec_ctrl, X86_FEATURE_SC_MSR_HVM

xen/arch/x86/hvm/vmx/entry.S ENTRY(vmx_asm_vmexit_handler) · line 53

Quoted 7 lines
xen/arch/x86/hvm/vmx/entry.S +0 -1
@@ -49,7 +48,6 @@ ENTRY(vmx_asm_vmexit_handler)
         .macro restore_lbr
             mov $IA32_DEBUGCTLMSR_LBR, %eax
             mov $MSR_IA32_DEBUGCTLMSR, %ecx
-            xor %edx, %edx
             wrmsr
         .endm
         ALTERNATIVE "", restore_lbr, X86_FEATURE_XEN_LBR

... here, but that's not necessarily what a reader might gain. Plus with the change the register isn't clobbered anymore.

Quoted 12 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +1 -1
@@ -176,7 +176,7 @@
 .macro DO_SPEC_CTRL_ENTRY maybexen:req
 /*
  * Requires %rsp=regs (also cpuinfo if !maybexen)
- * Requires %r14=stack_end (if maybexen)
+ * Requires %r14=stack_end (if maybexen), %rdx=0
  * Clobbers %rax, %rcx, %rdx
  *
  * PV guests can't update MSR_SPEC_CTRL behind Xen's back, so no need to read

xen/arch/x86/include/asm/spec_ctrl_asm.h line 189

Quoted 7 lines
xen/arch/x86/include/asm/spec_ctrl_asm.h +0 -1
@@ -184,7 +184,6 @@
  * while entries from Xen must leave shadowing in its current state.
  */
     mov $MSR_SPEC_CTRL, %ecx
-    xor %edx, %edx
 
     /*
      * Clear SPEC_CTRL shadowing *before* loading Xen's value.  If entering

This is used in SPEC_CTRL_ENTRY_FROM_{INTR,PV} after DO_SPEC_CTRL_COND_IBPB, which documents %rdx as clobbered. Since it doesn't actually clobber the register, it's the documentation line which needs updating along with making this code change.

With the respective adjustments

Jan