Skip to contentSkip to conversation

kvm/speculation: Allow KVM guests to use SSBD even if host does not

PATCH In review Alejandro Jimenez 5 participants 11 messages Jun 10, 2019 – Jun 26, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm/speculation: Allow KVM guests to use SSBD even if host does not

The bits set in x86_spec_ctrl_mask are used to calculate the guest's value of SPEC_CTRL that is written to the MSR before VMENTRY, and control which mitigations the guest can enable. In the case of SSBD, unless the host has enabled SSBD always on mode (by passing "spec_store_bypass_disable=on" in the kernel parameters), the SSBD bit is not set in the mask and the guest can not properly enable the SSBD always on mitigation mode.

This is confirmed by running the SSBD PoC on a guest using the SSBD always on mitigation mode (booted with kernel parameter "spec_store_bypass_disable=on"), and verifying that the guest is vulnerable unless the host is also using SSBD always on mode. In addition, the guest OS incorrectly reports the SSB vulnerability as mitigated.

Always set the SSBD bit in x86_spec_ctrl_mask when the host CPU supports it, allowing the guest to use SSBD whether or not the host has chosen to enable the mitigation in any of its modes.

---
 arch/x86/kernel/cpu/bugs.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/bugs.c +10 -1
index 03b4cc0..66ca906 100644
@@ -836,6 +836,16 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
 	}
 
 	/*
+	 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
+	 * bit in the mask to allow guests to use the mitigation even in the
+	 * case where the host does not enable it.
+	 */
+	if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
+	    static_cpu_has(X86_FEATURE_AMD_SSBD)) {
+		x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
+	}
+
+	/*
 	 * We have three CPU feature flags that are in play here:
 	 *  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
 	 *  - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
@@ -852,7 +862,6 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
 			x86_amd_ssb_disable();
 		} else {
 			x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
-			x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
 			wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 		}
 	}
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Alejandro Jimenez

The bits set in x86_spec_ctrl_mask are used to calculate the guest's value of SPEC_CTRL that is written to the MSR before VMENTRY, and control which mitigations the guest can enable. In the case of SSBD, unless the host has enabled SSBD always on mode (by passing "spec_store_bypass_disable=on" in the kernel parameters), the SSBD bit is not set in the mask and the guest can not properly enable the SSBD always on mitigation mode.

This is confirmed by running the SSBD PoC on a guest using the SSBD always on mitigation mode (booted with kernel parameter "spec_store_bypass_disable=on"), and verifying that the guest is vulnerable unless the host is also using SSBD always on mode. In addition, the guest OS incorrectly reports the SSB vulnerability as mitigated.

Quoted 7 lines

Always set the SSBD bit in x86_spec_ctrl_mask when the host CPU supports it, allowing the guest to use SSBD whether or not the host has chosen to enable the mitigation in any of its modes.

Quoted 35 lines
---
  arch/x86/kernel/cpu/bugs.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/bugs.c +10 -1
index 03b4cc0..66ca906 100644
@@ -836,6 +836,16 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
  	}
 
  	/*
+	 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
+	 * bit in the mask to allow guests to use the mitigation even in the
+	 * case where the host does not enable it.
+	 */
+	if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
+	    static_cpu_has(X86_FEATURE_AMD_SSBD)) {
+		x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
+	}
+
+	/*
  	 * We have three CPU feature flags that are in play here:
  	 *  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
  	 *  - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
@@ -852,7 +862,6 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
  			x86_amd_ssb_disable();
  		} else {
  			x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
-			x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
  			wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
  		}
  	}
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Alejandro Jimenez
Quoted 16 lines · Alejandro Jimenez

The bits set in x86_spec_ctrl_mask are used to calculate the guest's value of SPEC_CTRL that is written to the MSR before VMENTRY, and control which mitigations the guest can enable. In the case of SSBD, unless the host has enabled SSBD always on mode (by passing "spec_store_bypass_disable=on" in the kernel parameters), the SSBD bit is not set in the mask and the guest can not properly enable the SSBD always on mitigation mode.

This is confirmed by running the SSBD PoC on a guest using the SSBD always on mitigation mode (booted with kernel parameter "spec_store_bypass_disable=on"), and verifying that the guest is vulnerable unless the host is also using SSBD always on mode. In addition, the guest OS incorrectly reports the SSB vulnerability as mitigated.

Quoted 8 lines

Always set the SSBD bit in x86_spec_ctrl_mask when the host CPU supports it, allowing the guest to use SSBD whether or not the host has chosen to enable the mitigation in any of its modes.

Quoted 34 lines
---
 arch/x86/kernel/cpu/bugs.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/bugs.c +10 -1
index 03b4cc0..66ca906 100644
@@ -836,6 +836,16 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
 	}
 
 	/*
+	 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
+	 * bit in the mask to allow guests to use the mitigation even in the
+	 * case where the host does not enable it.
+	 */
+	if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
+	    static_cpu_has(X86_FEATURE_AMD_SSBD)) {
+		x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
+	}
+
+	/*
 	 * We have three CPU feature flags that are in play here:
 	 *  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
 	 *  - X86_FEATURE_SSBD - CPU is able to turn off speculative store bypass
@@ -852,7 +862,6 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
 			x86_amd_ssb_disable();
 		} else {
 			x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
-			x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
 			wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 		}
 	}
#3 lore raw
Quoted 1 line · Paolo Bonzini

On 10/06/19 19:20, Alejandro Jimenez wrote:

Btw, the proper prefix is: x86/speculation: Allow guests ....

Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/cpu/bugs.c +0 -0
index 03b4cc0..66ca906 100644
@@ -836,6 +836,16 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
 	}
 
Quoted 8 lines

arch/x86/kernel/cpu/bugs.c static enum ssb_mitigation __init __ssb_select_mitigation(void) · line 845

Quoted 8 lines
arch/x86/kernel/cpu/bugs.c +7 -0
 	/*
+	 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
+	 * bit in the mask to allow guests to use the mitigation even in the
+	 * case where the host does not enable it.
+	 */
+	if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
+	    static_cpu_has(X86_FEATURE_AMD_SSBD)) {
+		x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;

Well, yes. But that also allows the guest to turn off SSBD if the host has it disabled globally. So this needs to be conditional depending on the host mode. It affects two places:

1) If the host has it globally disabled then the mask needs to be clear.

2) If the host has it specifically disabled for the VCPU thread, then it shouldn't be allowed to be cleared by the guest either.

Thanks,

tglx

#4 lore raw
Quoted 3 lines · Thomas Gleixner
Quoted 1 line · Paolo Bonzini

On 10/06/19 19:20, Alejandro Jimenez wrote:

Btw, the proper prefix is: x86/speculation: Allow guests ....

I'll correct it on the next iteration of the patch.

Quoted 19 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/cpu/bugs.c +0 -0
index 03b4cc0..66ca906 100644
@@ -836,6 +836,16 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
  	}
 
Quoted 8 lines

arch/x86/kernel/cpu/bugs.c static enum ssb_mitigation __init __ssb_select_mitigation(void) · line 845

Quoted 8 lines
arch/x86/kernel/cpu/bugs.c +7 -0
  	/*
+	 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
+	 * bit in the mask to allow guests to use the mitigation even in the
+	 * case where the host does not enable it.
+	 */
+	if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
+	    static_cpu_has(X86_FEATURE_AMD_SSBD)) {
+		x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;

Well, yes. But that also allows the guest to turn off SSBD if the host has it disabled globally. So this needs to be conditional depending on the host mode. It affects two places:

Quoted 4 lines

1) If the host has it globally disabled then the mask needs to be clear.

2) If the host has it specifically disabled for the VCPU thread, then it shouldn't be allowed to be cleared by the guest either.

I see the argument that the host must be able to enforce its security policies on the guests running on it. The guest OS would still be 'lying' by reporting that is running with the mitigation turned off, but I suppose this is preferable to overriding the host's security policy.

I think that even with that approach there is still an unsolved problem, as I believe guests are allowed to write directly to SPEC_CTRL MSR without causing a VMEXIT, which bypasses the host masking entirely.  e.g. a guest using IBRS writes frequently to SPEC_CTRL, and could turn off SSBD on the VPCU while is running after the first non-zero write to the MSR. Do you agree?

Thank you for the feedback,
Alejandro

Quoted 7 lines

Thanks,

tglx

#5 lore raw
Quoted 18 lines · Alejandro Jimenez
Quoted 17 lines · Thomas Gleixner
Quoted 9 lines · Paolo Bonzini

arch/x86/kernel/cpu/bugs.c static enum ssb_mitigation __init __ssb_select_mitigation(void) · line 845

Quoted 8 lines · Alejandro Jimenez
    	/*
arch/x86/kernel/cpu/bugs.c +7 -0
+	 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
+	 * bit in the mask to allow guests to use the mitigation even in the
+	 * case where the host does not enable it.
+	 */
+	if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
+	    static_cpu_has(X86_FEATURE_AMD_SSBD)) {
+		x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;

Well, yes. But that also allows the guest to turn off SSBD if the host has it disabled globally. So this needs to be conditional depending on the host mode. It affects two places:

1) If the host has it globally disabled then the mask needs to be clear.

Quoted 7 lines
Quoted 2 lines

2) If the host has it specifically disabled for the VCPU thread, then it shouldn't be allowed to be cleared by the guest either.

I see the argument that the host must be able to enforce its security policies on the guests running on it. The guest OS would still be 'lying' by reporting that is running with the mitigation turned off, but I suppose this is preferable to overriding the host's security policy.

True.

Quoted 5 lines

I think that even with that approach there is still an unsolved problem, as I believe guests are allowed to write directly to SPEC_CTRL MSR without causing a VMEXIT, which bypasses the host masking entirely.  e.g. a guest using IBRS writes frequently to SPEC_CTRL, and could turn off SSBD on the VPCU while is running after the first non-zero write to the MSR. Do you agree?

Indeed. Of course that was a decision we made _before_ all the other fancy things came around. Looks like we have to reopen that discussion.

Thanks,

tglx

maintainer
#6 lore raw
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines

I think that even with that approach there is still an unsolved problem, as I believe guests are allowed to write directly to SPEC_CTRL MSR without causing a VMEXIT, which bypasses the host masking entirely.  e.g. a guest using IBRS writes frequently to SPEC_CTRL, and could turn off SSBD on the VPCU while is running after the first non-zero write to the MSR. Do you agree?

Indeed. Of course that was a decision we made _before_ all the other fancy things came around. Looks like we have to reopen that discussion.

It's not just that, it's a decision that was made because otherwise performance is absolutely horrible (like 4-5x slower syscalls if the guest is using IBRS).

I think it's better to leave the guest in control of SSBD even if it's globally disabled. The harm cannot escape the guest and in particular it cannot escape to the sibling hyperthread.

Paolo

#7 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines

I think that even with that approach there is still an unsolved problem, as I believe guests are allowed to write directly to SPEC_CTRL MSR without causing a VMEXIT, which bypasses the host masking entirely.  e.g. a guest using IBRS writes frequently to SPEC_CTRL, and could turn off SSBD on the VPCU while is running after the first non-zero write to the MSR. Do you agree?

Indeed. Of course that was a decision we made _before_ all the other fancy things came around. Looks like we have to reopen that discussion.

Quoted 7 lines

It's not just that, it's a decision that was made because otherwise performance is absolutely horrible (like 4-5x slower syscalls if the guest is using IBRS).

I think it's better to leave the guest in control of SSBD even if it's globally disabled. The harm cannot escape the guest and in particular it cannot escape to the sibling hyperthread.

SSB allows guest to guest attacks IIRC

Thanks,

tglx

maintainer
#8 lore raw
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines

I think it's better to leave the guest in control of SSBD even if it's globally disabled. The harm cannot escape the guest and in particular it cannot escape to the sibling hyperthread.

SSB allows guest to guest attacks IIRC

SSB requires something like

   p = &foo;
   ...
   p = &bar;
   q = *p;

where "p = &foo;" is executed from one privilege domain and the others are executed by another process or privilege domain. Unless two guests share memory, it is not possible to use it for guest-to-guest attacks.

Paolo

#9 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines

I think it's better to leave the guest in control of SSBD even if it's globally disabled. The harm cannot escape the guest and in particular it cannot escape to the sibling hyperthread.

SSB allows guest to guest attacks IIRC

SSB requires something like

Quoted 8 lines
   p = &foo;
   ...
   p = &bar;
   q = *p;

where "p = &foo;" is executed from one privilege domain and the others are executed by another process or privilege domain. Unless two guests share memory, it is not possible to use it for guest-to-guest attacks.

Fair enough. It's way too hot to think clearly about these kind of problems and there are simply way too many of them...

Thanks,

tglx

#10 lore raw

[tip:x86/urgent] x86/speculation: Allow guests to use SSBD even if host does not

In reply to Alejandro Jimenez
x86/speculation: Allow guests to use SSBD even if host does not
Commit-ID:  c1f7fec1eb6a2c86d01bc22afce772c743451d88
Gitweb:     https://git.kernel.org/tip/c1f7fec1eb6a2c86d01bc22afce772c743451d88
Author:     Alejandro Jimenez <[email protected]>
AuthorDate: Mon, 10 Jun 2019 13:20:10 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 26 Jun 2019 16:38:36 +0200

x86/speculation: Allow guests to use SSBD even if host does not

The bits set in x86_spec_ctrl_mask are used to calculate the guest's value of SPEC_CTRL that is written to the MSR before VMENTRY, and control which mitigations the guest can enable. In the case of SSBD, unless the host has enabled SSBD always on mode (by passing "spec_store_bypass_disable=on" in the kernel parameters), the SSBD bit is not set in the mask and the guest can not properly enable the SSBD always on mitigation mode.

This has been confirmed by running the SSBD PoC on a guest using the SSBD always on mitigation mode (booted with kernel parameter "spec_store_bypass_disable=on"), and verifying that the guest is vulnerable unless the host is also using SSBD always on mode. In addition, the guest OS incorrectly reports the SSB vulnerability as mitigated.

Always set the SSBD bit in x86_spec_ctrl_mask when the host CPU supports it, allowing the guest to use SSBD whether or not the host has chosen to enable the mitigation in any of its modes.

---
 arch/x86/kernel/cpu/bugs.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/bugs.c +10 -1
index 03b4cc0ec3a7..66ca906aa790 100644
@@ -835,6 +835,16 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
 		break;
 	}
 
+	/*
+	 * If SSBD is controlled by the SPEC_CTRL MSR, then set the proper
+	 * bit in the mask to allow guests to use the mitigation even in the
+	 * case where the host does not enable it.
+	 */
+	if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
+	    static_cpu_has(X86_FEATURE_AMD_SSBD)) {
+		x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
+	}
+
 	/*
 	 * We have three CPU feature flags that are in play here:
 	 *  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
@@ -852,7 +862,6 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
 			x86_amd_ssb_disable();
 		} else {
 			x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
-			x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
 			wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 		}
 	}