Skip to content

Commit

Permalink
KVM: Prevent kvm_init from corrupting debugfs structures
Browse files Browse the repository at this point in the history
I'm seeing an oops condition when kvm-intel and kvm-amd are modprobe'd
during boot (say on an Intel system) and then rmmod'd:

   # modprobe kvm-intel
     kvm_init()
     kvm_init_debug()
     kvm_arch_init()  <-- stores debugfs dentries internally
     (success, etc)

   # modprobe kvm-amd
     kvm_init()
     kvm_init_debug() <-- second initialization clobbers kvm's
                          internal pointers to dentries
     kvm_arch_init()
     kvm_exit_debug() <-- and frees them

   # rmmod kvm-intel
     kvm_exit()
     kvm_exit_debug() <-- double free of debugfs files!

     *BOOM*

If execution gets to the end of kvm_init(), then the calling module has been
established as the kvm provider.  Move the debugfs initialization to the end of
the function, and remove the now-unnecessary call to kvm_exit_debug() from the
error path.  That way we avoid trampling on the debugfs entries and freeing
them twice.

Cc: stable@kernel.org
Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
  • Loading branch information
Darrick J. Wong authored and Marcelo Tosatti committed Oct 16, 2009
1 parent 8a8365c commit 0ea4ed8
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions virt/kvm/kvm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2717,8 +2717,6 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
int r;
int cpu;

kvm_init_debug();

r = kvm_arch_init(opaque);
if (r)
goto out_fail;
Expand Down Expand Up @@ -2785,6 +2783,8 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
kvm_preempt_ops.sched_in = kvm_sched_in;
kvm_preempt_ops.sched_out = kvm_sched_out;

kvm_init_debug();

return 0;

out_free:
Expand All @@ -2807,14 +2807,14 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
out:
kvm_arch_exit();
out_fail:
kvm_exit_debug();
return r;
}
EXPORT_SYMBOL_GPL(kvm_init);

void kvm_exit(void)
{
tracepoint_synchronize_unregister();
kvm_exit_debug();
misc_deregister(&kvm_dev);
kmem_cache_destroy(kvm_vcpu_cache);
sysdev_unregister(&kvm_sysdev);
Expand All @@ -2824,7 +2824,6 @@ void kvm_exit(void)
on_each_cpu(hardware_disable, NULL, 1);
kvm_arch_hardware_unsetup();
kvm_arch_exit();
kvm_exit_debug();
free_cpumask_var(cpus_hardware_enabled);
__free_page(bad_page);
}
Expand Down

0 comments on commit 0ea4ed8

Please sign in to comment.