Skip to contentSkip to conversation

KVM: x86: Account for 32-bit kernels when handling address in TSC attrs

PATCH In review Sean Christopherson 3 participants 3 messages Oct 7, 2021 – Oct 15, 2021

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: Account for 32-bit kernels when handling address in TSC attrs

When handling TSC attributes, cast the userspace provided virtual address to an unsigned long before casting it to a pointer to fix warnings on 32-bit kernels due to casting a 64-bit integer to a 32-bit pointer.

Add a check that the truncated address matches the original address, e.g. to prevent userspace specifying garbage in bits 63:32.

arch/x86/kvm/x86.c: In function ‘kvm_arch_tsc_get_attr’:
arch/x86/kvm/x86.c:4947:22: error: cast to pointer from integer of different size
4947 | u64 __user *uaddr = (u64 __user *)attr->addr;
| ^
arch/x86/kvm/x86.c: In function ‘kvm_arch_tsc_set_attr’:
arch/x86/kvm/x86.c:4967:22: error: cast to pointer from integer of different size
4967 | u64 __user *uaddr = (u64 __user *)attr->addr;
| ^

Cc Oliver Upton <[email protected]>
Fixes 469fde25e680 ("KVM: x86: Expose TSC offset controls to userspace")
---
 arch/x86/kvm/x86.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +8 -2
index 196ac33ef958..4a52a08707de 100644
@@ -4944,9 +4944,12 @@ static int kvm_arch_tsc_has_attr(struct kvm_vcpu *vcpu,
 static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
 				 struct kvm_device_attr *attr)
 {
-	u64 __user *uaddr = (u64 __user *)attr->addr;
+	u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
 	int r;
 
+	if ((u64)(unsigned long)uaddr != attr->addr)
+		return -EFAULT;
+
 	switch (attr->attr) {
 	case KVM_VCPU_TSC_OFFSET:
 		r = -EFAULT;
@@ -4964,10 +4967,13 @@ static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
 static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu,
 				 struct kvm_device_attr *attr)
 {
-	u64 __user *uaddr = (u64 __user *)attr->addr;
+	u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
 	struct kvm *kvm = vcpu->kvm;
 	int r;
 
+	if ((u64)(unsigned long)uaddr != attr->addr)
+		return -EFAULT;
+
 	switch (attr->attr) {
 	case KVM_VCPU_TSC_OFFSET: {
 		u64 offset, tsc, ns;
Signature · 1 line
2.33.0.882.g93a45727a2-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Sean Christopherson

When handling TSC attributes, cast the userspace provided virtual address to an unsigned long before casting it to a pointer to fix warnings on 32-bit kernels due to casting a 64-bit integer to a 32-bit pointer.

Add a check that the truncated address matches the original address, e.g. to prevent userspace specifying garbage in bits 63:32.

arch/x86/kvm/x86.c: In function ‘kvm_arch_tsc_get_attr’:
arch/x86/kvm/x86.c:4947:22: error: cast to pointer from integer of different size
4947 | u64 __user *uaddr = (u64 __user *)attr->addr;
| ^
arch/x86/kvm/x86.c: In function ‘kvm_arch_tsc_set_attr’:
arch/x86/kvm/x86.c:4967:22: error: cast to pointer from integer of different size
4967 | u64 __user *uaddr = (u64 __user *)attr->addr;
| ^

Quoted 7 lines
Cc Oliver Upton <[email protected]>
Fixes 469fde25e680 ("KVM: x86: Expose TSC offset controls to userspace")
---
 arch/x86/kvm/x86.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
maintainer
#2 lore raw
Quoted 26 lines · Oliver Upton
Quoted 17 lines · Sean Christopherson

When handling TSC attributes, cast the userspace provided virtual address to an unsigned long before casting it to a pointer to fix warnings on 32-bit kernels due to casting a 64-bit integer to a 32-bit pointer.

Add a check that the truncated address matches the original address, e.g. to prevent userspace specifying garbage in bits 63:32.

arch/x86/kvm/x86.c: In function ‘kvm_arch_tsc_get_attr’:
arch/x86/kvm/x86.c:4947:22: error: cast to pointer from integer of different size

    4947 |  u64 __user *uaddr = (u64 __user *)attr->addr;
         |                      ^
   arch/x86/kvm/x86.c: In function ‘kvm_arch_tsc_set_attr’:
   arch/x86/kvm/x86.c:4967:22: error: cast to pointer from integer of different size
    4967 |  u64 __user *uaddr = (u64 __user *)attr->addr;
         |                      ^
Quoted 7 lines
Cc Oliver Upton <[email protected]>
Fixes 469fde25e680 ("KVM: x86: Expose TSC offset controls to userspace")
---
  arch/x86/kvm/x86.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
Quoted 2 lines

Squashed, thanks.

Paolo