Skip to contentSkip to conversation

hw/misc/ivshmem: clear chardev handlers before freeing peers

PATCH In review sundayjiang(蒋浩天) 2 participants 2 messages Jun 29 – Jul 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/misc/ivshmem: clear chardev handlers before freeing peers

From 2a7fa17cd21d0a5c1ddf0f13f5c6b39375052aef Mon Sep 17 00:00:00 2001From: Haotian Jiang <[email protected]&gt;
Date: Mon, 29 Jun 2026 10:50:21 +0800
Subject: [PATCH] hw/misc/ivshmem: clear chardev handlers before freeing peers

ivshmem_exit() frees s-&gt;peers and s-&gt;msi_vectors but does not clear the chardev handlers registered in ivshmem_common_realize(). &nbsp;Those handlers are only removed later in object_finalize() via release_chr, which runs after ivshmem_exit().

Between exit and finalize, ivshmem_read() can fire on pending chardev data and process_msg_connect() dereferences the freed s-&gt;peers. Additionally, s-&gt;peers, s-&gt;nb_peers, and s-&gt;msi_vectors are not zeroed after free, leaving dangling pointers that make the UAF code paths reachable.

Fix by clearing chardev handlers at the beginning of ivshmem_exit(), before any resources they access are freed, and nullifying freed pointers.

---
&nbsp;hw/misc/ivshmem-pci.c | 6 ++++++
&nbsp;1 file changed, 6 insertions(+)

hw/misc/ivshmem-pci.c +0 -0
index c987eebb98..ce0de5271e 100644
@@ -938,6 +938,9 @@ static void ivshmem_exit(PCIDevice *dev)

&nbsp; &nbsp; &nbsp;IVShmemState *s = IVSHMEM_COMMON(dev);
&nbsp; &nbsp; &nbsp;int i;
&nbsp;
+ &nbsp; &nbsp;qemu_chr_fe_set_handlers(&amp;s-&gt;server_chr, NULL, NULL, NULL, NULL,
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL, NULL, true); +
&nbsp; &nbsp; &nbsp;migrate_del_blocker(&amp;s-&gt;migration_blocker);
&nbsp;
&nbsp; &nbsp; &nbsp;if (memory_region_is_mapped(s-&gt;ivshmem_bar2)) {
@@ -966,6 +969,8 @@ static void ivshmem_exit(PCIDevice *dev)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;close_peer_eventfds(s, i);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;g_free(s-&gt;peers);
+ &nbsp; &nbsp; &nbsp; &nbsp;s-&gt;peers = NULL;
+ &nbsp; &nbsp; &nbsp; &nbsp;s-&gt;nb_peers = 0;
&nbsp; &nbsp; &nbsp;}
&nbsp;
&nbsp; &nbsp; &nbsp;if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
@@ -973,6 +978,7 @@ static void ivshmem_exit(PCIDevice *dev)
&nbsp; &nbsp; &nbsp;}
&nbsp;
&nbsp; &nbsp; &nbsp;g_free(s-&gt;msi_vectors);
+ &nbsp; &nbsp;s-&gt;msi_vectors = NULL;
&nbsp;}
&nbsp;
&nbsp;static int ivshmem_pre_load(void *opaque)

Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

Cc'ing Gustavo

Quoted 57 lines · sundayjiang(èæµ©å¤©)

From 2a7fa17cd21d0a5c1ddf0f13f5c6b39375052aef Mon Sep 17 00:00:00 2001From: Haotian Jiang <[email protected]&gt;
Date: Mon, 29 Jun 2026 10:50:21 +0800
Subject: [PATCH] hw/misc/ivshmem: clear chardev handlers before freeing peers

ivshmem_exit() frees s-&gt;peers and s-&gt;msi_vectors but does not clear the chardev handlers registered in ivshmem_common_realize(). &nbsp;Those handlers are only removed later in object_finalize() via release_chr, which runs after ivshmem_exit().

Between exit and finalize, ivshmem_read() can fire on pending chardev data and process_msg_connect() dereferences the freed s-&gt;peers. Additionally, s-&gt;peers, s-&gt;nb_peers, and s-&gt;msi_vectors are not zeroed after free, leaving dangling pointers that make the UAF code paths reachable.

Fix by clearing chardev handlers at the beginning of ivshmem_exit(), before any resources they access are freed, and nullifying freed pointers.

---
&nbsp;hw/misc/ivshmem-pci.c | 6 ++++++
&nbsp;1 file changed, 6 insertions(+)

hw/misc/ivshmem-pci.c +5 -0
index c987eebb98..ce0de5271e 100644
@@ -938,6 +938,9 @@ static void ivshmem_exit(PCIDevice *dev)
 &nbsp; &nbsp; &nbsp;IVShmemState *s = IVSHMEM_COMMON(dev);
 &nbsp; &nbsp; &nbsp;int i;
 &nbsp;
+ &nbsp; &nbsp;qemu_chr_fe_set_handlers(&amp;s-&gt;server_chr, NULL, NULL, NULL, NULL,
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL, NULL, true);
+
 &nbsp; &nbsp; &nbsp;migrate_del_blocker(&amp;s-&gt;migration_blocker);
 &nbsp;
 &nbsp; &nbsp; &nbsp;if (memory_region_is_mapped(s-&gt;ivshmem_bar2)) {
@@ -966,6 +969,8 @@ static void ivshmem_exit(PCIDevice *dev)
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;close_peer_eventfds(s, i);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;g_free(s-&gt;peers);
+ &nbsp; &nbsp; &nbsp; &nbsp;s-&gt;peers = NULL;
+ &nbsp; &nbsp; &nbsp; &nbsp;s-&gt;nb_peers = 0;
 &nbsp; &nbsp; &nbsp;}
 &nbsp;
 &nbsp; &nbsp; &nbsp;if (ivshmem_has_feature(s, IVSHMEM_MSI)) {

hw/misc/ivshmem-pci.c static void ivshmem_exit(PCIDevice *dev) · line 984

Quoted 7 lines
hw/misc/ivshmem-pci.c +1 -0
@@ -973,6 +978,7 @@ static void ivshmem_exit(PCIDevice *dev)
 &nbsp; &nbsp; &nbsp;}
 &nbsp;
 &nbsp; &nbsp; &nbsp;g_free(s-&gt;msi_vectors);
+ &nbsp; &nbsp;s-&gt;msi_vectors = NULL;
 &nbsp;}
 &nbsp;
 &nbsp;static int ivshmem_pre_load(void *opaque)