KVM: Use vmemdup_user()

Replace opencoded alloc and copy with vmemdup_user().

Signed-off-by: Denis Efremov <efremov@linux.com>
Message-Id: <20200603101131.2107303-1-efremov@linux.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Denis Efremov 2020-06-03 13:11:31 +03:00 committed by Paolo Bonzini
parent 0e96edd9a9
commit 7ec28e264f
2 changed files with 15 additions and 21 deletions

View File

@ -181,17 +181,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
r = -E2BIG; r = -E2BIG;
if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
goto out; goto out;
r = -ENOMEM;
if (cpuid->nent) { if (cpuid->nent) {
cpuid_entries = cpuid_entries = vmemdup_user(entries,
vmalloc(array_size(sizeof(struct kvm_cpuid_entry), array_size(sizeof(struct kvm_cpuid_entry),
cpuid->nent)); cpuid->nent));
if (!cpuid_entries) if (IS_ERR(cpuid_entries)) {
goto out; r = PTR_ERR(cpuid_entries);
r = -EFAULT;
if (copy_from_user(cpuid_entries, entries,
cpuid->nent * sizeof(struct kvm_cpuid_entry)))
goto out; goto out;
}
} }
for (i = 0; i < cpuid->nent; i++) { for (i = 0; i < cpuid->nent; i++) {
vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
@ -211,8 +208,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
kvm_x86_ops.cpuid_update(vcpu); kvm_x86_ops.cpuid_update(vcpu);
r = kvm_update_cpuid(vcpu); r = kvm_update_cpuid(vcpu);
kvfree(cpuid_entries);
out: out:
vfree(cpuid_entries);
return r; return r;
} }

View File

@ -3746,21 +3746,18 @@ static long kvm_vm_ioctl(struct file *filp,
if (routing.flags) if (routing.flags)
goto out; goto out;
if (routing.nr) { if (routing.nr) {
r = -ENOMEM;
entries = vmalloc(array_size(sizeof(*entries),
routing.nr));
if (!entries)
goto out;
r = -EFAULT;
urouting = argp; urouting = argp;
if (copy_from_user(entries, urouting->entries, entries = vmemdup_user(urouting->entries,
routing.nr * sizeof(*entries))) array_size(sizeof(*entries),
goto out_free_irq_routing; routing.nr));
if (IS_ERR(entries)) {
r = PTR_ERR(entries);
goto out;
}
} }
r = kvm_set_irq_routing(kvm, entries, routing.nr, r = kvm_set_irq_routing(kvm, entries, routing.nr,
routing.flags); routing.flags);
out_free_irq_routing: kvfree(entries);
vfree(entries);
break; break;
} }
#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */