]> xenbits.xensource.com Git - people/tklengyel/xen.git/commitdiff
x86/spec-ctrl: Mitigate IBPB not flushing the RSB/RAS
authorAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 14 Jun 2022 15:18:36 +0000 (16:18 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 8 Nov 2022 17:26:08 +0000 (17:26 +0000)
Introduce spec_ctrl_new_guest_context() to encapsulate all logic pertaining to
using MSR_PRED_CMD for a new guest context, even if it only has one user
presently.

Introduce X86_BUG_IBPB_NO_RET, and use it extend spec_ctrl_new_guest_context()
with a manual fixup for hardware which mis-implements IBPB.

This is part of XSA-422 / CVE-2022-23824.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/asm-macros.c
xen/arch/x86/domain.c
xen/arch/x86/include/asm/cpufeatures.h
xen/arch/x86/include/asm/spec_ctrl.h
xen/arch/x86/spec_ctrl.c

index 7e536b0d82f51d682630d9e78347f02fe14e35b1..891d86c7655ce18cae8f77c1c6d83897527f5cae 100644 (file)
@@ -1,2 +1,3 @@
 #include <asm/asm-defns.h>
 #include <asm/alternative-asm.h>
+#include <asm/spec_ctrl_asm.h>
index ce82c502bb5f678ec5007557f8a372c8641cd4ea..79107dac6922d12609d9b3cbee60618a212d902a 100644 (file)
@@ -2117,7 +2117,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
              */
             if ( *last_id != next_id )
             {
-                wrmsrl(MSR_PRED_CMD, PRED_CMD_IBPB);
+                spec_ctrl_new_guest_context();
                 *last_id = next_id;
             }
         }
index 3895de4faf8f2b11666b24d470ac189d3319a295..c68ced1b82479df4e56b94008d921d8b24cf0d0b 100644 (file)
@@ -49,6 +49,7 @@ XEN_CPUFEATURE(IBPB_ENTRY_HVM,    X86_SYNTH(29)) /* MSR_PRED_CMD used by Xen for
 #define X86_BUG_FPU_PTRS          X86_BUG( 0) /* (F)X{SAVE,RSTOR} doesn't save/restore FOP/FIP/FDP. */
 #define X86_BUG_NULL_SEG          X86_BUG( 1) /* NULL-ing a selector preserves the base and limit. */
 #define X86_BUG_CLFLUSH_MFENCE    X86_BUG( 2) /* MFENCE needed to serialise CLFLUSH */
+#define X86_BUG_IBPB_NO_RET       X86_BUG( 3) /* IBPB doesn't flush the RSB/RAS */
 
 /* Total number of capability words, inc synth and bug words. */
 #define NCAPINTS (FSCAPINTS + X86_NR_SYNTH + X86_NR_BUG) /* N 32-bit words worth of info */
index 9403b81dc7af70c4ec31ae24e257d6667b60b245..6a77c39378448bbf775a5db87fcd0d71f095d780 100644 (file)
 void init_speculation_mitigations(void);
 void spec_ctrl_init_domain(struct domain *d);
 
+/*
+ * Switch to a new guest prediction context.
+ *
+ * This flushes all indirect branch predictors (BTB, RSB/RAS), so guest code
+ * which has previously run on this CPU can't attack subsequent guest code.
+ *
+ * As this flushes the RSB/RAS, it destroys the predictions of the calling
+ * context.  For best performace, arrange for this to be used when we're going
+ * to jump out of the current context, e.g. with reset_stack_and_jump().
+ *
+ * For hardware which mis-implements IBPB, fix up by flushing the RSB/RAS
+ * manually.
+ */
+static always_inline void spec_ctrl_new_guest_context(void)
+{
+    wrmsrl(MSR_PRED_CMD, PRED_CMD_IBPB);
+
+    /* (ab)use alternative_input() to specify clobbers. */
+    alternative_input("", "DO_OVERWRITE_RSB", X86_BUG_IBPB_NO_RET,
+                      : "rax", "rcx");
+}
+
 extern int8_t opt_ibpb_ctxt_switch;
 extern bool opt_ssbd;
 extern int8_t opt_eager_fpu;
index 0c3503c9cdfe0e3da47fbd06794efb1010efa5eb..a0835143e37c744b42cca9e7bbcdc946f7b2c3e1 100644 (file)
@@ -810,6 +810,14 @@ static void __init ibpb_calculations(void)
         return;
     }
 
+    /*
+     * AMD/Hygon CPUs to date (June 2022) don't flush the the RAS.  Future
+     * CPUs are expected to enumerate IBPB_RET when this has been fixed.
+     * Until then, cover the difference with the software sequence.
+     */
+    if ( boot_cpu_has(X86_FEATURE_IBPB) && !boot_cpu_has(X86_FEATURE_IBPB_RET) )
+        setup_force_cpu_cap(X86_BUG_IBPB_NO_RET);
+
     /*
      * IBPB-on-entry mitigations for Branch Type Confusion.
      *