Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: bobbyeshleman@meta.com
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, bobbyeshleman@gmail.com
Subject: Re: [PATCH net-next v8 4/7] vsock: add a netlink command to assign the g2h device to a netns
Date: Wed, 02 Dec 2026 19:07:13 +0000	[thread overview]
Message-ID: <179623843306.3146.12501387936973395451@kernel.org> (raw)
In-Reply-To: <20261130-vsock-guest-ns-v8-4-5fff852d325b@meta.com>

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():

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;

[ ... ]

> +	ret = vsock_nl_dev_netns_fill(msg, info, net);
> +	if (ret)
> +		goto unlock;
> +
> +	ret = genlmsg_reply(msg, info);

[ ... ]

> @@ -742,8 +837,236 @@ bool vsock_find_cid(unsigned int cid)
>  
>  	return false;
>  }
> +EXPORT_SYMBOL_GPL(__vsock_find_cid);
> +
> +bool vsock_find_cid(struct net *net, unsigned int cid)
> +{
> +	bool found;
> +
> +	mutex_lock(&vsock_register_mutex);
> +	found = __vsock_find_cid(net, cid);
> +	mutex_unlock(&vsock_register_mutex);
> +
> +	return found;
> +}
>  EXPORT_SYMBOL_GPL(vsock_find_cid);
>  
> +/* Return the namespace the g2h device is assigned to, with a reference held.
> + *
> + * The assignment itself holds a reference, so the namespace cannot be going
> + * away here and this never fails.
> + */
> +struct net *vsock_g2h_net_get(netns_tracker *tracker)
> +{
> +	struct net *net;
> +
> +	rcu_read_lock();
> +	net = rcu_dereference(vsock_g2h_net);
> +	get_net_track(net, tracker, GFP_ATOMIC);
> +	rcu_read_unlock();
> +
> +	return net;
> +}
> +EXPORT_SYMBOL_GPL(vsock_g2h_net_get);
> +
> +/* True if @net is still the namespace the device is assigned to. Cheap enough
> + * to call per packet on the RX path.
> + */
> +bool vsock_g2h_net_eq(const struct net *net)
> +{
> +	return rcu_access_pointer(vsock_g2h_net) == net;
> +}
> +EXPORT_SYMBOL_GPL(vsock_g2h_net_eq);
> +
> +bool vsock_g2h_net_reachable(struct net *net)
> +{
> +	bool reachable;
> +
> +	rcu_read_lock();
> +	reachable = vsock_net_check_mode(net, rcu_dereference(vsock_g2h_net));
> +	rcu_read_unlock();
> +
> +	return reachable;
> +}
> +EXPORT_SYMBOL_GPL(vsock_g2h_net_reachable);
> +
> +static bool vsock_g2h_reachable_sk(struct vsock_sock *vsk)
> +{
> +	if (!vsock_net_assignable(vsk->transport))
> +		return true;
> +
> +	return vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));
> +}
> +
> +/* Move @vsk to TCP_ESTABLISHED and into the connected table, unless the device
> + * has moved to a namespace @vsk cannot reach. Returns false without doing
> + * either in that case.
> + *
> + * The reset sweep walks the same table under the same lock, so an assign
> + * cannot land between the check and the insert: either the sweep finds @vsk
> + * and resets it, or @vsk is never added.
> + */
> +bool vsock_maybe_set_connected(struct vsock_sock *vsk)
> +{
> +	struct list_head *list;
> +	bool reachable;
> +
> +	list = vsock_connected_sockets(&vsk->remote_addr, &vsk->local_addr);
> +
> +	spin_lock_bh(&vsock_table_lock);
> +	reachable = vsock_g2h_reachable_sk(vsk);
> +	if (reachable) {
> +		sk_vsock(vsk)->sk_state = TCP_ESTABLISHED;
> +		__vsock_insert_connected(list, vsk);
> +	}
> +	spin_unlock_bh(&vsock_table_lock);
> +
> +	return reachable;
> +}
> +EXPORT_SYMBOL_GPL(vsock_maybe_set_connected);
> +
> +/* Reset @sk if it can no longer reach the g2h device, as an unplug of the
> + * device does (see virtio_vsock_reset_sock()).
> + */
> +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);
> +}
> +
> +/* Point the g2h device at @net, taking the reference that keeps @net alive,
> + * and return the namespace it leaves behind.
> + */
> +static struct net *vsock_g2h_net_swap(struct net *net, netns_tracker *old)
> +{
> +	lockdep_assert_held(&vsock_register_mutex);
> +
> +	*old = vsock_g2h_net_tracker;
> +	get_net_track(net, &vsock_g2h_net_tracker, GFP_KERNEL);
> +
> +	return rcu_replace_pointer(vsock_g2h_net, net,
> +				   lockdep_is_held(&vsock_register_mutex));
> +}
> +
> +/* Drop the namespace the device just left, once the readers that may still
> + * see it in vsock_g2h_net_get() are gone. Runs without the mutex held.
> + */
> +static void vsock_g2h_net_release(struct net *old, netns_tracker *tracker)
> +{
> +	synchronize_rcu();
> +	put_net_track(old, tracker);
> +}
> +
> +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);
> +	}
> +
> +	hdr = genlmsg_iput(msg, info);
> +	if (!hdr)
> +		return -EMSGSIZE;
> +
> +	if (!equal && nla_put_s32(msg, VSOCK_A_NETNS_ID, id)) {
> +		genlmsg_cancel(msg, hdr);
> +		return -EMSGSIZE;
> +	}
> +
> +	genlmsg_end(msg, hdr);
> +
> +	return 0;
> +}
> +
> +/* Move the g2h device to @net, which keeps @net alive until the device leaves
> + * again. Returns -ENODEV if no g2h transport is loaded, -EOPNOTSUPP if the
> + * loaded one cannot be moved, and -EADDRINUSE if a guest in @net already uses
> + * the device's CID.
> + */
> +static int vsock_g2h_net_assign(struct net *net, struct sk_buff *msg,
> +				struct genl_info *info)
> +{
> +	struct netlink_ext_ack *extack = info->extack;
> +	netns_tracker old_tracker;
> +	struct net *old;
> +	int ret;
> +
> +	mutex_lock(&vsock_register_mutex);
> +
> +	ret = -ENODEV;
> +	if (!transport_g2h) {
> +		NL_SET_ERR_MSG(extack, "no guest-to-host transport is loaded");
> +		goto unlock;
> +	}
> +
> +	ret = -EOPNOTSUPP;
> +	if (!vsock_net_assignable(transport_g2h)) {
> +		NL_SET_ERR_MSG(extack,
> +			       "the loaded guest-to-host transport does not support namespace assignment");
> +		goto unlock;
> +	}
> +
> +	if (transport_h2g && !transport_h2g->has_remote_cid) {
> +		NL_SET_ERR_MSG(extack,
> +			       "the loaded host-to-guest transport cannot check the device's CID");
> +		goto unlock;
> +	}
> +
> +	ret = -EADDRINUSE;
> +	if (transport_h2g &&
> +	    transport_h2g->has_remote_cid(net,
> +					  transport_g2h->get_local_cid())) {
> +		NL_SET_ERR_MSG(extack,
> +			       "a guest in the target namespace already uses the device's CID");
> +		goto unlock;
> +	}
> +
> +	ret = vsock_nl_dev_netns_fill(msg, info, net);
> +	if (ret)
> +		goto unlock;
> +
> +	ret = genlmsg_reply(msg, info);
> +	msg = NULL;
> +	if (ret)
> +		goto unlock;
> +
> +	old = vsock_g2h_net_swap(net, &old_tracker);
> +
> +	/* See vsock_maybe_set_connected() comment about synchronizing with
> +	 * connecting sockets.
> +	 */
> +	vsock_for_each_connected_socket(transport_g2h,
> +					vsock_g2h_reset_unreachable_sk);
> +
> +	mutex_unlock(&vsock_register_mutex);
> +
> +	vsock_g2h_net_release(old, &old_tracker);
> +
> +	return 0;
> +
> +unlock:
> +	mutex_unlock(&vsock_register_mutex);
> +	nlmsg_free(msg);
> +
> +	return ret;
> +}
> +
>  static struct sock *vsock_dequeue_accept(struct sock *listener)
>  {
>  	struct vsock_sock *vlistener;

[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?

[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?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130-vsock-guest-ns-v8-0-5fff852d325b%40meta.com

  parent reply	other threads:[~2026-12-02 19:07 UTC|newest]

Thread overview: 16+ 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 19:07   ` netdev-bot+sashiko [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 19:07   ` netdev-bot+sashiko
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 18:15 ` [PATCH net-next v8 0/7] vsock: assign the guest vsock device to a network namespace Stefano Garzarella

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=179623843306.3146.12501387936973395451@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=acking@vmware.com \
    --cc=bobbyeshleman@gmail.com \
    --cc=bobbyeshleman@meta.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@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox