Skip to contentSkip to conversation

KVM: vmx: add mismatched size in vmcs_check32

PATCH Superseded [email protected] 3 participants 4 messages Apr 8, 2021 – Apr 9, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: vmx: add mismatched size in vmcs_check32

From: Haiwei Li <[email protected]>

vmcs_check32 misses the check for 64-bit and 64-bit high.

---
 arch/x86/kvm/vmx/vmx_ops.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kvm/vmx/vmx_ops.h +4 -0
index 692b0c3..164b64f 100644
@@ -37,6 +37,10 @@ static __always_inline void vmcs_check32(unsigned long field)
 {
 	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
 			 "32-bit accessor invalid for 16-bit field");
+	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
+			 "32-bit accessor invalid for 64-bit field");
+	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
+			 "32-bit accessor invalid for 64-bit high field");
 	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
 			 "32-bit accessor invalid for natural width field");
 }
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 3 lines · On Thu, Apr 08, 2021, [email protected] wrote:

From: Haiwei Li <[email protected]>

vmcs_check32 misses the check for 64-bit and 64-bit high.

Can you clarify in the changelog that, while it is architecturally legal to access 64-bit and 64-bit high fields with a 32-bit read/write in 32-bit mode, KVM should never do partial accesses to VMCS fields. And/or note that the 32-bit accesses are done in vmcs_{read,write}64() when necessary? Hmm, maybe:

Add compile-time assertions in vmcs_check32() to disallow accesses to 64-bit and 64-bit high fields via vmcs_{read,write}32(). Upper level KVM code should never do partial accesses to VMCS fields. KVM handles the split accesses automatically in vmcs_{read,write}64() when running as a 32-bit kernel.

With something along those lines:

Quoted 23 lines
---
 arch/x86/kvm/vmx/vmx_ops.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kvm/vmx/vmx_ops.h +4 -0
index 692b0c3..164b64f 100644
@@ -37,6 +37,10 @@ static __always_inline void vmcs_check32(unsigned long field)
 {
 	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
 			 "32-bit accessor invalid for 16-bit field");
+	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
+			 "32-bit accessor invalid for 64-bit field");
+	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
+			 "32-bit accessor invalid for 64-bit high field");
 	BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
 			 "32-bit accessor invalid for natural width field");
 }
Signature · 1 line
1.8.3.1
maintainer
#2 lore raw
Quoted 5 lines · Sean Christopherson

Add compile-time assertions in vmcs_check32() to disallow accesses to 64-bit and 64-bit high fields via vmcs_{read,write}32(). Upper level KVM code should never do partial accesses to VMCS fields. KVM handles the split accesses automatically in vmcs_{read,write}64() when running as a 32-bit kernel.

KVM also uses raw vmread/vmwrite (__vmcs_readl/__vmcs_writel) when copying to and from the shadow VMCS, so that path will not go through vmcs_check32 either.

Paolo

author
#3 lore raw
In reply to Sean Christopherson
Quoted 11 lines · Sean Christopherson
Quoted 3 lines · On Thu, Apr 08, 2021, [email protected] wrote:

From: Haiwei Li <[email protected]>

vmcs_check32 misses the check for 64-bit and 64-bit high.

Can you clarify in the changelog that, while it is architecturally legal to access 64-bit and 64-bit high fields with a 32-bit read/write in 32-bit mode, KVM should never do partial accesses to VMCS fields. And/or note that the 32-bit accesses are done in vmcs_{read,write}64() when necessary? Hmm, maybe:

Quoted 5 lines

Add compile-time assertions in vmcs_check32() to disallow accesses to 64-bit and 64-bit high fields via vmcs_{read,write}32(). Upper level KVM code should never do partial accesses to VMCS fields. KVM handles the split accesses automatically in vmcs_{read,write}64() when running as a 32-bit kernel.

Good suggestion, thanks. I will send v2.