Skip to content

Commit

Permalink
i386/kvm: fix a use-after-free when vcpu plug/unplug
Browse files Browse the repository at this point in the history
When we hotplug vcpus, cpu_update_state is added to vm_change_state_head
in kvm_arch_init_vcpu(). But it forgot to delete in kvm_arch_destroy_vcpu() after
unplug. Then it will cause a use-after-free access. This patch delete it in
kvm_arch_destroy_vcpu() to fix that.

Reproducer:
    virsh setvcpus vm1 4 --live
    virsh setvcpus vm1 2 --live
    virsh suspend vm1
    virsh resume vm1

The UAF stack:
==qemu-system-x86_64==28233==ERROR: AddressSanitizer: heap-use-after-free on address 0x62e00002e798 at pc 0x5573c6917d9e bp 0x7fff07139e50 sp 0x7fff07139e40
WRITE of size 1 at 0x62e00002e798 thread T0
    #0 0x5573c6917d9d in cpu_update_state /mnt/sdb/qemu/target/i386/kvm.c:742
    #1 0x5573c699121a in vm_state_notify /mnt/sdb/qemu/vl.c:1290
    #2 0x5573c636287e in vm_prepare_start /mnt/sdb/qemu/cpus.c:2144
    #3 0x5573c6362927 in vm_start /mnt/sdb/qemu/cpus.c:2150
    qemu#4 0x5573c71e8304 in qmp_cont /mnt/sdb/qemu/monitor/qmp-cmds.c:173
    qemu#5 0x5573c727cb1e in qmp_marshal_cont qapi/qapi-commands-misc.c:835
    qemu#6 0x5573c7694c7a in do_qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:132
    qemu#7 0x5573c7694c7a in qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:175
    qemu#8 0x5573c71d9110 in monitor_qmp_dispatch /mnt/sdb/qemu/monitor/qmp.c:145
    qemu#9 0x5573c71dad4f in monitor_qmp_bh_dispatcher /mnt/sdb/qemu/monitor/qmp.c:234

Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20200513132630.13412-1-pannengyuan@huawei.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
panny060 authored and bonzini committed Jun 10, 2020
1 parent 34a0950 commit 2a69314
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions target/i386/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,7 @@ struct X86CPU {

CPUNegativeOffsetState neg;
CPUX86State env;
VMChangeStateEntry *vmsentry;

uint64_t ucode_rev;

Expand Down
4 changes: 3 additions & 1 deletion target/i386/kvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
}
}

qemu_add_vm_change_state_handler(cpu_update_state, env);
cpu->vmsentry = qemu_add_vm_change_state_handler(cpu_update_state, env);

c = cpuid_find_entry(&cpuid_data.cpuid, 1, 0);
if (c) {
Expand Down Expand Up @@ -1852,6 +1852,8 @@ int kvm_arch_destroy_vcpu(CPUState *cs)
env->nested_state = NULL;
}

qemu_del_vm_change_state_handler(cpu->vmsentry);

return 0;
}

Expand Down

0 comments on commit 2a69314

Please sign in to comment.