From nobody Mon Feb 9 17:49:03 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1589353395; cv=none; d=zohomail.com; s=zohoarc; b=T6l7IvQl+lWmK348gqeqWKKLEPWqPQbwQNHB5Ahuf+8ROkJ53QzLOzy1RbjrZ2yEqnXFvFXxlZAyNCl5lEY/j8f2m4oeyvGPQfZ1AaodniUbdNn2emCqd0rAKF3vMK4ahaBw3j7ZhPKNFLeKDxPLonWHHZojDiVxSWkd+Mdki+Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589353395; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=8C6n01nkAYPl2QWp9jff2EwfxQ7YSBak1E4n8irW6pY=; b=n7agRM7FDHaciwIE8BSEsynitG50UaIOg97M1Rgv+LBsaSmIxdjW6cbAckiobAwzlgYkoBarkiv+ocEz0eGVkVYaOSkeG2wXtC0nYQR5YnMM6J+NM50+CmP6A48/PWzGzwGqLBGRtbuzZ1BK4DVnTRhzXy5iakzNusFKBRAg5Sw= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1589353395896246.64622248630542; Wed, 13 May 2020 00:03:15 -0700 (PDT) Received: from localhost ([::1]:36168 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jYlQA-00050y-Ev for importer@patchew.org; Wed, 13 May 2020 03:03:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50604) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jYlPM-0004CF-41 for qemu-devel@nongnu.org; Wed, 13 May 2020 03:02:24 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:49694 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jYlPK-00060n-7f for qemu-devel@nongnu.org; Wed, 13 May 2020 03:02:23 -0400 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id D2CC1BA2BD7B96820548; Wed, 13 May 2020 15:02:15 +0800 (CST) Received: from opensource.huawei.com (10.175.100.98) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.487.0; Wed, 13 May 2020 15:02:05 +0800 From: Pan Nengyuan To: , , , Subject: [PATCH v2] i386/kvm: fix a use-after-free when vcpu plug/unplug Date: Wed, 13 May 2020 09:26:30 -0400 Message-ID: <20200513132630.13412-1-pannengyuan@huawei.com> X-Mailer: git-send-email 2.18.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.100.98] X-CFilter-Loop: Reflected Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=45.249.212.35; envelope-from=pannengyuan@huawei.com; helo=huawei.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/13 03:02:16 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DATE_IN_FUTURE_06_12=1.947, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: euler.robot@huawei.com, Pan Nengyuan , qemu-devel@nongnu.org, kvm@vger.kernel.org, zhang.zhanghailiang@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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: =3D=3Dqemu-system-x86_64=3D=3D28233=3D=3DERROR: 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:7= 42 #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:1= 45 #9 0x5573c71dad4f in monitor_qmp_bh_dispatcher /mnt/sdb/qemu/monitor/qm= p.c:234 Reported-by: Euler Robot Signed-off-by: Pan Nengyuan Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Igor Mammedov --- - v2: remove unnecessary set vmsentry to null(there is no non-null check). --- target/i386/cpu.h | 1 + target/i386/kvm.c | 4 +++- 2 files changed, 4 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 { =20 CPUNegativeOffsetState neg; CPUX86State env; + VMChangeStateEntry *vmsentry; =20 uint64_t ucode_rev; =20 diff --git a/target/i386/kvm.c b/target/i386/kvm.c index 4901c6dd74..0a4eca5a85 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -1770,7 +1770,7 @@ int kvm_arch_init_vcpu(CPUState *cs) } } =20 - qemu_add_vm_change_state_handler(cpu_update_state, env); + cpu->vmsentry =3D qemu_add_vm_change_state_handler(cpu_update_state, e= nv); =20 c =3D cpuid_find_entry(&cpuid_data.cpuid, 1, 0); if (c) { @@ -1883,6 +1883,8 @@ int kvm_arch_destroy_vcpu(CPUState *cs) env->nested_state =3D NULL; } =20 + qemu_del_vm_change_state_handler(cpu->vmsentry); + return 0; } =20 --=20 2.18.2