Skip to content

Commit 56f289a

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Add a helper to retrieve userspace address from kvm_device_attr
Add a helper to handle converting the u64 userspace address embedded in struct kvm_device_attr into a userspace pointer, it's all too easy to forget the intermediate "unsigned long" cast as well as the truncation check. No functional change intended. Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent dd4516a commit 56f289a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

arch/x86/kvm/x86.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4332,7 +4332,15 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
43324332
break;
43334333
}
43344334
return r;
4335+
}
4336+
4337+
static inline void __user *kvm_get_attr_addr(struct kvm_device_attr *attr)
4338+
{
4339+
void __user *uaddr = (void __user*)(unsigned long)attr->addr;
43354340

4341+
if ((u64)(unsigned long)uaddr != attr->addr)
4342+
return ERR_PTR(-EFAULT);
4343+
return uaddr;
43364344
}
43374345

43384346
long kvm_arch_dev_ioctl(struct file *filp,
@@ -5025,11 +5033,11 @@ static int kvm_arch_tsc_has_attr(struct kvm_vcpu *vcpu,
50255033
static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
50265034
struct kvm_device_attr *attr)
50275035
{
5028-
u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
5036+
u64 __user *uaddr = kvm_get_attr_addr(attr);
50295037
int r;
50305038

5031-
if ((u64)(unsigned long)uaddr != attr->addr)
5032-
return -EFAULT;
5039+
if (IS_ERR(uaddr))
5040+
return PTR_ERR(uaddr);
50335041

50345042
switch (attr->attr) {
50355043
case KVM_VCPU_TSC_OFFSET:
@@ -5048,12 +5056,12 @@ static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
50485056
static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu,
50495057
struct kvm_device_attr *attr)
50505058
{
5051-
u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
5059+
u64 __user *uaddr = kvm_get_attr_addr(attr);
50525060
struct kvm *kvm = vcpu->kvm;
50535061
int r;
50545062

5055-
if ((u64)(unsigned long)uaddr != attr->addr)
5056-
return -EFAULT;
5063+
if (IS_ERR(uaddr))
5064+
return PTR_ERR(uaddr);
50575065

50585066
switch (attr->attr) {
50595067
case KVM_VCPU_TSC_OFFSET: {

0 commit comments

Comments
 (0)