diff mbox series

i386/kvm: fix a use-after-free when vcpu plug/unplug

Message ID 20200512133933.8696-1-pannengyuan@huawei.com (mailing list archive)
State New, archived
Headers show
Series i386/kvm: fix a use-after-free when vcpu plug/unplug | expand

Commit Message

Pan Nengyuan May 12, 2020, 1:39 p.m. UTC
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
    #4 0x5573c71e8304 in qmp_cont /mnt/sdb/qemu/monitor/qmp-cmds.c:173
    #5 0x5573c727cb1e in qmp_marshal_cont qapi/qapi-commands-misc.c:835
    #6 0x5573c7694c7a in do_qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:132
    #7 0x5573c7694c7a in qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:175
    #8 0x5573c71d9110 in monitor_qmp_dispatch /mnt/sdb/qemu/monitor/qmp.c:145
    #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>
---
 target/i386/cpu.h | 1 +
 target/i386/kvm.c | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)

Comments

Philippe Mathieu-Daudé May 12, 2020, 7:54 a.m. UTC | #1
On 5/12/20 3:39 PM, Pan Nengyuan wrote:
> 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
>      #4 0x5573c71e8304 in qmp_cont /mnt/sdb/qemu/monitor/qmp-cmds.c:173
>      #5 0x5573c727cb1e in qmp_marshal_cont qapi/qapi-commands-misc.c:835
>      #6 0x5573c7694c7a in do_qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:132
>      #7 0x5573c7694c7a in qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:175
>      #8 0x5573c71d9110 in monitor_qmp_dispatch /mnt/sdb/qemu/monitor/qmp.c:145
>      #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>
> ---
>   target/i386/cpu.h | 1 +
>   target/i386/kvm.c | 5 ++++-
>   2 files changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/target/i386/cpu.h b/target/i386/cpu.h
> index e818fc712a..afbd11b7a3 100644
> --- a/target/i386/cpu.h
> +++ b/target/i386/cpu.h
> @@ -1631,6 +1631,7 @@ struct X86CPU {
>   
>       CPUNegativeOffsetState neg;
>       CPUX86State env;
> +    VMChangeStateEntry *vmsentry;
>   
>       uint64_t ucode_rev;
>   
> diff --git a/target/i386/kvm.c b/target/i386/kvm.c
> index 4901c6dd74..ff2848357e 100644
> --- a/target/i386/kvm.c
> +++ b/target/i386/kvm.c
> @@ -1770,7 +1770,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) {
> @@ -1883,6 +1883,9 @@ int kvm_arch_destroy_vcpu(CPUState *cs)
>           env->nested_state = NULL;
>       }
>   
> +    qemu_del_vm_change_state_handler(cpu->vmsentry);
> +    cpu->vmsentry = NULL;

Why set it to NULL? there is no non-NULL check.

Anyway if it matters to you, why not do it in 
qemu_del_vm_change_state_handler()?

Otherwise:
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> +
>       return 0;
>   }
>   
>
Pan Nengyuan May 12, 2020, 8:09 a.m. UTC | #2
On 5/12/2020 3:54 PM, Philippe Mathieu-Daudé wrote:
> On 5/12/20 3:39 PM, Pan Nengyuan wrote:
>> 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
>>      #4 0x5573c71e8304 in qmp_cont /mnt/sdb/qemu/monitor/qmp-cmds.c:173
>>      #5 0x5573c727cb1e in qmp_marshal_cont qapi/qapi-commands-misc.c:835
>>      #6 0x5573c7694c7a in do_qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:132
>>      #7 0x5573c7694c7a in qmp_dispatch /mnt/sdb/qemu/qapi/qmp-dispatch.c:175
>>      #8 0x5573c71d9110 in monitor_qmp_dispatch /mnt/sdb/qemu/monitor/qmp.c:145
>>      #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>
>> ---
>>   target/i386/cpu.h | 1 +
>>   target/i386/kvm.c | 5 ++++-
>>   2 files changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/target/i386/cpu.h b/target/i386/cpu.h
>> index e818fc712a..afbd11b7a3 100644
>> --- a/target/i386/cpu.h
>> +++ b/target/i386/cpu.h
>> @@ -1631,6 +1631,7 @@ struct X86CPU {
>>         CPUNegativeOffsetState neg;
>>       CPUX86State env;
>> +    VMChangeStateEntry *vmsentry;
>>         uint64_t ucode_rev;
>>   diff --git a/target/i386/kvm.c b/target/i386/kvm.c
>> index 4901c6dd74..ff2848357e 100644
>> --- a/target/i386/kvm.c
>> +++ b/target/i386/kvm.c
>> @@ -1770,7 +1770,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) {
>> @@ -1883,6 +1883,9 @@ int kvm_arch_destroy_vcpu(CPUState *cs)
>>           env->nested_state = NULL;
>>       }
>>   +    qemu_del_vm_change_state_handler(cpu->vmsentry);
>> +    cpu->vmsentry = NULL;
> 
> Why set it to NULL? there is no non-NULL check.
> 
> Anyway if it matters to you, why not do it in qemu_del_vm_change_state_handler()?

Yes, there is no non-NULL check and it will not be NULL at all.

Actually it doesn't matters to me, just habitually set to null after free.

I will remove it.

Thanks.

> 
> Otherwise:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> 
>> +
>>       return 0;
>>   }
>>  
> 
> .
diff mbox series

Patch

diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index e818fc712a..afbd11b7a3 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -1631,6 +1631,7 @@  struct X86CPU {
 
     CPUNegativeOffsetState neg;
     CPUX86State env;
+    VMChangeStateEntry *vmsentry;
 
     uint64_t ucode_rev;
 
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
index 4901c6dd74..ff2848357e 100644
--- a/target/i386/kvm.c
+++ b/target/i386/kvm.c
@@ -1770,7 +1770,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) {
@@ -1883,6 +1883,9 @@  int kvm_arch_destroy_vcpu(CPUState *cs)
         env->nested_state = NULL;
     }
 
+    qemu_del_vm_change_state_handler(cpu->vmsentry);
+    cpu->vmsentry = NULL;
+
     return 0;
 }