From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796238435; b=emXs2JRaBv4Yz0f+Ktib7dGe6msBnpjBuTrj4dJIrXYWAtM7NsrNFe9RwqvEUvTmRHbL dV9aJOSDb4bo4QwMxCRPcoHn3Wzjsi+Aj2ATyM+Fv2owQ9AUzzizStJpqRSbmARp24Exo y6ozKLSxMHkcc8BzVRj6fq0+iNUW62nA8bh3T1UL/YaAj4ncAFnGycWE8fSQACFXC8/eI 1jxRBm/CqVS7PO/xWiC/OAGW7gVzL1gdL2g06Qr/z9MNC+Mfo/ZR1AwD3fVRg+vIW0PpG xt1tHEDn30Gdz2uoBDi3UvHVjup1HZTm4gba2iYZE2qmQedj0UvGnKdsUS29m8KvSng== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796238435; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=Ksti2wlaHDXUZeh6mkBgrhfa1QndRCmzFT27FUwNJ8Q=; b=0fs+f+NyRvBa4EaHibMQS7PpQG7buy+LL3dWToQ4DQwCTszt8kHpatH+a8apOsNgIykY PwQ6y0GYqbM6O6bCoMvqYMUTK6JMFa6YYncE1s6g2vgqVvOjFNNvoV675KoWn4ezcl45L CU27axxjOoYAYRLSnq3ywejE6m8XzP7MR8iUdeUopHBMO9WzFmRLdnak/lLkm/UthqoBj 2m9jzFu6/kLSWppDZahafbxn1yX/SbDfmVz4R/PScmK/iTfetyKUzkJWcXmPWz2GixcXy V5qC/NXdIX91nYWHb9mMsWoaFTGLCWJXm0v8uKX3aBDF2p1RA6jYXqjHMCPoSXydwJQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=F85n06t3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64B0B606A322; Wed, 2 Dec 2026 19:07:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=F85n06t3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 890F61F00893; Wed, 2 Dec 2026 19:07:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796238435; bh=Ksti2wlaHDXUZeh6mkBgrhfa1QndRCmzFT27FUwNJ8Q=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=F85n06t36nVvOY7caqK6ff6t73fovnxoASoVdnSudqFCgY5Plr3oWJ3DsyTp5E6jy PRuiPYoaoiF5xxBKEEHG2Ppi4gwTYMpsK5mlAFqko8FzEMMPdqFhl5acRL7Tv1nVo9 01taD5Yq7zbICs5WKBswfEIRgkog6BySTy5aGjO/1XjcLkez+H38BBytHbVOWyx+Kv L9PNdL8sIsmFfrNOcmFhvb0WEaqBXcbUnIxww9lxnGVafftW31CBmd7O5Q6mwuiHdd kKWk2jAYd1SGZ5jO39IunaJ9Gv3rYLYfXPbBGVgCMwzRElID0gwRznwgUfgvTXkYg3 7HxA+Shef0Ayw== Subject: Re: [PATCH net-next v8 4/7] vsock: add a netlink command to assign the g2h device to a netns 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 Date: Wed, 02 Dec 2026 19:07:13 +0000 Message-ID: <179623843306.3146.12501387936973395451@kernel.org> In-Reply-To: <20261130-vsock-guest-ns-v8-4-5fff852d325b@meta.com> References: <20261130-vsock-guest-ns-v8-4-5fff852d325b@meta.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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