Re: [PATCH net-next v8 4/7] vsock: add a netlink command to assign the g2h device to a netns

From: Bobby Eshleman

Date: Fri Dec 04 2026 - 10:55:47 EST


On Wed, Dec 02, 2026 at 07:07:13PM +0000, netdev-bot+sashiko@xxxxxxxxxx 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@xxxxxxxx/

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