From: Samuel Thibault <samuel.thibault@gnu.org>
To: David Woodhouse <dwmw2@infradead.org>
Cc: qemu-devel@nongnu.org, Jason Wang <jasowangio@gmail.com>,
"Michael S . Tsirkin" <mst@redhat.com>,
Stefano Garzarella <sgarzare@redhat.com>,
qemu-stable@nongnu.org, David Woodhouse <dwmw@amazon.co.uk>
Subject: Re: [PATCH 1/5] net: Make slirp cleanup idempotent
Date: Sat, 5 Dec 2026 01:40:30 +0100 [thread overview]
Message-ID: <axNdetY9jfJzEoBj@end> (raw)
In-Reply-To: <20261204104345.3422972-2-dwmw2@infradead.org>
[-- 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 --]
next prev parent reply other threads:[~2026-12-05 0:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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-05 0:40 ` Samuel Thibault [this message]
2026-12-04 10:43 ` [PATCH 2/5] net: Make vde " David Woodhouse
2026-12-04 10:43 ` [PATCH 3/5] net: Make l2tpv3 " 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axNdetY9jfJzEoBj@end \
--to=samuel.thibault@gnu.org \
--cc=dwmw2@infradead.org \
--cc=dwmw@amazon.co.uk \
--cc=jasowangio@gmail.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-stable@nongnu.org \
--cc=sgarzare@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.