Skip to content

Commit 1a19354

Browse files
sean-jcbonzini
authored andcommitted
KVM: VMX: Do _all_ initialization before exposing /dev/kvm to userspace
Call kvm_init() only after _all_ setup is complete, as kvm_init() exposes /dev/kvm to userspace and thus allows userspace to create VMs (and call other ioctls). E.g. KVM will encounter a NULL pointer when attempting to add a vCPU to the per-CPU loaded_vmcss_on_cpu list if userspace is able to create a VM before vmx_init() configures said list. BUG: kernel NULL pointer dereference, address: 0000000000000008 #PF: supervisor write access in kernel mode #PF: error_code(0x0002) - not-present page PGD 0 P4D 0 Oops: 0002 [#1] SMP CPU: 6 PID: 1143 Comm: stable Not tainted 6.0.0-rc7+ #988 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:vmx_vcpu_load_vmcs+0x68/0x230 [kvm_intel] <TASK> vmx_vcpu_load+0x16/0x60 [kvm_intel] kvm_arch_vcpu_load+0x32/0x1f0 [kvm] vcpu_load+0x2f/0x40 [kvm] kvm_arch_vcpu_create+0x231/0x310 [kvm] kvm_vm_ioctl+0x79f/0xe10 [kvm] ? handle_mm_fault+0xb1/0x220 __x64_sys_ioctl+0x80/0xb0 do_syscall_64+0x2b/0x50 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x7f5a6b05743b </TASK> Modules linked in: vhost_net vhost vhost_iotlb tap kvm_intel(+) kvm irqbypass Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20221130230934.1014142-15-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent e73852e commit 1a19354

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

arch/x86/kvm/vmx/vmx.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8530,19 +8530,23 @@ static void vmx_cleanup_l1d_flush(void)
85308530
l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
85318531
}
85328532

8533-
static void vmx_exit(void)
8533+
static void __vmx_exit(void)
85348534
{
8535+
allow_smaller_maxphyaddr = false;
8536+
85358537
#ifdef CONFIG_KEXEC_CORE
85368538
RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
85378539
synchronize_rcu();
85388540
#endif
8541+
vmx_cleanup_l1d_flush();
8542+
}
85398543

8544+
static void vmx_exit(void)
8545+
{
85408546
kvm_exit();
85418547
kvm_x86_vendor_exit();
85428548

8543-
vmx_cleanup_l1d_flush();
8544-
8545-
allow_smaller_maxphyaddr = false;
8549+
__vmx_exit();
85468550
}
85478551
module_exit(vmx_exit);
85488552

@@ -8560,11 +8564,6 @@ static int __init vmx_init(void)
85608564
if (r)
85618565
return r;
85628566

8563-
r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
8564-
__alignof__(struct vcpu_vmx), THIS_MODULE);
8565-
if (r)
8566-
goto err_kvm_init;
8567-
85688567
/*
85698568
* Must be called after common x86 init so enable_ept is properly set
85708569
* up. Hand the parameter mitigation value in which was stored in
@@ -8598,11 +8597,20 @@ static int __init vmx_init(void)
85988597
if (!enable_ept)
85998598
allow_smaller_maxphyaddr = true;
86008599

8600+
/*
8601+
* Common KVM initialization _must_ come last, after this, /dev/kvm is
8602+
* exposed to userspace!
8603+
*/
8604+
r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
8605+
__alignof__(struct vcpu_vmx), THIS_MODULE);
8606+
if (r)
8607+
goto err_kvm_init;
8608+
86018609
return 0;
86028610

8603-
err_l1d_flush:
8604-
vmx_exit();
86058611
err_kvm_init:
8612+
__vmx_exit();
8613+
err_l1d_flush:
86068614
kvm_x86_vendor_exit();
86078615
return r;
86088616
}

0 commit comments

Comments
 (0)