* [PATCH 1/5] net: Make slirp cleanup idempotent
2026-12-04 10:43 [PATCH 0/5] net: make client cleanup callbacks idempotent David Woodhouse
@ 2026-12-04 10:43 ` David Woodhouse
2026-12-05 0:40 ` Samuel Thibault
2026-12-04 10:43 ` [PATCH 2/5] net: Make vde " David Woodhouse
` (3 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: David Woodhouse @ 2026-12-04 10:43 UTC (permalink / raw)
To: qemu-devel
Cc: Jason Wang, Michael S . Tsirkin, Stefano Garzarella,
Samuel Thibault, qemu-stable, David Woodhouse
From: David Woodhouse <dwmw@amazon.co.uk>
Since commit e7891c575fb2 ("net: move backend cleanup to NIC cleanup"),
a backend whose peer NIC has taken ownership of it (nic->peer_deleted)
has its cleanup callback invoked from both net_cleanup() and
qemu_del_nic(). Normally only one of those runs, but the xen-net-device
NIC is deleted from the XenDevice exit notifier, after net_cleanup():
the backend is cleaned up twice.
For slirp the second invocation frees the Slirp state again, and glibc
aborts with "double free or corruption (!prev)". Trivially reproducible
with:
qemu-system-x86_64 -accel kvm,xen-version=0x4000a,kernel-irqchip=split \
-netdev user,id=n0 -device xen-net-device,netdev=n0
and quitting.
Most cleanup callbacks are already idempotent. Document that NetCleanup,
like a GObject dispose method, must cope with being invoked more than
once, and make slirp bail out if s->slirp is already NULL. Also guard
net_slirp_receive(): the NIC owns the client beyond cleanup, and a
packet delivered in that window was a use-after-free. (Slirp's timers
all fire from the main loop and slirp_cleanup() deletes them, so none
can fire after the first cleanup.)
The remaining non-idempotent backends will be fixed in subsequent
patches, in roughly decreasing order of reachability.
Verified with valgrind that runtime deletion (device_del and netdev_del,
in both orders, for e1000 and xen-net-device) still cleans up exactly
once, and that the reproducer now exits cleanly.
Fixes: e7891c575fb2 ("net: move backend cleanup to NIC cleanup")
Cc: qemu-stable@nongnu.org
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
include/net/net.h | 8 ++++++++
net/slirp.c | 20 +++++++++++++++++++-
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/include/net/net.h b/include/net/net.h
index c2fd451f17..6394df827f 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -62,6 +62,14 @@ typedef int (NetLoad)(NetClientState *);
typedef void (NetStop)(NetClientState *);
typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
+/*
+ * NetCleanup may be invoked more than once on the same client; like a
+ * GObject dispose method it must be idempotent. In particular, a NIC
+ * which has taken ownership of its deleted peer (nic->peer_deleted)
+ * cleans the peer up from both net_cleanup() and qemu_del_nic(), and
+ * for the xen-net-device NIC the latter runs from an exit notifier
+ * after net_cleanup() has already done so.
+ */
typedef void (NetCleanup) (NetClientState *);
typedef void (LinkStatusChanged)(NetClientState *);
typedef void (NetClientDestructor)(NetClientState *);
diff --git a/net/slirp.c b/net/slirp.c
index eea12e7981..bf59639120 100644
--- a/net/slirp.c
+++ b/net/slirp.c
@@ -128,7 +128,10 @@ static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t
{
SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
- slirp_input(s->slirp, buf, size);
+ /* The client may live on after cleanup, owned by its peer NIC. */
+ if (s->slirp) {
+ slirp_input(s->slirp, buf, size);
+ }
return size;
}
@@ -151,12 +154,27 @@ static void net_slirp_cleanup(NetClientState *nc)
{
SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
+ /*
+ * A NIC which has taken ownership of its deleted peer
+ * (nic->peer_deleted) cleans the peer up both from net_cleanup()
+ * and from qemu_del_nic(); for the xen-net-device NIC the latter
+ * happens in an exit notifier after net_cleanup() has already run.
+ * Like a GObject dispose method, cleanup must therefore cope with
+ * being invoked more than once.
+ */
+ if (!s->slirp) {
+ return;
+ }
+
g_slist_free_full(s->fwd, slirp_free_fwd);
+ s->fwd = NULL;
main_loop_poll_remove_notifier(&s->poll_notifier);
unregister_savevm(NULL, "slirp", s->slirp);
slirp_cleanup(s->slirp);
+ s->slirp = NULL;
if (s->exit_notifier.notify) {
qemu_remove_exit_notifier(&s->exit_notifier);
+ s->exit_notifier.notify = NULL;
}
slirp_smb_cleanup(s);
QTAILQ_REMOVE(&slirp_stacks, s, entry);
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 1/5] net: Make slirp cleanup idempotent
2026-12-04 10:43 ` [PATCH 1/5] net: Make slirp cleanup idempotent David Woodhouse
@ 2026-12-05 0:40 ` Samuel Thibault
0 siblings, 0 replies; 7+ messages in thread
From: Samuel Thibault @ 2026-12-05 0:40 UTC (permalink / raw)
To: David Woodhouse
Cc: qemu-devel, Jason Wang, Michael S . Tsirkin, Stefano Garzarella,
qemu-stable, David Woodhouse
[-- Attachment #1: Type: text/plain, Size: 4822 bytes --]
David Woodhouse, le ven. 04 déc. 2026 10:43:41 +0000, a ecrit:
> From: David Woodhouse <dwmw@amazon.co.uk>
>
> Since commit e7891c575fb2 ("net: move backend cleanup to NIC cleanup"),
> a backend whose peer NIC has taken ownership of it (nic->peer_deleted)
> has its cleanup callback invoked from both net_cleanup() and
> qemu_del_nic(). Normally only one of those runs, but the xen-net-device
> NIC is deleted from the XenDevice exit notifier, after net_cleanup():
> the backend is cleaned up twice.
>
> For slirp the second invocation frees the Slirp state again, and glibc
> aborts with "double free or corruption (!prev)". Trivially reproducible
> with:
>
> qemu-system-x86_64 -accel kvm,xen-version=0x4000a,kernel-irqchip=split \
> -netdev user,id=n0 -device xen-net-device,netdev=n0
>
> and quitting.
>
> Most cleanup callbacks are already idempotent. Document that NetCleanup,
> like a GObject dispose method, must cope with being invoked more than
> once, and make slirp bail out if s->slirp is already NULL. Also guard
> net_slirp_receive(): the NIC owns the client beyond cleanup, and a
> packet delivered in that window was a use-after-free. (Slirp's timers
> all fire from the main loop and slirp_cleanup() deletes them, so none
> can fire after the first cleanup.)
>
> The remaining non-idempotent backends will be fixed in subsequent
> patches, in roughly decreasing order of reachability.
>
> Verified with valgrind that runtime deletion (device_del and netdev_del,
> in both orders, for e1000 and xen-net-device) still cleans up exactly
> once, and that the reproducer now exits cleanly.
>
> Fixes: e7891c575fb2 ("net: move backend cleanup to NIC cleanup")
> Cc: qemu-stable@nongnu.org
> Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Thanks!
Samuel
> ---
> include/net/net.h | 8 ++++++++
> net/slirp.c | 20 +++++++++++++++++++-
> 2 files changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/include/net/net.h b/include/net/net.h
> index c2fd451f17..6394df827f 100644
> --- a/include/net/net.h
> +++ b/include/net/net.h
> @@ -62,6 +62,14 @@ typedef int (NetLoad)(NetClientState *);
> typedef void (NetStop)(NetClientState *);
> typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
> typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
> +/*
> + * NetCleanup may be invoked more than once on the same client; like a
> + * GObject dispose method it must be idempotent. In particular, a NIC
> + * which has taken ownership of its deleted peer (nic->peer_deleted)
> + * cleans the peer up from both net_cleanup() and qemu_del_nic(), and
> + * for the xen-net-device NIC the latter runs from an exit notifier
> + * after net_cleanup() has already done so.
> + */
> typedef void (NetCleanup) (NetClientState *);
> typedef void (LinkStatusChanged)(NetClientState *);
> typedef void (NetClientDestructor)(NetClientState *);
> diff --git a/net/slirp.c b/net/slirp.c
> index eea12e7981..bf59639120 100644
> --- a/net/slirp.c
> +++ b/net/slirp.c
> @@ -128,7 +128,10 @@ static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t
> {
> SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
>
> - slirp_input(s->slirp, buf, size);
> + /* The client may live on after cleanup, owned by its peer NIC. */
> + if (s->slirp) {
> + slirp_input(s->slirp, buf, size);
> + }
>
> return size;
> }
> @@ -151,12 +154,27 @@ static void net_slirp_cleanup(NetClientState *nc)
> {
> SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
>
> + /*
> + * A NIC which has taken ownership of its deleted peer
> + * (nic->peer_deleted) cleans the peer up both from net_cleanup()
> + * and from qemu_del_nic(); for the xen-net-device NIC the latter
> + * happens in an exit notifier after net_cleanup() has already run.
> + * Like a GObject dispose method, cleanup must therefore cope with
> + * being invoked more than once.
> + */
> + if (!s->slirp) {
> + return;
> + }
> +
> g_slist_free_full(s->fwd, slirp_free_fwd);
> + s->fwd = NULL;
> main_loop_poll_remove_notifier(&s->poll_notifier);
> unregister_savevm(NULL, "slirp", s->slirp);
> slirp_cleanup(s->slirp);
> + s->slirp = NULL;
> if (s->exit_notifier.notify) {
> qemu_remove_exit_notifier(&s->exit_notifier);
> + s->exit_notifier.notify = NULL;
> }
> slirp_smb_cleanup(s);
> QTAILQ_REMOVE(&slirp_stacks, s, entry);
> --
> 2.43.0
>
--
Samuel
<c> xlnt comme sujet de stage je peux essayer de donner une description formelle de automake
-+- #ens-mim -+-
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/5] net: Make vde cleanup idempotent
2026-12-04 10:43 [PATCH 0/5] net: make client cleanup callbacks idempotent David Woodhouse
2026-12-04 10:43 ` [PATCH 1/5] net: Make slirp cleanup idempotent David Woodhouse
@ 2026-12-04 10:43 ` David Woodhouse
2026-12-04 10:43 ` [PATCH 3/5] net: Make l2tpv3 " David Woodhouse
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: David Woodhouse @ 2026-12-04 10:43 UTC (permalink / raw)
To: qemu-devel
Cc: Jason Wang, Michael S . Tsirkin, Stefano Garzarella,
Samuel Thibault, qemu-stable, David Woodhouse
From: David Woodhouse <dwmw@amazon.co.uk>
The vde backend has no check_peer_type, so like slirp it can peer the
xen-net-device NIC and have its cleanup invoked twice. Don't close the
VDECONN twice.
Fixes: e7891c575fb2 ("net: move backend cleanup to NIC cleanup")
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
net/vde.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/net/vde.c b/net/vde.c
index c0a08662cc..f9aca82ef0 100644
--- a/net/vde.c
+++ b/net/vde.c
@@ -63,8 +63,14 @@ static ssize_t vde_receive(NetClientState *nc, const uint8_t *buf, size_t size)
static void vde_cleanup(NetClientState *nc)
{
VDEState *s = DO_UPCAST(VDEState, nc, nc);
+
+ /* Cleanup may be invoked more than once; see NetCleanup. */
+ if (!s->vde) {
+ return;
+ }
qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
vde_close(s->vde);
+ s->vde = NULL;
}
static NetClientInfo net_vde_info = {
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 3/5] net: Make l2tpv3 cleanup idempotent
2026-12-04 10:43 [PATCH 0/5] net: make client cleanup callbacks idempotent David Woodhouse
2026-12-04 10:43 ` [PATCH 1/5] net: Make slirp cleanup idempotent David Woodhouse
2026-12-04 10:43 ` [PATCH 2/5] net: Make vde " David Woodhouse
@ 2026-12-04 10:43 ` David Woodhouse
2026-12-04 10:43 ` [PATCH 4/5] net: Make passt " David Woodhouse
2026-12-04 10:43 ` [PATCH 5/5] net: Make vhost-vdpa " David Woodhouse
4 siblings, 0 replies; 7+ messages in thread
From: David Woodhouse @ 2026-12-04 10:43 UTC (permalink / raw)
To: qemu-devel
Cc: Jason Wang, Michael S . Tsirkin, Stefano Garzarella,
Samuel Thibault, qemu-stable, David Woodhouse
From: David Woodhouse <dwmw@amazon.co.uk>
As for vde: no check_peer_type, so reachable the same way. Reset s->fd
after closing it, and clear the buffer pointers after freeing them.
Fixes: e7891c575fb2 ("net: move backend cleanup to NIC cleanup")
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
net/l2tpv3.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/net/l2tpv3.c b/net/l2tpv3.c
index 68c9555268..1b5866a385 100644
--- a/net/l2tpv3.c
+++ b/net/l2tpv3.c
@@ -502,11 +502,19 @@ static void net_l2tpv3_cleanup(NetClientState *nc)
l2tpv3_write_poll(s, false);
if (s->fd >= 0) {
close(s->fd);
+ s->fd = -1;
+ }
+ /* Cleanup may be invoked more than once; see NetCleanup. */
+ if (s->msgvec) {
+ destroy_vector(s->msgvec, MAX_L2TPV3_MSGCNT, IOVSIZE);
+ s->msgvec = NULL;
}
- destroy_vector(s->msgvec, MAX_L2TPV3_MSGCNT, IOVSIZE);
g_free(s->vec);
+ s->vec = NULL;
g_free(s->header_buf);
+ s->header_buf = NULL;
g_free(s->dgram_dst);
+ s->dgram_dst = NULL;
}
static NetClientInfo net_l2tpv3_info = {
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 4/5] net: Make passt cleanup idempotent
2026-12-04 10:43 [PATCH 0/5] net: make client cleanup callbacks idempotent David Woodhouse
` (2 preceding siblings ...)
2026-12-04 10:43 ` [PATCH 3/5] net: Make l2tpv3 " David Woodhouse
@ 2026-12-04 10:43 ` David Woodhouse
2026-12-04 10:43 ` [PATCH 5/5] net: Make vhost-vdpa " David Woodhouse
4 siblings, 0 replies; 7+ messages in thread
From: David Woodhouse @ 2026-12-04 10:43 UTC (permalink / raw)
To: qemu-devel
Cc: Jason Wang, Michael S . Tsirkin, Stefano Garzarella,
Samuel Thibault, qemu-stable, David Woodhouse
From: David Woodhouse <dwmw@amazon.co.uk>
Although the passt backend's check_peer_type restricts it to
virtio-net NICs, which are deleted before net_cleanup() runs, the
NetCleanup contract now requires cleanup callbacks to cope with being
invoked more than once. Bring passt into compliance rather than
relying on reachability arguments: don't signal the daemon, remove
and free the pidfile, or free the argument array twice.
Fixes: e7891c575fb2 ("net: move backend cleanup to NIC cleanup")
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
net/passt.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/net/passt.c b/net/passt.c
index e65198960e..ac56d6a64a 100644
--- a/net/passt.c
+++ b/net/passt.c
@@ -101,13 +101,21 @@ static void net_passt_cleanup(NetClientState *nc)
if (s->pid > 0) {
kill(s->pid, SIGTERM);
+ s->pid = -1;
}
- if (g_remove(s->pidfile) != 0) {
- warn_report("Failed to remove passt pidfile %s: %s",
- s->pidfile, strerror(errno));
+ /* Cleanup may be invoked more than once; see NetCleanup. */
+ if (s->pidfile) {
+ if (g_remove(s->pidfile) != 0) {
+ warn_report("Failed to remove passt pidfile %s: %s",
+ s->pidfile, strerror(errno));
+ }
+ g_free(s->pidfile);
+ s->pidfile = NULL;
+ }
+ if (s->args) {
+ g_ptr_array_free(s->args, TRUE);
+ s->args = NULL;
}
- g_free(s->pidfile);
- g_ptr_array_free(s->args, TRUE);
}
static ssize_t net_passt_receive(NetClientState *nc, const uint8_t *buf,
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 5/5] net: Make vhost-vdpa cleanup idempotent
2026-12-04 10:43 [PATCH 0/5] net: make client cleanup callbacks idempotent David Woodhouse
` (3 preceding siblings ...)
2026-12-04 10:43 ` [PATCH 4/5] net: Make passt " David Woodhouse
@ 2026-12-04 10:43 ` David Woodhouse
4 siblings, 0 replies; 7+ messages in thread
From: David Woodhouse @ 2026-12-04 10:43 UTC (permalink / raw)
To: qemu-devel
Cc: Jason Wang, Michael S . Tsirkin, Stefano Garzarella,
Samuel Thibault, qemu-stable, David Woodhouse
From: David Woodhouse <dwmw@amazon.co.uk>
The vhost-vdpa cleanup is not idempotent: a second invocation closes
the device fd again and double-frees the shared state. It cannot
currently be reached twice, because its check_peer_type restricts it to
virtio-net NICs, which are deleted before net_cleanup() runs — but as
with passt in the previous patch, bring it into compliance with the
NetCleanup contract rather than relying on reachability arguments.
Clear the CVQ mappings, and skip the shared-state teardown when it has
already been freed.
Fixes: e7891c575fb2 ("net: move backend cleanup to NIC cleanup")
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
net/vhost-vdpa.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
index 1052361a4a..3b4ba4811c 100644
--- a/net/vhost-vdpa.c
+++ b/net/vhost-vdpa.c
@@ -231,19 +231,25 @@ static void vhost_vdpa_cleanup(NetClientState *nc)
{
VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
- munmap(s->cvq_cmd_out_buffer, vhost_vdpa_net_cvq_cmd_page_len());
- munmap(s->status, vhost_vdpa_net_cvq_cmd_page_len());
+ if (s->cvq_cmd_out_buffer) {
+ munmap(s->cvq_cmd_out_buffer, vhost_vdpa_net_cvq_cmd_page_len());
+ s->cvq_cmd_out_buffer = NULL;
+ }
+ if (s->status) {
+ munmap(s->status, vhost_vdpa_net_cvq_cmd_page_len());
+ s->status = NULL;
+ }
if (s->vhost_net) {
vhost_net_cleanup(s->vhost_net);
g_free(s->vhost_net);
s->vhost_net = NULL;
}
- if (s->vhost_vdpa.index != 0) {
+ if (s->vhost_vdpa.index != 0 || !s->vhost_vdpa.shared) {
return;
}
qemu_close(s->vhost_vdpa.shared->device_fd);
g_clear_pointer(&s->vhost_vdpa.shared->iova_tree, vhost_iova_tree_delete);
- g_free(s->vhost_vdpa.shared);
+ g_clear_pointer(&s->vhost_vdpa.shared, g_free);
}
static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread