Skip to content

Commit 7ec28e2

Browse files
evdenisbonzini
authored andcommitted
KVM: Use vmemdup_user()
Replace opencoded alloc and copy with vmemdup_user(). Signed-off-by: Denis Efremov <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 0e96edd commit 7ec28e2

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
181181
r = -E2BIG;
182182
if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
183183
goto out;
184-
r = -ENOMEM;
185184
if (cpuid->nent) {
186-
cpuid_entries =
187-
vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
188-
cpuid->nent));
189-
if (!cpuid_entries)
190-
goto out;
191-
r = -EFAULT;
192-
if (copy_from_user(cpuid_entries, entries,
193-
cpuid->nent * sizeof(struct kvm_cpuid_entry)))
185+
cpuid_entries = vmemdup_user(entries,
186+
array_size(sizeof(struct kvm_cpuid_entry),
187+
cpuid->nent));
188+
if (IS_ERR(cpuid_entries)) {
189+
r = PTR_ERR(cpuid_entries);
194190
goto out;
191+
}
195192
}
196193
for (i = 0; i < cpuid->nent; i++) {
197194
vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
@@ -211,8 +208,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
211208
kvm_x86_ops.cpuid_update(vcpu);
212209
r = kvm_update_cpuid(vcpu);
213210

211+
kvfree(cpuid_entries);
214212
out:
215-
vfree(cpuid_entries);
216213
return r;
217214
}
218215

virt/kvm/kvm_main.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,21 +3746,18 @@ static long kvm_vm_ioctl(struct file *filp,
37463746
if (routing.flags)
37473747
goto out;
37483748
if (routing.nr) {
3749-
r = -ENOMEM;
3750-
entries = vmalloc(array_size(sizeof(*entries),
3751-
routing.nr));
3752-
if (!entries)
3753-
goto out;
3754-
r = -EFAULT;
37553749
urouting = argp;
3756-
if (copy_from_user(entries, urouting->entries,
3757-
routing.nr * sizeof(*entries)))
3758-
goto out_free_irq_routing;
3750+
entries = vmemdup_user(urouting->entries,
3751+
array_size(sizeof(*entries),
3752+
routing.nr));
3753+
if (IS_ERR(entries)) {
3754+
r = PTR_ERR(entries);
3755+
goto out;
3756+
}
37593757
}
37603758
r = kvm_set_irq_routing(kvm, entries, routing.nr,
37613759
routing.flags);
3762-
out_free_irq_routing:
3763-
vfree(entries);
3760+
kvfree(entries);
37643761
break;
37653762
}
37663763
#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */

0 commit comments

Comments
 (0)