Skip to content

Commit f8c1b85

Browse files
bonzinirkrcmar
authored andcommitted
KVM: x86: avoid vmalloc(0) in the KVM_SET_CPUID
This causes an ugly dmesg splat. Beautified syzkaller testcase: #include <unistd.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <fcntl.h> #include <linux/kvm.h> long r[8]; int main() { struct kvm_irq_routing ir = { 0 }; r[2] = open("/dev/kvm", O_RDWR); r[3] = ioctl(r[2], KVM_CREATE_VM, 0); r[4] = ioctl(r[3], KVM_SET_GSI_ROUTING, &ir); return 0; } Reported-by: Dmitry Vyukov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: Radim Krčmář <[email protected]>
1 parent c622a3c commit f8c1b85

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

virt/kvm/kvm_main.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,7 @@ static long kvm_vm_ioctl(struct file *filp,
29352935
case KVM_SET_GSI_ROUTING: {
29362936
struct kvm_irq_routing routing;
29372937
struct kvm_irq_routing __user *urouting;
2938-
struct kvm_irq_routing_entry *entries;
2938+
struct kvm_irq_routing_entry *entries = NULL;
29392939

29402940
r = -EFAULT;
29412941
if (copy_from_user(&routing, argp, sizeof(routing)))
@@ -2945,15 +2945,17 @@ static long kvm_vm_ioctl(struct file *filp,
29452945
goto out;
29462946
if (routing.flags)
29472947
goto out;
2948-
r = -ENOMEM;
2949-
entries = vmalloc(routing.nr * sizeof(*entries));
2950-
if (!entries)
2951-
goto out;
2952-
r = -EFAULT;
2953-
urouting = argp;
2954-
if (copy_from_user(entries, urouting->entries,
2955-
routing.nr * sizeof(*entries)))
2956-
goto out_free_irq_routing;
2948+
if (routing.nr) {
2949+
r = -ENOMEM;
2950+
entries = vmalloc(routing.nr * sizeof(*entries));
2951+
if (!entries)
2952+
goto out;
2953+
r = -EFAULT;
2954+
urouting = argp;
2955+
if (copy_from_user(entries, urouting->entries,
2956+
routing.nr * sizeof(*entries)))
2957+
goto out_free_irq_routing;
2958+
}
29572959
r = kvm_set_irq_routing(kvm, entries, routing.nr,
29582960
routing.flags);
29592961
out_free_irq_routing:

0 commit comments

Comments
 (0)