Skip to content

Commit 5c1f50a

Browse files
Dan Carpentersean-jc
authored andcommitted
KVM: Fix a goof where kvm_create_vm() returns 0 instead of -ENOMEM
The error path for OOM when allocating buses used to return -ENOMEM using the local variable 'r', where 'r' was initialized at the top of the function. But a new "r = kvm_init_irq_routing(kvm);" was introduced in the middle of the function, so now the error code is not set and it eventually leads to a NULL dereference due to kvm_dev_ioctl_create_vm() thinking kvm_create_vm() succeeded. Set the error code back to -ENOMEM. Opportunistically tweak the logic to pre-set "r = -ENOMEM" immediately before the flows that can fail due to memory allocation failure to make it less likely that the bug recurs in the future. Fixes: fbe4a7e ("KVM: Setup empty IRQ routing when creating a VM") Signed-off-by: Dan Carpenter <[email protected]> Link: https://lore.kernel.org/r/[email protected] [sean: tweak all of the "r = -ENOMEM" sites, massage changelog] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 3dee3b1 commit 5c1f50a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

virt/kvm/kvm_main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,8 +1143,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
11431143
{
11441144
struct kvm *kvm = kvm_arch_alloc_vm();
11451145
struct kvm_memslots *slots;
1146-
int r = -ENOMEM;
1147-
int i, j;
1146+
int r, i, j;
11481147

11491148
if (!kvm)
11501149
return ERR_PTR(-ENOMEM);
@@ -1181,6 +1180,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
11811180
snprintf(kvm->stats_id, sizeof(kvm->stats_id), "kvm-%d",
11821181
task_pid_nr(current));
11831182

1183+
r = -ENOMEM;
11841184
if (init_srcu_struct(&kvm->srcu))
11851185
goto out_err_no_srcu;
11861186
if (init_srcu_struct(&kvm->irq_srcu))
@@ -1209,6 +1209,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
12091209
rcu_assign_pointer(kvm->memslots[i], &kvm->__memslots[i][0]);
12101210
}
12111211

1212+
r = -ENOMEM;
12121213
for (i = 0; i < KVM_NR_BUSES; i++) {
12131214
rcu_assign_pointer(kvm->buses[i],
12141215
kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));

0 commit comments

Comments
 (0)