Skip to content

Commit 5c697c3

Browse files
sean-jcbonzini
authored andcommitted
KVM: Initialize debugfs_dentry when a VM is created to avoid NULL deref
Initialize debugfs_entry to its semi-magical -ENOENT value when the VM is created. KVM's teardown when VM creation fails is kludgy and calls kvm_uevent_notify_change() and kvm_destroy_vm_debugfs() even if KVM never attempted kvm_create_vm_debugfs(). Because debugfs_entry is zero initialized, the IS_ERR() checks pass and KVM derefs a NULL pointer. BUG: kernel NULL pointer dereference, address: 0000000000000018 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 1068b1067 P4D 1068b1067 PUD 1068b0067 PMD 0 Oops: 0000 [#1] SMP CPU: 0 PID: 871 Comm: repro Not tainted 5.18.0-rc1+ #825 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:__dentry_path+0x7b/0x130 Call Trace: <TASK> dentry_path_raw+0x42/0x70 kvm_uevent_notify_change.part.0+0x10c/0x200 [kvm] kvm_put_kvm+0x63/0x2b0 [kvm] kvm_dev_ioctl+0x43a/0x920 [kvm] __x64_sys_ioctl+0x83/0xb0 do_syscall_64+0x31/0x50 entry_SYSCALL_64_after_hwframe+0x44/0xae </TASK> Modules linked in: kvm_intel kvm irqbypass Fixes: a44a4cc ("KVM: Don't create VM debugfs files outside of the VM directory") Cc: stable@vger.kernel.org Cc: Marc Zyngier <maz@kernel.org> Cc: Oliver Upton <oupton@google.com> Reported-by: syzbot+df6fbbd2ee39f21289ef@syzkaller.appspotmail.com Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Oliver Upton <oupton@google.com> Message-Id: <20220415004622.2207751-1-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 2031f28 commit 5c697c3

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

virt/kvm/kvm_main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,6 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
955955
int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc +
956956
kvm_vcpu_stats_header.num_desc;
957957

958-
/*
959-
* Force subsequent debugfs file creations to fail if the VM directory
960-
* is not created.
961-
*/
962-
kvm->debugfs_dentry = ERR_PTR(-ENOENT);
963-
964958
if (!debugfs_initialized())
965959
return 0;
966960

@@ -1081,6 +1075,12 @@ static struct kvm *kvm_create_vm(unsigned long type)
10811075

10821076
BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
10831077

1078+
/*
1079+
* Force subsequent debugfs file creations to fail if the VM directory
1080+
* is not created (by kvm_create_vm_debugfs()).
1081+
*/
1082+
kvm->debugfs_dentry = ERR_PTR(-ENOENT);
1083+
10841084
if (init_srcu_struct(&kvm->srcu))
10851085
goto out_err_no_srcu;
10861086
if (init_srcu_struct(&kvm->irq_srcu))

0 commit comments

Comments
 (0)