[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v1 03/21] i386/xen: Free Xen vCPU timers when a vCPU is destroyed



From: David Woodhouse <dwmw@xxxxxxxxxxxx>

kvm_xen_init_vcpu() allocates the per-vCPU timers with the CPUState as
callback opaque, and nothing freed them on vCPU destruction. So a guest
could arm the periodic timer for a vCPU (VCPUOP_set_periodic_timer
always reaches QEMU; the kernel doesn't handle it) and eject that vCPU
via ACPI hotplug, leaving the armed timer pointing at a freed X86CPU.

Free both timers in kvm_xen_destroy_vcpu(), called from
kvm_arch_destroy_vcpu(). That runs under the BQL, as do the timer
callbacks, so it cannot race an in-flight callback. It *can* race a
timer hypercall from another vCPU — hypercalls don't take the BQL,
which is why xen_timers_lock exists — so clear the pointers under that
lock and have the periodic timer paths return -ENOENT when the timer is
gone, as for a nonexistent vCPU. The singleshot ops act only on the
calling vCPU, so cannot race its destruction.

Also move the timer_del() in do_set_periodic_timer() inside the lock,
where it should have been all along.

Fixes: CVE-2026-123069
Fixes: b746a779264f ("i386/xen: handle PV timer hypercalls")
Reported-by: MyeonghunPak <https://gitlab.com/mhun512>
Closes: https://gitlab.com/qemu-project/qemu/-/work_items/4098
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
 target/i386/kvm/kvm.c     |  6 ++++++
 target/i386/kvm/xen-emu.c | 36 ++++++++++++++++++++++++++++++++++--
 target/i386/kvm/xen-emu.h |  1 +
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 76528f1123f..302621f30af 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -2556,6 +2556,12 @@ int kvm_arch_destroy_vcpu(CPUState *cs)
     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
 
+#ifdef CONFIG_XEN_EMU
+    if (cs->kvm_state->xen_version) {
+        kvm_xen_destroy_vcpu(cs);
+    }
+#endif
+
     g_free(env->xsave_buf);
 
     g_free(cpu->kvm_msr_buf);
diff --git a/target/i386/kvm/xen-emu.c b/target/i386/kvm/xen-emu.c
index 7337561b159..67d1379e024 100644
--- a/target/i386/kvm/xen-emu.c
+++ b/target/i386/kvm/xen-emu.c
@@ -276,6 +276,28 @@ int kvm_xen_init_vcpu(CPUState *cs)
     return 0;
 }
 
+void kvm_xen_destroy_vcpu(CPUState *cs)
+{
+    CPUX86State *env = &X86_CPU(cs)->env;
+
+    /*
+     * This runs on the vCPU's own thread with the BQL held, and the
+     * timer callbacks run from the main loop thread also with the BQL
+     * held, so freeing the timers cannot race against an in-flight
+     * callback. It *can* race against a Xen hypercall from another
+     * vCPU targeting this one (hypercalls are handled without the
+     * BQL), so clear the pointers under xen_timers_lock to let those
+     * paths observe the vCPU's demise.
+     */
+    QEMU_LOCK_GUARD(&env->xen_timers_lock);
+
+    timer_free(env->xen_singleshot_timer);
+    env->xen_singleshot_timer = NULL;
+
+    timer_free(env->xen_periodic_timer);
+    env->xen_periodic_timer = NULL;
+}
+
 uint32_t kvm_xen_get_caps(void)
 {
     return kvm_state->xen_caps;
@@ -1079,10 +1101,15 @@ static int do_set_periodic_timer(CPUState *target, 
uint64_t period_ns)
     CPUX86State *tenv = &X86_CPU(target)->env;
     int64_t qemu_now;
 
-    timer_del(tenv->xen_periodic_timer);
-
     qemu_mutex_lock(&tenv->xen_timers_lock);
 
+    if (!tenv->xen_periodic_timer) {
+        qemu_mutex_unlock(&tenv->xen_timers_lock);
+        return -ENOENT;
+    }
+
+    timer_del(tenv->xen_periodic_timer);
+
     qemu_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
     timer_mod_ns(tenv->xen_periodic_timer, qemu_now + period_ns);
     tenv->xen_periodic_timer_period = period_ns;
@@ -1120,6 +1147,11 @@ static int vcpuop_stop_periodic_timer(CPUState *target)
 
     qemu_mutex_lock(&tenv->xen_timers_lock);
 
+    if (!tenv->xen_periodic_timer) {
+        qemu_mutex_unlock(&tenv->xen_timers_lock);
+        return -ENOENT;
+    }
+
     timer_del(tenv->xen_periodic_timer);
     tenv->xen_periodic_timer_period = 0;
 
diff --git a/target/i386/kvm/xen-emu.h b/target/i386/kvm/xen-emu.h
index fe85e0b1958..467b9f088ae 100644
--- a/target/i386/kvm/xen-emu.h
+++ b/target/i386/kvm/xen-emu.h
@@ -25,6 +25,7 @@
 
 int kvm_xen_init(KVMState *s, uint32_t hypercall_msr);
 int kvm_xen_init_vcpu(CPUState *cs);
+void kvm_xen_destroy_vcpu(CPUState *cs);
 int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit);
 int kvm_put_xen_state(CPUState *cs);
 int kvm_get_xen_state(CPUState *cs);
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.