All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bobby Eshleman <bobbyeshleman@meta.com>
To: <netdev-bot+sashiko@kernel.org>
Cc: <sgarzare@redhat.com>, <davem@davemloft.net>, <kuba@kernel.org>,
	<pabeni@redhat.com>, <horms@kernel.org>, <corbet@lwn.net>,
	<skhan@linuxfoundation.org>, <stefanha@redhat.com>,
	<mst@redhat.com>, <jasowangio@gmail.com>,
	<xuanzhuo@linux.alibaba.com>, <eperezma@redhat.com>,
	<shuah@kernel.org>, <rdunlap@infradead.org>,
	<donald.hunter@gmail.com>, <edumazet@kernel.org>,
	<georgezhang@vmware.com>, <acking@vmware.com>, <dtor@vmware.com>,
	<willemb@google.com>, <virtualization@lists.linux.dev>,
	<netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-doc@vger.kernel.org>, <kvm@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <sargun@sargun.me>,
	<jlinbox@meta.com>
Subject: Re: [PATCH net-next v8 4/7] vsock: add a netlink command to assign the g2h device to a netns
Date: Fri, 4 Dec 2026 07:53:51 -0800	[thread overview]
Message-ID: <axLiD6nPQDYwpjNS@devvm29614.prn0.facebook.com> (raw)
In-Reply-To: <179623843306.3146.12501387936973395451@kernel.org>

On Wed, Dec 02, 2026 at 07:07:13PM +0000, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issue(s) to consider.
> 
> Critical: 0 · High: 0 · Medium: 2 · Low: 2
> 
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
> 
> - [Medium] vsock: unsafe lockless TCP_CLOSE reset in
>   vsock_g2h_reset_unreachable_sk()
> - [Medium] vsock: vsock_g2h_reset_unreachable_sk() TCP_CLOSE races with
>   vsock_connect()
> - [Low] In the main use case, the VSOCK_CMD_DEV_NETNS_SET reply does not
>   say where the device went.
> - [Low] Documentation/admin-guide/sysctl/net.rst says that assigning the
>   device to another namespace "is the only way to undo an assignment".
> 
> --
> 
> vsock: add a netlink command to assign the g2h device to a netns
> 
> This commit adds a vsock generic netlink family. VSOCK_CMD_DEV_NETNS_SET
> moves the guest's G2H vsock device into a network namespace, and
> VSOCK_CMD_DEV_NETNS_GET reads the assignment back as an nsid relative to
> the caller. Connected sockets that can no longer reach the device after
> a move are reset with ECONNRESET, and assignments that would cause a CID
> collision are refused.
> 
> Link: https://lore.kernel.org/all/20200427142518.uwssa6dtasrp3bfc@steredhat/
> 
> > diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst
> > index fe43e85959583..0d219d80e6741 100644
> > --- a/Documentation/admin-guide/sysctl/net.rst
> > +++ b/Documentation/admin-guide/sysctl/net.rst
> > @@ -529,6 +529,46 @@ their hosts. The behavior of VSOCK sockets in a network namespace is determined
> 
> [ ... ]
> 
> > +An assignment keeps the namespace alive, the same way an open socket does, so
> > +deleting that namespace does not move the device. Assigning the device to
> > +another namespace, such as init_net, is the only way to undo an assignment, and
> > +releases the reference the assignment held on the previous namespace.
> 
> [Severity: Low]
> Is "the only way to undo an assignment" accurate? vsock_core_unregister()
> also moves the device back to init_net when the g2h transport
> unregisters, for example when rmmod of the virtio transport runs
> virtio_vsock_exit():

Not worth a respin alone IMHO, but I can tighten the language there if
we need a new rev.

> 
> net/vmw_vsock/af_vsock.c:vsock_core_unregister() {
>     ...
> 	if (transport_g2h == t) {
> 		transport_g2h = NULL;
> 		old = vsock_g2h_net_swap(&init_net, &old_tracker);
> 	}
>     ...
> }
> 
> When the transport registers again, the device is in init_net, so every
> global mode namespace can reach it again. Userspace is not told about
> this, because the family has no multicast group.
> 
> The comment in af_vsock.c covers this path ("on another assignment or when
> the transport unregisters"), but the admin guide does not. Could the admin
> guide also mention the unregister path? This text is unchanged at the end
> of the series.
> 
> [ ... ]
> 
> > diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> > index 649bdf48d03bd..a20446f2c6fbb 100644
> > --- a/net/vmw_vsock/af_vsock.c
> > +++ b/net/vmw_vsock/af_vsock.c
> 
> [ ... ]
> 
> > +static int vsock_nl_dev_netns_fill(struct sk_buff *msg, struct genl_info *info,
> > +				   struct net *g2h)
> > +{
> > +	struct net *net = genl_info_net(info);
> > +	bool equal;
> > +	void *hdr;
> > +	s32 id;
> > +
> > +	lockdep_assert_held(&vsock_register_mutex);
> > +
> > +	equal = net_eq(net, g2h);
> > +	if (!equal) {
> > +		if (vsock_net_assignable(transport_g2h) &&
> > +		    !vsock_net_check_mode(net, g2h))
> > +			id = NETNSA_NSID_NOT_ASSIGNED;
> > +		else
> > +			id = peernet2id_alloc(net, g2h, GFP_KERNEL);
> > +	}
> 
> [Severity: Low]
> Does this give a useful VSOCK_CMD_DEV_NETNS_SET reply when the target is
> a local mode namespace?
> 
> vsock_g2h_net_assign() builds the SET reply with the target namespace
> passed as g2h. Here, net is genl_info_net(info), the caller's namespace.
> vsock_net_check_mode() returns false whenever the two namespaces differ
> and either one is in local mode.
> 
> Consider an admin in init_net, or in any global namespace, who moves the
> device into a local mode namespace with netns-fd or netns-pid:
> 
> vsock_nl_dev_netns_set_doit()
>   vsock_g2h_net_assign()
>     vsock_nl_dev_netns_fill(msg, info, net)
>       net_eq(init_net, target)                  -> false
>       vsock_net_assignable(transport_g2h)       -> true
>       vsock_net_check_mode(init_net, target)    -> false
>       id = NETNSA_NSID_NOT_ASSIGNED
> 
> vsock_net_assignable() returns true for virtio once the later commit
> "vsock/virtio: support guest device network namespace" sets
> .netns_assign_allow = true.
> 
> The admin always gets NETNSA_NSID_NOT_ASSIGNED back in this case. That
> is the main isolation use case for the feature. SET is GENL_ADMIN_PERM,
> and the caller picked the target itself, so hiding the target from this
> caller does not seem to protect anything.
> 
> The commit message says:
> 
>     VSOCK_CMD_DEV_NETNS_SET
>     replies the same way for the namespace the device was moved to.
> 
> That reads as if the reply identifies the new namespace, but it does not
> in this case.
> 
> The same rule also means a GET from init_net can never report which
> local namespace holds the device.
> 
> This reply format becomes uAPI once released. Could the SET reply skip
> the mode check, so that it identifies the namespace the device was moved
> to? The code is unchanged at the end of the series.
> 
> > +
> > +	hdr = genlmsg_iput(msg, info);
> > +	if (!hdr)
> > +		return -EMSGSIZE;

Yes, this is a good catch. Will change next rev.

> 
> [ ... ]
> 
> > +static void vsock_g2h_reset_unreachable_sk(struct sock *sk)
> > +{
> > +	if (sk->sk_state == TCP_CLOSE || vsock_g2h_reachable_sk(vsock_sk(sk)))
> > +		return;
> > +
> > +	sk->sk_state = TCP_CLOSE;
> > +	sk->sk_err = ECONNRESET;
> > +	sk_error_report(sk);
> > +}
> 
> [ ... ]
> 
> > +	vsock_for_each_connected_socket(transport_g2h,
> > +					vsock_g2h_reset_unreachable_sk);
> 
> [ ... ]
> 
> [Severity: Medium]
> Can this sweep turn a connect() that just succeeded into a failed one?
> 
> vsock_connect() drops lock_sock() and sleeps in schedule_timeout().
> When the OP_RESPONSE arrives, virtio_transport_recv_connecting() calls
> vsock_maybe_set_connected(). That sets TCP_ESTABLISHED and links vsk
> into vsock_connected_table with a reference held, and sock->state
> becomes SS_CONNECTED.
> 
> If the assignment runs before the woken vsock_connect() looks at
> sk_state, vsock_g2h_reset_unreachable_sk() stores TCP_CLOSE and
> ECONNRESET. Since the sweep does not take lock_sock(), this can also
> happen after vsock_connect() has taken the lock back.
> 
> vsock_connect() only treats TCP_ESTABLISHED and TCP_CLOSING as
> connected. It therefore falls into the failure path with a socket that
> is still linked in vsock_connected_table.
> 
> On a signal or timeout it calls vsock_transport_cancel_pkt() on what
> are now data skbs of an established connection. That is the
> bytes_unsent leak the v8 changelog removed cancel_pkt() from the sweep
> to avoid. Otherwise it consumes sk_err with sock_error() and sets
> sock->state back to SS_UNCONNECTED.
> 
> A second connect() on that socket is then accepted:
> 
> - To a CID served by another transport, vsock_assign_transport()
>   calls virtio_transport_release(), which does not linger because the
>   socket is TCP_CLOSE. vsock_deassign_transport() ->
>   virtio_transport_destruct() then frees vsk->trans. TX skbs queued
>   before the sweep are still in flight, so
>   virtio_transport_consume_skb_sent() then dereferences freed memory.
> 
> - Through transport_g2h, once the device is reachable from the
>   socket's namespace again, the transport is unchanged, so release()
>   is skipped. The next vsock_maybe_set_connected() then calls
>   __vsock_insert_connected() on a node that is already on the list.
>   That corrupts vsock_connected_table and leaks a socket reference.
> 
> v6 moved swept sockets to TCP_CLOSING because vsock_connect() handles
> that state as established. v8 goes back to TCP_CLOSE to mirror an
> unplug. Should the sweep keep a state that vsock_connect() recognises?
> Or should vsock_connect() check for SS_CONNECTED before treating
> sk_err as a failed connect?

This goes away with this patch:
https://lore.kernel.org/all/20261130-vsock-connect-close-v3-1-7a9abc4e3474@meta.com/

The vsock_transport_cancel_pkt() on signal/timeout is pre-existing,
since TRANSPORT_RESET can land in the same window after another thread
has already queued data.

The bytes_unsent leak comes from virtio_transport_purge_skbs(), which
does not account for bytes_unsent (I think it was originally for non-RW
pkts). I'd rather fix that there (adjust bytes_unsent if pkt type is
OP_RW) than work around the issue in this series.

It doesn't really affect users either. bytes_unsent is only read by
SIOCOUTQ and by SO_LINGER on close(). The socket is TCP_CLOSE after the
failed connect(), so close() doesn't linger, and with the connect() fix
above it can't be connected again and carry the stale count into a new
connection. What's left is SIOCOUTQ reporting stale bytes on a socket
whose connect() just failed.

> 
> [Severity: Medium]
> Is it safe to update sk_state and sk_err here without lock_sock()?
> 
> The changelog cites virtio_vsock_reset_sock() as prior art, but there
> the device is being removed and nothing more will be delivered for the
> socket. Here the device stays live across vsock_g2h_net_assign(), so
> virtio_transport_recv_pkt() can be working on the same socket under
> lock_sock(sk) at the same time. If it looked the socket up before
> vsock_g2h_net_swap(), virtio_transport_recv_connected() ->
> virtio_transport_do_close() can store TCP_CLOSING after the sweep
> stored TCP_CLOSE. The socket then ends up in a state the sweep did not
> intend.
> 
> The reset also leaves sk_shutdown and vsk->peer_shutdown untouched, so
> sk_err is the only thing a sleeper can notice. In recvmsg(),
> vsock_connectible_wait_data() breaks out on sk_err, RCV_SHUTDOWN or
> the peer's SEND_SHUTDOWN, but never looks at sk_state.
> 
> Another thread can consume sk_err with sock_error() first, for example
> through getsockopt(SO_ERROR) or a woken vsock_connect(). The reader
> then finds sk_err == 0 and peer_shutdown == 0, and goes back to sleep
> on a TCP_CLOSE socket. Nothing else will wake it, so it blocks until
> its receive timeout expires, or forever if none is set.
> 
> The same unlocked TCP_CLOSE store also lets a sleeping vsock_connect()
> treat an established socket as a failed connect. See the comment on
> the sweep in vsock_g2h_net_assign().
> 
> Could the reset be done under lock_sock(), for example from a work
> item? Or could the sweep mark the socket shut down, so that waiters
> see a terminal condition that another thread cannot consume first?

First, these aren't real bugs that impact users. If a peer RST races the
sweep, the socket ends up TCP_CLOSING instead of TCP_CLOSE, still w/
ECONNRESET. poll() and close() treat those the same, so the only
difference is recv() returning 0 instead of -ENOTCONN, which is also
what you'd see if the RST had come in just before the netns move. I
don't really see this as a problem.

Second, these symptoms all show up on TRANSPORT_RESET and device unplug
too, where contrary to what Sashiko writes above, the reset does happen
on live devices with live sockets (see how qemu migration works...
TRANSPORT_RESET + live sockets + live device).

Even for virtio_vsock_vqs_del() we reset the sockets before truly
shutting down the device by stopping rx_work.

lock_sock() doesn't help with the sock_error() issue...
getsockopt(SO_ERROR) consumes sk_err without the lock.

It's also pre-existing. Connecting to a port with no listener gets an
RST back, which only sets sk_err. If another thread reads SO_ERROR
first, the thread in connect() goes back to sleep until connect_timeout.
That happens today w/o this series, and any transport reset can do the
same to a reader in recv().

Will respin for at least the API change though.

pw-bot: cr

Best,
Bobby

  reply	other threads:[~2026-12-04 15:54 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  1:06 [PATCH net-next v8 0/7] vsock: assign the guest vsock device to a network namespace Bobby Eshleman
2026-12-01  1:06 ` [PATCH net-next v8 1/7] vsock: constify the transport in vsock_for_each_connected_socket() Bobby Eshleman
2026-12-01  1:06 ` [PATCH net-next v8 2/7] vsock: rename the vsock pernet operations Bobby Eshleman
2026-12-01  1:06 ` [PATCH net-next v8 3/7] vsock: pass the netns to ->has_remote_cid() Bobby Eshleman
2026-12-01  1:06 ` [PATCH net-next v8 4/7] vsock: add a netlink command to assign the g2h device to a netns Bobby Eshleman
2026-12-01 16:19   ` Michal Luczaj
2026-12-01 21:05     ` Bobby Eshleman
2026-12-02 19:26       ` Michal Luczaj
2026-12-02  1:08   ` sashiko-bot
2026-12-02 19:07   ` netdev-bot+sashiko
2026-12-04 15:53     ` Bobby Eshleman [this message]
2026-12-01  1:06 ` [PATCH net-next v8 5/7] vsock/virtio: support guest device network namespace Bobby Eshleman
2026-12-02  1:08   ` sashiko-bot
2026-12-02 19:07   ` netdev-bot+sashiko
2026-12-04 19:06   ` Michal Luczaj
2026-12-01  1:06 ` [PATCH net-next v8 6/7] selftests/vsock: test the guest vsock " Bobby Eshleman
2026-12-02 19:07   ` netdev-bot+sashiko
2026-12-01  1:06 ` [PATCH net-next v8 7/7] selftests/vsock: test the netns assign privilege checks Bobby Eshleman
2026-12-02 19:07   ` netdev-bot+sashiko
2026-12-02  1:08 ` [PATCH net-next v8 0/7] vsock: assign the guest vsock device to a network namespace sashiko-bot
2026-12-02 18:15 ` Stefano Garzarella
2026-12-03  2:06   ` Bobby Eshleman

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=axLiD6nPQDYwpjNS@devvm29614.prn0.facebook.com \
    --to=bobbyeshleman@meta.com \
    --cc=acking@vmware.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=dtor@vmware.com \
    --cc=edumazet@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=georgezhang@vmware.com \
    --cc=horms@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=jlinbox@meta.com \
    --cc=kuba@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev-bot+sashiko@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=sargun@sargun.me \
    --cc=sgarzare@redhat.com \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=willemb@google.com \
    --cc=xuanzhuo@linux.alibaba.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.