Skip to contentSkip to conversation

x86/spec-ctrl: Use IST RSB protection for !SVM systems

PATCH In review Andrew Cooper 2 participants 5 messages Aug 5, 2022 – Aug 15, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/spec-ctrl: Use IST RSB protection for !SVM systems

There is a corner case where a VT-x guest which manages to reliably trigger non-fatal #MC's could evade the rogue RSB speculation protections that were supposed to be in place.

This is a lack of defence in depth; Xen does not architecturally execute more RET than CALL instructions, so an attacker would have to locate a different gadget (e.g. SpectreRSB) first to execute a transient path of excess RET instructions.

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

---
 xen/arch/x86/spec_ctrl.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
xen/arch/x86/spec_ctrl.c +16 -0
index 44e86f3d674d..d2cd5459739f 100644
@@ -1327,8 +1327,24 @@ void __init init_speculation_mitigations(void)
      * mappings.
      */
     if ( opt_rsb_hvm )
+    {
         setup_force_cpu_cap(X86_FEATURE_SC_RSB_HVM);
 
+        /*
+         * For SVM, Xen's RSB safety actions are performed before STGI, so
+         * behave atomically with respect to IST sources.
+         *
+         * For VT-x, NMIs are atomic with VMExit (the NMI gets queued but not
+         * delivered) whereas other IST sources are not atomic.  Specifically,
+         * #MC can hit ahead the RSB safety action in the vmexit path.
+         *
+         * Therefore, it is necessary for the IST logic to protect Xen against
+         * possible rogue RSB speculation.
+         */
+        if ( !cpu_has_svm )
+            default_spec_ctrl_flags |= SCF_ist_rsb;
+    }
+
     ibpb_calculations();
 
     /* Check whether Eager FPU should be enabled by default. */
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andrew Cooper

There is a corner case where a VT-x guest which manages to reliably trigger non-fatal #MC's could evade the rogue RSB speculation protections that were supposed to be in place.

This is a lack of defence in depth; Xen does not architecturally execute more RET than CALL instructions, so an attacker would have to locate a different gadget (e.g. SpectreRSB) first to execute a transient path of excess RET instructions.

#2 lore raw
In reply to Andrew Cooper
Quoted 12 lines · Andrew Cooper
xen/arch/x86/spec_ctrl.c +4 -0
@@ -1327,8 +1327,24 @@ void __init init_speculation_mitigations(void)
      * mappings.
      */
     if ( opt_rsb_hvm )
+    {
         setup_force_cpu_cap(X86_FEATURE_SC_RSB_HVM);
 
+        /*
+         * For SVM, Xen's RSB safety actions are performed before STGI, so
+         * behave atomically with respect to IST sources.

xen/arch/x86/spec_ctrl.c void __init init_speculation_mitigations(void) · line 1345

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +10 -0
+         *
+         * For VT-x, NMIs are atomic with VMExit (the NMI gets queued but not
+         * delivered) whereas other IST sources are not atomic.  Specifically,
+         * #MC can hit ahead the RSB safety action in the vmexit path.
+         *
+         * Therefore, it is necessary for the IST logic to protect Xen against
+         * possible rogue RSB speculation.
+         */
+        if ( !cpu_has_svm )
+            default_spec_ctrl_flags |= SCF_ist_rsb;

Only now, when I'm about to backport this, it occurs to me to ask: Why is this !cpu_has_svm rather than cpu_has_vmx? Plus shouldn't this further be gated upon HVM actually being in use (i.e. in particular CONFIG_HVM=y in the first place)?

Jan

#3 lore raw
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
xen/arch/x86/spec_ctrl.c +1 -0
@@ -1327,8 +1327,24 @@ void __init init_speculation_mitigations(void)
      * mappings.
      */
     if ( opt_rsb_hvm )
+    {
         setup_force_cpu_cap(X86_FEATURE_SC_RSB_HVM);
 
Quoted 15 lines

xen/arch/x86/spec_ctrl.c void __init init_speculation_mitigations(void) · line 1345

Quoted 13 lines
xen/arch/x86/spec_ctrl.c +13 -0
+        /*
+         * For SVM, Xen's RSB safety actions are performed before STGI, so
+         * behave atomically with respect to IST sources.
+         *
+         * For VT-x, NMIs are atomic with VMExit (the NMI gets queued but not
+         * delivered) whereas other IST sources are not atomic.  Specifically,
+         * #MC can hit ahead the RSB safety action in the vmexit path.
+         *
+         * Therefore, it is necessary for the IST logic to protect Xen against
+         * possible rogue RSB speculation.
+         */
+        if ( !cpu_has_svm )
+            default_spec_ctrl_flags |= SCF_ist_rsb;

Only now, when I'm about to backport this, it occurs to me to ask: Why is this !cpu_has_svm rather than cpu_has_vmx?

Because it is only SVM known to be safe.

Quoted 3 lines

Plus shouldn't this further
be gated upon HVM actually being in use (i.e. in particular CONFIG_HVM=y in the first place)?

Perhaps, but not locally here.  All of init_speculation_mitigations() wants reconsidering if you're going down that route.

~Andrew

#4 lore raw
Quoted 27 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
xen/arch/x86/spec_ctrl.c +1 -0
@@ -1327,8 +1327,24 @@ void __init init_speculation_mitigations(void)
      * mappings.
      */
     if ( opt_rsb_hvm )
+    {
         setup_force_cpu_cap(X86_FEATURE_SC_RSB_HVM);
 
Quoted 15 lines

xen/arch/x86/spec_ctrl.c void __init init_speculation_mitigations(void) · line 1345

Quoted 13 lines
xen/arch/x86/spec_ctrl.c +13 -0
+        /*
+         * For SVM, Xen's RSB safety actions are performed before STGI, so
+         * behave atomically with respect to IST sources.
+         *
+         * For VT-x, NMIs are atomic with VMExit (the NMI gets queued but not
+         * delivered) whereas other IST sources are not atomic.  Specifically,
+         * #MC can hit ahead the RSB safety action in the vmexit path.
+         *
+         * Therefore, it is necessary for the IST logic to protect Xen against
+         * possible rogue RSB speculation.
+         */
+        if ( !cpu_has_svm )
+            default_spec_ctrl_flags |= SCF_ist_rsb;

Only now, when I'm about to backport this, it occurs to me to ask: Why is this !cpu_has_svm rather than cpu_has_vmx?

Quoted 1 line

Because it is only SVM known to be safe.

Yes. Which amounts to only VT-x being unsafe. And in particular PV alone (e.g. shim, from the perspective of the shim itself) is safe as well, no matter what CPU we're on.

Quoted 6 lines
Quoted 3 lines

Plus shouldn't this further
be gated upon HVM actually being in use (i.e. in particular CONFIG_HVM=y in the first place)?

Perhaps, but not locally here.  All of init_speculation_mitigations() wants reconsidering if you're going down that route.

Not sure - many of the settings (like X86_FEATURE_SC_RSB_HVM also being set in the enclosing if()) only affect HVM-specific code paths, so which way they are set wouldn't matter when !CONFIG_HVM. But the one here clearly affects a common code path, for no gains at all. It's not an overly hot code path, sure, but it still strikes me as odd.

Jan