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 <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
dd4516aee3
commit
56f289a8d2
1 changed files with 14 additions and 6 deletions
|
@ -4332,7 +4332,15 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
|
|||
break;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline void __user *kvm_get_attr_addr(struct kvm_device_attr *attr)
|
||||
{
|
||||
void __user *uaddr = (void __user*)(unsigned long)attr->addr;
|
||||
|
||||
if ((u64)(unsigned long)uaddr != attr->addr)
|
||||
return ERR_PTR(-EFAULT);
|
||||
return uaddr;
|
||||
}
|
||||
|
||||
long kvm_arch_dev_ioctl(struct file *filp,
|
||||
|
@ -5025,11 +5033,11 @@ 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 *)(unsigned long)attr->addr;
|
||||
u64 __user *uaddr = kvm_get_attr_addr(attr);
|
||||
int r;
|
||||
|
||||
if ((u64)(unsigned long)uaddr != attr->addr)
|
||||
return -EFAULT;
|
||||
if (IS_ERR(uaddr))
|
||||
return PTR_ERR(uaddr);
|
||||
|
||||
switch (attr->attr) {
|
||||
case KVM_VCPU_TSC_OFFSET:
|
||||
|
@ -5048,12 +5056,12 @@ 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 *)(unsigned long)attr->addr;
|
||||
u64 __user *uaddr = kvm_get_attr_addr(attr);
|
||||
struct kvm *kvm = vcpu->kvm;
|
||||
int r;
|
||||
|
||||
if ((u64)(unsigned long)uaddr != attr->addr)
|
||||
return -EFAULT;
|
||||
if (IS_ERR(uaddr))
|
||||
return PTR_ERR(uaddr);
|
||||
|
||||
switch (attr->attr) {
|
||||
case KVM_VCPU_TSC_OFFSET: {
|
||||
|
|
Loading…
Add table
Reference in a new issue