Skip to contentSkip to conversation

KVM: VMX: Fix a TSX_CTRL_CPUID_CLEAR field mask issue

PATCH Superseded Zhenzhong Duan 2 participants 3 messages Sep 6, 2021 – Sep 26, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: VMX: Fix a TSX_CTRL_CPUID_CLEAR field mask issue

Host value of TSX_CTRL_CPUID_CLEAR field should be unchangable by guest, but the mask for this purpose is set to a wrong value. So it doesn't take effect.

Fixes 8ea8b8d6f869 ("KVM: VMX: Use common x86's uret MSR list as the one true list")
---
 arch/x86/kvm/vmx/vmx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +1 -1
index 927a552393b9..36588b5feee6 100644
@@ -6812,7 +6812,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
 		 */
 		tsx_ctrl = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
 		if (tsx_ctrl)
-			vmx->guest_uret_msrs[i].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
+			tsx_ctrl->mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
 	}
 
 	err = alloc_loaded_vmcs(&vmx->vmcs01);
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Zhenzhong Duan

Host value of TSX_CTRL_CPUID_CLEAR field should be unchangable by guest, but the mask for this purpose is set to a wrong value. So it doesn't take effect.

It would be helpful to provide a bit more info as to just how bad/boneheaded this bug is. E.g.

When updating the host's mask for its MSR_IA32_TSX_CTRL user return entry, clear the mask in the found uret MSR instead of vmx->guest_uret_msrs[i]. Modifying guest_uret_msrs directly is completely broken as 'i' does not point at the MSR_IA32_TSX_CTRL entry. In fact, it's guaranteed to be an out-of-bounds accesses as is always set to kvm_nr_uret_msrs in a prior loop. By sheer dumb luck, the fallout is limited to "only" failing to preserve the host's TSX_CTRL_CPUID_CLEAR. The out-of-bounds access is benign as it's guaranteed to clear a bit in a guest MSR value, which are always zero at vCPU creation on both x86-64 and i386.

Quoted 1 line
Fixes 8ea8b8d6f869 ("KVM: VMX: Use common x86's uret MSR list as the one true list")
Quoted 10 lines
---
 arch/x86/kvm/vmx/vmx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +0 -0
index 927a552393b9..36588b5feee6 100644

arch/x86/kvm/vmx/vmx.c static int vmx_create_vcpu(struct kvm_vcpu *vcpu) · line 6815

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -6812,7 +6812,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
 		 */
 		tsx_ctrl = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
 		if (tsx_ctrl)
-			vmx->guest_uret_msrs[i].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
+			tsx_ctrl->mask = ~(u64)TSX_CTRL_CPUID_CLEAR;

Egad, that's a horrific oversight on my part.

#2 lore raw
Quoted 18 lines

-----Original Message-----
From: Sean Christopherson <[email protected]>
Sent: Wednesday, September 8, 2021 8:08 AM
To: Duan, Zhenzhong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] KVM: VMX: Fix a TSX_CTRL_CPUID_CLEAR field mask issue

Quoted 3 lines · Zhenzhong Duan

Host value of TSX_CTRL_CPUID_CLEAR field should be unchangable by guest, but the mask for this purpose is set to a wrong value. So it doesn't take effect.

It would be helpful to provide a bit more info as to just how bad/boneheaded this bug is. E.g.

Quoted 9 lines

When updating the host's mask for its MSR_IA32_TSX_CTRL user return entry, clear the mask in the found uret MSR instead of vmx->guest_uret_msrs[i]. Modifying guest_uret_msrs directly is completely broken as 'i' does not point at the MSR_IA32_TSX_CTRL entry. In fact, it's guaranteed to be an out-of-bounds accesses as is always set to kvm_nr_uret_msrs in a prior loop. By sheer dumb luck, the fallout is limited to "only" failing to preserve the host's TSX_CTRL_CPUID_CLEAR. The out-of-bounds access is benign as it's guaranteed to clear a bit in a guest MSR value, which are always zero at vCPU creation on both x86-64 and i386.

Sorry for late response, I missed this mail by a wrong mail rule. Your comment is more clear, I'll use it in v2.

Thanks
Zhenzhong