AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/10/21 10:16 flow patch-triage
2m Args:
null

Results:
{
  "EnableConfigs": [
    "VSOCKETS",
    "VIRTIO_VSOCKETS",
    "VHOST_VSOCK",
    "NET_NS"
  ],
  "FocusSymbols": [
    "vsock_nl_dev_netns_set_doit",
    "vsock_nl_dev_netns_get_doit",
    "vsock_g2h_net_assign",
    "vsock_g2h_reset_unreachable",
    "vsock_maybe_set_connected",
    "vsock_g2h_net_reachable",
    "vsock_find_cid",
    "vhost_vsock_set_cid",
    "virtio_transport_rx_work"
  ],
  "KMSANReasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET) allowing the guest-to-host vsock device to be assigned to specific network namespaces, along with reachability checks, namespace refcounting, and unreachable socket teardown.\n\nEvaluation of uninitialized memory and info-leak risks:\n1. User-space data transfers:\n   - In `vsock_nl_dev_netns_fill()`, the only netlink attribute emitted is `VSOCK_A_NETNS_ID` via `nla_put_s32(msg, VSOCK_A_NETNS_ID, id)`. The scalar variable `id` is explicitly assigned (`NETNSA_NSID_NOT_ASSIGNED` or `peernet2id_alloc()`) before emission and guarded by `!equal`. If `equal` is true, no attribute is sent.\n   - `vsock_nl_dev_netns_set_doit()` sends an empty Netlink reply containing only the generic netlink header.\n   - In `vsock_dev_do_ioctl()`, scalar `u32 cid` is returned by `vsock_registered_transport_cid()` and written with `put_user(cid, p)`.\n   - No uninitialized struct padding or partially initialized buffers are exposed to user space.\n2. Variables and control flow:\n   - All local variables, flags, and netns_tracker instances (`id`, `reset`, `old_tracker`, `ret`, `cid`) are initialized prior to branching or consumption.\n3. Complex allocations / buffer lengths:\n   - No new variable-length buffers or complex uninitialized data structures are introduced or modified.\n\nKASAN vs KMSAN applicability:\n- The predominant risk surface in this series relates to concurrency, locking hierarchies (`vsock_g2h_assign_mutex`, `vsock_register_mutex`, `vsock_table_lock`, `rx_lock`), netns reference counting, and socket lifecycle / use-after-free during device movement. These are detected by KASAN, LOCKDEP, and refcount tracking.\n- There are no uninitialized memory usage or info-leak risks requiring a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch introduces a Generic Netlink interface (\"vsock\") to query and move the guest-to-host (G2H) vsock device across network namespaces. It introduces new locking (vsock_g2h_assign_mutex, vsock_register_mutex), namespace reference tracking (netns_tracker), connection validation (vsock_maybe_set_connected), and an active sweep that resets connected sockets that lose access to the G2H device (vsock_g2h_reset_unreachable). These modifications touch core AF_VSOCK and virtio/vhost transport logic reachable via unprivileged/privileged netlink messages and socket syscalls in virtualized QEMU environments.",
  "WorthFuzzing": true
}

1/1 2026/10/21 10:16 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 6d3d4d152a343e619168df57f7806299fa392909\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Wed Oct 21 10:16:14 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst\nindex fe43e85959583..eb60903e4eb23 100644\n--- a/Documentation/admin-guide/sysctl/net.rst\n+++ b/Documentation/admin-guide/sysctl/net.rst\n@@ -529,6 +529,48 @@ their hosts. The behavior of VSOCK sockets in a network namespace is determined\n by the namespace's mode (``global`` or ``local``), which controls how CIDs\n (Context IDs) are allocated and how sockets interact across namespaces.\n \n+In a guest, the vsock device owned by the guest-to-host (G2H) transport belongs\n+to one network namespace at a time. The ``VSOCK_CMD_DEV_NETNS_SET`` command of\n+the ``vsock`` netlink family, described in\n+Documentation/netlink/specs/vsock.yaml, moves it to the namespace the request\n+was sent from, which requires ``CAP_NET_ADMIN`` in the initial user namespace.\n+The namespace's mode decides who may then use the device:\n+\n+- ``global`` - every ``global`` mode namespace may use it.\n+- ``local`` - only that namespace may use it, which reserves the connection to\n+  the host for it alone.\n+\n+The device starts out in init_net, so until the command is issued nothing has\n+moved and no mode has changed.\n+\n+Support is transport dependent. A G2H transport that does not implement the\n+move refuses the command with ``EOPNOTSUPP``. Of the in-tree guest transports,\n+only virtio-vsock implements it.\n+\n+A namespace that cannot reach the device sees the vsock addresses it would see\n+if no G2H transport were loaded: the device's CID is not one of its local CIDs,\n+and with ``vsock_loopback`` loaded, ``VMADDR_CID_HOST`` (2) is served by\n+loopback.\n+\n+Connections established before the move, from a namespace that can no longer\n+reach the device, are closed with ``ECONNRESET``. Nothing is sent to the peer,\n+which is what already happens when the device is removed altogether: the peer\n+finds out on its next send, which no longer matches a socket and so is answered\n+with a reset. A peer that only receives waits for its own timeout. A connection\n+still being set up when the device moves is refused with a reset once the peer\n+answers it.\n+\n+An assignment keeps the namespace alive, the same way an open socket does, so\n+deleting that namespace does not move the device. Assigning the device to\n+another namespace, such as init_net, is the only way to undo an assignment, and\n+releases the reference the assignment held on the previous namespace.\n+\n+The move is refused with ``EADDRINUSE`` if a guest registered in the target\n+namespace already answers to the device's CID, since the CID would otherwise\n+name two different peers there. If a loaded H2G transport cannot check that,\n+the move is refused with ``EOPNOTSUPP``. Of the in-tree host transports, only\n+vhost-vsock can.\n+\n ns_mode\n -------\n \ndiff --git a/Documentation/netlink/specs/vsock.yaml b/Documentation/netlink/specs/vsock.yaml\nnew file mode 100644\nindex 0000000000000..74cf2472bf917\n--- /dev/null\n+++ b/Documentation/netlink/specs/vsock.yaml\n@@ -0,0 +1,75 @@\n+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)\n+#\n+# Copyright (c) 2026 Meta Platforms, Inc. and affiliates\n+#\n+---\n+name: vsock\n+\n+doc: |\n+  Control interface for the vsock (AF_VSOCK) core.\n+\n+protocol: genetlink\n+\n+uapi-header: linux/vsock.h\n+\n+attribute-sets:\n+  -\n+    name: vsock\n+    attributes:\n+      -\n+        name: netns-id\n+        type: s32\n+        doc: |\n+          Network namespace the guest's vsock device is assigned to, as an\n+          nsid relative to the caller.\n+\n+          Absent when the device is in the caller's own namespace, and\n+          NETNSA_NSID_NOT_ASSIGNED when the g2h transport supports netns\n+          assignment and the caller's namespace cannot reach the device under\n+          the mode rules, or when no nsid could be allocated for it.\n+\n+operations:\n+  list:\n+    -\n+      name: dev-netns-set\n+      doc: |\n+        Set the network namespace of the guest's vsock device.\n+\n+        The device is moved to the namespace the request was sent from. It\n+        starts out in init_net, and moving it back there is how an\n+        assignment is undone.\n+\n+        A g2h transport that does not support netns assignment returns\n+        EOPNOTSUPP. If the netns assignment would cause a CID collision, it\n+        is rejected with EADDRINUSE.\n+\n+        The namespace's mode decides who may then use the device: a global\n+        mode namespace shares it with every other global mode namespace,\n+        while a local mode namespace reserves it for itself.\n+\n+        Movement to an unreachable local namespace results in sockets being\n+        closed with ECONNRESET. Peers are not guaranteed to be notified via\n+        an RST.\n+\n+        The assignment holds a reference on the namespace.\n+      attribute-set: vsock\n+      flags: [admin-perm]\n+      do:\n+        reply:\n+          attributes:\n+            - netns-id\n+    -\n+      name: dev-netns-get\n+      doc: |\n+        Get the network namespace the guest's vsock device is assigned to.\n+\n+        The namespace is reported as an nsid relative to the caller, so the\n+        attribute is absent when the device is already in the caller's own\n+        namespace. If the g2h transport supports netns assignment, a caller\n+        whose namespace cannot reach the device is told\n+        NETNSA_NSID_NOT_ASSIGNED rather than where the device went.\n+      attribute-set: vsock\n+      do:\n+        reply:\n+          attributes:\n+            - netns-id\ndiff --git a/MAINTAINERS b/MAINTAINERS\nindex 7636ab50d44fb..6bca71c935cd0 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -29169,10 +29169,12 @@ M:\tStefano Garzarella \u003csgarzare@redhat.com\u003e\n L:\tvirtualization@lists.linux.dev\n L:\tnetdev@vger.kernel.org\n S:\tMaintained\n+F:\tDocumentation/netlink/specs/vsock.yaml\n F:\tdrivers/net/vsockmon.c\n F:\tinclude/net/af_vsock.h\n F:\tinclude/uapi/linux/vm_sockets.h\n F:\tinclude/uapi/linux/vm_sockets_diag.h\n+F:\tinclude/uapi/linux/vsock.h\n F:\tinclude/uapi/linux/vsockmon.h\n F:\tnet/vmw_vsock/\n F:\ttools/testing/selftests/vsock/\ndiff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c\nindex abed1fbcf66cc..11893f1943c69 100644\n--- a/drivers/vhost/vsock.c\n+++ b/drivers/vhost/vsock.c\n@@ -91,10 +91,8 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net)\n \treturn NULL;\n }\n \n-static bool vhost_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid)\n+static bool vhost_transport_has_remote_cid(struct net *net, u32 cid)\n {\n-\tstruct sock *sk = sk_vsock(vsk);\n-\tstruct net *net = sock_net(sk);\n \tbool found;\n \n \trcu_read_lock();\n@@ -817,6 +815,7 @@ static int vhost_vsock_dev_release(struct inode *inode, struct file *file)\n static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)\n {\n \tstruct vhost_vsock *other;\n+\tint ret;\n \n \t/* Refuse reserved CIDs */\n \tif (guest_cid \u003c= VMADDR_CID_HOST ||\n@@ -830,15 +829,20 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)\n \t/* Refuse if CID is assigned to the guest-\u003ehost transport (i.e. nested\n \t * VM), to make the loopback work.\n \t */\n-\tif (vsock_find_cid(guest_cid))\n-\t\treturn -EADDRINUSE;\n+\tmutex_lock(\u0026vsock_register_mutex);\n+\n+\tif (__vsock_find_cid(vsock-\u003enet, guest_cid)) {\n+\t\tret = -EADDRINUSE;\n+\t\tgoto unlock;\n+\t}\n \n \t/* Refuse if CID is already in use */\n \tmutex_lock(\u0026vhost_vsock_mutex);\n \tother = vhost_vsock_get(guest_cid, vsock-\u003enet);\n \tif (other \u0026\u0026 other != vsock) {\n \t\tmutex_unlock(\u0026vhost_vsock_mutex);\n-\t\treturn -EADDRINUSE;\n+\t\tret = -EADDRINUSE;\n+\t\tgoto unlock;\n \t}\n \n \tif (vsock-\u003eguest_cid)\n@@ -848,7 +852,12 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)\n \thash_add_rcu(vhost_vsock_hash, \u0026vsock-\u003ehash, vsock-\u003eguest_cid);\n \tmutex_unlock(\u0026vhost_vsock_mutex);\n \n-\treturn 0;\n+\tret = 0;\n+\n+unlock:\n+\tmutex_unlock(\u0026vsock_register_mutex);\n+\n+\treturn ret;\n }\n \n static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)\ndiff --git a/include/net/af_vsock.h b/include/net/af_vsock.h\nindex 5549298c1ec6b..883a98dab36d4 100644\n--- a/include/net/af_vsock.h\n+++ b/include/net/af_vsock.h\n@@ -9,6 +9,7 @@\n #define __AF_VSOCK_H__\n \n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/mutex.h\u003e\n #include \u003clinux/workqueue.h\u003e\n #include \u003cnet/netns/vsock.h\u003e\n #include \u003cnet/sock.h\u003e\n@@ -22,6 +23,7 @@\n extern struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];\n extern struct list_head vsock_connected_table[VSOCK_HASH_SIZE];\n extern spinlock_t vsock_table_lock;\n+extern struct mutex vsock_register_mutex;\n \n #define vsock_sk(__sk)    ((struct vsock_sock *)__sk)\n #define sk_vsock(__vsk)   (\u0026(__vsk)-\u003esk)\n@@ -183,13 +185,16 @@ struct vsock_transport {\n \t * CIDs via the hypervisor (i.e. supports the fallback overlay). VMCI\n \t * does not implement this as it only serves CIDs 0 and 2.\n \t */\n-\tbool (*has_remote_cid)(struct vsock_sock *vsk, u32 remote_cid);\n+\tbool (*has_remote_cid)(struct net *net, u32 remote_cid);\n \n \t/* Read a single skb */\n \tint (*read_skb)(struct vsock_sock *, skb_read_actor_t);\n \n \t/* Zero-copy. */\n \tbool (*msgzerocopy_allow)(void);\n+\n+\t/* True if the G2H transport honours VSOCK_CMD_DEV_NETNS_SET. */\n+\tbool netns_assign_allow;\n };\n \n /**** CORE ****/\n@@ -233,11 +238,17 @@ bool vsock_check_source(const struct vsock_sock *vsk,\n \t\t\tconst struct vsock_transport *transport,\n \t\t\tconst struct sockaddr_vm *src);\n void vsock_remove_sock(struct vsock_sock *vsk);\n-void vsock_for_each_connected_socket(struct vsock_transport *transport,\n+void vsock_for_each_connected_socket(const struct vsock_transport *transport,\n \t\t\t\t     void (*fn)(struct sock *sk));\n int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);\n-bool vsock_find_cid(unsigned int cid);\n+bool vsock_find_cid(struct net *net, unsigned int cid);\n+/* Caller must hold vsock_register_mutex. */\n+bool __vsock_find_cid(struct net *net, unsigned int cid);\n void vsock_linger(struct sock *sk);\n+struct net *vsock_g2h_net_get(netns_tracker *tracker);\n+bool vsock_g2h_net_eq(const struct net *net);\n+bool vsock_g2h_net_reachable(struct net *net);\n+bool vsock_maybe_set_connected(struct vsock_sock *vsk);\n \n /**** TAP ****/\n \ndiff --git a/include/uapi/linux/vsock.h b/include/uapi/linux/vsock.h\nnew file mode 100644\nindex 0000000000000..803b9aa1f0b3a\n--- /dev/null\n+++ b/include/uapi/linux/vsock.h\n@@ -0,0 +1,28 @@\n+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */\n+/* Do not edit directly, auto-generated from: */\n+/*\tDocumentation/netlink/specs/vsock.yaml */\n+/* YNL-GEN uapi header */\n+/* To regenerate run: tools/net/ynl/ynl-regen.sh */\n+\n+#ifndef _UAPI_LINUX_VSOCK_H\n+#define _UAPI_LINUX_VSOCK_H\n+\n+#define VSOCK_FAMILY_NAME\t\"vsock\"\n+#define VSOCK_FAMILY_VERSION\t1\n+\n+enum {\n+\tVSOCK_A_NETNS_ID = 1,\n+\n+\t__VSOCK_A_MAX,\n+\tVSOCK_A_MAX = (__VSOCK_A_MAX - 1)\n+};\n+\n+enum {\n+\tVSOCK_CMD_DEV_NETNS_SET = 1,\n+\tVSOCK_CMD_DEV_NETNS_GET,\n+\n+\t__VSOCK_CMD_MAX,\n+\tVSOCK_CMD_MAX = (__VSOCK_CMD_MAX - 1)\n+};\n+\n+#endif /* _UAPI_LINUX_VSOCK_H */\ndiff --git a/net/vmw_vsock/Makefile b/net/vmw_vsock/Makefile\nindex 5da74c4a9f1d1..97e2a559e2bf3 100644\n--- a/net/vmw_vsock/Makefile\n+++ b/net/vmw_vsock/Makefile\n@@ -7,7 +7,7 @@ obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o\n obj-$(CONFIG_HYPERV_VSOCKETS) += hv_sock.o\n obj-$(CONFIG_VSOCKETS_LOOPBACK) += vsock_loopback.o\n \n-vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o\n+vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o vsock_nl_gen.o\n vsock-$(CONFIG_BPF_SYSCALL) += vsock_bpf.o\n \n vsock_diag-y += diag.o\ndiff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c\nindex 483af15a5109e..e16e72471b4a3 100644\n--- a/net/vmw_vsock/af_vsock.c\n+++ b/net/vmw_vsock/af_vsock.c\n@@ -130,6 +130,27 @@\n  *       a different transport that *does* support local mode. For\n  *       example, virtio-vsock may not support local mode, but the socket\n  *       may still accept a connection from vhost-vsock which does.\n+ *\n+ * - A guest has a single vsock device, owned by the guest-\u003ehost transport.\n+ *   The VSOCK_CMD_DEV_NETNS_SET netlink command moves it to the namespace the\n+ *   request was sent from. It starts out in init_net. The mode rules then\n+ *   decide who may use it, and which namespace packets from the host are\n+ *   delivered to:\n+ *\n+ *   - assigned to a global mode namespace - every global mode namespace may\n+ *     use it. Until the command is issued nothing has moved and no mode has\n+ *     changed, so the default is the behaviour that predates it.\n+ *   - assigned to a local mode namespace - only that namespace may use it.\n+ *     This is how a nested VM is isolated from the rest of the guest.\n+ *\n+ *   Connections made before an assignment, from a namespace that can no\n+ *   longer reach the device, are closed with ECONNRESET. Nothing is sent to\n+ *   the peer. As with device removal, it finds out on its next send.\n+ *\n+ *   The assignment holds a reference on the namespace, the way an open socket\n+ *   does, so deleting that namespace does not move the device: its destruction\n+ *   is deferred until the device moves to another namespace, on another\n+ *   assignment or when the transport unregisters.\n  */\n \n #include \u003clinux/compat.h\u003e\n@@ -161,10 +182,14 @@\n #include \u003clinux/workqueue.h\u003e\n #include \u003cnet/sock.h\u003e\n #include \u003cnet/af_vsock.h\u003e\n+#include \u003clinux/net_namespace.h\u003e\n+#include \u003cnet/genetlink.h\u003e\n #include \u003cnet/netns/vsock.h\u003e\n #include \u003cuapi/linux/vm_sockets.h\u003e\n #include \u003cuapi/asm-generic/ioctls.h\u003e\n \n+#include \"vsock_nl_gen.h\"\n+\n #define VSOCK_NET_MODE_STR_GLOBAL \"global\"\n #define VSOCK_NET_MODE_STR_LOCAL \"local\"\n \n@@ -206,7 +231,21 @@ static const struct vsock_transport *transport_g2h;\n static const struct vsock_transport *transport_dgram;\n /* Transport used for local communication */\n static const struct vsock_transport *transport_local;\n-static DEFINE_MUTEX(vsock_register_mutex);\n+/* Protects transport registration and the g2h device's netns. */\n+DEFINE_MUTEX(vsock_register_mutex);\n+EXPORT_SYMBOL_GPL(vsock_register_mutex);\n+\n+/* Network namespace of the g2h device. Protected by\n+ * vsock_register_mutex/RCU.\n+ */\n+static struct net __rcu *vsock_g2h_net = RCU_INITIALIZER(\u0026init_net);\n+static netns_tracker vsock_g2h_net_tracker;\n+\n+/* Held across an assign's swap and reset sweep, so that a later assign cannot\n+ * move the device back before the sweep has closed every socket that lost it.\n+ * Must be taken before vsock_register_mutex.\n+ */\n+static DEFINE_MUTEX(vsock_g2h_assign_mutex);\n \n /**** UTILS ****/\n \n@@ -480,7 +519,7 @@ void vsock_remove_sock(struct vsock_sock *vsk)\n }\n EXPORT_SYMBOL_GPL(vsock_remove_sock);\n \n-void vsock_for_each_connected_socket(struct vsock_transport *transport,\n+void vsock_for_each_connected_socket(const struct vsock_transport *transport,\n \t\t\t\t     void (*fn)(struct sock *sk))\n {\n \tint i;\n@@ -553,7 +592,42 @@ void vsock_enqueue_accept(struct sock *listener, struct sock *connected)\n }\n EXPORT_SYMBOL_GPL(vsock_enqueue_accept);\n \n-static bool vsock_use_local_transport(unsigned int remote_cid)\n+/* Return true if @t honours namespace assignment. One that does not keeps the\n+ * reachability rules it had before VSOCK_CMD_DEV_NETNS_SET existed.\n+ */\n+static bool vsock_net_assignable(const struct vsock_transport *t)\n+{\n+\treturn t \u0026\u0026 t-\u003enetns_assign_allow;\n+}\n+\n+static bool vsock_g2h_unreachable(const struct vsock_transport *t,\n+\t\t\t\t  struct net *net)\n+{\n+\treturn t == transport_g2h \u0026\u0026 vsock_net_assignable(t) \u0026\u0026\n+\t       !vsock_g2h_net_reachable(net);\n+}\n+\n+/* Return the CID of the transport in @transport, as seen from @net.\n+ *\n+ * Returns VMADDR_CID_ANY if the transport supports net assignment but is not\n+ * reachable from the net.\n+ */\n+static u32\n+__vsock_registered_transport_cid(const struct vsock_transport **transport,\n+\t\t\t\t struct net *net)\n+{\n+\tlockdep_assert_held(\u0026vsock_register_mutex);\n+\n+\tif (!*transport)\n+\t\treturn VMADDR_CID_ANY;\n+\n+\tif (vsock_g2h_unreachable(*transport, net))\n+\t\treturn VMADDR_CID_ANY;\n+\n+\treturn (*transport)-\u003eget_local_cid();\n+}\n+\n+static bool vsock_use_local_transport(struct net *net, unsigned int remote_cid)\n {\n \tlockdep_assert_held(\u0026vsock_register_mutex);\n \n@@ -563,11 +637,10 @@ static bool vsock_use_local_transport(unsigned int remote_cid)\n \tif (remote_cid == VMADDR_CID_LOCAL)\n \t\treturn true;\n \n-\tif (transport_g2h) {\n+\tif (transport_g2h \u0026\u0026 !vsock_g2h_unreachable(transport_g2h, net))\n \t\treturn remote_cid == transport_g2h-\u003eget_local_cid();\n-\t} else {\n-\t\treturn remote_cid == VMADDR_CID_HOST;\n-\t}\n+\n+\treturn remote_cid == VMADDR_CID_HOST;\n }\n \n static void vsock_deassign_transport(struct vsock_sock *vsk)\n@@ -626,18 +699,20 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)\n \t\tbreak;\n \tcase SOCK_STREAM:\n \tcase SOCK_SEQPACKET:\n-\t\tif (vsock_use_local_transport(remote_cid))\n+\t\tif (vsock_use_local_transport(sock_net(sk), remote_cid))\n \t\t\tnew_transport = transport_local;\n \t\telse if (remote_cid \u003c= VMADDR_CID_HOST ||\n \t\t\t (remote_flags \u0026 VMADDR_FLAG_TO_HOST))\n \t\t\tnew_transport = transport_g2h;\n \t\telse if (transport_h2g \u0026\u0026\n \t\t\t (!transport_h2g-\u003ehas_remote_cid ||\n-\t\t\t  transport_h2g-\u003ehas_remote_cid(vsk, remote_cid)))\n+\t\t\t  transport_h2g-\u003ehas_remote_cid(sock_net(sk),\n+\t\t\t\t\t\t\tremote_cid)))\n \t\t\tnew_transport = transport_h2g;\n \t\telse if (sock_net(sk)-\u003evsock.g2h_fallback \u0026\u0026\n \t\t\t transport_g2h \u0026\u0026 transport_g2h-\u003ehas_remote_cid \u0026\u0026\n-\t\t\t transport_g2h-\u003ehas_remote_cid(vsk, remote_cid)) {\n+\t\t\t transport_g2h-\u003ehas_remote_cid(sock_net(sk),\n+\t\t\t\t\t\t       remote_cid)) {\n \t\t\tvsk-\u003eremote_addr.svm_flags |= VMADDR_FLAG_TO_HOST;\n \t\t\tnew_transport = transport_g2h;\n \t\t} else {\n@@ -649,6 +724,11 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)\n \t\tgoto err;\n \t}\n \n+\tif (vsock_g2h_unreachable(new_transport, sock_net(sk))) {\n+\t\tret = -ENETUNREACH;\n+\t\tgoto err;\n+\t}\n+\n \tif (vsk-\u003etransport \u0026\u0026 vsk-\u003etransport == new_transport) {\n \t\tret = 0;\n \t\tgoto err;\n@@ -715,21 +795,24 @@ EXPORT_SYMBOL_GPL(vsock_assign_transport);\n  * Provide safe access to static transport_{h2g,g2h,dgram,local} callbacks.\n  * Otherwise we may race with module removal. Do not use on `vsk-\u003etransport`.\n  */\n-static u32 vsock_registered_transport_cid(const struct vsock_transport **transport)\n+static u32\n+vsock_registered_transport_cid(const struct vsock_transport **transport,\n+\t\t\t       struct net *net)\n {\n-\tu32 cid = VMADDR_CID_ANY;\n+\tu32 cid;\n \n \tmutex_lock(\u0026vsock_register_mutex);\n-\tif (*transport)\n-\t\tcid = (*transport)-\u003eget_local_cid();\n+\tcid = __vsock_registered_transport_cid(transport, net);\n \tmutex_unlock(\u0026vsock_register_mutex);\n \n \treturn cid;\n }\n \n-bool vsock_find_cid(unsigned int cid)\n+bool __vsock_find_cid(struct net *net, unsigned int cid)\n {\n-\tif (cid == vsock_registered_transport_cid(\u0026transport_g2h))\n+\tlockdep_assert_held(\u0026vsock_register_mutex);\n+\n+\tif (cid == __vsock_registered_transport_cid(\u0026transport_g2h, net))\n \t\treturn true;\n \n \tif (transport_h2g \u0026\u0026 cid == VMADDR_CID_HOST)\n@@ -740,8 +823,303 @@ bool vsock_find_cid(unsigned int cid)\n \n \treturn false;\n }\n+EXPORT_SYMBOL_GPL(__vsock_find_cid);\n+\n+bool vsock_find_cid(struct net *net, unsigned int cid)\n+{\n+\tbool found;\n+\n+\tmutex_lock(\u0026vsock_register_mutex);\n+\tfound = __vsock_find_cid(net, cid);\n+\tmutex_unlock(\u0026vsock_register_mutex);\n+\n+\treturn found;\n+}\n EXPORT_SYMBOL_GPL(vsock_find_cid);\n \n+/* Return the namespace the g2h device is assigned to, with a reference held.\n+ *\n+ * The assignment itself holds a reference, so the namespace cannot be going\n+ * away here and this never fails.\n+ */\n+struct net *vsock_g2h_net_get(netns_tracker *tracker)\n+{\n+\tstruct net *net;\n+\n+\trcu_read_lock();\n+\tnet = rcu_dereference(vsock_g2h_net);\n+\tget_net_track(net, tracker, GFP_ATOMIC);\n+\trcu_read_unlock();\n+\n+\treturn net;\n+}\n+EXPORT_SYMBOL_GPL(vsock_g2h_net_get);\n+\n+/* True if @net is still the namespace the device is assigned to. Cheap enough\n+ * to call per packet on the RX path.\n+ */\n+bool vsock_g2h_net_eq(const struct net *net)\n+{\n+\treturn rcu_access_pointer(vsock_g2h_net) == net;\n+}\n+EXPORT_SYMBOL_GPL(vsock_g2h_net_eq);\n+\n+bool vsock_g2h_net_reachable(struct net *net)\n+{\n+\tbool reachable;\n+\n+\trcu_read_lock();\n+\treachable = vsock_net_check_mode(net, rcu_dereference(vsock_g2h_net));\n+\trcu_read_unlock();\n+\n+\treturn reachable;\n+}\n+EXPORT_SYMBOL_GPL(vsock_g2h_net_reachable);\n+\n+static bool vsock_g2h_reachable_sk(struct vsock_sock *vsk)\n+{\n+\tif (!vsock_net_assignable(vsk-\u003etransport))\n+\t\treturn true;\n+\n+\treturn vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));\n+}\n+\n+/* Move @vsk to TCP_ESTABLISHED and into the connected table, unless the device\n+ * has moved to a namespace @vsk cannot reach. Returns false without doing\n+ * either in that case.\n+ *\n+ * The reset sweep walks the same table under the same lock, so an assign\n+ * cannot land between the check and the insert: either the sweep finds @vsk\n+ * and closes it, or @vsk is never added.\n+ */\n+bool vsock_maybe_set_connected(struct vsock_sock *vsk)\n+{\n+\tstruct list_head *list;\n+\tbool reachable;\n+\n+\tlist = vsock_connected_sockets(\u0026vsk-\u003eremote_addr, \u0026vsk-\u003elocal_addr);\n+\n+\tspin_lock_bh(\u0026vsock_table_lock);\n+\treachable = vsock_g2h_reachable_sk(vsk);\n+\tif (reachable) {\n+\t\tsk_vsock(vsk)-\u003esk_state = TCP_ESTABLISHED;\n+\t\t__vsock_insert_connected(list, vsk);\n+\t}\n+\tspin_unlock_bh(\u0026vsock_table_lock);\n+\n+\treturn reachable;\n+}\n+EXPORT_SYMBOL_GPL(vsock_maybe_set_connected);\n+\n+static bool vsock_g2h_should_reset(struct vsock_sock *vsk,\n+\t\t\t\t   const struct vsock_transport *t)\n+{\n+\treturn vsk-\u003etransport == t \u0026\u0026\n+\t       READ_ONCE(sk_vsock(vsk)-\u003esk_state) != TCP_CLOSE \u0026\u0026\n+\t       !READ_ONCE(vsk-\u003eclose_work_scheduled) \u0026\u0026\n+\t       !vsock_g2h_reachable_sk(vsk);\n+}\n+\n+static struct vsock_sock *\n+vsock_g2h_next_unreachable(const struct vsock_transport *t, unsigned int *i)\n+{\n+\tstruct vsock_sock *vsk;\n+\n+\tspin_lock_bh(\u0026vsock_table_lock);\n+\n+\tfor (; *i \u003c ARRAY_SIZE(vsock_connected_table); (*i)++) {\n+\t\tlist_for_each_entry(vsk, \u0026vsock_connected_table[*i],\n+\t\t\t\t    connected_table) {\n+\t\t\tif (vsock_g2h_should_reset(vsk, t)) {\n+\t\t\t\tsock_hold(sk_vsock(vsk));\n+\t\t\t\tspin_unlock_bh(\u0026vsock_table_lock);\n+\t\t\t\treturn vsk;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tspin_unlock_bh(\u0026vsock_table_lock);\n+\n+\treturn NULL;\n+}\n+\n+/* Close every connected socket of @t that can no longer reach the g2h device.\n+ */\n+static void vsock_g2h_reset_unreachable(const struct vsock_transport *t)\n+{\n+\tstruct vsock_sock *vsk;\n+\tunsigned int i = 0;\n+\tstruct sock *sk;\n+\tbool reset;\n+\n+\tlockdep_assert_held(\u0026vsock_g2h_assign_mutex);\n+\tlockdep_assert_not_held(\u0026vsock_register_mutex);\n+\n+\twhile ((vsk = vsock_g2h_next_unreachable(t, \u0026i))) {\n+\t\tsk = sk_vsock(vsk);\n+\n+\t\tlock_sock(sk);\n+\n+\t\treset = false;\n+\t\tspin_lock_bh(\u0026vsock_table_lock);\n+\t\tif (__vsock_in_connected_table(vsk) \u0026\u0026\n+\t\t    vsock_g2h_should_reset(vsk, t)) {\n+\t\t\t__vsock_remove_connected(vsk);\n+\t\t\treset = true;\n+\t\t}\n+\t\tspin_unlock_bh(\u0026vsock_table_lock);\n+\n+\t\tif (reset) {\n+\t\t\tsk-\u003esk_state = TCP_CLOSE;\n+\t\t\tsk-\u003esk_err = ECONNRESET;\n+\t\t\tsk_error_report(sk);\n+\t\t}\n+\n+\t\trelease_sock(sk);\n+\t\tsock_put(sk);\n+\t}\n+}\n+\n+/* Point the g2h device at @net, taking the reference that keeps @net alive,\n+ * and return the namespace it leaves behind.\n+ */\n+static struct net *vsock_g2h_net_swap(struct net *net, netns_tracker *old)\n+{\n+\tlockdep_assert_held(\u0026vsock_register_mutex);\n+\n+\t*old = vsock_g2h_net_tracker;\n+\tget_net_track(net, \u0026vsock_g2h_net_tracker, GFP_KERNEL);\n+\n+\treturn rcu_replace_pointer(vsock_g2h_net, net,\n+\t\t\t\t   lockdep_is_held(\u0026vsock_register_mutex));\n+}\n+\n+/* Drop the namespace the device just left, once the readers that may still\n+ * see it in vsock_g2h_net_get() are gone. Runs without the mutex held.\n+ */\n+static void vsock_g2h_net_release(struct net *old, netns_tracker *tracker)\n+{\n+\tsynchronize_rcu();\n+\tput_net_track(old, tracker);\n+}\n+\n+static int vsock_nl_dev_netns_fill(struct sk_buff *msg, struct genl_info *info)\n+{\n+\tstruct net *net = genl_info_net(info);\n+\tstruct net *assigned;\n+\tbool equal;\n+\tvoid *hdr;\n+\ts32 id;\n+\n+\tlockdep_assert_held(\u0026vsock_register_mutex);\n+\n+\trcu_read_lock();\n+\tassigned = rcu_dereference(vsock_g2h_net);\n+\tequal = net_eq(net, assigned);\n+\tif (!equal) {\n+\t\tif (vsock_net_assignable(transport_g2h) \u0026\u0026\n+\t\t    !vsock_net_check_mode(net, assigned))\n+\t\t\tid = NETNSA_NSID_NOT_ASSIGNED;\n+\t\telse\n+\t\t\tid = peernet2id_alloc(net, assigned, GFP_ATOMIC);\n+\t}\n+\trcu_read_unlock();\n+\n+\thdr = genlmsg_iput(msg, info);\n+\tif (!hdr)\n+\t\treturn -EMSGSIZE;\n+\n+\tif (!equal \u0026\u0026 nla_put_s32(msg, VSOCK_A_NETNS_ID, id)) {\n+\t\tgenlmsg_cancel(msg, hdr);\n+\t\treturn -EMSGSIZE;\n+\t}\n+\n+\tgenlmsg_end(msg, hdr);\n+\n+\treturn 0;\n+}\n+\n+/* Move the g2h device to @net, which keeps @net alive until the device leaves\n+ * again. Returns -ENODEV if no g2h transport is loaded, -EOPNOTSUPP if the\n+ * loaded one cannot be moved, and -EADDRINUSE if a guest in @net already uses\n+ * the device's CID.\n+ */\n+static int vsock_g2h_net_assign(struct net *net, struct sk_buff *msg,\n+\t\t\t\tstruct genl_info *info)\n+{\n+\tstruct netlink_ext_ack *extack = info-\u003eextack;\n+\tconst struct vsock_transport *t;\n+\tnetns_tracker old_tracker;\n+\tstruct net *old;\n+\tvoid *hdr;\n+\tint ret;\n+\n+\tmutex_lock(\u0026vsock_g2h_assign_mutex);\n+\tmutex_lock(\u0026vsock_register_mutex);\n+\n+\tret = -ENODEV;\n+\tif (!transport_g2h) {\n+\t\tNL_SET_ERR_MSG(extack, \"no guest-to-host transport is loaded\");\n+\t\tgoto unlock;\n+\t}\n+\n+\tret = -EOPNOTSUPP;\n+\tif (!vsock_net_assignable(transport_g2h)) {\n+\t\tNL_SET_ERR_MSG(extack,\n+\t\t\t       \"the loaded guest-to-host transport does not support namespace assignment\");\n+\t\tgoto unlock;\n+\t}\n+\n+\tif (transport_h2g \u0026\u0026 !transport_h2g-\u003ehas_remote_cid) {\n+\t\tNL_SET_ERR_MSG(extack,\n+\t\t\t       \"the loaded host-to-guest transport cannot check the device's CID\");\n+\t\tgoto unlock;\n+\t}\n+\n+\tret = -EADDRINUSE;\n+\tif (transport_h2g \u0026\u0026\n+\t    transport_h2g-\u003ehas_remote_cid(net,\n+\t\t\t\t\t  transport_g2h-\u003eget_local_cid())) {\n+\t\tNL_SET_ERR_MSG(extack,\n+\t\t\t       \"a guest in the target namespace already uses the device's CID\");\n+\t\tgoto unlock;\n+\t}\n+\n+\tret = -EMSGSIZE;\n+\thdr = genlmsg_iput(msg, info);\n+\tif (!hdr)\n+\t\tgoto unlock;\n+\tgenlmsg_end(msg, hdr);\n+\n+\tret = genlmsg_reply(msg, info);\n+\tmsg = NULL;\n+\tif (ret)\n+\t\tgoto unlock;\n+\n+\told = vsock_g2h_net_swap(net, \u0026old_tracker);\n+\tt = transport_g2h;\n+\n+\tmutex_unlock(\u0026vsock_register_mutex);\n+\n+\t/* See vsock_maybe_set_connected() comment about synchronizing with\n+\t * connecting sockets.\n+\t */\n+\tvsock_g2h_reset_unreachable(t);\n+\n+\tmutex_unlock(\u0026vsock_g2h_assign_mutex);\n+\n+\tvsock_g2h_net_release(old, \u0026old_tracker);\n+\n+\treturn 0;\n+\n+unlock:\n+\tmutex_unlock(\u0026vsock_register_mutex);\n+\tmutex_unlock(\u0026vsock_g2h_assign_mutex);\n+\tnlmsg_free(msg);\n+\n+\treturn ret;\n+}\n+\n static struct sock *vsock_dequeue_accept(struct sock *listener)\n {\n \tstruct vsock_sock *vlistener;\n@@ -908,7 +1286,8 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)\n \t * like AF_INET prevents binding to a non-local IP address (in most\n \t * cases), we only allow binding to a local CID.\n \t */\n-\tif (addr-\u003esvm_cid != VMADDR_CID_ANY \u0026\u0026 !vsock_find_cid(addr-\u003esvm_cid))\n+\tif (addr-\u003esvm_cid != VMADDR_CID_ANY \u0026\u0026\n+\t    !vsock_find_cid(sock_net(sk_vsock(vsk)), addr-\u003esvm_cid))\n \t\treturn -EADDRNOTAVAIL;\n \n \tswitch (sk-\u003esk_socket-\u003etype) {\n@@ -2796,6 +3175,7 @@ static long vsock_dev_do_ioctl(struct file *filp,\n {\n \tu32 __user *p = ptr;\n \tint retval = 0;\n+\tstruct net *net;\n \tu32 cid;\n \n \tswitch (cmd) {\n@@ -2803,11 +3183,14 @@ static long vsock_dev_do_ioctl(struct file *filp,\n \t\t/* To be compatible with the VMCI behavior, we prioritize the\n \t\t * guest CID instead of well-know host CID (VMADDR_CID_HOST).\n \t\t */\n-\t\tcid = vsock_registered_transport_cid(\u0026transport_g2h);\n+\t\tnet = current-\u003ensproxy-\u003enet_ns;\n+\t\tcid = vsock_registered_transport_cid(\u0026transport_g2h, net);\n \t\tif (cid == VMADDR_CID_ANY)\n-\t\t\tcid = vsock_registered_transport_cid(\u0026transport_h2g);\n+\t\t\tcid = vsock_registered_transport_cid(\u0026transport_h2g,\n+\t\t\t\t\t\t\t     net);\n \t\tif (cid == VMADDR_CID_ANY)\n-\t\t\tcid = vsock_registered_transport_cid(\u0026transport_local);\n+\t\t\tcid = vsock_registered_transport_cid(\u0026transport_local,\n+\t\t\t\t\t\t\t     net);\n \n \t\tif (put_user(cid, p) != 0)\n \t\t\tretval = -EFAULT;\n@@ -2848,6 +3231,44 @@ static struct miscdevice vsock_device = {\n \t.fops\t\t= \u0026vsock_device_ops,\n };\n \n+int vsock_nl_dev_netns_get_doit(struct sk_buff *skb, struct genl_info *info)\n+{\n+\tstruct sk_buff *msg;\n+\tint err;\n+\n+\tmsg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);\n+\tif (!msg)\n+\t\treturn -ENOMEM;\n+\n+\tmutex_lock(\u0026vsock_register_mutex);\n+\tif (transport_g2h) {\n+\t\terr = vsock_nl_dev_netns_fill(msg, info);\n+\t} else {\n+\t\tNL_SET_ERR_MSG(info-\u003eextack,\n+\t\t\t       \"no guest-to-host transport is loaded\");\n+\t\terr = -ENODEV;\n+\t}\n+\tmutex_unlock(\u0026vsock_register_mutex);\n+\n+\tif (err) {\n+\t\tnlmsg_free(msg);\n+\t\treturn err;\n+\t}\n+\n+\treturn genlmsg_reply(msg, info);\n+}\n+\n+int vsock_nl_dev_netns_set_doit(struct sk_buff *skb, struct genl_info *info)\n+{\n+\tstruct sk_buff *msg;\n+\n+\tmsg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);\n+\tif (!msg)\n+\t\treturn -ENOMEM;\n+\n+\treturn vsock_g2h_net_assign(genl_info_net(info), msg, info);\n+}\n+\n static int __vsock_net_mode_string(const struct ctl_table *table, int write,\n \t\t\t\t   void *buffer, size_t *lenp, loff_t *ppos,\n \t\t\t\t   enum vsock_net_mode mode,\n@@ -3037,7 +3458,7 @@ static void vsock_net_init(struct net *net)\n \tnet-\u003evsock.g2h_fallback = 1;\n }\n \n-static __net_init int vsock_sysctl_init_net(struct net *net)\n+static __net_init int vsock_pernet_init(struct net *net)\n {\n \tvsock_net_init(net);\n \n@@ -3047,14 +3468,14 @@ static __net_init int vsock_sysctl_init_net(struct net *net)\n \treturn 0;\n }\n \n-static __net_exit void vsock_sysctl_exit_net(struct net *net)\n+static __net_exit void vsock_pernet_exit(struct net *net)\n {\n \tvsock_sysctl_unregister(net);\n }\n \n-static struct pernet_operations vsock_sysctl_ops = {\n-\t.init = vsock_sysctl_init_net,\n-\t.exit = vsock_sysctl_exit_net,\n+static struct pernet_operations vsock_pernet_ops = {\n+\t.init = vsock_pernet_init,\n+\t.exit = vsock_pernet_exit,\n };\n \n static int __init vsock_init(void)\n@@ -3063,6 +3484,8 @@ static int __init vsock_init(void)\n \n \tvsock_init_tables();\n \n+\tget_net_track(\u0026init_net, \u0026vsock_g2h_net_tracker, GFP_KERNEL);\n+\n \tvsock_proto.owner = THIS_MODULE;\n \tvsock_device.minor = MISC_DYNAMIC_MINOR;\n \terr = misc_register(\u0026vsock_device);\n@@ -3084,15 +3507,23 @@ static int __init vsock_init(void)\n \t\tgoto err_unregister_proto;\n \t}\n \n-\tif (register_pernet_subsys(\u0026vsock_sysctl_ops)) {\n+\tif (register_pernet_subsys(\u0026vsock_pernet_ops)) {\n \t\terr = -ENOMEM;\n \t\tgoto err_unregister_sock;\n \t}\n \n+\terr = genl_register_family(\u0026vsock_nl_family);\n+\tif (err) {\n+\t\tpr_err(\"Cannot register vsock netlink family: %d\\n\", err);\n+\t\tgoto err_unregister_pernet;\n+\t}\n+\n \tvsock_bpf_build_proto();\n \n \treturn 0;\n \n+err_unregister_pernet:\n+\tunregister_pernet_subsys(\u0026vsock_pernet_ops);\n err_unregister_sock:\n \tsock_unregister(AF_VSOCK);\n err_unregister_proto:\n@@ -3100,15 +3531,20 @@ static int __init vsock_init(void)\n err_deregister_misc:\n \tmisc_deregister(\u0026vsock_device);\n err_reset_transport:\n+\tput_net_track(\u0026init_net, \u0026vsock_g2h_net_tracker);\n \treturn err;\n }\n \n static void __exit vsock_exit(void)\n {\n+\tgenl_unregister_family(\u0026vsock_nl_family);\n \tmisc_deregister(\u0026vsock_device);\n \tsock_unregister(AF_VSOCK);\n \tproto_unregister(\u0026vsock_proto);\n-\tunregister_pernet_subsys(\u0026vsock_sysctl_ops);\n+\tunregister_pernet_subsys(\u0026vsock_pernet_ops);\n+\n+\tput_net_track(rcu_dereference_protected(vsock_g2h_net, true),\n+\t\t      \u0026vsock_g2h_net_tracker);\n }\n \n const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk)\n@@ -3175,13 +3611,18 @@ EXPORT_SYMBOL_GPL(vsock_core_register);\n \n void vsock_core_unregister(const struct vsock_transport *t)\n {\n+\tnetns_tracker old_tracker;\n+\tstruct net *old = NULL;\n+\n \tmutex_lock(\u0026vsock_register_mutex);\n \n \tif (transport_h2g == t)\n \t\ttransport_h2g = NULL;\n \n-\tif (transport_g2h == t)\n+\tif (transport_g2h == t) {\n \t\ttransport_g2h = NULL;\n+\t\told = vsock_g2h_net_swap(\u0026init_net, \u0026old_tracker);\n+\t}\n \n \tif (transport_dgram == t)\n \t\ttransport_dgram = NULL;\n@@ -3190,6 +3631,10 @@ void vsock_core_unregister(const struct vsock_transport *t)\n \t\ttransport_local = NULL;\n \n \tmutex_unlock(\u0026vsock_register_mutex);\n+\n+\t/* The device is gone, so release the namespace it was holding. */\n+\tif (old)\n+\t\tvsock_g2h_net_release(old, \u0026old_tracker);\n }\n EXPORT_SYMBOL_GPL(vsock_core_unregister);\n \ndiff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c\nindex 4f9aa9c4c3aa5..06df82b130cff 100644\n--- a/net/vmw_vsock/virtio_transport.c\n+++ b/net/vmw_vsock/virtio_transport.c\n@@ -256,6 +256,15 @@ virtio_transport_send_pkt(struct sk_buff *skb, struct net *net)\n \t\tgoto out_rcu;\n \t}\n \n+\t/* A socket reset by an assign can still send credit updates from\n+\t * recvmsg() and setsockopt(SO_RCVLOWAT).\n+\t */\n+\tif (!vsock_g2h_net_reachable(net)) {\n+\t\tkfree_skb(skb);\n+\t\tlen = -ENETUNREACH;\n+\t\tgoto out_rcu;\n+\t}\n+\n \t/* If send_pkt_queue is empty, we can safely bypass this queue\n \t * because packet order is maintained and (try) to put the packet\n \t * on the virtqueue using virtio_transport_send_skb_fast_path.\n@@ -542,13 +551,13 @@ static bool virtio_transport_msgzerocopy_allow(void)\n \n bool virtio_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port)\n {\n-\treturn vsock_net_mode_global(vsk);\n+\treturn vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));\n }\n \n static bool virtio_transport_seqpacket_allow(struct vsock_sock *vsk,\n \t\t\t\t\t     u32 remote_cid);\n \n-static bool virtio_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid)\n+static bool virtio_transport_has_remote_cid(struct net *net, u32 cid)\n {\n \t/* The CID could be implemented by the host. Always assume it is. */\n \treturn true;\n@@ -587,6 +596,7 @@ static struct virtio_transport virtio_transport = {\n \t\t.seqpacket_has_data       = virtio_transport_seqpacket_has_data,\n \n \t\t.msgzerocopy_allow        = virtio_transport_msgzerocopy_allow,\n+\t\t.netns_assign_allow       = true,\n \n \t\t.notify_poll_in           = virtio_transport_notify_poll_in,\n \t\t.notify_poll_out          = virtio_transport_notify_poll_out,\n@@ -616,7 +626,7 @@ virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid)\n \tstruct virtio_vsock *vsock;\n \tbool seqpacket_allow;\n \n-\tif (!vsock_net_mode_global(vsk))\n+\tif (!vsock_g2h_net_reachable(sock_net(sk_vsock(vsk))))\n \t\treturn false;\n \n \tseqpacket_allow = false;\n@@ -633,7 +643,12 @@ static void virtio_transport_rx_work(struct work_struct *work)\n {\n \tstruct virtio_vsock *vsock =\n \t\tcontainer_of(work, struct virtio_vsock, rx_work);\n+\tstruct virtio_transport *t = \u0026virtio_transport;\n+\tnetns_tracker ns_tracker;\n \tstruct virtqueue *vq;\n+\tstruct net *net;\n+\n+\tnet = vsock_g2h_net_get(\u0026ns_tracker);\n \n \tmutex_lock(\u0026vsock-\u003erx_lock);\n \n@@ -682,10 +697,15 @@ static void virtio_transport_rx_work(struct work_struct *work)\n \n \t\t\tvirtio_transport_deliver_tap_pkt(skb);\n \n-\t\t\t/* Force virtio-transport into global mode since it\n-\t\t\t * does not yet support local-mode namespacing.\n+\t\t\t/* Only grab a reference if the net changes underneath\n+\t\t\t * us.\n \t\t\t */\n-\t\t\tvirtio_transport_recv_pkt(\u0026virtio_transport, skb, NULL);\n+\t\t\tif (unlikely(!vsock_g2h_net_eq(net))) {\n+\t\t\t\tput_net_track(net, \u0026ns_tracker);\n+\t\t\t\tnet = vsock_g2h_net_get(\u0026ns_tracker);\n+\t\t\t}\n+\n+\t\t\tvirtio_transport_recv_pkt(t, skb, net);\n \t\t}\n \t} while (!virtqueue_enable_cb(vq));\n \n@@ -694,6 +714,8 @@ static void virtio_transport_rx_work(struct work_struct *work)\n \t\tvirtio_vsock_rx_fill(vsock);\n out_nofill:\n \tmutex_unlock(\u0026vsock-\u003erx_lock);\n+\n+\tput_net_track(net, \u0026ns_tracker);\n }\n \n static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)\ndiff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c\nindex 1e762a480df49..0c864db2bc65b 100644\n--- a/net/vmw_vsock/virtio_transport_common.c\n+++ b/net/vmw_vsock/virtio_transport_common.c\n@@ -1478,9 +1478,13 @@ virtio_transport_recv_connecting(struct sock *sk,\n \n \tswitch (le16_to_cpu(hdr-\u003eop)) {\n \tcase VIRTIO_VSOCK_OP_RESPONSE:\n-\t\tsk-\u003esk_state = TCP_ESTABLISHED;\n+\t\tif (!vsock_maybe_set_connected(vsk)) {\n+\t\t\tskerr = ECONNRESET;\n+\t\t\terr = -ENETUNREACH;\n+\t\t\tgoto destroy;\n+\t\t}\n+\n \t\tsk-\u003esk_socket-\u003estate = SS_CONNECTED;\n-\t\tvsock_insert_connected(vsk);\n \t\tsk-\u003esk_state_change(sk);\n \t\tbreak;\n \tcase VIRTIO_VSOCK_OP_INVALID:\n@@ -1736,8 +1740,6 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,\n \n \tlock_sock_nested(child, SINGLE_DEPTH_NESTING);\n \n-\tchild-\u003esk_state = TCP_ESTABLISHED;\n-\n \tvchild = vsock_sk(child);\n \tvsock_addr_init(\u0026vchild-\u003elocal_addr, le64_to_cpu(hdr-\u003edst_cid),\n \t\t\tle32_to_cpu(hdr-\u003edst_port));\n@@ -1758,7 +1760,13 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,\n \tif (virtio_transport_space_update(child, skb))\n \t\tchild-\u003esk_write_space(child);\n \n-\tvsock_insert_connected(vchild);\n+\tif (!vsock_maybe_set_connected(vchild)) {\n+\t\trelease_sock(child);\n+\t\tvirtio_transport_reset_no_sock(t, skb, sock_net(sk));\n+\t\tsock_put(child);\n+\t\treturn -ENETUNREACH;\n+\t}\n+\n \tvsock_enqueue_accept(sk, child);\n \tvirtio_transport_send_response(vchild, skb);\n \ndiff --git a/net/vmw_vsock/vsock_nl_gen.c b/net/vmw_vsock/vsock_nl_gen.c\nnew file mode 100644\nindex 0000000000000..81db4c61d7bb9\n--- /dev/null\n+++ b/net/vmw_vsock/vsock_nl_gen.c\n@@ -0,0 +1,36 @@\n+// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)\n+/* Do not edit directly, auto-generated from: */\n+/*\tDocumentation/netlink/specs/vsock.yaml */\n+/* YNL-GEN kernel source */\n+/* To regenerate run: tools/net/ynl/ynl-regen.sh */\n+\n+#include \u003cnet/netlink.h\u003e\n+#include \u003cnet/genetlink.h\u003e\n+\n+#include \"vsock_nl_gen.h\"\n+\n+#include \u003cuapi/linux/vsock.h\u003e\n+\n+/* Ops table for vsock */\n+static const struct genl_split_ops vsock_nl_ops[] = {\n+\t{\n+\t\t.cmd\t= VSOCK_CMD_DEV_NETNS_SET,\n+\t\t.doit\t= vsock_nl_dev_netns_set_doit,\n+\t\t.flags\t= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,\n+\t},\n+\t{\n+\t\t.cmd\t= VSOCK_CMD_DEV_NETNS_GET,\n+\t\t.doit\t= vsock_nl_dev_netns_get_doit,\n+\t\t.flags\t= GENL_CMD_CAP_DO,\n+\t},\n+};\n+\n+struct genl_family vsock_nl_family __ro_after_init = {\n+\t.name\t\t= VSOCK_FAMILY_NAME,\n+\t.version\t= VSOCK_FAMILY_VERSION,\n+\t.netnsok\t= true,\n+\t.parallel_ops\t= true,\n+\t.module\t\t= THIS_MODULE,\n+\t.split_ops\t= vsock_nl_ops,\n+\t.n_split_ops\t= ARRAY_SIZE(vsock_nl_ops),\n+};\ndiff --git a/net/vmw_vsock/vsock_nl_gen.h b/net/vmw_vsock/vsock_nl_gen.h\nnew file mode 100644\nindex 0000000000000..c041195584a09\n--- /dev/null\n+++ b/net/vmw_vsock/vsock_nl_gen.h\n@@ -0,0 +1,20 @@\n+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */\n+/* Do not edit directly, auto-generated from: */\n+/*\tDocumentation/netlink/specs/vsock.yaml */\n+/* YNL-GEN kernel header */\n+/* To regenerate run: tools/net/ynl/ynl-regen.sh */\n+\n+#ifndef _LINUX_VSOCK_GEN_H\n+#define _LINUX_VSOCK_GEN_H\n+\n+#include \u003cnet/netlink.h\u003e\n+#include \u003cnet/genetlink.h\u003e\n+\n+#include \u003cuapi/linux/vsock.h\u003e\n+\n+int vsock_nl_dev_netns_set_doit(struct sk_buff *skb, struct genl_info *info);\n+int vsock_nl_dev_netns_get_doit(struct sk_buff *skb, struct genl_info *info);\n+\n+extern struct genl_family vsock_nl_family;\n+\n+#endif /* _LINUX_VSOCK_GEN_H */\ndiff --git a/tools/net/ynl/Makefile.deps b/tools/net/ynl/Makefile.deps\nindex 3eee34e0efcf9..f3150b41dd333 100644\n--- a/tools/net/ynl/Makefile.deps\n+++ b/tools/net/ynl/Makefile.deps\n@@ -62,4 +62,5 @@ CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \\\n \t$(call get_hdr_inc,_TC_SKBEDIT_H,tc_act/tc_skbedit.h) \\\n \t$(call get_hdr_inc,_TC_TUNNEL_KEY_H,tc_act/tc_tunnel_key.h)\n CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)\n+CFLAGS_vsock:=$(call get_hdr_inc,_LINUX_VSOCK_H,vsock.h)\n CFLAGS_wireguard:=$(call get_hdr_inc2,_LINUX_WIREGUARD_H,_WG_UAPI_WIREGUARD_H,wireguard.h)\ndiff --git a/tools/testing/selftests/vsock/Makefile b/tools/testing/selftests/vsock/Makefile\nindex c407c0afd9388..2b066ba91bcd7 100644\n--- a/tools/testing/selftests/vsock/Makefile\n+++ b/tools/testing/selftests/vsock/Makefile\n@@ -13,5 +13,10 @@ $(VSOCK_TEST_DIR)/vsock_test: $(VSOCK_TEST_SRCS)\n TEST_PROGS += vmtest.sh\n TEST_GEN_FILES := vsock_test\n \n+TEST_FILES := \\\n+\t../../../net/ynl/pyynl/cli.py \\\n+\t../../../net/ynl/pyynl/lib \\\n+\t../../../../Documentation/netlink/specs/vsock.yaml\n+\n include ../lib.mk\n \ndiff --git a/tools/testing/selftests/vsock/config b/tools/testing/selftests/vsock/config\nindex 5f0a4f17dfc96..4b31085558faa 100644\n--- a/tools/testing/selftests/vsock/config\n+++ b/tools/testing/selftests/vsock/config\n@@ -109,3 +109,4 @@ CONFIG_FS_DAX=y\n CONFIG_MEMORY_HOTPLUG=y\n CONFIG_MEMORY_HOTREMOVE=y\n CONFIG_ZONE_DEVICE=y\n+CONFIG_USER_NS=y\ndiff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh\nindex 310dfc2a39ad5..5bd90a0c23802 100755\n--- a/tools/testing/selftests/vsock/vmtest.sh\n+++ b/tools/testing/selftests/vsock/vmtest.sh\n@@ -17,6 +17,20 @@ readonly KERNEL_CHECKOUT=$(realpath \"${SCRIPT_DIR}\"/../../../../)\n source \"${SCRIPT_DIR}\"/../kselftest/ktap_helpers.sh\n \n readonly VSOCK_TEST=\"${SCRIPT_DIR}\"/vsock_test\n+\n+# \"make install\" copies the ynl CLI, its lib package and the spec next to this\n+# script (see TEST_FILES); in a source tree they stay where they are built.\n+if [[ -r \"${SCRIPT_DIR}\"/cli.py ]]; then\n+\treadonly YNL_CLI=\"${SCRIPT_DIR}\"/cli.py\n+\treadonly YNL_LIB=\"${SCRIPT_DIR}\"/lib\n+\treadonly VSOCK_SPEC=\"${SCRIPT_DIR}\"/vsock.yaml\n+else\n+\treadonly YNL_DIR=\"${KERNEL_CHECKOUT}\"/tools/net/ynl/pyynl\n+\treadonly YNL_CLI=\"${YNL_DIR}\"/cli.py\n+\treadonly YNL_LIB=\"${YNL_DIR}\"/lib\n+\treadonly SPEC_DIR=\"${KERNEL_CHECKOUT}\"/Documentation/netlink/specs\n+\treadonly VSOCK_SPEC=\"${SPEC_DIR}\"/vsock.yaml\n+fi\n readonly TEST_GUEST_PORT=51000\n readonly TEST_HOST_PORT=50000\n readonly TEST_HOST_PORT_LISTENER=50001\n@@ -73,6 +87,14 @@ readonly TEST_NAMES=(\n \tns_delete_vm_ok\n \tns_delete_host_ok\n \tns_delete_both_ok\n+\tns_guest_local_connect_to_host_fails\n+\tns_guest_assign_g2h_netns_connect_to_host_ok\n+\tns_guest_assign_g2h_netns_init_ns_connect_fails\n+\tns_guest_assign_g2h_netns_host_connect_ok\n+\tns_guest_assign_g2h_netns_pins_ns_ok\n+\tns_guest_assign_g2h_netns_old_conn_send_fails\n+\tns_guest_assign_g2h_netns_no_cap_net_admin_fails\n+\tns_guest_assign_g2h_netns_unpriv_user_ns_fails\n )\n readonly TEST_DESCS=(\n \t# vm_server_host_client\n@@ -149,12 +171,44 @@ readonly TEST_DESCS=(\n \n \t# ns_delete_both_ok\n \t\"Check that deleting the VM and host's namespaces does not break the socket connection\"\n+\n+\t# ns_guest_local_connect_to_host_fails\n+\t\"Check a guest process in a local ns cannot reach the host without the netns assign.\"\n+\n+\t# ns_guest_assign_g2h_netns_connect_to_host_ok\n+\t\"Check a guest process in a local ns reaches the host once the vsock device is assigned to it.\"\n+\n+\t# ns_guest_assign_g2h_netns_init_ns_connect_fails\n+\t\"Check the guest's initial ns loses vsock once the device is assigned to another ns.\"\n+\n+\t# ns_guest_assign_g2h_netns_host_connect_ok\n+\t\"Check the host reaches a guest listener in the ns the vsock device is assigned to.\"\n+\n+\t# ns_guest_assign_g2h_netns_pins_ns_ok\n+\t\"Check the assignment keeps the ns alive when deleted, and that reassigning releases it.\"\n+\n+\t# ns_guest_assign_g2h_netns_old_conn_send_fails\n+\t\"Check connections made before the assign stop sending once they lose the device.\"\n+\n+\t# ns_guest_assign_g2h_netns_no_cap_net_admin_fails\n+\t\"Check assigning the guest's vsock device to a namespace needs CAP_NET_ADMIN.\"\n+\n+\t# ns_guest_assign_g2h_netns_unpriv_user_ns_fails\n+\t\"Check an unprivileged user cannot claim the guest's vsock device via a user ns.\"\n )\n \n readonly USE_SHARED_VM=(\n \tvm_server_host_client\n \tvm_client_host_server\n \tvm_loopback\n+\tns_guest_local_connect_to_host_fails\n+\tns_guest_assign_g2h_netns_connect_to_host_ok\n+\tns_guest_assign_g2h_netns_init_ns_connect_fails\n+\tns_guest_assign_g2h_netns_host_connect_ok\n+\tns_guest_assign_g2h_netns_pins_ns_ok\n+\tns_guest_assign_g2h_netns_old_conn_send_fails\n+\tns_guest_assign_g2h_netns_no_cap_net_admin_fails\n+\tns_guest_assign_g2h_netns_unpriv_user_ns_fails\n )\n readonly NS_MODES=(\"local\" \"global\")\n \n@@ -316,6 +370,34 @@ check_deps() {\n \tfi\n }\n \n+check_ns_guest_deps() {\n+\tlocal tname=$1\n+\tlocal dep\n+\n+\tif [[ ! \"${tname}\" =~ ^ns_guest_ ]]; then\n+\t\treturn 0\n+\tfi\n+\n+\tfor dep in unshare setpriv python3; do\n+\t\tif [[ ! -x $(command -v \"${dep}\") ]]; then\n+\t\t\tlog_host \"no ${dep} for test ${tname}\"\n+\t\t\treturn 1\n+\t\tfi\n+\tdone\n+\n+\tif ! python3 -c \"import yaml\" \u0026\u003e/dev/null; then\n+\t\tlog_host \"no python3 yaml module for test ${tname}\"\n+\t\treturn 1\n+\tfi\n+\n+\tif [[ ! -r \"${YNL_CLI}\" ]] || [[ ! -r \"${VSOCK_SPEC}\" ]]; then\n+\t\tlog_host \"no ynl cli or vsock spec for test ${tname}\"\n+\t\treturn 1\n+\tfi\n+\n+\treturn 0\n+}\n+\n check_netns() {\n \tlocal tname=$1\n \n@@ -401,6 +483,19 @@ setup_home() {\n \tmkdir -p \"$(dirname \"${SSH_KEY_PATH}\")\"\n \tssh-keygen -t ed25519 -f \"${SSH_KEY_PATH}\" -N \"\" -q\n \tcp \"${VSOCK_TEST}\" \"${TEST_HOME}\"/vsock_test\n+\n+\tmkdir -p \"${TEST_HOME}\"/ynl\n+\tif [[ -r \"${YNL_CLI}\" ]]; then\n+\t\tcp \"${YNL_CLI}\" \"${TEST_HOME}\"/ynl/\n+\t\tcp -r \"${YNL_LIB}\" \"${TEST_HOME}\"/ynl/\n+\t\tcp \"${VSOCK_SPEC}\" \"${TEST_HOME}\"/ynl/\n+\tfi\n+\n+\t# One of the tests runs the CLI as an unprivileged user, so the CLI\n+\t# has to be reachable by one.\n+\tchmod -R a+rX \"${TEST_HOME}\"/ynl\n+\n+\tchmod 755 \"${TEST_HOME}\"\n }\n \n create_pidfile() {\n@@ -528,6 +623,82 @@ vm_wait_for_ssh() {\n \tdone\n }\n \n+# Create a local mode namespace in the VM and echo the pid holding it open.\n+vm_ns_start() {\n+\tlocal ns=$1\n+\tlocal initns\n+\tlocal nspid\n+\tlocal newns\n+\tlocal i\n+\n+\tlocal mode=/proc/sys/net/vsock/child_ns_mode\n+\n+\tif ! vm_ssh \"${ns}\" -- \"echo local \u003e ${mode}\" \u0026\u003e/dev/null; then\n+\t\treturn\n+\tfi\n+\n+\tnspid=$(vm_ssh \"${ns}\" -- \"unshare -n sleep infinity\" \\\n+\t\t'\u003e/dev/null 2\u003e\u00261 \u0026 echo $!')\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\treturn\n+\tfi\n+\n+\tinitns=$(vm_ssh \"${ns}\" -- \"readlink /proc/self/ns/net\")\n+\tfor ((i = 0; i \u003c WAIT_PERIOD_MAX; i++)); do\n+\t\tnewns=$(vm_ssh \"${ns}\" -- \"readlink /proc/${nspid}/ns/net\" \\\n+\t\t\t2\u003e/dev/null)\n+\t\tif [[ -n \"${newns}\" ]] \u0026\u0026 [[ \"${newns}\" != \"${initns}\" ]]; then\n+\t\t\techo \"${nspid}\"\n+\t\t\treturn\n+\t\tfi\n+\t\tsleep 1\n+\tdone\n+\n+\tvm_ns_stop \"${ns}\" \"${nspid}\"\n+}\n+\n+# Returns once the holder is gone, so that the namespace is unreferenced and\n+# the kernel can start tearing it down.\n+vm_ns_stop() {\n+\tlocal ns=$1\n+\tlocal nspid=$2\n+\n+\tvm_ssh \"${ns}\" \u003c\u003c-EOF \u0026\u003e/dev/null\n+\t\tkill ${nspid}\n+\t\tfor ((i = 0; i \u003c ${WAIT_PERIOD_MAX}; i++)); do\n+\t\t\tkill -0 ${nspid} 2\u003e/dev/null || break\n+\t\t\tsleep 1\n+\t\tdone\n+\tEOF\n+}\n+\n+# Runs in the guest's initial namespace when \u003cnspid\u003e is empty. The command must\n+# not contain single quotes.\n+vm_ns_exec() {\n+\tlocal ns=$1\n+\tlocal nspid=$2\n+\tlocal cmd=$3\n+\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\tvm_ssh \"${ns}\" -- \"${cmd}\"\n+\t\treturn\n+\tfi\n+\n+\tvm_ssh \"${ns}\" -- nsenter -t \"${nspid}\" -n sh -c \"'${cmd}'\"\n+}\n+\n+vm_ns_assign_g2h() {\n+\tlocal ns=$1\n+\tlocal nspid=$2\n+\n+\tvm_ns_exec \"${ns}\" \"${nspid}\" \"python3 /root/ynl/cli.py --no-schema \\\n+\t\t--spec /root/ynl/vsock.yaml --do dev-netns-set\"\n+}\n+\n+vm_reset_g2h() {\n+\tvm_ns_assign_g2h \"init_ns\" \"\" \u0026\u003e/dev/null\n+}\n+\n # derived from selftests/net/net_helper.sh\n wait_for_listener()\n {\n@@ -564,17 +735,33 @@ wait_for_listener()\n \tdone\n }\n \n-vm_wait_for_listener() {\n+# Runs in the guest's initial namespace when \u003cnspid\u003e is empty.\n+vm_ns_wait_for_listener() {\n \tlocal ns=$1\n-\tlocal port=$2\n-\tlocal protocol=$3\n+\tlocal nspid=$2\n+\tlocal port=$3\n+\tlocal protocol=$4\n+\tlocal nsenter=\n+\tlocal args\n+\n+\t[[ -n \"${nspid}\" ]] \u0026\u0026 nsenter=\"nsenter -t ${nspid} -n\"\n+\targs=\"${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol}\"\n \n \tvm_ssh \"${ns}\" \u003c\u003cEOF\n $(declare -f wait_for_listener)\n-wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol}\n+export -f wait_for_listener\n+${nsenter} bash -c \"wait_for_listener ${args}\"\n EOF\n }\n \n+vm_wait_for_listener() {\n+\tlocal ns=$1\n+\tlocal port=$2\n+\tlocal protocol=$3\n+\n+\tvm_ns_wait_for_listener \"${ns}\" \"\" \"${port}\" \"${protocol}\"\n+}\n+\n host_wait_for_listener() {\n \tlocal ns=$1\n \tlocal port=$2\n@@ -1421,6 +1608,355 @@ test_ns_delete_both_ok() {\n \tcheck_ns_delete_doesnt_break_connection \"both\"\n }\n \n+# Send a string from the guest to a host listener and leave what the host\n+# received in \u003coutfile\u003e.\n+guest_send_to_host() {\n+\tlocal ns=$1\n+\tlocal nspid=$2\n+\tlocal port=$3\n+\tlocal outfile=$4\n+\tlocal cmd=\"echo TEST | socat -u STDIN VSOCK-CONNECT:2:${port}\"\n+\tlocal pid\n+\n+\tsocat -u VSOCK-LISTEN:\"${port}\" STDOUT \u003e \"${outfile}\" 2\u003e/dev/null \u0026\n+\tpid=$!\n+\thost_wait_for_listener \"${ns}\" \"${port}\" \"vsock\"\n+\n+\tvm_ns_exec \"${ns}\" \"${nspid}\" \"${cmd}\" 2\u003e/dev/null\n+\n+\ttimeout \"${WAIT_PERIOD}\" \\\n+\t\tbash -c 'while [[ ! -s '\"${outfile}\"' ]]; do sleep 1; done'\n+\n+\t# Wait for the listener to actually go, or the next test binding the\n+\t# same port races it and silently reads nothing.\n+\tterminate_pids \"${pid}\"\n+\twait \"${pid}\" 2\u003e/dev/null\n+}\n+\n+# Send a string from the host to a listener in the guest and leave what the\n+# guest received in \u003coutfile\u003e.\n+host_send_to_guest() {\n+\tlocal ns=$1\n+\tlocal nspid=$2\n+\tlocal port=$3\n+\tlocal outfile=$4\n+\tlocal cmd=\"socat -u VSOCK-LISTEN:${port} STDOUT\"\n+\tlocal dst=\"VSOCK-CONNECT:${VSOCK_CID}:${port}\"\n+\tlocal pid\n+\n+\tvm_ns_exec \"${ns}\" \"${nspid}\" \"${cmd}\" \u003e \"${outfile}\" 2\u003e/dev/null \u0026\n+\tpid=$!\n+\tvm_ns_wait_for_listener \"${ns}\" \"${nspid}\" \"${port}\" \"vsock\"\n+\n+\techo TEST | socat -u STDIN \"${dst}\" 2\u003e/dev/null\n+\n+\ttimeout \"${WAIT_PERIOD}\" \\\n+\t\tbash -c 'while [[ ! -s '\"${outfile}\"' ]]; do sleep 1; done'\n+\n+\tvm_ns_exec \"${ns}\" \"${nspid}\" \\\n+\t\t\"pkill -f VSOCK-LISTEN:${port}\" \u0026\u003e/dev/null\n+\tterminate_pids \"${pid}\"\n+\twait \"${pid}\" 2\u003e/dev/null\n+}\n+\n+ns_old_conn_cleanup() {\n+\tlocal port=$1\n+\tlocal pid=$2\n+\tlocal sender=$3\n+\tlocal outfile=$4\n+\tlocal nspid=$5\n+\n+\tvm_ns_exec \"init_ns\" \"\" \"pkill -f VSOCK-CONNECT:2:${port}\" \u0026\u003e/dev/null\n+\tterminate_pids \"${pid}\" \"${sender}\"\n+\twait \"${pid}\" \"${sender}\" 2\u003e/dev/null\n+\trm -f \"${outfile}\"\n+\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\tvm_reset_g2h\n+}\n+\n+test_ns_guest_assign_g2h_netns_old_conn_send_fails() {\n+\tlocal gap=$(( WAIT_PERIOD * 3 ))\n+\tlocal port=12346\n+\tlocal elapsed\n+\tlocal started\n+\tlocal outfile\n+\tlocal result\n+\tlocal sender\n+\tlocal nspid\n+\tlocal pid\n+\n+\tnspid=$(vm_ns_start \"init_ns\")\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\tlog_host \"failed to create a namespace inside the guest\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\toutfile=$(mktemp)\n+\tsocat -u VSOCK-LISTEN:\"${port}\" STDOUT \u003e \"${outfile}\" 2\u003e/dev/null \u0026\n+\tpid=$!\n+\thost_wait_for_listener \"init_ns\" \"${port}\" \"vsock\"\n+\n+\t# Send a message, wait, then send another. While waiting, assign the\n+\t# device to a namespace. Confirm the second message does not arrive.\n+\tstarted=${SECONDS}\n+\tvm_ssh \"init_ns\" -- \\\n+\t\t\"(echo FIRST; sleep ${gap}; echo SECOND) |\" \\\n+\t\t\"socat -u STDIN VSOCK-CONNECT:2:${port}\" \u0026\u003e/dev/null \u0026\n+\tsender=$!\n+\n+\ttimeout \"$(( WAIT_PERIOD * WAIT_PERIOD_MAX ))\" \\\n+\t\tbash -c 'while [[ ! -s '\"${outfile}\"' ]]; do sleep 1; done'\n+\n+\tif ! vm_ns_assign_g2h \"init_ns\" \"${nspid}\"; then\n+\t\tlog_host \"failed to assign the vsock device to the guest ns\"\n+\t\tns_old_conn_cleanup \"${port}\" \"${pid}\" \"${sender}\" \\\n+\t\t\t\"${outfile}\" \"${nspid}\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\telapsed=$(( SECONDS - started ))\n+\n+\tsleep $(( gap + WAIT_PERIOD ))\n+\n+\tresult=$(cat \"${outfile}\")\n+\tns_old_conn_cleanup \"${port}\" \"${pid}\" \"${sender}\" \\\n+\t\t\"${outfile}\" \"${nspid}\"\n+\n+\tif [[ \"${result}\" != *FIRST* ]]; then\n+\t\tlog_host \"no connection before the assign: [${result}]\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\tif [[ $(( elapsed + 1 )) -ge ${gap} ]]; then\n+\t\tlog_host \"connect and assign took ${elapsed}s of a ${gap}s gap\"\n+\t\treturn \"${KSFT_SKIP}\"\n+\tfi\n+\n+\tif [[ \"${result}\" == *SECOND* ]]; then\n+\t\tlog_host \"old connection still delivered after the assign\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+test_ns_guest_local_connect_to_host_fails() {\n+\tlocal port=12345\n+\tlocal outfile\n+\tlocal result\n+\tlocal nspid\n+\n+\tnspid=$(vm_ns_start \"init_ns\")\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\tlog_host \"failed to create a namespace inside the guest\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\toutfile=$(mktemp)\n+\tguest_send_to_host \"init_ns\" \"${nspid}\" \"${port}\" \"${outfile}\"\n+\n+\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\tvm_reset_g2h\n+\n+\tresult=$(cat \"${outfile}\")\n+\trm -f \"${outfile}\"\n+\n+\tif [[ \"${result}\" == TEST ]]; then\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+test_ns_guest_assign_g2h_netns_connect_to_host_ok() {\n+\tlocal port=12345\n+\tlocal outfile\n+\tlocal result\n+\tlocal nspid\n+\n+\tnspid=$(vm_ns_start \"init_ns\")\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\tlog_host \"failed to create a namespace inside the guest\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\tif ! vm_ns_assign_g2h \"init_ns\" \"${nspid}\"; then\n+\t\tlog_host \"failed to assign the vsock device to the guest ns\"\n+\t\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\t\tvm_reset_g2h\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\toutfile=$(mktemp)\n+\tguest_send_to_host \"init_ns\" \"${nspid}\" \"${port}\" \"${outfile}\"\n+\n+\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\tvm_reset_g2h\n+\n+\tresult=$(cat \"${outfile}\")\n+\trm -f \"${outfile}\"\n+\n+\tif [[ \"${result}\" != TEST ]]; then\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+test_ns_guest_assign_g2h_netns_init_ns_connect_fails() {\n+\tlocal port=12345\n+\tlocal outfile\n+\tlocal result\n+\tlocal nspid\n+\n+\tnspid=$(vm_ns_start \"init_ns\")\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\tlog_host \"failed to create a namespace inside the guest\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\tif ! vm_ns_assign_g2h \"init_ns\" \"${nspid}\"; then\n+\t\tlog_host \"failed to assign the vsock device to the guest ns\"\n+\t\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\t\tvm_reset_g2h\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\toutfile=$(mktemp)\n+\tguest_send_to_host \"init_ns\" \"\" \"${port}\" \"${outfile}\"\n+\n+\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\tvm_reset_g2h\n+\n+\tresult=$(cat \"${outfile}\")\n+\trm -f \"${outfile}\"\n+\n+\tif [[ \"${result}\" == TEST ]]; then\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+test_ns_guest_assign_g2h_netns_host_connect_ok() {\n+\tlocal port=12345\n+\tlocal outfile\n+\tlocal result\n+\tlocal nspid\n+\n+\tnspid=$(vm_ns_start \"init_ns\")\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\tlog_host \"failed to create a namespace inside the guest\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\tif ! vm_ns_assign_g2h \"init_ns\" \"${nspid}\"; then\n+\t\tlog_host \"failed to assign the vsock device to the guest ns\"\n+\t\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\t\tvm_reset_g2h\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\toutfile=$(mktemp)\n+\thost_send_to_guest \"init_ns\" \"${nspid}\" \"${port}\" \"${outfile}\"\n+\n+\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\tvm_reset_g2h\n+\n+\tresult=$(cat \"${outfile}\")\n+\trm -f \"${outfile}\"\n+\n+\tif [[ \"${result}\" != TEST ]]; then\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+test_ns_guest_assign_g2h_netns_pins_ns_ok() {\n+\tlocal port=12345\n+\tlocal outfile\n+\tlocal result\n+\tlocal nspid\n+\n+\tnspid=$(vm_ns_start \"init_ns\")\n+\tif [[ -z \"${nspid}\" ]]; then\n+\t\tlog_host \"failed to create a namespace inside the guest\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\tif ! vm_ns_assign_g2h \"init_ns\" \"${nspid}\"; then\n+\t\tlog_host \"failed to assign the vsock device to the guest ns\"\n+\t\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\t\tvm_reset_g2h\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\tvm_ns_stop \"init_ns\" \"${nspid}\"\n+\tsleep \"${WAIT_PERIOD}\"\n+\n+\toutfile=$(mktemp)\n+\tguest_send_to_host \"init_ns\" \"\" \"${port}\" \"${outfile}\"\n+\tresult=$(cat \"${outfile}\")\n+\tif [[ \"${result}\" == TEST ]]; then\n+\t\tlog_host \"device returned to the init ns instead of pinning it\"\n+\t\trm -f \"${outfile}\"\n+\t\tvm_reset_g2h\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\t# Assigning it back is what releases the deleted namespace.\n+\tvm_reset_g2h\n+\n+\tguest_send_to_host \"init_ns\" \"\" \"$(( port + 1 ))\" \"${outfile}\"\n+\tresult=$(cat \"${outfile}\")\n+\trm -f \"${outfile}\"\n+\n+\tif [[ \"${result}\" != TEST ]]; then\n+\t\tlog_host \"device did not come back on reassign: [${result}]\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+check_assign_g2h_eperm() {\n+\tlocal wrapper=$1\n+\tlocal out\n+\n+\tif out=$(vm_ssh \"init_ns\" -- \"${wrapper}\" python3 /root/ynl/cli.py \\\n+\t\t--no-schema --spec /root/ynl/vsock.yaml \\\n+\t\t--do dev-netns-set 2\u003e\u00261); then\n+\t\tlog_host \"assign unexpectedly succeeded under '${wrapper}'\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\tif [[ \"${out}\" != *\"Operation not permitted\"* ]]; then\n+\t\tlog_host \"no EPERM under '${wrapper}': [${out}]\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+test_ns_guest_assign_g2h_netns_no_cap_net_admin_fails() {\n+\tcheck_assign_g2h_eperm \"unshare -n setpriv --bounding-set=-net_admin\"\n+}\n+\n+test_ns_guest_assign_g2h_netns_unpriv_user_ns_fails() {\n+\tlocal unpriv_uid=65534\n+\tlocal unpriv\n+\n+\tunpriv=\"setpriv --reuid=${unpriv_uid} --regid=${unpriv_uid}\"\n+\tunpriv=\"${unpriv} --clear-groups\"\n+\n+\tif ! vm_ssh \"init_ns\" -- \"${unpriv} unshare -Urn true\"; then\n+\t\tlog_host \"unprivileged user namespaces unavailable, skipping\"\n+\t\treturn \"${KSFT_SKIP}\"\n+\tfi\n+\n+\tcheck_assign_g2h_eperm \"${unpriv} unshare -Urn\"\n+}\n+\n shared_vm_test() {\n \tlocal tname\n \n@@ -1453,7 +1989,8 @@ run_shared_vm_tests() {\n \t\t\tcontinue\n \t\tfi\n \n-\t\tif ! check_netns \"${arg}\"; then\n+\t\tif ! check_netns \"${arg}\" ||\n+\t\t   ! check_ns_guest_deps \"${arg}\"; then\n \t\t\tcheck_result \"${KSFT_SKIP}\" \"${arg}\"\n \t\t\tcontinue\n \t\tfi\n@@ -1517,7 +2054,8 @@ run_ns_tests() {\n \t\t\tcontinue\n \t\tfi\n \n-\t\tif ! check_netns \"${arg}\"; then\n+\t\tif ! check_netns \"${arg}\" ||\n+\t\t   ! check_ns_guest_deps \"${arg}\"; then\n \t\t\tcheck_result \"${KSFT_SKIP}\" \"${arg}\"\n \t\t\tcontinue\n \t\tfi\n"
}

2/1 2026/10/21 10:16 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "VSOCKETS",
    "VIRTIO_VSOCKETS",
    "VHOST_VSOCK",
    "NET_NS"
  ],
  "FocusSymbols": [
    "vsock_nl_dev_netns_set_doit",
    "vsock_nl_dev_netns_get_doit",
    "vsock_g2h_net_assign",
    "vsock_g2h_reset_unreachable",
    "vsock_maybe_set_connected",
    "vsock_g2h_net_reachable",
    "vsock_find_cid",
    "vhost_vsock_set_cid",
    "virtio_transport_rx_work"
  ],
  "Reasoning": "The patch introduces a Generic Netlink interface (\"vsock\") to query and move the guest-to-host (G2H) vsock device across network namespaces. It introduces new locking (vsock_g2h_assign_mutex, vsock_register_mutex), namespace reference tracking (netns_tracker), connection validation (vsock_maybe_set_connected), and an active sweep that resets connected sockets that lose access to the G2H device (vsock_g2h_reset_unreachable). These modifications touch core AF_VSOCK and virtio/vhost transport logic reachable via unprivileged/privileged netlink messages and socket syscalls in virtualized QEMU environments.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 6d3d4d152a343e619168df57f7806299fa392909
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Oct 21 10:16:14 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst
index fe43e85959583..eb60903e4eb23 100644
--- a/Documentation/admin-guide/sysctl/net.rst
+++ b/Documentation/admin-guide/sysctl/net.rst
@@ -529,6 +529,48 @@ their hosts. The behavior of VSOCK sockets in a network namespace is determined
 by the namespace's mode (``global`` or ``local``), which controls how CIDs
 (Context IDs) are allocated and how sockets interact across namespaces.
 
+In a guest, the vsock device owned by the guest-to-host (G2H) transport belongs
+to one network namespace at a time. The ``VSOCK_CMD_DEV_NETNS_SET`` command of
+the ``vsock`` netlink family, described in
+Documentation/netlink/specs/vsock.yaml, moves it to the namespace the request
+was sent from, which requires ``CAP_NET_ADMIN`` in the initial user namespace.
+The namespace's mode decides who may then use the device:
+
+- ``global`` - every ``global`` mode namespace may use it.
+- ``local`` - only that namespace may use it, which reserves the connection to
+  the host for it alone.
+
+The device starts out in init_net, so until the command is issued nothing has
+moved and no mode has changed.
+
+Support is transport dependent. A G2H transport that does not implement the
+move refuses the command with ``EOPNOTSUPP``. Of the in-tree guest transports,
+only virtio-vsock implements it.
+
+A namespace that cannot reach the device sees the vsock addresses it would see
+if no G2H transport were loaded: the device's CID is not one of its local CIDs,
+and with ``vsock_loopback`` loaded, ``VMADDR_CID_HOST`` (2) is served by
+loopback.
+
+Connections established before the move, from a namespace that can no longer
+reach the device, are closed with ``ECONNRESET``. Nothing is sent to the peer,
+which is what already happens when the device is removed altogether: the peer
+finds out on its next send, which no longer matches a socket and so is answered
+with a reset. A peer that only receives waits for its own timeout. A connection
+still being set up when the device moves is refused with a reset once the peer
+answers it.
+
+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.
+
+The move is refused with ``EADDRINUSE`` if a guest registered in the target
+namespace already answers to the device's CID, since the CID would otherwise
+name two different peers there. If a loaded H2G transport cannot check that,
+the move is refused with ``EOPNOTSUPP``. Of the in-tree host transports, only
+vhost-vsock can.
+
 ns_mode
 -------
 
diff --git a/Documentation/netlink/specs/vsock.yaml b/Documentation/netlink/specs/vsock.yaml
new file mode 100644
index 0000000000000..74cf2472bf917
--- /dev/null
+++ b/Documentation/netlink/specs/vsock.yaml
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+#
+# Copyright (c) 2026 Meta Platforms, Inc. and affiliates
+#
+---
+name: vsock
+
+doc: |
+  Control interface for the vsock (AF_VSOCK) core.
+
+protocol: genetlink
+
+uapi-header: linux/vsock.h
+
+attribute-sets:
+  -
+    name: vsock
+    attributes:
+      -
+        name: netns-id
+        type: s32
+        doc: |
+          Network namespace the guest's vsock device is assigned to, as an
+          nsid relative to the caller.
+
+          Absent when the device is in the caller's own namespace, and
+          NETNSA_NSID_NOT_ASSIGNED when the g2h transport supports netns
+          assignment and the caller's namespace cannot reach the device under
+          the mode rules, or when no nsid could be allocated for it.
+
+operations:
+  list:
+    -
+      name: dev-netns-set
+      doc: |
+        Set the network namespace of the guest's vsock device.
+
+        The device is moved to the namespace the request was sent from. It
+        starts out in init_net, and moving it back there is how an
+        assignment is undone.
+
+        A g2h transport that does not support netns assignment returns
+        EOPNOTSUPP. If the netns assignment would cause a CID collision, it
+        is rejected with EADDRINUSE.
+
+        The namespace's mode decides who may then use the device: a global
+        mode namespace shares it with every other global mode namespace,
+        while a local mode namespace reserves it for itself.
+
+        Movement to an unreachable local namespace results in sockets being
+        closed with ECONNRESET. Peers are not guaranteed to be notified via
+        an RST.
+
+        The assignment holds a reference on the namespace.
+      attribute-set: vsock
+      flags: [admin-perm]
+      do:
+        reply:
+          attributes:
+            - netns-id
+    -
+      name: dev-netns-get
+      doc: |
+        Get the network namespace the guest's vsock device is assigned to.
+
+        The namespace is reported as an nsid relative to the caller, so the
+        attribute is absent when the device is already in the caller's own
+        namespace. If the g2h transport supports netns assignment, a caller
+        whose namespace cannot reach the device is told
+        NETNSA_NSID_NOT_ASSIGNED rather than where the device went.
+      attribute-set: vsock
+      do:
+        reply:
+          attributes:
+            - netns-id
diff --git a/MAINTAINERS b/MAINTAINERS
index 7636ab50d44fb..6bca71c935cd0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -29169,10 +29169,12 @@ M:	Stefano Garzarella <sgarzare@redhat.com>
 L:	virtualization@lists.linux.dev
 L:	netdev@vger.kernel.org
 S:	Maintained
+F:	Documentation/netlink/specs/vsock.yaml
 F:	drivers/net/vsockmon.c
 F:	include/net/af_vsock.h
 F:	include/uapi/linux/vm_sockets.h
 F:	include/uapi/linux/vm_sockets_diag.h
+F:	include/uapi/linux/vsock.h
 F:	include/uapi/linux/vsockmon.h
 F:	net/vmw_vsock/
 F:	tools/testing/selftests/vsock/
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index abed1fbcf66cc..11893f1943c69 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -91,10 +91,8 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net)
 	return NULL;
 }
 
-static bool vhost_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid)
+static bool vhost_transport_has_remote_cid(struct net *net, u32 cid)
 {
-	struct sock *sk = sk_vsock(vsk);
-	struct net *net = sock_net(sk);
 	bool found;
 
 	rcu_read_lock();
@@ -817,6 +815,7 @@ static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
 static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 {
 	struct vhost_vsock *other;
+	int ret;
 
 	/* Refuse reserved CIDs */
 	if (guest_cid <= VMADDR_CID_HOST ||
@@ -830,15 +829,20 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	/* Refuse if CID is assigned to the guest->host transport (i.e. nested
 	 * VM), to make the loopback work.
 	 */
-	if (vsock_find_cid(guest_cid))
-		return -EADDRINUSE;
+	mutex_lock(&vsock_register_mutex);
+
+	if (__vsock_find_cid(vsock->net, guest_cid)) {
+		ret = -EADDRINUSE;
+		goto unlock;
+	}
 
 	/* Refuse if CID is already in use */
 	mutex_lock(&vhost_vsock_mutex);
 	other = vhost_vsock_get(guest_cid, vsock->net);
 	if (other && other != vsock) {
 		mutex_unlock(&vhost_vsock_mutex);
-		return -EADDRINUSE;
+		ret = -EADDRINUSE;
+		goto unlock;
 	}
 
 	if (vsock->guest_cid)
@@ -848,7 +852,12 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
 	mutex_unlock(&vhost_vsock_mutex);
 
-	return 0;
+	ret = 0;
+
+unlock:
+	mutex_unlock(&vsock_register_mutex);
+
+	return ret;
 }
 
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 5549298c1ec6b..883a98dab36d4 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -9,6 +9,7 @@
 #define __AF_VSOCK_H__
 
 #include <linux/kernel.h>
+#include <linux/mutex.h>
 #include <linux/workqueue.h>
 #include <net/netns/vsock.h>
 #include <net/sock.h>
@@ -22,6 +23,7 @@
 extern struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
 extern struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
 extern spinlock_t vsock_table_lock;
+extern struct mutex vsock_register_mutex;
 
 #define vsock_sk(__sk)    ((struct vsock_sock *)__sk)
 #define sk_vsock(__vsk)   (&(__vsk)->sk)
@@ -183,13 +185,16 @@ struct vsock_transport {
 	 * CIDs via the hypervisor (i.e. supports the fallback overlay). VMCI
 	 * does not implement this as it only serves CIDs 0 and 2.
 	 */
-	bool (*has_remote_cid)(struct vsock_sock *vsk, u32 remote_cid);
+	bool (*has_remote_cid)(struct net *net, u32 remote_cid);
 
 	/* Read a single skb */
 	int (*read_skb)(struct vsock_sock *, skb_read_actor_t);
 
 	/* Zero-copy. */
 	bool (*msgzerocopy_allow)(void);
+
+	/* True if the G2H transport honours VSOCK_CMD_DEV_NETNS_SET. */
+	bool netns_assign_allow;
 };
 
 /**** CORE ****/
@@ -233,11 +238,17 @@ bool vsock_check_source(const struct vsock_sock *vsk,
 			const struct vsock_transport *transport,
 			const struct sockaddr_vm *src);
 void vsock_remove_sock(struct vsock_sock *vsk);
-void vsock_for_each_connected_socket(struct vsock_transport *transport,
+void vsock_for_each_connected_socket(const struct vsock_transport *transport,
 				     void (*fn)(struct sock *sk));
 int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);
-bool vsock_find_cid(unsigned int cid);
+bool vsock_find_cid(struct net *net, unsigned int cid);
+/* Caller must hold vsock_register_mutex. */
+bool __vsock_find_cid(struct net *net, unsigned int cid);
 void vsock_linger(struct sock *sk);
+struct net *vsock_g2h_net_get(netns_tracker *tracker);
+bool vsock_g2h_net_eq(const struct net *net);
+bool vsock_g2h_net_reachable(struct net *net);
+bool vsock_maybe_set_connected(struct vsock_sock *vsk);
 
 /**** TAP ****/
 
diff --git a/include/uapi/linux/vsock.h b/include/uapi/linux/vsock.h
new file mode 100644
index 0000000000000..803b9aa1f0b3a
--- /dev/null
+++ b/include/uapi/linux/vsock.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/vsock.yaml */
+/* YNL-GEN uapi header */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
+
+#ifndef _UAPI_LINUX_VSOCK_H
+#define _UAPI_LINUX_VSOCK_H
+
+#define VSOCK_FAMILY_NAME	"vsock"
+#define VSOCK_FAMILY_VERSION	1
+
+enum {
+	VSOCK_A_NETNS_ID = 1,
+
+	__VSOCK_A_MAX,
+	VSOCK_A_MAX = (__VSOCK_A_MAX - 1)
+};
+
+enum {
+	VSOCK_CMD_DEV_NETNS_SET = 1,
+	VSOCK_CMD_DEV_NETNS_GET,
+
+	__VSOCK_CMD_MAX,
+	VSOCK_CMD_MAX = (__VSOCK_CMD_MAX - 1)
+};
+
+#endif /* _UAPI_LINUX_VSOCK_H */
diff --git a/net/vmw_vsock/Makefile b/net/vmw_vsock/Makefile
index 5da74c4a9f1d1..97e2a559e2bf3 100644
--- a/net/vmw_vsock/Makefile
+++ b/net/vmw_vsock/Makefile
@@ -7,7 +7,7 @@ obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o
 obj-$(CONFIG_HYPERV_VSOCKETS) += hv_sock.o
 obj-$(CONFIG_VSOCKETS_LOOPBACK) += vsock_loopback.o
 
-vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o
+vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o vsock_nl_gen.o
 vsock-$(CONFIG_BPF_SYSCALL) += vsock_bpf.o
 
 vsock_diag-y += diag.o
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 483af15a5109e..e16e72471b4a3 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -130,6 +130,27 @@
  *       a different transport that *does* support local mode. For
  *       example, virtio-vsock may not support local mode, but the socket
  *       may still accept a connection from vhost-vsock which does.
+ *
+ * - A guest has a single vsock device, owned by the guest->host transport.
+ *   The VSOCK_CMD_DEV_NETNS_SET netlink command moves it to the namespace the
+ *   request was sent from. It starts out in init_net. The mode rules then
+ *   decide who may use it, and which namespace packets from the host are
+ *   delivered to:
+ *
+ *   - assigned to a global mode namespace - every global mode namespace may
+ *     use it. Until the command is issued nothing has moved and no mode has
+ *     changed, so the default is the behaviour that predates it.
+ *   - assigned to a local mode namespace - only that namespace may use it.
+ *     This is how a nested VM is isolated from the rest of the guest.
+ *
+ *   Connections made before an assignment, from a namespace that can no
+ *   longer reach the device, are closed with ECONNRESET. Nothing is sent to
+ *   the peer. As with device removal, it finds out on its next send.
+ *
+ *   The assignment holds a reference on the namespace, the way an open socket
+ *   does, so deleting that namespace does not move the device: its destruction
+ *   is deferred until the device moves to another namespace, on another
+ *   assignment or when the transport unregisters.
  */
 
 #include <linux/compat.h>
@@ -161,10 +182,14 @@
 #include <linux/workqueue.h>
 #include <net/sock.h>
 #include <net/af_vsock.h>
+#include <linux/net_namespace.h>
+#include <net/genetlink.h>
 #include <net/netns/vsock.h>
 #include <uapi/linux/vm_sockets.h>
 #include <uapi/asm-generic/ioctls.h>
 
+#include "vsock_nl_gen.h"
+
 #define VSOCK_NET_MODE_STR_GLOBAL "global"
 #define VSOCK_NET_MODE_STR_LOCAL "local"
 
@@ -206,7 +231,21 @@ static const struct vsock_transport *transport_g2h;
 static const struct vsock_transport *transport_dgram;
 /* Transport used for local communication */
 static const struct vsock_transport *transport_local;
-static DEFINE_MUTEX(vsock_register_mutex);
+/* Protects transport registration and the g2h device's netns. */
+DEFINE_MUTEX(vsock_register_mutex);
+EXPORT_SYMBOL_GPL(vsock_register_mutex);
+
+/* Network namespace of the g2h device. Protected by
+ * vsock_register_mutex/RCU.
+ */
+static struct net __rcu *vsock_g2h_net = RCU_INITIALIZER(&init_net);
+static netns_tracker vsock_g2h_net_tracker;
+
+/* Held across an assign's swap and reset sweep, so that a later assign cannot
+ * move the device back before the sweep has closed every socket that lost it.
+ * Must be taken before vsock_register_mutex.
+ */
+static DEFINE_MUTEX(vsock_g2h_assign_mutex);
 
 /**** UTILS ****/
 
@@ -480,7 +519,7 @@ void vsock_remove_sock(struct vsock_sock *vsk)
 }
 EXPORT_SYMBOL_GPL(vsock_remove_sock);
 
-void vsock_for_each_connected_socket(struct vsock_transport *transport,
+void vsock_for_each_connected_socket(const struct vsock_transport *transport,
 				     void (*fn)(struct sock *sk))
 {
 	int i;
@@ -553,7 +592,42 @@ void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
 }
 EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
 
-static bool vsock_use_local_transport(unsigned int remote_cid)
+/* Return true if @t honours namespace assignment. One that does not keeps the
+ * reachability rules it had before VSOCK_CMD_DEV_NETNS_SET existed.
+ */
+static bool vsock_net_assignable(const struct vsock_transport *t)
+{
+	return t && t->netns_assign_allow;
+}
+
+static bool vsock_g2h_unreachable(const struct vsock_transport *t,
+				  struct net *net)
+{
+	return t == transport_g2h && vsock_net_assignable(t) &&
+	       !vsock_g2h_net_reachable(net);
+}
+
+/* Return the CID of the transport in @transport, as seen from @net.
+ *
+ * Returns VMADDR_CID_ANY if the transport supports net assignment but is not
+ * reachable from the net.
+ */
+static u32
+__vsock_registered_transport_cid(const struct vsock_transport **transport,
+				 struct net *net)
+{
+	lockdep_assert_held(&vsock_register_mutex);
+
+	if (!*transport)
+		return VMADDR_CID_ANY;
+
+	if (vsock_g2h_unreachable(*transport, net))
+		return VMADDR_CID_ANY;
+
+	return (*transport)->get_local_cid();
+}
+
+static bool vsock_use_local_transport(struct net *net, unsigned int remote_cid)
 {
 	lockdep_assert_held(&vsock_register_mutex);
 
@@ -563,11 +637,10 @@ static bool vsock_use_local_transport(unsigned int remote_cid)
 	if (remote_cid == VMADDR_CID_LOCAL)
 		return true;
 
-	if (transport_g2h) {
+	if (transport_g2h && !vsock_g2h_unreachable(transport_g2h, net))
 		return remote_cid == transport_g2h->get_local_cid();
-	} else {
-		return remote_cid == VMADDR_CID_HOST;
-	}
+
+	return remote_cid == VMADDR_CID_HOST;
 }
 
 static void vsock_deassign_transport(struct vsock_sock *vsk)
@@ -626,18 +699,20 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
 		break;
 	case SOCK_STREAM:
 	case SOCK_SEQPACKET:
-		if (vsock_use_local_transport(remote_cid))
+		if (vsock_use_local_transport(sock_net(sk), remote_cid))
 			new_transport = transport_local;
 		else if (remote_cid <= VMADDR_CID_HOST ||
 			 (remote_flags & VMADDR_FLAG_TO_HOST))
 			new_transport = transport_g2h;
 		else if (transport_h2g &&
 			 (!transport_h2g->has_remote_cid ||
-			  transport_h2g->has_remote_cid(vsk, remote_cid)))
+			  transport_h2g->has_remote_cid(sock_net(sk),
+							remote_cid)))
 			new_transport = transport_h2g;
 		else if (sock_net(sk)->vsock.g2h_fallback &&
 			 transport_g2h && transport_g2h->has_remote_cid &&
-			 transport_g2h->has_remote_cid(vsk, remote_cid)) {
+			 transport_g2h->has_remote_cid(sock_net(sk),
+						       remote_cid)) {
 			vsk->remote_addr.svm_flags |= VMADDR_FLAG_TO_HOST;
 			new_transport = transport_g2h;
 		} else {
@@ -649,6 +724,11 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
 		goto err;
 	}
 
+	if (vsock_g2h_unreachable(new_transport, sock_net(sk))) {
+		ret = -ENETUNREACH;
+		goto err;
+	}
+
 	if (vsk->transport && vsk->transport == new_transport) {
 		ret = 0;
 		goto err;
@@ -715,21 +795,24 @@ EXPORT_SYMBOL_GPL(vsock_assign_transport);
  * Provide safe access to static transport_{h2g,g2h,dgram,local} callbacks.
  * Otherwise we may race with module removal. Do not use on `vsk->transport`.
  */
-static u32 vsock_registered_transport_cid(const struct vsock_transport **transport)
+static u32
+vsock_registered_transport_cid(const struct vsock_transport **transport,
+			       struct net *net)
 {
-	u32 cid = VMADDR_CID_ANY;
+	u32 cid;
 
 	mutex_lock(&vsock_register_mutex);
-	if (*transport)
-		cid = (*transport)->get_local_cid();
+	cid = __vsock_registered_transport_cid(transport, net);
 	mutex_unlock(&vsock_register_mutex);
 
 	return cid;
 }
 
-bool vsock_find_cid(unsigned int cid)
+bool __vsock_find_cid(struct net *net, unsigned int cid)
 {
-	if (cid == vsock_registered_transport_cid(&transport_g2h))
+	lockdep_assert_held(&vsock_register_mutex);
+
+	if (cid == __vsock_registered_transport_cid(&transport_g2h, net))
 		return true;
 
 	if (transport_h2g && cid == VMADDR_CID_HOST)
@@ -740,8 +823,303 @@ 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 closes 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);
+
+static bool vsock_g2h_should_reset(struct vsock_sock *vsk,
+				   const struct vsock_transport *t)
+{
+	return vsk->transport == t &&
+	       READ_ONCE(sk_vsock(vsk)->sk_state) != TCP_CLOSE &&
+	       !READ_ONCE(vsk->close_work_scheduled) &&
+	       !vsock_g2h_reachable_sk(vsk);
+}
+
+static struct vsock_sock *
+vsock_g2h_next_unreachable(const struct vsock_transport *t, unsigned int *i)
+{
+	struct vsock_sock *vsk;
+
+	spin_lock_bh(&vsock_table_lock);
+
+	for (; *i < ARRAY_SIZE(vsock_connected_table); (*i)++) {
+		list_for_each_entry(vsk, &vsock_connected_table[*i],
+				    connected_table) {
+			if (vsock_g2h_should_reset(vsk, t)) {
+				sock_hold(sk_vsock(vsk));
+				spin_unlock_bh(&vsock_table_lock);
+				return vsk;
+			}
+		}
+	}
+
+	spin_unlock_bh(&vsock_table_lock);
+
+	return NULL;
+}
+
+/* Close every connected socket of @t that can no longer reach the g2h device.
+ */
+static void vsock_g2h_reset_unreachable(const struct vsock_transport *t)
+{
+	struct vsock_sock *vsk;
+	unsigned int i = 0;
+	struct sock *sk;
+	bool reset;
+
+	lockdep_assert_held(&vsock_g2h_assign_mutex);
+	lockdep_assert_not_held(&vsock_register_mutex);
+
+	while ((vsk = vsock_g2h_next_unreachable(t, &i))) {
+		sk = sk_vsock(vsk);
+
+		lock_sock(sk);
+
+		reset = false;
+		spin_lock_bh(&vsock_table_lock);
+		if (__vsock_in_connected_table(vsk) &&
+		    vsock_g2h_should_reset(vsk, t)) {
+			__vsock_remove_connected(vsk);
+			reset = true;
+		}
+		spin_unlock_bh(&vsock_table_lock);
+
+		if (reset) {
+			sk->sk_state = TCP_CLOSE;
+			sk->sk_err = ECONNRESET;
+			sk_error_report(sk);
+		}
+
+		release_sock(sk);
+		sock_put(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 *net = genl_info_net(info);
+	struct net *assigned;
+	bool equal;
+	void *hdr;
+	s32 id;
+
+	lockdep_assert_held(&vsock_register_mutex);
+
+	rcu_read_lock();
+	assigned = rcu_dereference(vsock_g2h_net);
+	equal = net_eq(net, assigned);
+	if (!equal) {
+		if (vsock_net_assignable(transport_g2h) &&
+		    !vsock_net_check_mode(net, assigned))
+			id = NETNSA_NSID_NOT_ASSIGNED;
+		else
+			id = peernet2id_alloc(net, assigned, GFP_ATOMIC);
+	}
+	rcu_read_unlock();
+
+	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;
+	const struct vsock_transport *t;
+	netns_tracker old_tracker;
+	struct net *old;
+	void *hdr;
+	int ret;
+
+	mutex_lock(&vsock_g2h_assign_mutex);
+	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 = -EMSGSIZE;
+	hdr = genlmsg_iput(msg, info);
+	if (!hdr)
+		goto unlock;
+	genlmsg_end(msg, hdr);
+
+	ret = genlmsg_reply(msg, info);
+	msg = NULL;
+	if (ret)
+		goto unlock;
+
+	old = vsock_g2h_net_swap(net, &old_tracker);
+	t = transport_g2h;
+
+	mutex_unlock(&vsock_register_mutex);
+
+	/* See vsock_maybe_set_connected() comment about synchronizing with
+	 * connecting sockets.
+	 */
+	vsock_g2h_reset_unreachable(t);
+
+	mutex_unlock(&vsock_g2h_assign_mutex);
+
+	vsock_g2h_net_release(old, &old_tracker);
+
+	return 0;
+
+unlock:
+	mutex_unlock(&vsock_register_mutex);
+	mutex_unlock(&vsock_g2h_assign_mutex);
+	nlmsg_free(msg);
+
+	return ret;
+}
+
 static struct sock *vsock_dequeue_accept(struct sock *listener)
 {
 	struct vsock_sock *vlistener;
@@ -908,7 +1286,8 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
 	 * like AF_INET prevents binding to a non-local IP address (in most
 	 * cases), we only allow binding to a local CID.
 	 */
-	if (addr->svm_cid != VMADDR_CID_ANY && !vsock_find_cid(addr->svm_cid))
+	if (addr->svm_cid != VMADDR_CID_ANY &&
+	    !vsock_find_cid(sock_net(sk_vsock(vsk)), addr->svm_cid))
 		return -EADDRNOTAVAIL;
 
 	switch (sk->sk_socket->type) {
@@ -2796,6 +3175,7 @@ static long vsock_dev_do_ioctl(struct file *filp,
 {
 	u32 __user *p = ptr;
 	int retval = 0;
+	struct net *net;
 	u32 cid;
 
 	switch (cmd) {
@@ -2803,11 +3183,14 @@ static long vsock_dev_do_ioctl(struct file *filp,
 		/* To be compatible with the VMCI behavior, we prioritize the
 		 * guest CID instead of well-know host CID (VMADDR_CID_HOST).
 		 */
-		cid = vsock_registered_transport_cid(&transport_g2h);
+		net = current->nsproxy->net_ns;
+		cid = vsock_registered_transport_cid(&transport_g2h, net);
 		if (cid == VMADDR_CID_ANY)
-			cid = vsock_registered_transport_cid(&transport_h2g);
+			cid = vsock_registered_transport_cid(&transport_h2g,
+							     net);
 		if (cid == VMADDR_CID_ANY)
-			cid = vsock_registered_transport_cid(&transport_local);
+			cid = vsock_registered_transport_cid(&transport_local,
+							     net);
 
 		if (put_user(cid, p) != 0)
 			retval = -EFAULT;
@@ -2848,6 +3231,44 @@ static struct miscdevice vsock_device = {
 	.fops		= &vsock_device_ops,
 };
 
+int vsock_nl_dev_netns_get_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	struct sk_buff *msg;
+	int err;
+
+	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+
+	mutex_lock(&vsock_register_mutex);
+	if (transport_g2h) {
+		err = vsock_nl_dev_netns_fill(msg, info);
+	} else {
+		NL_SET_ERR_MSG(info->extack,
+			       "no guest-to-host transport is loaded");
+		err = -ENODEV;
+	}
+	mutex_unlock(&vsock_register_mutex);
+
+	if (err) {
+		nlmsg_free(msg);
+		return err;
+	}
+
+	return genlmsg_reply(msg, info);
+}
+
+int vsock_nl_dev_netns_set_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	struct sk_buff *msg;
+
+	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+
+	return vsock_g2h_net_assign(genl_info_net(info), msg, info);
+}
+
 static int __vsock_net_mode_string(const struct ctl_table *table, int write,
 				   void *buffer, size_t *lenp, loff_t *ppos,
 				   enum vsock_net_mode mode,
@@ -3037,7 +3458,7 @@ static void vsock_net_init(struct net *net)
 	net->vsock.g2h_fallback = 1;
 }
 
-static __net_init int vsock_sysctl_init_net(struct net *net)
+static __net_init int vsock_pernet_init(struct net *net)
 {
 	vsock_net_init(net);
 
@@ -3047,14 +3468,14 @@ static __net_init int vsock_sysctl_init_net(struct net *net)
 	return 0;
 }
 
-static __net_exit void vsock_sysctl_exit_net(struct net *net)
+static __net_exit void vsock_pernet_exit(struct net *net)
 {
 	vsock_sysctl_unregister(net);
 }
 
-static struct pernet_operations vsock_sysctl_ops = {
-	.init = vsock_sysctl_init_net,
-	.exit = vsock_sysctl_exit_net,
+static struct pernet_operations vsock_pernet_ops = {
+	.init = vsock_pernet_init,
+	.exit = vsock_pernet_exit,
 };
 
 static int __init vsock_init(void)
@@ -3063,6 +3484,8 @@ static int __init vsock_init(void)
 
 	vsock_init_tables();
 
+	get_net_track(&init_net, &vsock_g2h_net_tracker, GFP_KERNEL);
+
 	vsock_proto.owner = THIS_MODULE;
 	vsock_device.minor = MISC_DYNAMIC_MINOR;
 	err = misc_register(&vsock_device);
@@ -3084,15 +3507,23 @@ static int __init vsock_init(void)
 		goto err_unregister_proto;
 	}
 
-	if (register_pernet_subsys(&vsock_sysctl_ops)) {
+	if (register_pernet_subsys(&vsock_pernet_ops)) {
 		err = -ENOMEM;
 		goto err_unregister_sock;
 	}
 
+	err = genl_register_family(&vsock_nl_family);
+	if (err) {
+		pr_err("Cannot register vsock netlink family: %d\n", err);
+		goto err_unregister_pernet;
+	}
+
 	vsock_bpf_build_proto();
 
 	return 0;
 
+err_unregister_pernet:
+	unregister_pernet_subsys(&vsock_pernet_ops);
 err_unregister_sock:
 	sock_unregister(AF_VSOCK);
 err_unregister_proto:
@@ -3100,15 +3531,20 @@ static int __init vsock_init(void)
 err_deregister_misc:
 	misc_deregister(&vsock_device);
 err_reset_transport:
+	put_net_track(&init_net, &vsock_g2h_net_tracker);
 	return err;
 }
 
 static void __exit vsock_exit(void)
 {
+	genl_unregister_family(&vsock_nl_family);
 	misc_deregister(&vsock_device);
 	sock_unregister(AF_VSOCK);
 	proto_unregister(&vsock_proto);
-	unregister_pernet_subsys(&vsock_sysctl_ops);
+	unregister_pernet_subsys(&vsock_pernet_ops);
+
+	put_net_track(rcu_dereference_protected(vsock_g2h_net, true),
+		      &vsock_g2h_net_tracker);
 }
 
 const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk)
@@ -3175,13 +3611,18 @@ EXPORT_SYMBOL_GPL(vsock_core_register);
 
 void vsock_core_unregister(const struct vsock_transport *t)
 {
+	netns_tracker old_tracker;
+	struct net *old = NULL;
+
 	mutex_lock(&vsock_register_mutex);
 
 	if (transport_h2g == t)
 		transport_h2g = NULL;
 
-	if (transport_g2h == t)
+	if (transport_g2h == t) {
 		transport_g2h = NULL;
+		old = vsock_g2h_net_swap(&init_net, &old_tracker);
+	}
 
 	if (transport_dgram == t)
 		transport_dgram = NULL;
@@ -3190,6 +3631,10 @@ void vsock_core_unregister(const struct vsock_transport *t)
 		transport_local = NULL;
 
 	mutex_unlock(&vsock_register_mutex);
+
+	/* The device is gone, so release the namespace it was holding. */
+	if (old)
+		vsock_g2h_net_release(old, &old_tracker);
 }
 EXPORT_SYMBOL_GPL(vsock_core_unregister);
 
diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 4f9aa9c4c3aa5..06df82b130cff 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -256,6 +256,15 @@ virtio_transport_send_pkt(struct sk_buff *skb, struct net *net)
 		goto out_rcu;
 	}
 
+	/* A socket reset by an assign can still send credit updates from
+	 * recvmsg() and setsockopt(SO_RCVLOWAT).
+	 */
+	if (!vsock_g2h_net_reachable(net)) {
+		kfree_skb(skb);
+		len = -ENETUNREACH;
+		goto out_rcu;
+	}
+
 	/* If send_pkt_queue is empty, we can safely bypass this queue
 	 * because packet order is maintained and (try) to put the packet
 	 * on the virtqueue using virtio_transport_send_skb_fast_path.
@@ -542,13 +551,13 @@ static bool virtio_transport_msgzerocopy_allow(void)
 
 bool virtio_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port)
 {
-	return vsock_net_mode_global(vsk);
+	return vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));
 }
 
 static bool virtio_transport_seqpacket_allow(struct vsock_sock *vsk,
 					     u32 remote_cid);
 
-static bool virtio_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid)
+static bool virtio_transport_has_remote_cid(struct net *net, u32 cid)
 {
 	/* The CID could be implemented by the host. Always assume it is. */
 	return true;
@@ -587,6 +596,7 @@ static struct virtio_transport virtio_transport = {
 		.seqpacket_has_data       = virtio_transport_seqpacket_has_data,
 
 		.msgzerocopy_allow        = virtio_transport_msgzerocopy_allow,
+		.netns_assign_allow       = true,
 
 		.notify_poll_in           = virtio_transport_notify_poll_in,
 		.notify_poll_out          = virtio_transport_notify_poll_out,
@@ -616,7 +626,7 @@ virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid)
 	struct virtio_vsock *vsock;
 	bool seqpacket_allow;
 
-	if (!vsock_net_mode_global(vsk))
+	if (!vsock_g2h_net_reachable(sock_net(sk_vsock(vsk))))
 		return false;
 
 	seqpacket_allow = false;
@@ -633,7 +643,12 @@ static void virtio_transport_rx_work(struct work_struct *work)
 {
 	struct virtio_vsock *vsock =
 		container_of(work, struct virtio_vsock, rx_work);
+	struct virtio_transport *t = &virtio_transport;
+	netns_tracker ns_tracker;
 	struct virtqueue *vq;
+	struct net *net;
+
+	net = vsock_g2h_net_get(&ns_tracker);
 
 	mutex_lock(&vsock->rx_lock);
 
@@ -682,10 +697,15 @@ static void virtio_transport_rx_work(struct work_struct *work)
 
 			virtio_transport_deliver_tap_pkt(skb);
 
-			/* Force virtio-transport into global mode since it
-			 * does not yet support local-mode namespacing.
+			/* Only grab a reference if the net changes underneath
+			 * us.
 			 */
-			virtio_transport_recv_pkt(&virtio_transport, skb, NULL);
+			if (unlikely(!vsock_g2h_net_eq(net))) {
+				put_net_track(net, &ns_tracker);
+				net = vsock_g2h_net_get(&ns_tracker);
+			}
+
+			virtio_transport_recv_pkt(t, skb, net);
 		}
 	} while (!virtqueue_enable_cb(vq));
 
@@ -694,6 +714,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 		virtio_vsock_rx_fill(vsock);
 out_nofill:
 	mutex_unlock(&vsock->rx_lock);
+
+	put_net_track(net, &ns_tracker);
 }
 
 static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 1e762a480df49..0c864db2bc65b 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1478,9 +1478,13 @@ virtio_transport_recv_connecting(struct sock *sk,
 
 	switch (le16_to_cpu(hdr->op)) {
 	case VIRTIO_VSOCK_OP_RESPONSE:
-		sk->sk_state = TCP_ESTABLISHED;
+		if (!vsock_maybe_set_connected(vsk)) {
+			skerr = ECONNRESET;
+			err = -ENETUNREACH;
+			goto destroy;
+		}
+
 		sk->sk_socket->state = SS_CONNECTED;
-		vsock_insert_connected(vsk);
 		sk->sk_state_change(sk);
 		break;
 	case VIRTIO_VSOCK_OP_INVALID:
@@ -1736,8 +1740,6 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,
 
 	lock_sock_nested(child, SINGLE_DEPTH_NESTING);
 
-	child->sk_state = TCP_ESTABLISHED;
-
 	vchild = vsock_sk(child);
 	vsock_addr_init(&vchild->local_addr, le64_to_cpu(hdr->dst_cid),
 			le32_to_cpu(hdr->dst_port));
@@ -1758,7 +1760,13 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,
 	if (virtio_transport_space_update(child, skb))
 		child->sk_write_space(child);
 
-	vsock_insert_connected(vchild);
+	if (!vsock_maybe_set_connected(vchild)) {
+		release_sock(child);
+		virtio_transport_reset_no_sock(t, skb, sock_net(sk));
+		sock_put(child);
+		return -ENETUNREACH;
+	}
+
 	vsock_enqueue_accept(sk, child);
 	virtio_transport_send_response(vchild, skb);
 
diff --git a/net/vmw_vsock/vsock_nl_gen.c b/net/vmw_vsock/vsock_nl_gen.c
new file mode 100644
index 0000000000000..81db4c61d7bb9
--- /dev/null
+++ b/net/vmw_vsock/vsock_nl_gen.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/vsock.yaml */
+/* YNL-GEN kernel source */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include "vsock_nl_gen.h"
+
+#include <uapi/linux/vsock.h>
+
+/* Ops table for vsock */
+static const struct genl_split_ops vsock_nl_ops[] = {
+	{
+		.cmd	= VSOCK_CMD_DEV_NETNS_SET,
+		.doit	= vsock_nl_dev_netns_set_doit,
+		.flags	= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd	= VSOCK_CMD_DEV_NETNS_GET,
+		.doit	= vsock_nl_dev_netns_get_doit,
+		.flags	= GENL_CMD_CAP_DO,
+	},
+};
+
+struct genl_family vsock_nl_family __ro_after_init = {
+	.name		= VSOCK_FAMILY_NAME,
+	.version	= VSOCK_FAMILY_VERSION,
+	.netnsok	= true,
+	.parallel_ops	= true,
+	.module		= THIS_MODULE,
+	.split_ops	= vsock_nl_ops,
+	.n_split_ops	= ARRAY_SIZE(vsock_nl_ops),
+};
diff --git a/net/vmw_vsock/vsock_nl_gen.h b/net/vmw_vsock/vsock_nl_gen.h
new file mode 100644
index 0000000000000..c041195584a09
--- /dev/null
+++ b/net/vmw_vsock/vsock_nl_gen.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/vsock.yaml */
+/* YNL-GEN kernel header */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
+
+#ifndef _LINUX_VSOCK_GEN_H
+#define _LINUX_VSOCK_GEN_H
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include <uapi/linux/vsock.h>
+
+int vsock_nl_dev_netns_set_doit(struct sk_buff *skb, struct genl_info *info);
+int vsock_nl_dev_netns_get_doit(struct sk_buff *skb, struct genl_info *info);
+
+extern struct genl_family vsock_nl_family;
+
+#endif /* _LINUX_VSOCK_GEN_H */
diff --git a/tools/net/ynl/Makefile.deps b/tools/net/ynl/Makefile.deps
index 3eee34e0efcf9..f3150b41dd333 100644
--- a/tools/net/ynl/Makefile.deps
+++ b/tools/net/ynl/Makefile.deps
@@ -62,4 +62,5 @@ CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \
 	$(call get_hdr_inc,_TC_SKBEDIT_H,tc_act/tc_skbedit.h) \
 	$(call get_hdr_inc,_TC_TUNNEL_KEY_H,tc_act/tc_tunnel_key.h)
 CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)
+CFLAGS_vsock:=$(call get_hdr_inc,_LINUX_VSOCK_H,vsock.h)
 CFLAGS_wireguard:=$(call get_hdr_inc2,_LINUX_WIREGUARD_H,_WG_UAPI_WIREGUARD_H,wireguard.h)
diff --git a/tools/testing/selftests/vsock/Makefile b/tools/testing/selftests/vsock/Makefile
index c407c0afd9388..2b066ba91bcd7 100644
--- a/tools/testing/selftests/vsock/Makefile
+++ b/tools/testing/selftests/vsock/Makefile
@@ -13,5 +13,10 @@ $(VSOCK_TEST_DIR)/vsock_test: $(VSOCK_TEST_SRCS)
 TEST_PROGS += vmtest.sh
 TEST_GEN_FILES := vsock_test
 
+TEST_FILES := \
+	../../../net/ynl/pyynl/cli.py \
+	../../../net/ynl/pyynl/lib \
+	../../../../Documentation/netlink/specs/vsock.yaml
+
 include ../lib.mk
 
diff --git a/tools/testing/selftests/vsock/config b/tools/testing/selftests/vsock/config
index 5f0a4f17dfc96..4b31085558faa 100644
--- a/tools/testing/selftests/vsock/config
+++ b/tools/testing/selftests/vsock/config
@@ -109,3 +109,4 @@ CONFIG_FS_DAX=y
 CONFIG_MEMORY_HOTPLUG=y
 CONFIG_MEMORY_HOTREMOVE=y
 CONFIG_ZONE_DEVICE=y
+CONFIG_USER_NS=y
diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
index 310dfc2a39ad5..5bd90a0c23802 100755
--- a/tools/testing/selftests/vsock/vmtest.sh
+++ b/tools/testing/selftests/vsock/vmtest.sh
@@ -17,6 +17,20 @@ readonly KERNEL_CHECKOUT=$(realpath "${SCRIPT_DIR}"/../../../../)
 source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh
 
 readonly VSOCK_TEST="${SCRIPT_DIR}"/vsock_test
+
+# "make install" copies the ynl CLI, its lib package and the spec next to this
+# script (see TEST_FILES); in a source tree they stay where they are built.
+if [[ -r "${SCRIPT_DIR}"/cli.py ]]; then
+	readonly YNL_CLI="${SCRIPT_DIR}"/cli.py
+	readonly YNL_LIB="${SCRIPT_DIR}"/lib
+	readonly VSOCK_SPEC="${SCRIPT_DIR}"/vsock.yaml
+else
+	readonly YNL_DIR="${KERNEL_CHECKOUT}"/tools/net/ynl/pyynl
+	readonly YNL_CLI="${YNL_DIR}"/cli.py
+	readonly YNL_LIB="${YNL_DIR}"/lib
+	readonly SPEC_DIR="${KERNEL_CHECKOUT}"/Documentation/netlink/specs
+	readonly VSOCK_SPEC="${SPEC_DIR}"/vsock.yaml
+fi
 readonly TEST_GUEST_PORT=51000
 readonly TEST_HOST_PORT=50000
 readonly TEST_HOST_PORT_LISTENER=50001
@@ -73,6 +87,14 @@ readonly TEST_NAMES=(
 	ns_delete_vm_ok
 	ns_delete_host_ok
 	ns_delete_both_ok
+	ns_guest_local_connect_to_host_fails
+	ns_guest_assign_g2h_netns_connect_to_host_ok
+	ns_guest_assign_g2h_netns_init_ns_connect_fails
+	ns_guest_assign_g2h_netns_host_connect_ok
+	ns_guest_assign_g2h_netns_pins_ns_ok
+	ns_guest_assign_g2h_netns_old_conn_send_fails
+	ns_guest_assign_g2h_netns_no_cap_net_admin_fails
+	ns_guest_assign_g2h_netns_unpriv_user_ns_fails
 )
 readonly TEST_DESCS=(
 	# vm_server_host_client
@@ -149,12 +171,44 @@ readonly TEST_DESCS=(
 
 	# ns_delete_both_ok
 	"Check that deleting the VM and host's namespaces does not break the socket connection"
+
+	# ns_guest_local_connect_to_host_fails
+	"Check a guest process in a local ns cannot reach the host without the netns assign."
+
+	# ns_guest_assign_g2h_netns_connect_to_host_ok
+	"Check a guest process in a local ns reaches the host once the vsock device is assigned to it."
+
+	# ns_guest_assign_g2h_netns_init_ns_connect_fails
+	"Check the guest's initial ns loses vsock once the device is assigned to another ns."
+
+	# ns_guest_assign_g2h_netns_host_connect_ok
+	"Check the host reaches a guest listener in the ns the vsock device is assigned to."
+
+	# ns_guest_assign_g2h_netns_pins_ns_ok
+	"Check the assignment keeps the ns alive when deleted, and that reassigning releases it."
+
+	# ns_guest_assign_g2h_netns_old_conn_send_fails
+	"Check connections made before the assign stop sending once they lose the device."
+
+	# ns_guest_assign_g2h_netns_no_cap_net_admin_fails
+	"Check assigning the guest's vsock device to a namespace needs CAP_NET_ADMIN."
+
+	# ns_guest_assign_g2h_netns_unpriv_user_ns_fails
+	"Check an unprivileged user cannot claim the guest's vsock device via a user ns."
 )
 
 readonly USE_SHARED_VM=(
 	vm_server_host_client
 	vm_client_host_server
 	vm_loopback
+	ns_guest_local_connect_to_host_fails
+	ns_guest_assign_g2h_netns_connect_to_host_ok
+	ns_guest_assign_g2h_netns_init_ns_connect_fails
+	ns_guest_assign_g2h_netns_host_connect_ok
+	ns_guest_assign_g2h_netns_pins_ns_ok
+	ns_guest_assign_g2h_netns_old_conn_send_fails
+	ns_guest_assign_g2h_netns_no_cap_net_admin_fails
+	ns_guest_assign_g2h_netns_unpriv_user_ns_fails
 )
 readonly NS_MODES=("local" "global")
 
@@ -316,6 +370,34 @@ check_deps() {
 	fi
 }
 
+check_ns_guest_deps() {
+	local tname=$1
+	local dep
+
+	if [[ ! "${tname}" =~ ^ns_guest_ ]]; then
+		return 0
+	fi
+
+	for dep in unshare setpriv python3; do
+		if [[ ! -x $(command -v "${dep}") ]]; then
+			log_host "no ${dep} for test ${tname}"
+			return 1
+		fi
+	done
+
+	if ! python3 -c "import yaml" &>/dev/null; then
+		log_host "no python3 yaml module for test ${tname}"
+		return 1
+	fi
+
+	if [[ ! -r "${YNL_CLI}" ]] || [[ ! -r "${VSOCK_SPEC}" ]]; then
+		log_host "no ynl cli or vsock spec for test ${tname}"
+		return 1
+	fi
+
+	return 0
+}
+
 check_netns() {
 	local tname=$1
 
@@ -401,6 +483,19 @@ setup_home() {
 	mkdir -p "$(dirname "${SSH_KEY_PATH}")"
 	ssh-keygen -t ed25519 -f "${SSH_KEY_PATH}" -N "" -q
 	cp "${VSOCK_TEST}" "${TEST_HOME}"/vsock_test
+
+	mkdir -p "${TEST_HOME}"/ynl
+	if [[ -r "${YNL_CLI}" ]]; then
+		cp "${YNL_CLI}" "${TEST_HOME}"/ynl/
+		cp -r "${YNL_LIB}" "${TEST_HOME}"/ynl/
+		cp "${VSOCK_SPEC}" "${TEST_HOME}"/ynl/
+	fi
+
+	# One of the tests runs the CLI as an unprivileged user, so the CLI
+	# has to be reachable by one.
+	chmod -R a+rX "${TEST_HOME}"/ynl
+
+	chmod 755 "${TEST_HOME}"
 }
 
 create_pidfile() {
@@ -528,6 +623,82 @@ vm_wait_for_ssh() {
 	done
 }
 
+# Create a local mode namespace in the VM and echo the pid holding it open.
+vm_ns_start() {
+	local ns=$1
+	local initns
+	local nspid
+	local newns
+	local i
+
+	local mode=/proc/sys/net/vsock/child_ns_mode
+
+	if ! vm_ssh "${ns}" -- "echo local > ${mode}" &>/dev/null; then
+		return
+	fi
+
+	nspid=$(vm_ssh "${ns}" -- "unshare -n sleep infinity" \
+		'>/dev/null 2>&1 & echo $!')
+	if [[ -z "${nspid}" ]]; then
+		return
+	fi
+
+	initns=$(vm_ssh "${ns}" -- "readlink /proc/self/ns/net")
+	for ((i = 0; i < WAIT_PERIOD_MAX; i++)); do
+		newns=$(vm_ssh "${ns}" -- "readlink /proc/${nspid}/ns/net" \
+			2>/dev/null)
+		if [[ -n "${newns}" ]] && [[ "${newns}" != "${initns}" ]]; then
+			echo "${nspid}"
+			return
+		fi
+		sleep 1
+	done
+
+	vm_ns_stop "${ns}" "${nspid}"
+}
+
+# Returns once the holder is gone, so that the namespace is unreferenced and
+# the kernel can start tearing it down.
+vm_ns_stop() {
+	local ns=$1
+	local nspid=$2
+
+	vm_ssh "${ns}" <<-EOF &>/dev/null
+		kill ${nspid}
+		for ((i = 0; i < ${WAIT_PERIOD_MAX}; i++)); do
+			kill -0 ${nspid} 2>/dev/null || break
+			sleep 1
+		done
+	EOF
+}
+
+# Runs in the guest's initial namespace when <nspid> is empty. The command must
+# not contain single quotes.
+vm_ns_exec() {
+	local ns=$1
+	local nspid=$2
+	local cmd=$3
+
+	if [[ -z "${nspid}" ]]; then
+		vm_ssh "${ns}" -- "${cmd}"
+		return
+	fi
+
+	vm_ssh "${ns}" -- nsenter -t "${nspid}" -n sh -c "'${cmd}'"
+}
+
+vm_ns_assign_g2h() {
+	local ns=$1
+	local nspid=$2
+
+	vm_ns_exec "${ns}" "${nspid}" "python3 /root/ynl/cli.py --no-schema \
+		--spec /root/ynl/vsock.yaml --do dev-netns-set"
+}
+
+vm_reset_g2h() {
+	vm_ns_assign_g2h "init_ns" "" &>/dev/null
+}
+
 # derived from selftests/net/net_helper.sh
 wait_for_listener()
 {
@@ -564,17 +735,33 @@ wait_for_listener()
 	done
 }
 
-vm_wait_for_listener() {
+# Runs in the guest's initial namespace when <nspid> is empty.
+vm_ns_wait_for_listener() {
 	local ns=$1
-	local port=$2
-	local protocol=$3
+	local nspid=$2
+	local port=$3
+	local protocol=$4
+	local nsenter=
+	local args
+
+	[[ -n "${nspid}" ]] && nsenter="nsenter -t ${nspid} -n"
+	args="${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol}"
 
 	vm_ssh "${ns}" <<EOF
 $(declare -f wait_for_listener)
-wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol}
+export -f wait_for_listener
+${nsenter} bash -c "wait_for_listener ${args}"
 EOF
 }
 
+vm_wait_for_listener() {
+	local ns=$1
+	local port=$2
+	local protocol=$3
+
+	vm_ns_wait_for_listener "${ns}" "" "${port}" "${protocol}"
+}
+
 host_wait_for_listener() {
 	local ns=$1
 	local port=$2
@@ -1421,6 +1608,355 @@ test_ns_delete_both_ok() {
 	check_ns_delete_doesnt_break_connection "both"
 }
 
+# Send a string from the guest to a host listener and leave what the host
+# received in <outfile>.
+guest_send_to_host() {
+	local ns=$1
+	local nspid=$2
+	local port=$3
+	local outfile=$4
+	local cmd="echo TEST | socat -u STDIN VSOCK-CONNECT:2:${port}"
+	local pid
+
+	socat -u VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" 2>/dev/null &
+	pid=$!
+	host_wait_for_listener "${ns}" "${port}" "vsock"
+
+	vm_ns_exec "${ns}" "${nspid}" "${cmd}" 2>/dev/null
+
+	timeout "${WAIT_PERIOD}" \
+		bash -c 'while [[ ! -s '"${outfile}"' ]]; do sleep 1; done'
+
+	# Wait for the listener to actually go, or the next test binding the
+	# same port races it and silently reads nothing.
+	terminate_pids "${pid}"
+	wait "${pid}" 2>/dev/null
+}
+
+# Send a string from the host to a listener in the guest and leave what the
+# guest received in <outfile>.
+host_send_to_guest() {
+	local ns=$1
+	local nspid=$2
+	local port=$3
+	local outfile=$4
+	local cmd="socat -u VSOCK-LISTEN:${port} STDOUT"
+	local dst="VSOCK-CONNECT:${VSOCK_CID}:${port}"
+	local pid
+
+	vm_ns_exec "${ns}" "${nspid}" "${cmd}" > "${outfile}" 2>/dev/null &
+	pid=$!
+	vm_ns_wait_for_listener "${ns}" "${nspid}" "${port}" "vsock"
+
+	echo TEST | socat -u STDIN "${dst}" 2>/dev/null
+
+	timeout "${WAIT_PERIOD}" \
+		bash -c 'while [[ ! -s '"${outfile}"' ]]; do sleep 1; done'
+
+	vm_ns_exec "${ns}" "${nspid}" \
+		"pkill -f VSOCK-LISTEN:${port}" &>/dev/null
+	terminate_pids "${pid}"
+	wait "${pid}" 2>/dev/null
+}
+
+ns_old_conn_cleanup() {
+	local port=$1
+	local pid=$2
+	local sender=$3
+	local outfile=$4
+	local nspid=$5
+
+	vm_ns_exec "init_ns" "" "pkill -f VSOCK-CONNECT:2:${port}" &>/dev/null
+	terminate_pids "${pid}" "${sender}"
+	wait "${pid}" "${sender}" 2>/dev/null
+	rm -f "${outfile}"
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+}
+
+test_ns_guest_assign_g2h_netns_old_conn_send_fails() {
+	local gap=$(( WAIT_PERIOD * 3 ))
+	local port=12346
+	local elapsed
+	local started
+	local outfile
+	local result
+	local sender
+	local nspid
+	local pid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	socat -u VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" 2>/dev/null &
+	pid=$!
+	host_wait_for_listener "init_ns" "${port}" "vsock"
+
+	# Send a message, wait, then send another. While waiting, assign the
+	# device to a namespace. Confirm the second message does not arrive.
+	started=${SECONDS}
+	vm_ssh "init_ns" -- \
+		"(echo FIRST; sleep ${gap}; echo SECOND) |" \
+		"socat -u STDIN VSOCK-CONNECT:2:${port}" &>/dev/null &
+	sender=$!
+
+	timeout "$(( WAIT_PERIOD * WAIT_PERIOD_MAX ))" \
+		bash -c 'while [[ ! -s '"${outfile}"' ]]; do sleep 1; done'
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		ns_old_conn_cleanup "${port}" "${pid}" "${sender}" \
+			"${outfile}" "${nspid}"
+		return "${KSFT_FAIL}"
+	fi
+	elapsed=$(( SECONDS - started ))
+
+	sleep $(( gap + WAIT_PERIOD ))
+
+	result=$(cat "${outfile}")
+	ns_old_conn_cleanup "${port}" "${pid}" "${sender}" \
+		"${outfile}" "${nspid}"
+
+	if [[ "${result}" != *FIRST* ]]; then
+		log_host "no connection before the assign: [${result}]"
+		return "${KSFT_FAIL}"
+	fi
+
+	if [[ $(( elapsed + 1 )) -ge ${gap} ]]; then
+		log_host "connect and assign took ${elapsed}s of a ${gap}s gap"
+		return "${KSFT_SKIP}"
+	fi
+
+	if [[ "${result}" == *SECOND* ]]; then
+		log_host "old connection still delivered after the assign"
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_local_connect_to_host_fails() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "${nspid}" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" == TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_connect_to_host_ok() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "${nspid}" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" != TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_init_ns_connect_fails() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" == TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_host_connect_ok() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	host_send_to_guest "init_ns" "${nspid}" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" != TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_pins_ns_ok() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	vm_ns_stop "init_ns" "${nspid}"
+	sleep "${WAIT_PERIOD}"
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "" "${port}" "${outfile}"
+	result=$(cat "${outfile}")
+	if [[ "${result}" == TEST ]]; then
+		log_host "device returned to the init ns instead of pinning it"
+		rm -f "${outfile}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	# Assigning it back is what releases the deleted namespace.
+	vm_reset_g2h
+
+	guest_send_to_host "init_ns" "" "$(( port + 1 ))" "${outfile}"
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" != TEST ]]; then
+		log_host "device did not come back on reassign: [${result}]"
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+check_assign_g2h_eperm() {
+	local wrapper=$1
+	local out
+
+	if out=$(vm_ssh "init_ns" -- "${wrapper}" python3 /root/ynl/cli.py \
+		--no-schema --spec /root/ynl/vsock.yaml \
+		--do dev-netns-set 2>&1); then
+		log_host "assign unexpectedly succeeded under '${wrapper}'"
+		return "${KSFT_FAIL}"
+	fi
+
+	if [[ "${out}" != *"Operation not permitted"* ]]; then
+		log_host "no EPERM under '${wrapper}': [${out}]"
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_no_cap_net_admin_fails() {
+	check_assign_g2h_eperm "unshare -n setpriv --bounding-set=-net_admin"
+}
+
+test_ns_guest_assign_g2h_netns_unpriv_user_ns_fails() {
+	local unpriv_uid=65534
+	local unpriv
+
+	unpriv="setpriv --reuid=${unpriv_uid} --regid=${unpriv_uid}"
+	unpriv="${unpriv} --clear-groups"
+
+	if ! vm_ssh "init_ns" -- "${unpriv} unshare -Urn true"; then
+		log_host "unprivileged user namespaces unavailable, skipping"
+		return "${KSFT_SKIP}"
+	fi
+
+	check_assign_g2h_eperm "${unpriv} unshare -Urn"
+}
+
 shared_vm_test() {
 	local tname
 
@@ -1453,7 +1989,8 @@ run_shared_vm_tests() {
 			continue
 		fi
 
-		if ! check_netns "${arg}"; then
+		if ! check_netns "${arg}" ||
+		   ! check_ns_guest_deps "${arg}"; then
 			check_result "${KSFT_SKIP}" "${arg}"
 			continue
 		fi
@@ -1517,7 +2054,8 @@ run_ns_tests() {
 			continue
 		fi
 
-		if ! check_netns "${arg}"; then
+		if ! check_netns "${arg}" ||
+		   ! check_ns_guest_deps "${arg}"; then
 			check_result "${KSFT_SKIP}" "${arg}"
 			continue
 		fi

3/2 2026/10/21 10:16 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 25554
							output: 33
							thoughts: 1150
						

Thoughts:
**Fuzzing Feasibility Analysis: vsock NetNS Changes**

Alright, let's break down this patch and see if it's worth throwing at syzkaller. My initial impression is that this is a solid candidate, given the scope of the modifications.

First, I need to understand the changes. It looks like we're dealing with vsock and netns integration. There's new documentation (`Documentation/admin-guide/sysctl/net.rst`, `Documentation/netlink/specs/vsock.yaml`), which is a good sign for understanding the new functionality, which is the netns assignment stuff. The `MAINTAINERS` file has been updated, so someone cares about it.

Looking deeper, the changes in `drivers/vhost/vsock.c` are significant, particularly the signature change in `vhost_transport_has_remote_cid` and the locking around `vsock_register_mutex` in `vhost_vsock_set_cid`. We have new header definitions in `include/net/af_vsock.h` - including the mutex declaration and new helper functions are a good indicator of changes. The new include for the netlink genetlink family, `include/uapi/linux/vsock.h`, hints at a new user-space interface that will be reachable.

The real meat seems to be in `net/vmw_vsock/`. This is where all the `vsock_g2h_net` and netns tracking, assignment, and reset functions live. We have `vsock_nl_dev_netns_set_doit` and `vsock_nl_dev_netns_get_doit`, which are the netlink handlers. The `vsock_g2h_net_assign` function, with its netns swapping and socket reset, looks particularly interesting from a fuzzing perspective, especially the `vsock_g2h_reset_unreachable` function and the walk through `vsock_connected_table`. We also see modifications in `__vsock_bind` and `vsock_dev_do_ioctl`, meaning we should focus on the syscall interface. The `vsock_nl_family` registration indicates the netlink interface, we'll need to figure out the right calls to trigger this.

The changes in `net/vmw_vsock/virtio_transport.c` and `virtio_transport_common.c` are also worth noting. Checks like `!vsock_g2h_net_reachable(net)` and the use of `vsock_g2h_net_get`, `vsock_g2h_net_eq`, and `vsock_maybe_set_connected` suggest the transport layer is involved. I'll need to analyze how these functions interact with the netns changes.

Okay, so is this code reachable? Yes, absolutely. This is core vsock networking (`net/vmw_vsock/`) and virtio-vsock/vhost-vsock. It's used in standard Linux kernel deployments with QEMU/KVM. The new netlink interface, `vsock` genetlink family, can be triggered via standard Generic Netlink sockets (`AF_NETLINK`, `NETLINK_GENERIC`). On top of that, we're seeing changes to standard AF_VSOCK socket operations (bind, connect, listen, ioctl).

Next, I need to check the Kconfig. I'll use `grepper` or check the `net/vmw_vsock/Kconfig` to verify that AF_VSOCK and related transports are enabled by default or easily configured. That will tell me how easily this code can be exposed through a standard setup. This patch appears to modify the core of the vsock implementation and networking, which is ideal for a good fuzzing campaign.



4/2 2026/10/21 10:16 tool grepper
0m Args:
{
  "Expression": "config VSOCKETS",
  "PathPrefixes": [
    "net/vmw_vsock/"
  ]
}

Results:
{
  "Output": "net/vmw_vsock/Kconfig-5-\nnet/vmw_vsock/Kconfig:6:config VSOCKETS\nnet/vmw_vsock/Kconfig-7-\ttristate \"Virtual Socket protocol\"\n--\nnet/vmw_vsock/Kconfig-18-\nnet/vmw_vsock/Kconfig:19:config VSOCKETS_DIAG\nnet/vmw_vsock/Kconfig-20-\ttristate \"Virtual Sockets monitoring interface\"\n--\nnet/vmw_vsock/Kconfig-28-\nnet/vmw_vsock/Kconfig:29:config VSOCKETS_LOOPBACK\nnet/vmw_vsock/Kconfig-30-\ttristate \"Virtual Sockets loopback transport\"\n"
}

5/2 2026/10/21 10:16 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 27759
							output: 36
							thoughts: 16
						

Thoughts:
Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:

**Diving into the vmw_vsock Kconfig**

Alright, time to crack open the `net/vmw_vsock/Kconfig` file. Specifically, I'm thinking I need to understand the configuration options related to the VMware vSocket driver. This likely means I'm trying to figure out how to enable, disable, or fine-tune its behavior within a specific kernel build. Given the context, I'm probably looking at dependencies, default values, and the descriptions of each option to inform a configuration change, investigate a reported issue, or possibly assess the driver's feature set. I need to get a clear picture of what options are available and what they control. This file is the key to understanding the build-time configuration of this important network component, and I expect it to reveal the controls I'm interested in. Let's see what gems are hidden in there.



6/2 2026/10/21 10:16 tool read-file
0m Args:
{
  "File": "net/vmw_vsock/Kconfig",
  "FirstLine": 1,
  "LineCount": 70
}

Results:
{
  "Contents": "   1:\t# SPDX-License-Identifier: GPL-2.0-only\n   2:\t#\n   3:\t# Vsock protocol\n   4:\t#\n   5:\t\n   6:\tconfig VSOCKETS\n   7:\t\ttristate \"Virtual Socket protocol\"\n   8:\t\thelp\n   9:\t\t  Virtual Socket Protocol is a socket protocol similar to TCP/IP\n  10:\t\t  allowing communication between Virtual Machines and hypervisor\n  11:\t\t  or host.\n  12:\t\n  13:\t\t  You should also select one or more hypervisor-specific transports\n  14:\t\t  below.\n  15:\t\n  16:\t\t  To compile this driver as a module, choose M here: the module\n  17:\t\t  will be called vsock. If unsure, say N.\n  18:\t\n  19:\tconfig VSOCKETS_DIAG\n  20:\t\ttristate \"Virtual Sockets monitoring interface\"\n  21:\t\tdepends on VSOCKETS\n  22:\t\tdefault y\n  23:\t\thelp\n  24:\t\t  Support for PF_VSOCK sockets monitoring interface used by the ss tool.\n  25:\t\t  If unsure, say Y.\n  26:\t\n  27:\t\t  Enable this module so userspace applications can query open sockets.\n  28:\t\n  29:\tconfig VSOCKETS_LOOPBACK\n  30:\t\ttristate \"Virtual Sockets loopback transport\"\n  31:\t\tdepends on VSOCKETS\n  32:\t\tdefault y\n  33:\t\tselect VIRTIO_VSOCKETS_COMMON\n  34:\t\thelp\n  35:\t\t  This module implements a loopback transport for Virtual Sockets,\n  36:\t\t  using vmw_vsock_virtio_transport_common.\n  37:\t\n  38:\t\t  To compile this driver as a module, choose M here: the module\n  39:\t\t  will be called vsock_loopback. If unsure, say N.\n  40:\t\n  41:\tconfig VMWARE_VMCI_VSOCKETS\n  42:\t\ttristate \"VMware VMCI transport for Virtual Sockets\"\n  43:\t\tdepends on VSOCKETS \u0026\u0026 VMWARE_VMCI\n  44:\t\thelp\n  45:\t\t  This module implements a VMCI transport for Virtual Sockets.\n  46:\t\n  47:\t\t  Enable this transport if your Virtual Machine runs on a VMware\n  48:\t\t  hypervisor.\n  49:\t\n  50:\t\t  To compile this driver as a module, choose M here: the module\n  51:\t\t  will be called vmw_vsock_vmci_transport. If unsure, say N.\n  52:\t\n  53:\tconfig VIRTIO_VSOCKETS\n  54:\t\ttristate \"virtio transport for Virtual Sockets\"\n  55:\t\tdepends on VSOCKETS \u0026\u0026 VIRTIO\n  56:\t\tselect VIRTIO_VSOCKETS_COMMON\n  57:\t\thelp\n  58:\t\t  This module implements a virtio transport for Virtual Sockets.\n  59:\t\n  60:\t\t  Enable this transport if your Virtual Machine host supports Virtual\n  61:\t\t  Sockets over virtio.\n  62:\t\n  63:\t\t  To compile this driver as a module, choose M here: the module will be\n  64:\t\t  called vmw_vsock_virtio_transport. If unsure, say N.\n  65:\t\n  66:\tconfig VIRTIO_VSOCKETS_COMMON\n  67:\t\ttristate\n  68:\t\thelp\n  69:\t\t  This option is selected by any driver which needs to access\n  70:\t\t  the virtio_vsock.  The module will be called\n"
}

7/2 2026/10/21 10:16 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 28954
							output: 31
							thoughts: 26
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience:

**Investigating vhost/vsock.c and Its Configuration**

Alright, before diving too deep into the code, I need to make sure I'm not missing something fundamental in the build process. Given my understanding of the `vhost` subsystem and its dependency on configuration options, my first thought is to double-check the Kconfig entry for the relevant `vsock.c` module. Specifically, I'm going to navigate to `drivers/vhost/Kconfig`. This is where the core configuration options are defined, and it's essential to understand the build flags controlling this particular functionality. This will allow me to confirm the necessary options are enabled for `vsock` support, and will ensure no basic configuration errors are present before I move on to debugging any potential issues in the module itself. I need to make sure I haven't overlooked any obvious configuration dependencies that might be blocking the inclusion or functionality of `vhost/vsock.c`.



8/2 2026/10/21 10:16 tool grepper
0m Args:
{
  "Expression": "VHOST_VSOCK",
  "PathPrefixes": [
    "drivers/vhost/"
  ]
}

Results:
{
  "Output": "drivers/vhost/Kconfig=46=config VHOST_SCSI\n--\ndrivers/vhost/Kconfig-55-\ndrivers/vhost/Kconfig:56:config VHOST_VSOCK\ndrivers/vhost/Kconfig-57-\ttristate \"vhost virtio-vsock driver\"\n--\ndrivers/vhost/Makefile=6=vhost_scsi-y := scsi.o\ndrivers/vhost/Makefile-7-\ndrivers/vhost/Makefile:8:obj-$(CONFIG_VHOST_VSOCK) += vhost_vsock.o\ndrivers/vhost/Makefile-9-vhost_vsock-y := vsock.o\n--\ndrivers/vhost/vhost.c=870=static void __vhost_vq_attach_worker(struct vhost_virtqueue *vq,\n--\ndrivers/vhost/vhost.c-919-\t\t/*\ndrivers/vhost/vhost.c:920:\t\t * vsock can queue anytime after VHOST_VSOCK_SET_GUEST_CID.\ndrivers/vhost/vhost.c-921-\t\t * Warn if it adds support for multiple workers but forgets to\n--\ndrivers/vhost/vsock.c-21-\ndrivers/vhost/vsock.c:22:#define VHOST_VSOCK_DEFAULT_HOST_CID\t2\ndrivers/vhost/vsock.c-23-/* Max number of bytes transferred before requeueing the job.\ndrivers/vhost/vsock.c-24- * Using this limit prevents one virtqueue from starving others. */\ndrivers/vhost/vsock.c:25:#define VHOST_VSOCK_WEIGHT 0x80000\ndrivers/vhost/vsock.c-26-/* Max number of packets transferred before requeueing the job.\n--\ndrivers/vhost/vsock.c-29- */\ndrivers/vhost/vsock.c:30:#define VHOST_VSOCK_PKT_WEIGHT 256\ndrivers/vhost/vsock.c-31-\ndrivers/vhost/vsock.c=32=static const int vhost_vsock_bits[] = {\n--\ndrivers/vhost/vsock.c-37-\ndrivers/vhost/vsock.c:38:#define VHOST_VSOCK_FEATURES VHOST_FEATURES_U64(vhost_vsock_bits, 0)\ndrivers/vhost/vsock.c-39-\ndrivers/vhost/vsock.c=40=enum {\ndrivers/vhost/vsock.c:41:\tVHOST_VSOCK_BACKEND_FEATURES = (1ULL \u003c\u003c VHOST_BACKEND_F_IOTLB_MSG_V2)\ndrivers/vhost/vsock.c-42-};\n--\ndrivers/vhost/vsock.c=66=static u32 vhost_transport_get_local_cid(void)\ndrivers/vhost/vsock.c-67-{\ndrivers/vhost/vsock.c:68:\treturn VHOST_VSOCK_DEFAULT_HOST_CID;\ndrivers/vhost/vsock.c-69-}\n--\ndrivers/vhost/vsock.c=696=static int vhost_vsock_dev_open(struct inode *inode, struct file *file)\n--\ndrivers/vhost/vsock.c-729-\tvhost_dev_init(\u0026vsock-\u003edev, vqs, ARRAY_SIZE(vsock-\u003evqs),\ndrivers/vhost/vsock.c:730:\t\t       UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT,\ndrivers/vhost/vsock.c:731:\t\t       VHOST_VSOCK_WEIGHT, true, NULL);\ndrivers/vhost/vsock.c-732-\n--\ndrivers/vhost/vsock.c=863=static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)\n--\ndrivers/vhost/vsock.c-867-\ndrivers/vhost/vsock.c:868:\tif (features \u0026 ~VHOST_VSOCK_FEATURES)\ndrivers/vhost/vsock.c-869-\t\treturn -EOPNOTSUPP;\n--\ndrivers/vhost/vsock.c=900=static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,\n--\ndrivers/vhost/vsock.c-910-\tswitch (ioctl) {\ndrivers/vhost/vsock.c:911:\tcase VHOST_VSOCK_SET_GUEST_CID:\ndrivers/vhost/vsock.c-912-\t\tif (copy_from_user(\u0026guest_cid, argp, sizeof(guest_cid)))\n--\ndrivers/vhost/vsock.c-914-\t\treturn vhost_vsock_set_cid(vsock, guest_cid);\ndrivers/vhost/vsock.c:915:\tcase VHOST_VSOCK_SET_RUNNING:\ndrivers/vhost/vsock.c-916-\t\tif (copy_from_user(\u0026start, argp, sizeof(start)))\n--\ndrivers/vhost/vsock.c-922-\tcase VHOST_GET_FEATURES:\ndrivers/vhost/vsock.c:923:\t\tfeatures = VHOST_VSOCK_FEATURES;\ndrivers/vhost/vsock.c-924-\t\tif (copy_to_user(argp, \u0026features, sizeof(features)))\n--\ndrivers/vhost/vsock.c-931-\tcase VHOST_GET_BACKEND_FEATURES:\ndrivers/vhost/vsock.c:932:\t\tfeatures = VHOST_VSOCK_BACKEND_FEATURES;\ndrivers/vhost/vsock.c-933-\t\tif (copy_to_user(argp, \u0026features, sizeof(features)))\n--\ndrivers/vhost/vsock.c-938-\t\t\treturn -EFAULT;\ndrivers/vhost/vsock.c:939:\t\tif (features \u0026 ~VHOST_VSOCK_BACKEND_FEATURES)\ndrivers/vhost/vsock.c-940-\t\t\treturn -EOPNOTSUPP;\n--\ndrivers/vhost/vsock.c=995=static struct miscdevice vhost_vsock_misc = {\ndrivers/vhost/vsock.c:996:\t.minor = VHOST_VSOCK_MINOR,\ndrivers/vhost/vsock.c-997-\t.name = \"vhost-vsock\",\n--\ndrivers/vhost/vsock.c=1029=MODULE_DESCRIPTION(\"vhost transport for vsock \");\ndrivers/vhost/vsock.c:1030:MODULE_ALIAS_MISCDEV(VHOST_VSOCK_MINOR);\ndrivers/vhost/vsock.c-1031-MODULE_ALIAS(\"devname:vhost-vsock\");\n"
}

9/2 2026/10/21 10:16 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 30935
							output: 34
							thoughts: 22
						

Thoughts:
Okay, here's my summary, tailored for an expert audience, presented as if it were my thought process:

**Diving into the Vhost Driver Dependencies**

Alright, before I dive too deep into this vhost module, I need to get a clear picture of its dependencies. I'm going to quickly check the `drivers/vhost/Kconfig` file, specifically around line 56, as that's usually where the meat of the configuration options and their dependencies are declared. I expect to find the relationships with other kernel features or hardware support defined there. This should give me a solid foundation for understanding what this part of the driver requires to function correctly and if there are any build options I need to consider for my target setup. I'm looking for anything that might impact its inclusion or exclusion based on other kernel configurations.



10/2 2026/10/21 10:16 tool read-file
0m Args:
{
  "File": "drivers/vhost/Kconfig",
  "FirstLine": 50,
  "LineCount": 20
}

Results:
{
  "Contents": "  50:\t\tselect SG_POOL\n  51:\t\tdefault n\n  52:\t\thelp\n  53:\t\tSay M here to enable the vhost_scsi TCM fabric module\n  54:\t\tfor use with virtio-scsi guests\n  55:\t\n  56:\tconfig VHOST_VSOCK\n  57:\t\ttristate \"vhost virtio-vsock driver\"\n  58:\t\tdepends on VSOCKETS \u0026\u0026 EVENTFD\n  59:\t\tselect VHOST\n  60:\t\tselect VIRTIO_VSOCKETS_COMMON\n  61:\t\tdefault n\n  62:\t\thelp\n  63:\t\tThis kernel module can be loaded in the host kernel to provide AF_VSOCK\n  64:\t\tsockets for communicating with guests.  The guests must have the\n  65:\t\tvirtio_transport.ko driver loaded to use the virtio-vsock device.\n  66:\t\n  67:\t\tTo compile this driver as a module, choose M here: the module will be called\n  68:\t\tvhost_vsock.\n  69:\t\n"
}

11/2 2026/10/21 10:16 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 31440
							output: 42
							thoughts: 1338
						

Thoughts:
**Analysis of the vsock Patch Series for Fuzzing Potential**

Okay, let's break down this patch series. My initial thought is to assess its fuzzing potential, so I need to understand what's changing and whether it’s reachable in standard testing environments like QEMU and syzkaller.

First, I see that the core of the changes revolves around vsock (Virtual Sockets), specifically introducing new Generic Netlink operations (`VSOCK_CMD_DEV_NETNS_SET` and `VSOCK_CMD_DEV_NETNS_GET`) to manage network namespaces for guest vsock devices. This is significant because it touches both the Generic Netlink layer and the device setup itself. The patch is also modifying key vsock functions, such as `vsock_assign_transport`, `vsock_registered_transport_cid`, and those related to `/dev/vsock` ioctls. These are prime candidates for fuzzing, so that is a good sign.

Digging deeper, the patch alters how the virtio_transport handles network namespaces. We see changes to `virtio_transport_send_pkt`, `virtio_transport_stream_allow`, `virtio_transport_seqpacket_allow`, and other functions, including using helpers like `vsock_g2h_net_reachable`. These changes indicate a deeper integration of network namespace awareness, and a lot more touch points with the underlying socket and transport handling. The tracking of `vsock_g2h_net` and `netns_tracker` in `virtio_transport_rx_work` is important, because any time a namespace changes there is bound to be a race condition. Also, the inclusion of `vsock_maybe_set_connected` in receive operations is interesting, because this might change the socket state or cause issues with other code which assumes the socket state is unchanged.

Crucially, the introduction of `vsock_g2h_reset_unreachable` to reset unreachable sockets, and the related state transitions and error reporting, is a critical point. This function iterates through connected sockets and essentially performs a disconnection sweep based on network reachability. This definitely looks interesting from a security perspective. It touches the socket state and error reporting, so I'll want to pay attention here.

Finally, the patch touches locking mechanisms with `vsock_register_mutex`, `vsock_g2h_assign_mutex`, and `vsock_table_lock`, along with associated lockdep assertions. Lock-related changes are always a red flag, because it implies potential race conditions, deadlocks, and general fun stuff.

Now, the crucial question: is this reachable? Yes, all the key components – `VSOCKETS`, `VIRTIO_VSOCKETS`, `VIRTIO_VSOCKETS_COMMON`, and `VHOST_VSOCK` – are standard virtualization and networking parts and run in QEMU. Syzkaller readily fuzzes `af_vsock` sockets, `/dev/vsock` ioctls, and Generic Netlink. Network namespaces, which are central to this patch, are also standard syzkaller targets. So, we're good to go.

Analyzing based on positive/negative criteria in Section 3, I am seeing that:
- Core subsystems touched are core networking, sockets, and netlink. Check!
- Reachable code? Absolutely!
- New functionality is there: new netlink commands (`VSOCK_CMD_DEV_NETNS_SET`, `VSOCK_CMD_DEV_NETNS_GET`), a new socket connection state transition helper (`vsock_maybe_set_connected`), device netns assignment and socket disconnection sweep (`vsock_g2h_reset_unreachable`), and locking changes (`vsock_g2h_assign_mutex`, `vsock_register_mutex`). Check!
- There is complex state management, RCU referencing `vsock_g2h_net`, `netns_tracker`, and locking interactions between `vsock_g2h_assign_mutex`, `vsock_register_mutex`, `vsock_table_lock`, and socket locks (`lock_sock(sk)`). Check!
- Potential race conditions: I see them! Locking order is super important here. Specifically, `vsock_g2h_assign_mutex`, `vsock_register_mutex`, `vsock_table_lock`, `lock_sock`, and the concurrency of `vsock_g2h_reset_unreachable` with packet processing and socket state changes.

Therefore, `WorthFuzzing=true`.

Now, let's confirm the necessary configs.
I need to check the dependencies. So, `VSOCKETS`, `VIRTIO_VSOCKETS`, `VIRTIO_VSOCKETS_COMMON`, `VHOST


12/2 2026/10/21 10:16 tool grepper
0m Error:
no matches

Args:
{
  "Expression": "CONFIG_NET_NS",
  "PathPrefixes": [
    "net/vmw_vsock/",
    "drivers/vhost/"
  ]
}

Results:
{
  "error": "no matches"
}

13/2 2026/10/21 10:16 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 33858
							output: 32
							thoughts: 16
						

Thoughts:
Here's my thought process, as requested:

**Navigating the Kernel Configuration Landscape**

Okay, so I need to figure out where `NET_NS` is defined within the kernel's Kconfig system. This is a fundamental step when I'm tracking down feature configurations, dependencies, or just trying to understand how a particular network namespace option is controlled during build. No need to go any deeper for now, but this is the starting point.



14/2 2026/10/21 10:16 tool grepper
0m Args:
{
  "Expression": "config NET_NS",
  "PathPrefixes": [
    "net/",
    "init/"
  ]
}

Results:
{
  "Output": "init/Kconfig=1465=config PID_NS\n--\ninit/Kconfig-1472-\ninit/Kconfig:1473:config NET_NS\ninit/Kconfig-1474-\tbool \"Network namespace\"\n--\nnet/Kconfig.debug=3=config NET_DEV_REFCNT_TRACKER\n--\nnet/Kconfig.debug-11-\nnet/Kconfig.debug:12:config NET_NS_REFCNT_TRACKER\nnet/Kconfig.debug-13-\tbool \"Enable networking namespace refcount tracking\"\n--\nnet/nsh/Kconfig-1-# SPDX-License-Identifier: GPL-2.0-only\nnet/nsh/Kconfig:2:menuconfig NET_NSH\nnet/nsh/Kconfig-3-\ttristate \"Network Service Header (NSH) protocol\"\n"
}

15/2 2026/10/21 10:16 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 34217
							output: 37
							thoughts: 1047
						

Thoughts:
**Analyzing vsock Net Namespace Implementation**

Okay, so I need to get a handle on how vsocks are being integrated with network namespaces. First thing's first: to move devices between namespaces, I need `NET_NS`, and if I'm playing around with unprivileged user namespaces, I'll need `USER_NS` too, but `NET_NS` is the fundamental one. And since we're dealing with network namespaces, `NETLINK` and `GENERIC_NETLINK` are almost certainly in the mix when `NET` is enabled. Let's confirm `NET_NS`'s dependencies – I see it depends on `NAMESPACES` in `init/Kconfig`, so that's good.

So, the core configs I'm looking for are `VSOCKETS`, `VIRTIO_VSOCKETS`, `VHOST_VSOCK`, and naturally, `NET_NS`. I should also check if something like `VSOCKETS_LOOPBACK` is involved, since that might use vsocks as well. And yep, it does.

Now, let's drill down into the nitty-gritty: What specific functions were added or modified that are non-ubiquitous? I'll need to know which functions are unique to this feature. Here's what the diff reveals:

1.  `vsock_nl_dev_netns_set_doit`: This is a netlink doit callback for setting the network namespace of a vsock device (`VSOCK_CMD_DEV_NETNS_SET`).
2.  `vsock_nl_dev_netns_get_doit`: A netlink doit callback for retrieving the network namespace of a vsock device (`VSOCK_CMD_DEV_NETNS_GET`).
3.  `vsock_g2h_net_assign`: This is internal logic for moving the guest-to-host device to a target network namespace and resetting any unreachable sockets.
4.  `vsock_g2h_reset_unreachable`: Iterates connected sockets and marks unreachable ones with TCP_CLOSE or ECONNRESET.
5.  `vsock_maybe_set_connected`: Checks reachability and transitions a socket to TCP_ESTABLISHED state, inserting it into the connected table under a lock.
6.  `vsock_g2h_net_reachable`: Checks the reachability between two network namespaces.
7.  `vsock_g2h_net_get`: Gets a refcounted network namespace for the guest-to-host device.
8.  `vsock_g2h_net_eq`: Checks if a given network namespace matches the guest-to-host network namespace.
9.  `__vsock_find_cid`: Checks if a specific CID (Connection ID) is found within a network namespace.
10. `vhost_vsock_set_cid`: Manages host CID setting, with new network namespace CID checking.
11. `virtio_transport_rx_work`: The rx work handler within virtio_transport that's now managing network namespace tracking and refcounting.
12. `virtio_transport_send_pkt`: This is where packet sending checks the reachability with `vsock_g2h_net_reachable`.

Now, I need to understand the scope of these functions – are they static, non-static, inlined, and are they ubiquitous or not? I want to avoid anything that's widespread in the kernel, and focus on the new/modified stuff. Let's start with `vsock_nl_dev_netns_set_doit`: It's defined in `af_vsock.c`, exported via `vsock_nl_gen.h`, and called directly by genetlink split ops, so it's non-static. `vsock_nl_dev_netns_get_doit` is similar, non-static and called by genetlink split ops as well. `vsock_g2h_net_assign` is static within `af_vsock.c` and is only called by `vsock_nl_dev_netns_set_doit`. `vsock_g2h_reset_unreachable` is also static within `af_vsock.c` and only called by `vsock_g2h_net_assign`.

`vsock_maybe_set_connected` is exported, using `EXPORT_SYMBOL_GPL`. `vsock_g2h_net_reachable`, `vsock_g2h_net_get` and `__vsock_find_cid` are all also exported. `vhost_vsock_set_cid` is static in `drivers/vhost/vsock


16/2 2026/10/21 10:17 tool grepper
0m Args:
{
  "Expression": "virtio_transport_send_pkt",
  "PathPrefixes": [
    "net/vmw_vsock/"
  ]
}

Results:
{
  "Output": "net/vmw_vsock/virtio_transport.c=30=struct virtio_vsock {\n--\nnet/vmw_vsock/virtio_transport.c-61-\t/* These fields are used only in tx path in function\nnet/vmw_vsock/virtio_transport.c:62:\t * 'virtio_transport_send_pkt_work()', so to save\nnet/vmw_vsock/virtio_transport.c-63-\t * stack space in it, place both of them here. Each\n--\nnet/vmw_vsock/virtio_transport.c=157=static void\nnet/vmw_vsock/virtio_transport.c:158:virtio_transport_send_pkt_work(struct work_struct *work)\nnet/vmw_vsock/virtio_transport.c-159-{\n--\nnet/vmw_vsock/virtio_transport.c=236=static int\nnet/vmw_vsock/virtio_transport.c:237:virtio_transport_send_pkt(struct sk_buff *skb, struct net *net)\nnet/vmw_vsock/virtio_transport.c-238-{\n--\nnet/vmw_vsock/virtio_transport.c=566=static struct virtio_transport virtio_transport = {\n--\nnet/vmw_vsock/virtio_transport.c-618-\nnet/vmw_vsock/virtio_transport.c:619:\t.send_pkt = virtio_transport_send_pkt,\nnet/vmw_vsock/virtio_transport.c-620-\t.can_msgzerocopy = virtio_transport_can_msgzerocopy,\n--\nnet/vmw_vsock/virtio_transport.c=749=static void virtio_vsock_vqs_start(struct virtio_vsock *vsock)\n--\nnet/vmw_vsock/virtio_transport.c-764-\nnet/vmw_vsock/virtio_transport.c:765:\t/* virtio_transport_send_pkt() can queue packets once\nnet/vmw_vsock/virtio_transport.c-766-\t * the_virtio_vsock is set, but they won't be processed until\n--\nnet/vmw_vsock/virtio_transport.c=822=static int virtio_vsock_probe(struct virtio_device *vdev)\n--\nnet/vmw_vsock/virtio_transport.c-854-\tINIT_WORK(\u0026vsock-\u003eevent_work, virtio_transport_event_work);\nnet/vmw_vsock/virtio_transport.c:855:\tINIT_WORK(\u0026vsock-\u003esend_pkt_work, virtio_transport_send_pkt_work);\nnet/vmw_vsock/virtio_transport.c-856-\n--\nnet/vmw_vsock/virtio_transport_common.c=212=static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *info,\n--\nnet/vmw_vsock/virtio_transport_common.c-298- */\nnet/vmw_vsock/virtio_transport_common.c:299:static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,\nnet/vmw_vsock/virtio_transport_common.c-300-\t\t\t\t\t  struct virtio_vsock_pkt_info *info)\n--\nnet/vmw_vsock/virtio_transport_common.c=668=static int virtio_transport_send_credit_update(struct vsock_sock *vsk)\n--\nnet/vmw_vsock/virtio_transport_common.c-675-\nnet/vmw_vsock/virtio_transport_common.c:676:\treturn virtio_transport_send_pkt_info(vsk, \u0026info);\nnet/vmw_vsock/virtio_transport_common.c-677-}\n--\nnet/vmw_vsock/virtio_transport_common.c=1215=int virtio_transport_connect(struct vsock_sock *vsk)\n--\nnet/vmw_vsock/virtio_transport_common.c-1222-\nnet/vmw_vsock/virtio_transport_common.c:1223:\treturn virtio_transport_send_pkt_info(vsk, \u0026info);\nnet/vmw_vsock/virtio_transport_common.c-1224-}\n--\nnet/vmw_vsock/virtio_transport_common.c=1227=int virtio_transport_shutdown(struct vsock_sock *vsk, int mode)\n--\nnet/vmw_vsock/virtio_transport_common.c-1238-\nnet/vmw_vsock/virtio_transport_common.c:1239:\treturn virtio_transport_send_pkt_info(vsk, \u0026info);\nnet/vmw_vsock/virtio_transport_common.c-1240-}\n--\nnet/vmw_vsock/virtio_transport_common.c=1254=virtio_transport_stream_enqueue(struct vsock_sock *vsk,\n--\nnet/vmw_vsock/virtio_transport_common.c-1265-\nnet/vmw_vsock/virtio_transport_common.c:1266:\treturn virtio_transport_send_pkt_info(vsk, \u0026info);\nnet/vmw_vsock/virtio_transport_common.c-1267-}\n--\nnet/vmw_vsock/virtio_transport_common.c=1294=static int virtio_transport_reset(struct vsock_sock *vsk,\n--\nnet/vmw_vsock/virtio_transport_common.c-1307-\nnet/vmw_vsock/virtio_transport_common.c:1308:\treturn virtio_transport_send_pkt_info(vsk, \u0026info);\nnet/vmw_vsock/virtio_transport_common.c-1309-}\n--\nnet/vmw_vsock/virtio_transport_common.c=1318=static int virtio_transport_reset_no_sock(const struct virtio_transport *t,\n--\nnet/vmw_vsock/virtio_transport_common.c-1334-\t\t * t-\u003esend_pkt(), instead of relying on\nnet/vmw_vsock/virtio_transport_common.c:1335:\t\t * virtio_transport_send_pkt_info() to pass it. It is not needed\nnet/vmw_vsock/virtio_transport_common.c-1336-\t\t * by virtio_transport_alloc_skb().\n--\nnet/vmw_vsock/virtio_transport_common.c=1665=virtio_transport_send_response(struct vsock_sock *vsk,\n--\nnet/vmw_vsock/virtio_transport_common.c-1677-\nnet/vmw_vsock/virtio_transport_common.c:1678:\treturn virtio_transport_send_pkt_info(vsk, \u0026info);\nnet/vmw_vsock/virtio_transport_common.c-1679-}\n"
}

17/2 2026/10/21 10:17 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 38180
							output: 321
							thoughts: 1200
						

Thoughts:
**Analysis of vsock NetNS Feature for Fuzzing**

Alright, let's break this down. My goal is to determine if this new vsock netns feature is a good target for fuzzing. First, I'm checking the relevant functions that were added or modified: `vsock_nl_dev_netns_set_doit` and `vsock_nl_dev_netns_get_doit` are netlink command handlers; `vsock_g2h_net_assign` handles the netns migration itself; `vsock_g2h_reset_unreachable` resets sockets; `vsock_maybe_set_connected` is a connection handler; `vsock_g2h_net_reachable` checks reachability; `vhost_vsock_set_cid` handles host CID setting with netns check; and `virtio_transport_rx_work` updates the netns reference tracker.

These all seem highly specific to this netns feature, which is a good starting point. Now, are they suitable as FocusSymbols? I think so, because they are the critical entrypoints to the netns feature. I will confirm that all the functions are present in `/proc/kallsyms` unless they are inlined.

The critical piece here is whether we can effectively exercise the new code. These changes touch core networking functionality: specifically, handling socket connections, the socket state machine (potentially causing changes from `TCP_ESTABLISHED` to `TCP_CLOSE` and `ECONNRESET`), and more complex lock synchronization mechanisms such as `vsock_g2h_assign_mutex`, `vsock_register_mutex`, `vsock_table_lock`. On top of these low-level concerns, this feature introduces netlink API interactions, pernet operations, net namespace reference tracking via `netns_tracker`, and changes to packet handling, both reception and transmission. Therefore, I'm confident that this should be marked as `WorthFuzzing=true`.

Now, regarding configuration, I'll need to confirm that all of the related configs are correct. I know I'll need `VSOCKETS`, `VIRTIO_VSOCKETS`, `VHOST_VSOCK`, and `NET_NS`. The rest appear to be pulled in as dependencies.

Based on all of this, the final parameters for `set-results` should be:

*   `WorthFuzzing`: `true`
*   `EnableConfigs`: `["VSOCKETS", "VIRTIO_VSOCKETS", "VHOST_VSOCK", "NET_NS"]`
*   `FocusSymbols`: `[ "vsock_nl_dev_netns_set_doit", "vsock_nl_dev_netns_get_doit", "vsock_g2h_net_assign", "vsock_g2h_reset_unreachable", "vsock_maybe_set_connected", "vsock_g2h_net_reachable", "vsock_find_cid", "vhost_vsock_set_cid", "virtio_transport_rx_work" ]`
*   `Reasoning`: "New genetlink interface for moving the vsock guest-to-host device across network namespaces, new socket state transitions and reset sweep of unreachable connected sockets, refcounting with netns_tracker, and locking changes."

Okay, I'm ready to call `set-results`.



18/2 2026/10/21 10:17 tool set-results
0m Args:
{
  "EnableConfigs": [
    "VSOCKETS",
    "VIRTIO_VSOCKETS",
    "VHOST_VSOCK",
    "NET_NS"
  ],
  "FocusSymbols": [
    "vsock_nl_dev_netns_set_doit",
    "vsock_nl_dev_netns_get_doit",
    "vsock_g2h_net_assign",
    "vsock_g2h_reset_unreachable",
    "vsock_maybe_set_connected",
    "vsock_g2h_net_reachable",
    "vsock_find_cid",
    "vhost_vsock_set_cid",
    "virtio_transport_rx_work"
  ],
  "Reasoning": "The patch introduces a Generic Netlink interface (\"vsock\") to query and move the guest-to-host (G2H) vsock device across network namespaces. It introduces new locking (vsock_g2h_assign_mutex, vsock_register_mutex), namespace reference tracking (netns_tracker), connection validation (vsock_maybe_set_connected), and an active sweep that resets connected sockets that lose access to the G2H device (vsock_g2h_reset_unreachable). These modifications touch core AF_VSOCK and virtio/vhost transport logic reachable via unprivileged/privileged netlink messages and socket syscalls in virtualized QEMU environments.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "VSOCKETS",
    "VIRTIO_VSOCKETS",
    "VHOST_VSOCK",
    "NET_NS"
  ],
  "FocusSymbols": [
    "vsock_nl_dev_netns_set_doit",
    "vsock_nl_dev_netns_get_doit",
    "vsock_g2h_net_assign",
    "vsock_g2h_reset_unreachable",
    "vsock_maybe_set_connected",
    "vsock_g2h_net_reachable",
    "vsock_find_cid",
    "vhost_vsock_set_cid",
    "virtio_transport_rx_work"
  ],
  "Reasoning": "The patch introduces a Generic Netlink interface (\"vsock\") to query and move the guest-to-host (G2H) vsock device across network namespaces. It introduces new locking (vsock_g2h_assign_mutex, vsock_register_mutex), namespace reference tracking (netns_tracker), connection validation (vsock_maybe_set_connected), and an active sweep that resets connected sockets that lose access to the G2H device (vsock_g2h_reset_unreachable). These modifications touch core AF_VSOCK and virtio/vhost transport logic reachable via unprivileged/privileged netlink messages and socket syscalls in virtualized QEMU environments.",
  "WorthFuzzing": true
}

19/1 2026/10/21 10:17 action If
0m Args:
{
  "WorthFuzzing": true
}

Results:
null

20/2 2026/10/21 10:17 agent kmsan-evaluator
0m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET) allowing the guest-to-host vsock device to be assigned to specific network namespaces, along with reachability checks, namespace refcounting, and unreachable socket teardown.\n\nEvaluation of uninitialized memory and info-leak risks:\n1. User-space data transfers:\n   - In `vsock_nl_dev_netns_fill()`, the only netlink attribute emitted is `VSOCK_A_NETNS_ID` via `nla_put_s32(msg, VSOCK_A_NETNS_ID, id)`. The scalar variable `id` is explicitly assigned (`NETNSA_NSID_NOT_ASSIGNED` or `peernet2id_alloc()`) before emission and guarded by `!equal`. If `equal` is true, no attribute is sent.\n   - `vsock_nl_dev_netns_set_doit()` sends an empty Netlink reply containing only the generic netlink header.\n   - In `vsock_dev_do_ioctl()`, scalar `u32 cid` is returned by `vsock_registered_transport_cid()` and written with `put_user(cid, p)`.\n   - No uninitialized struct padding or partially initialized buffers are exposed to user space.\n2. Variables and control flow:\n   - All local variables, flags, and netns_tracker instances (`id`, `reset`, `old_tracker`, `ret`, `cid`) are initialized prior to branching or consumption.\n3. Complex allocations / buffer lengths:\n   - No new variable-length buffers or complex uninitialized data structures are introduced or modified.\n\nKASAN vs KMSAN applicability:\n- The predominant risk surface in this series relates to concurrency, locking hierarchies (`vsock_g2h_assign_mutex`, `vsock_register_mutex`, `vsock_table_lock`, `rx_lock`), netns reference counting, and socket lifecycle / use-after-free during device movement. These are detected by KASAN, LOCKDEP, and refcount tracking.\n- There are no uninitialized memory usage or info-leak risks requiring a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 6d3d4d152a343e619168df57f7806299fa392909
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Oct 21 10:16:14 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst
index fe43e85959583..eb60903e4eb23 100644
--- a/Documentation/admin-guide/sysctl/net.rst
+++ b/Documentation/admin-guide/sysctl/net.rst
@@ -529,6 +529,48 @@ their hosts. The behavior of VSOCK sockets in a network namespace is determined
 by the namespace's mode (``global`` or ``local``), which controls how CIDs
 (Context IDs) are allocated and how sockets interact across namespaces.
 
+In a guest, the vsock device owned by the guest-to-host (G2H) transport belongs
+to one network namespace at a time. The ``VSOCK_CMD_DEV_NETNS_SET`` command of
+the ``vsock`` netlink family, described in
+Documentation/netlink/specs/vsock.yaml, moves it to the namespace the request
+was sent from, which requires ``CAP_NET_ADMIN`` in the initial user namespace.
+The namespace's mode decides who may then use the device:
+
+- ``global`` - every ``global`` mode namespace may use it.
+- ``local`` - only that namespace may use it, which reserves the connection to
+  the host for it alone.
+
+The device starts out in init_net, so until the command is issued nothing has
+moved and no mode has changed.
+
+Support is transport dependent. A G2H transport that does not implement the
+move refuses the command with ``EOPNOTSUPP``. Of the in-tree guest transports,
+only virtio-vsock implements it.
+
+A namespace that cannot reach the device sees the vsock addresses it would see
+if no G2H transport were loaded: the device's CID is not one of its local CIDs,
+and with ``vsock_loopback`` loaded, ``VMADDR_CID_HOST`` (2) is served by
+loopback.
+
+Connections established before the move, from a namespace that can no longer
+reach the device, are closed with ``ECONNRESET``. Nothing is sent to the peer,
+which is what already happens when the device is removed altogether: the peer
+finds out on its next send, which no longer matches a socket and so is answered
+with a reset. A peer that only receives waits for its own timeout. A connection
+still being set up when the device moves is refused with a reset once the peer
+answers it.
+
+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.
+
+The move is refused with ``EADDRINUSE`` if a guest registered in the target
+namespace already answers to the device's CID, since the CID would otherwise
+name two different peers there. If a loaded H2G transport cannot check that,
+the move is refused with ``EOPNOTSUPP``. Of the in-tree host transports, only
+vhost-vsock can.
+
 ns_mode
 -------
 
diff --git a/Documentation/netlink/specs/vsock.yaml b/Documentation/netlink/specs/vsock.yaml
new file mode 100644
index 0000000000000..74cf2472bf917
--- /dev/null
+++ b/Documentation/netlink/specs/vsock.yaml
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+#
+# Copyright (c) 2026 Meta Platforms, Inc. and affiliates
+#
+---
+name: vsock
+
+doc: |
+  Control interface for the vsock (AF_VSOCK) core.
+
+protocol: genetlink
+
+uapi-header: linux/vsock.h
+
+attribute-sets:
+  -
+    name: vsock
+    attributes:
+      -
+        name: netns-id
+        type: s32
+        doc: |
+          Network namespace the guest's vsock device is assigned to, as an
+          nsid relative to the caller.
+
+          Absent when the device is in the caller's own namespace, and
+          NETNSA_NSID_NOT_ASSIGNED when the g2h transport supports netns
+          assignment and the caller's namespace cannot reach the device under
+          the mode rules, or when no nsid could be allocated for it.
+
+operations:
+  list:
+    -
+      name: dev-netns-set
+      doc: |
+        Set the network namespace of the guest's vsock device.
+
+        The device is moved to the namespace the request was sent from. It
+        starts out in init_net, and moving it back there is how an
+        assignment is undone.
+
+        A g2h transport that does not support netns assignment returns
+        EOPNOTSUPP. If the netns assignment would cause a CID collision, it
+        is rejected with EADDRINUSE.
+
+        The namespace's mode decides who may then use the device: a global
+        mode namespace shares it with every other global mode namespace,
+        while a local mode namespace reserves it for itself.
+
+        Movement to an unreachable local namespace results in sockets being
+        closed with ECONNRESET. Peers are not guaranteed to be notified via
+        an RST.
+
+        The assignment holds a reference on the namespace.
+      attribute-set: vsock
+      flags: [admin-perm]
+      do:
+        reply:
+          attributes:
+            - netns-id
+    -
+      name: dev-netns-get
+      doc: |
+        Get the network namespace the guest's vsock device is assigned to.
+
+        The namespace is reported as an nsid relative to the caller, so the
+        attribute is absent when the device is already in the caller's own
+        namespace. If the g2h transport supports netns assignment, a caller
+        whose namespace cannot reach the device is told
+        NETNSA_NSID_NOT_ASSIGNED rather than where the device went.
+      attribute-set: vsock
+      do:
+        reply:
+          attributes:
+            - netns-id
diff --git a/MAINTAINERS b/MAINTAINERS
index 7636ab50d44fb..6bca71c935cd0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -29169,10 +29169,12 @@ M:	Stefano Garzarella <sgarzare@redhat.com>
 L:	virtualization@lists.linux.dev
 L:	netdev@vger.kernel.org
 S:	Maintained
+F:	Documentation/netlink/specs/vsock.yaml
 F:	drivers/net/vsockmon.c
 F:	include/net/af_vsock.h
 F:	include/uapi/linux/vm_sockets.h
 F:	include/uapi/linux/vm_sockets_diag.h
+F:	include/uapi/linux/vsock.h
 F:	include/uapi/linux/vsockmon.h
 F:	net/vmw_vsock/
 F:	tools/testing/selftests/vsock/
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index abed1fbcf66cc..11893f1943c69 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -91,10 +91,8 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net)
 	return NULL;
 }
 
-static bool vhost_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid)
+static bool vhost_transport_has_remote_cid(struct net *net, u32 cid)
 {
-	struct sock *sk = sk_vsock(vsk);
-	struct net *net = sock_net(sk);
 	bool found;
 
 	rcu_read_lock();
@@ -817,6 +815,7 @@ static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
 static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 {
 	struct vhost_vsock *other;
+	int ret;
 
 	/* Refuse reserved CIDs */
 	if (guest_cid <= VMADDR_CID_HOST ||
@@ -830,15 +829,20 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	/* Refuse if CID is assigned to the guest->host transport (i.e. nested
 	 * VM), to make the loopback work.
 	 */
-	if (vsock_find_cid(guest_cid))
-		return -EADDRINUSE;
+	mutex_lock(&vsock_register_mutex);
+
+	if (__vsock_find_cid(vsock->net, guest_cid)) {
+		ret = -EADDRINUSE;
+		goto unlock;
+	}
 
 	/* Refuse if CID is already in use */
 	mutex_lock(&vhost_vsock_mutex);
 	other = vhost_vsock_get(guest_cid, vsock->net);
 	if (other && other != vsock) {
 		mutex_unlock(&vhost_vsock_mutex);
-		return -EADDRINUSE;
+		ret = -EADDRINUSE;
+		goto unlock;
 	}
 
 	if (vsock->guest_cid)
@@ -848,7 +852,12 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
 	mutex_unlock(&vhost_vsock_mutex);
 
-	return 0;
+	ret = 0;
+
+unlock:
+	mutex_unlock(&vsock_register_mutex);
+
+	return ret;
 }
 
 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 5549298c1ec6b..883a98dab36d4 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -9,6 +9,7 @@
 #define __AF_VSOCK_H__
 
 #include <linux/kernel.h>
+#include <linux/mutex.h>
 #include <linux/workqueue.h>
 #include <net/netns/vsock.h>
 #include <net/sock.h>
@@ -22,6 +23,7 @@
 extern struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
 extern struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
 extern spinlock_t vsock_table_lock;
+extern struct mutex vsock_register_mutex;
 
 #define vsock_sk(__sk)    ((struct vsock_sock *)__sk)
 #define sk_vsock(__vsk)   (&(__vsk)->sk)
@@ -183,13 +185,16 @@ struct vsock_transport {
 	 * CIDs via the hypervisor (i.e. supports the fallback overlay). VMCI
 	 * does not implement this as it only serves CIDs 0 and 2.
 	 */
-	bool (*has_remote_cid)(struct vsock_sock *vsk, u32 remote_cid);
+	bool (*has_remote_cid)(struct net *net, u32 remote_cid);
 
 	/* Read a single skb */
 	int (*read_skb)(struct vsock_sock *, skb_read_actor_t);
 
 	/* Zero-copy. */
 	bool (*msgzerocopy_allow)(void);
+
+	/* True if the G2H transport honours VSOCK_CMD_DEV_NETNS_SET. */
+	bool netns_assign_allow;
 };
 
 /**** CORE ****/
@@ -233,11 +238,17 @@ bool vsock_check_source(const struct vsock_sock *vsk,
 			const struct vsock_transport *transport,
 			const struct sockaddr_vm *src);
 void vsock_remove_sock(struct vsock_sock *vsk);
-void vsock_for_each_connected_socket(struct vsock_transport *transport,
+void vsock_for_each_connected_socket(const struct vsock_transport *transport,
 				     void (*fn)(struct sock *sk));
 int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);
-bool vsock_find_cid(unsigned int cid);
+bool vsock_find_cid(struct net *net, unsigned int cid);
+/* Caller must hold vsock_register_mutex. */
+bool __vsock_find_cid(struct net *net, unsigned int cid);
 void vsock_linger(struct sock *sk);
+struct net *vsock_g2h_net_get(netns_tracker *tracker);
+bool vsock_g2h_net_eq(const struct net *net);
+bool vsock_g2h_net_reachable(struct net *net);
+bool vsock_maybe_set_connected(struct vsock_sock *vsk);
 
 /**** TAP ****/
 
diff --git a/include/uapi/linux/vsock.h b/include/uapi/linux/vsock.h
new file mode 100644
index 0000000000000..803b9aa1f0b3a
--- /dev/null
+++ b/include/uapi/linux/vsock.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/vsock.yaml */
+/* YNL-GEN uapi header */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
+
+#ifndef _UAPI_LINUX_VSOCK_H
+#define _UAPI_LINUX_VSOCK_H
+
+#define VSOCK_FAMILY_NAME	"vsock"
+#define VSOCK_FAMILY_VERSION	1
+
+enum {
+	VSOCK_A_NETNS_ID = 1,
+
+	__VSOCK_A_MAX,
+	VSOCK_A_MAX = (__VSOCK_A_MAX - 1)
+};
+
+enum {
+	VSOCK_CMD_DEV_NETNS_SET = 1,
+	VSOCK_CMD_DEV_NETNS_GET,
+
+	__VSOCK_CMD_MAX,
+	VSOCK_CMD_MAX = (__VSOCK_CMD_MAX - 1)
+};
+
+#endif /* _UAPI_LINUX_VSOCK_H */
diff --git a/net/vmw_vsock/Makefile b/net/vmw_vsock/Makefile
index 5da74c4a9f1d1..97e2a559e2bf3 100644
--- a/net/vmw_vsock/Makefile
+++ b/net/vmw_vsock/Makefile
@@ -7,7 +7,7 @@ obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o
 obj-$(CONFIG_HYPERV_VSOCKETS) += hv_sock.o
 obj-$(CONFIG_VSOCKETS_LOOPBACK) += vsock_loopback.o
 
-vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o
+vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o vsock_nl_gen.o
 vsock-$(CONFIG_BPF_SYSCALL) += vsock_bpf.o
 
 vsock_diag-y += diag.o
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 483af15a5109e..e16e72471b4a3 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -130,6 +130,27 @@
  *       a different transport that *does* support local mode. For
  *       example, virtio-vsock may not support local mode, but the socket
  *       may still accept a connection from vhost-vsock which does.
+ *
+ * - A guest has a single vsock device, owned by the guest->host transport.
+ *   The VSOCK_CMD_DEV_NETNS_SET netlink command moves it to the namespace the
+ *   request was sent from. It starts out in init_net. The mode rules then
+ *   decide who may use it, and which namespace packets from the host are
+ *   delivered to:
+ *
+ *   - assigned to a global mode namespace - every global mode namespace may
+ *     use it. Until the command is issued nothing has moved and no mode has
+ *     changed, so the default is the behaviour that predates it.
+ *   - assigned to a local mode namespace - only that namespace may use it.
+ *     This is how a nested VM is isolated from the rest of the guest.
+ *
+ *   Connections made before an assignment, from a namespace that can no
+ *   longer reach the device, are closed with ECONNRESET. Nothing is sent to
+ *   the peer. As with device removal, it finds out on its next send.
+ *
+ *   The assignment holds a reference on the namespace, the way an open socket
+ *   does, so deleting that namespace does not move the device: its destruction
+ *   is deferred until the device moves to another namespace, on another
+ *   assignment or when the transport unregisters.
  */
 
 #include <linux/compat.h>
@@ -161,10 +182,14 @@
 #include <linux/workqueue.h>
 #include <net/sock.h>
 #include <net/af_vsock.h>
+#include <linux/net_namespace.h>
+#include <net/genetlink.h>
 #include <net/netns/vsock.h>
 #include <uapi/linux/vm_sockets.h>
 #include <uapi/asm-generic/ioctls.h>
 
+#include "vsock_nl_gen.h"
+
 #define VSOCK_NET_MODE_STR_GLOBAL "global"
 #define VSOCK_NET_MODE_STR_LOCAL "local"
 
@@ -206,7 +231,21 @@ static const struct vsock_transport *transport_g2h;
 static const struct vsock_transport *transport_dgram;
 /* Transport used for local communication */
 static const struct vsock_transport *transport_local;
-static DEFINE_MUTEX(vsock_register_mutex);
+/* Protects transport registration and the g2h device's netns. */
+DEFINE_MUTEX(vsock_register_mutex);
+EXPORT_SYMBOL_GPL(vsock_register_mutex);
+
+/* Network namespace of the g2h device. Protected by
+ * vsock_register_mutex/RCU.
+ */
+static struct net __rcu *vsock_g2h_net = RCU_INITIALIZER(&init_net);
+static netns_tracker vsock_g2h_net_tracker;
+
+/* Held across an assign's swap and reset sweep, so that a later assign cannot
+ * move the device back before the sweep has closed every socket that lost it.
+ * Must be taken before vsock_register_mutex.
+ */
+static DEFINE_MUTEX(vsock_g2h_assign_mutex);
 
 /**** UTILS ****/
 
@@ -480,7 +519,7 @@ void vsock_remove_sock(struct vsock_sock *vsk)
 }
 EXPORT_SYMBOL_GPL(vsock_remove_sock);
 
-void vsock_for_each_connected_socket(struct vsock_transport *transport,
+void vsock_for_each_connected_socket(const struct vsock_transport *transport,
 				     void (*fn)(struct sock *sk))
 {
 	int i;
@@ -553,7 +592,42 @@ void vsock_enqueue_accept(struct sock *listener, struct sock *connected)
 }
 EXPORT_SYMBOL_GPL(vsock_enqueue_accept);
 
-static bool vsock_use_local_transport(unsigned int remote_cid)
+/* Return true if @t honours namespace assignment. One that does not keeps the
+ * reachability rules it had before VSOCK_CMD_DEV_NETNS_SET existed.
+ */
+static bool vsock_net_assignable(const struct vsock_transport *t)
+{
+	return t && t->netns_assign_allow;
+}
+
+static bool vsock_g2h_unreachable(const struct vsock_transport *t,
+				  struct net *net)
+{
+	return t == transport_g2h && vsock_net_assignable(t) &&
+	       !vsock_g2h_net_reachable(net);
+}
+
+/* Return the CID of the transport in @transport, as seen from @net.
+ *
+ * Returns VMADDR_CID_ANY if the transport supports net assignment but is not
+ * reachable from the net.
+ */
+static u32
+__vsock_registered_transport_cid(const struct vsock_transport **transport,
+				 struct net *net)
+{
+	lockdep_assert_held(&vsock_register_mutex);
+
+	if (!*transport)
+		return VMADDR_CID_ANY;
+
+	if (vsock_g2h_unreachable(*transport, net))
+		return VMADDR_CID_ANY;
+
+	return (*transport)->get_local_cid();
+}
+
+static bool vsock_use_local_transport(struct net *net, unsigned int remote_cid)
 {
 	lockdep_assert_held(&vsock_register_mutex);
 
@@ -563,11 +637,10 @@ static bool vsock_use_local_transport(unsigned int remote_cid)
 	if (remote_cid == VMADDR_CID_LOCAL)
 		return true;
 
-	if (transport_g2h) {
+	if (transport_g2h && !vsock_g2h_unreachable(transport_g2h, net))
 		return remote_cid == transport_g2h->get_local_cid();
-	} else {
-		return remote_cid == VMADDR_CID_HOST;
-	}
+
+	return remote_cid == VMADDR_CID_HOST;
 }
 
 static void vsock_deassign_transport(struct vsock_sock *vsk)
@@ -626,18 +699,20 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
 		break;
 	case SOCK_STREAM:
 	case SOCK_SEQPACKET:
-		if (vsock_use_local_transport(remote_cid))
+		if (vsock_use_local_transport(sock_net(sk), remote_cid))
 			new_transport = transport_local;
 		else if (remote_cid <= VMADDR_CID_HOST ||
 			 (remote_flags & VMADDR_FLAG_TO_HOST))
 			new_transport = transport_g2h;
 		else if (transport_h2g &&
 			 (!transport_h2g->has_remote_cid ||
-			  transport_h2g->has_remote_cid(vsk, remote_cid)))
+			  transport_h2g->has_remote_cid(sock_net(sk),
+							remote_cid)))
 			new_transport = transport_h2g;
 		else if (sock_net(sk)->vsock.g2h_fallback &&
 			 transport_g2h && transport_g2h->has_remote_cid &&
-			 transport_g2h->has_remote_cid(vsk, remote_cid)) {
+			 transport_g2h->has_remote_cid(sock_net(sk),
+						       remote_cid)) {
 			vsk->remote_addr.svm_flags |= VMADDR_FLAG_TO_HOST;
 			new_transport = transport_g2h;
 		} else {
@@ -649,6 +724,11 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
 		goto err;
 	}
 
+	if (vsock_g2h_unreachable(new_transport, sock_net(sk))) {
+		ret = -ENETUNREACH;
+		goto err;
+	}
+
 	if (vsk->transport && vsk->transport == new_transport) {
 		ret = 0;
 		goto err;
@@ -715,21 +795,24 @@ EXPORT_SYMBOL_GPL(vsock_assign_transport);
  * Provide safe access to static transport_{h2g,g2h,dgram,local} callbacks.
  * Otherwise we may race with module removal. Do not use on `vsk->transport`.
  */
-static u32 vsock_registered_transport_cid(const struct vsock_transport **transport)
+static u32
+vsock_registered_transport_cid(const struct vsock_transport **transport,
+			       struct net *net)
 {
-	u32 cid = VMADDR_CID_ANY;
+	u32 cid;
 
 	mutex_lock(&vsock_register_mutex);
-	if (*transport)
-		cid = (*transport)->get_local_cid();
+	cid = __vsock_registered_transport_cid(transport, net);
 	mutex_unlock(&vsock_register_mutex);
 
 	return cid;
 }
 
-bool vsock_find_cid(unsigned int cid)
+bool __vsock_find_cid(struct net *net, unsigned int cid)
 {
-	if (cid == vsock_registered_transport_cid(&transport_g2h))
+	lockdep_assert_held(&vsock_register_mutex);
+
+	if (cid == __vsock_registered_transport_cid(&transport_g2h, net))
 		return true;
 
 	if (transport_h2g && cid == VMADDR_CID_HOST)
@@ -740,8 +823,303 @@ 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 closes 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);
+
+static bool vsock_g2h_should_reset(struct vsock_sock *vsk,
+				   const struct vsock_transport *t)
+{
+	return vsk->transport == t &&
+	       READ_ONCE(sk_vsock(vsk)->sk_state) != TCP_CLOSE &&
+	       !READ_ONCE(vsk->close_work_scheduled) &&
+	       !vsock_g2h_reachable_sk(vsk);
+}
+
+static struct vsock_sock *
+vsock_g2h_next_unreachable(const struct vsock_transport *t, unsigned int *i)
+{
+	struct vsock_sock *vsk;
+
+	spin_lock_bh(&vsock_table_lock);
+
+	for (; *i < ARRAY_SIZE(vsock_connected_table); (*i)++) {
+		list_for_each_entry(vsk, &vsock_connected_table[*i],
+				    connected_table) {
+			if (vsock_g2h_should_reset(vsk, t)) {
+				sock_hold(sk_vsock(vsk));
+				spin_unlock_bh(&vsock_table_lock);
+				return vsk;
+			}
+		}
+	}
+
+	spin_unlock_bh(&vsock_table_lock);
+
+	return NULL;
+}
+
+/* Close every connected socket of @t that can no longer reach the g2h device.
+ */
+static void vsock_g2h_reset_unreachable(const struct vsock_transport *t)
+{
+	struct vsock_sock *vsk;
+	unsigned int i = 0;
+	struct sock *sk;
+	bool reset;
+
+	lockdep_assert_held(&vsock_g2h_assign_mutex);
+	lockdep_assert_not_held(&vsock_register_mutex);
+
+	while ((vsk = vsock_g2h_next_unreachable(t, &i))) {
+		sk = sk_vsock(vsk);
+
+		lock_sock(sk);
+
+		reset = false;
+		spin_lock_bh(&vsock_table_lock);
+		if (__vsock_in_connected_table(vsk) &&
+		    vsock_g2h_should_reset(vsk, t)) {
+			__vsock_remove_connected(vsk);
+			reset = true;
+		}
+		spin_unlock_bh(&vsock_table_lock);
+
+		if (reset) {
+			sk->sk_state = TCP_CLOSE;
+			sk->sk_err = ECONNRESET;
+			sk_error_report(sk);
+		}
+
+		release_sock(sk);
+		sock_put(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 *net = genl_info_net(info);
+	struct net *assigned;
+	bool equal;
+	void *hdr;
+	s32 id;
+
+	lockdep_assert_held(&vsock_register_mutex);
+
+	rcu_read_lock();
+	assigned = rcu_dereference(vsock_g2h_net);
+	equal = net_eq(net, assigned);
+	if (!equal) {
+		if (vsock_net_assignable(transport_g2h) &&
+		    !vsock_net_check_mode(net, assigned))
+			id = NETNSA_NSID_NOT_ASSIGNED;
+		else
+			id = peernet2id_alloc(net, assigned, GFP_ATOMIC);
+	}
+	rcu_read_unlock();
+
+	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;
+	const struct vsock_transport *t;
+	netns_tracker old_tracker;
+	struct net *old;
+	void *hdr;
+	int ret;
+
+	mutex_lock(&vsock_g2h_assign_mutex);
+	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 = -EMSGSIZE;
+	hdr = genlmsg_iput(msg, info);
+	if (!hdr)
+		goto unlock;
+	genlmsg_end(msg, hdr);
+
+	ret = genlmsg_reply(msg, info);
+	msg = NULL;
+	if (ret)
+		goto unlock;
+
+	old = vsock_g2h_net_swap(net, &old_tracker);
+	t = transport_g2h;
+
+	mutex_unlock(&vsock_register_mutex);
+
+	/* See vsock_maybe_set_connected() comment about synchronizing with
+	 * connecting sockets.
+	 */
+	vsock_g2h_reset_unreachable(t);
+
+	mutex_unlock(&vsock_g2h_assign_mutex);
+
+	vsock_g2h_net_release(old, &old_tracker);
+
+	return 0;
+
+unlock:
+	mutex_unlock(&vsock_register_mutex);
+	mutex_unlock(&vsock_g2h_assign_mutex);
+	nlmsg_free(msg);
+
+	return ret;
+}
+
 static struct sock *vsock_dequeue_accept(struct sock *listener)
 {
 	struct vsock_sock *vlistener;
@@ -908,7 +1286,8 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)
 	 * like AF_INET prevents binding to a non-local IP address (in most
 	 * cases), we only allow binding to a local CID.
 	 */
-	if (addr->svm_cid != VMADDR_CID_ANY && !vsock_find_cid(addr->svm_cid))
+	if (addr->svm_cid != VMADDR_CID_ANY &&
+	    !vsock_find_cid(sock_net(sk_vsock(vsk)), addr->svm_cid))
 		return -EADDRNOTAVAIL;
 
 	switch (sk->sk_socket->type) {
@@ -2796,6 +3175,7 @@ static long vsock_dev_do_ioctl(struct file *filp,
 {
 	u32 __user *p = ptr;
 	int retval = 0;
+	struct net *net;
 	u32 cid;
 
 	switch (cmd) {
@@ -2803,11 +3183,14 @@ static long vsock_dev_do_ioctl(struct file *filp,
 		/* To be compatible with the VMCI behavior, we prioritize the
 		 * guest CID instead of well-know host CID (VMADDR_CID_HOST).
 		 */
-		cid = vsock_registered_transport_cid(&transport_g2h);
+		net = current->nsproxy->net_ns;
+		cid = vsock_registered_transport_cid(&transport_g2h, net);
 		if (cid == VMADDR_CID_ANY)
-			cid = vsock_registered_transport_cid(&transport_h2g);
+			cid = vsock_registered_transport_cid(&transport_h2g,
+							     net);
 		if (cid == VMADDR_CID_ANY)
-			cid = vsock_registered_transport_cid(&transport_local);
+			cid = vsock_registered_transport_cid(&transport_local,
+							     net);
 
 		if (put_user(cid, p) != 0)
 			retval = -EFAULT;
@@ -2848,6 +3231,44 @@ static struct miscdevice vsock_device = {
 	.fops		= &vsock_device_ops,
 };
 
+int vsock_nl_dev_netns_get_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	struct sk_buff *msg;
+	int err;
+
+	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+
+	mutex_lock(&vsock_register_mutex);
+	if (transport_g2h) {
+		err = vsock_nl_dev_netns_fill(msg, info);
+	} else {
+		NL_SET_ERR_MSG(info->extack,
+			       "no guest-to-host transport is loaded");
+		err = -ENODEV;
+	}
+	mutex_unlock(&vsock_register_mutex);
+
+	if (err) {
+		nlmsg_free(msg);
+		return err;
+	}
+
+	return genlmsg_reply(msg, info);
+}
+
+int vsock_nl_dev_netns_set_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	struct sk_buff *msg;
+
+	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+
+	return vsock_g2h_net_assign(genl_info_net(info), msg, info);
+}
+
 static int __vsock_net_mode_string(const struct ctl_table *table, int write,
 				   void *buffer, size_t *lenp, loff_t *ppos,
 				   enum vsock_net_mode mode,
@@ -3037,7 +3458,7 @@ static void vsock_net_init(struct net *net)
 	net->vsock.g2h_fallback = 1;
 }
 
-static __net_init int vsock_sysctl_init_net(struct net *net)
+static __net_init int vsock_pernet_init(struct net *net)
 {
 	vsock_net_init(net);
 
@@ -3047,14 +3468,14 @@ static __net_init int vsock_sysctl_init_net(struct net *net)
 	return 0;
 }
 
-static __net_exit void vsock_sysctl_exit_net(struct net *net)
+static __net_exit void vsock_pernet_exit(struct net *net)
 {
 	vsock_sysctl_unregister(net);
 }
 
-static struct pernet_operations vsock_sysctl_ops = {
-	.init = vsock_sysctl_init_net,
-	.exit = vsock_sysctl_exit_net,
+static struct pernet_operations vsock_pernet_ops = {
+	.init = vsock_pernet_init,
+	.exit = vsock_pernet_exit,
 };
 
 static int __init vsock_init(void)
@@ -3063,6 +3484,8 @@ static int __init vsock_init(void)
 
 	vsock_init_tables();
 
+	get_net_track(&init_net, &vsock_g2h_net_tracker, GFP_KERNEL);
+
 	vsock_proto.owner = THIS_MODULE;
 	vsock_device.minor = MISC_DYNAMIC_MINOR;
 	err = misc_register(&vsock_device);
@@ -3084,15 +3507,23 @@ static int __init vsock_init(void)
 		goto err_unregister_proto;
 	}
 
-	if (register_pernet_subsys(&vsock_sysctl_ops)) {
+	if (register_pernet_subsys(&vsock_pernet_ops)) {
 		err = -ENOMEM;
 		goto err_unregister_sock;
 	}
 
+	err = genl_register_family(&vsock_nl_family);
+	if (err) {
+		pr_err("Cannot register vsock netlink family: %d\n", err);
+		goto err_unregister_pernet;
+	}
+
 	vsock_bpf_build_proto();
 
 	return 0;
 
+err_unregister_pernet:
+	unregister_pernet_subsys(&vsock_pernet_ops);
 err_unregister_sock:
 	sock_unregister(AF_VSOCK);
 err_unregister_proto:
@@ -3100,15 +3531,20 @@ static int __init vsock_init(void)
 err_deregister_misc:
 	misc_deregister(&vsock_device);
 err_reset_transport:
+	put_net_track(&init_net, &vsock_g2h_net_tracker);
 	return err;
 }
 
 static void __exit vsock_exit(void)
 {
+	genl_unregister_family(&vsock_nl_family);
 	misc_deregister(&vsock_device);
 	sock_unregister(AF_VSOCK);
 	proto_unregister(&vsock_proto);
-	unregister_pernet_subsys(&vsock_sysctl_ops);
+	unregister_pernet_subsys(&vsock_pernet_ops);
+
+	put_net_track(rcu_dereference_protected(vsock_g2h_net, true),
+		      &vsock_g2h_net_tracker);
 }
 
 const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk)
@@ -3175,13 +3611,18 @@ EXPORT_SYMBOL_GPL(vsock_core_register);
 
 void vsock_core_unregister(const struct vsock_transport *t)
 {
+	netns_tracker old_tracker;
+	struct net *old = NULL;
+
 	mutex_lock(&vsock_register_mutex);
 
 	if (transport_h2g == t)
 		transport_h2g = NULL;
 
-	if (transport_g2h == t)
+	if (transport_g2h == t) {
 		transport_g2h = NULL;
+		old = vsock_g2h_net_swap(&init_net, &old_tracker);
+	}
 
 	if (transport_dgram == t)
 		transport_dgram = NULL;
@@ -3190,6 +3631,10 @@ void vsock_core_unregister(const struct vsock_transport *t)
 		transport_local = NULL;
 
 	mutex_unlock(&vsock_register_mutex);
+
+	/* The device is gone, so release the namespace it was holding. */
+	if (old)
+		vsock_g2h_net_release(old, &old_tracker);
 }
 EXPORT_SYMBOL_GPL(vsock_core_unregister);
 
diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 4f9aa9c4c3aa5..06df82b130cff 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -256,6 +256,15 @@ virtio_transport_send_pkt(struct sk_buff *skb, struct net *net)
 		goto out_rcu;
 	}
 
+	/* A socket reset by an assign can still send credit updates from
+	 * recvmsg() and setsockopt(SO_RCVLOWAT).
+	 */
+	if (!vsock_g2h_net_reachable(net)) {
+		kfree_skb(skb);
+		len = -ENETUNREACH;
+		goto out_rcu;
+	}
+
 	/* If send_pkt_queue is empty, we can safely bypass this queue
 	 * because packet order is maintained and (try) to put the packet
 	 * on the virtqueue using virtio_transport_send_skb_fast_path.
@@ -542,13 +551,13 @@ static bool virtio_transport_msgzerocopy_allow(void)
 
 bool virtio_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port)
 {
-	return vsock_net_mode_global(vsk);
+	return vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));
 }
 
 static bool virtio_transport_seqpacket_allow(struct vsock_sock *vsk,
 					     u32 remote_cid);
 
-static bool virtio_transport_has_remote_cid(struct vsock_sock *vsk, u32 cid)
+static bool virtio_transport_has_remote_cid(struct net *net, u32 cid)
 {
 	/* The CID could be implemented by the host. Always assume it is. */
 	return true;
@@ -587,6 +596,7 @@ static struct virtio_transport virtio_transport = {
 		.seqpacket_has_data       = virtio_transport_seqpacket_has_data,
 
 		.msgzerocopy_allow        = virtio_transport_msgzerocopy_allow,
+		.netns_assign_allow       = true,
 
 		.notify_poll_in           = virtio_transport_notify_poll_in,
 		.notify_poll_out          = virtio_transport_notify_poll_out,
@@ -616,7 +626,7 @@ virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid)
 	struct virtio_vsock *vsock;
 	bool seqpacket_allow;
 
-	if (!vsock_net_mode_global(vsk))
+	if (!vsock_g2h_net_reachable(sock_net(sk_vsock(vsk))))
 		return false;
 
 	seqpacket_allow = false;
@@ -633,7 +643,12 @@ static void virtio_transport_rx_work(struct work_struct *work)
 {
 	struct virtio_vsock *vsock =
 		container_of(work, struct virtio_vsock, rx_work);
+	struct virtio_transport *t = &virtio_transport;
+	netns_tracker ns_tracker;
 	struct virtqueue *vq;
+	struct net *net;
+
+	net = vsock_g2h_net_get(&ns_tracker);
 
 	mutex_lock(&vsock->rx_lock);
 
@@ -682,10 +697,15 @@ static void virtio_transport_rx_work(struct work_struct *work)
 
 			virtio_transport_deliver_tap_pkt(skb);
 
-			/* Force virtio-transport into global mode since it
-			 * does not yet support local-mode namespacing.
+			/* Only grab a reference if the net changes underneath
+			 * us.
 			 */
-			virtio_transport_recv_pkt(&virtio_transport, skb, NULL);
+			if (unlikely(!vsock_g2h_net_eq(net))) {
+				put_net_track(net, &ns_tracker);
+				net = vsock_g2h_net_get(&ns_tracker);
+			}
+
+			virtio_transport_recv_pkt(t, skb, net);
 		}
 	} while (!virtqueue_enable_cb(vq));
 
@@ -694,6 +714,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 		virtio_vsock_rx_fill(vsock);
 out_nofill:
 	mutex_unlock(&vsock->rx_lock);
+
+	put_net_track(net, &ns_tracker);
 }
 
 static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 1e762a480df49..0c864db2bc65b 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1478,9 +1478,13 @@ virtio_transport_recv_connecting(struct sock *sk,
 
 	switch (le16_to_cpu(hdr->op)) {
 	case VIRTIO_VSOCK_OP_RESPONSE:
-		sk->sk_state = TCP_ESTABLISHED;
+		if (!vsock_maybe_set_connected(vsk)) {
+			skerr = ECONNRESET;
+			err = -ENETUNREACH;
+			goto destroy;
+		}
+
 		sk->sk_socket->state = SS_CONNECTED;
-		vsock_insert_connected(vsk);
 		sk->sk_state_change(sk);
 		break;
 	case VIRTIO_VSOCK_OP_INVALID:
@@ -1736,8 +1740,6 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,
 
 	lock_sock_nested(child, SINGLE_DEPTH_NESTING);
 
-	child->sk_state = TCP_ESTABLISHED;
-
 	vchild = vsock_sk(child);
 	vsock_addr_init(&vchild->local_addr, le64_to_cpu(hdr->dst_cid),
 			le32_to_cpu(hdr->dst_port));
@@ -1758,7 +1760,13 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,
 	if (virtio_transport_space_update(child, skb))
 		child->sk_write_space(child);
 
-	vsock_insert_connected(vchild);
+	if (!vsock_maybe_set_connected(vchild)) {
+		release_sock(child);
+		virtio_transport_reset_no_sock(t, skb, sock_net(sk));
+		sock_put(child);
+		return -ENETUNREACH;
+	}
+
 	vsock_enqueue_accept(sk, child);
 	virtio_transport_send_response(vchild, skb);
 
diff --git a/net/vmw_vsock/vsock_nl_gen.c b/net/vmw_vsock/vsock_nl_gen.c
new file mode 100644
index 0000000000000..81db4c61d7bb9
--- /dev/null
+++ b/net/vmw_vsock/vsock_nl_gen.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/vsock.yaml */
+/* YNL-GEN kernel source */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include "vsock_nl_gen.h"
+
+#include <uapi/linux/vsock.h>
+
+/* Ops table for vsock */
+static const struct genl_split_ops vsock_nl_ops[] = {
+	{
+		.cmd	= VSOCK_CMD_DEV_NETNS_SET,
+		.doit	= vsock_nl_dev_netns_set_doit,
+		.flags	= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
+	{
+		.cmd	= VSOCK_CMD_DEV_NETNS_GET,
+		.doit	= vsock_nl_dev_netns_get_doit,
+		.flags	= GENL_CMD_CAP_DO,
+	},
+};
+
+struct genl_family vsock_nl_family __ro_after_init = {
+	.name		= VSOCK_FAMILY_NAME,
+	.version	= VSOCK_FAMILY_VERSION,
+	.netnsok	= true,
+	.parallel_ops	= true,
+	.module		= THIS_MODULE,
+	.split_ops	= vsock_nl_ops,
+	.n_split_ops	= ARRAY_SIZE(vsock_nl_ops),
+};
diff --git a/net/vmw_vsock/vsock_nl_gen.h b/net/vmw_vsock/vsock_nl_gen.h
new file mode 100644
index 0000000000000..c041195584a09
--- /dev/null
+++ b/net/vmw_vsock/vsock_nl_gen.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/* Do not edit directly, auto-generated from: */
+/*	Documentation/netlink/specs/vsock.yaml */
+/* YNL-GEN kernel header */
+/* To regenerate run: tools/net/ynl/ynl-regen.sh */
+
+#ifndef _LINUX_VSOCK_GEN_H
+#define _LINUX_VSOCK_GEN_H
+
+#include <net/netlink.h>
+#include <net/genetlink.h>
+
+#include <uapi/linux/vsock.h>
+
+int vsock_nl_dev_netns_set_doit(struct sk_buff *skb, struct genl_info *info);
+int vsock_nl_dev_netns_get_doit(struct sk_buff *skb, struct genl_info *info);
+
+extern struct genl_family vsock_nl_family;
+
+#endif /* _LINUX_VSOCK_GEN_H */
diff --git a/tools/net/ynl/Makefile.deps b/tools/net/ynl/Makefile.deps
index 3eee34e0efcf9..f3150b41dd333 100644
--- a/tools/net/ynl/Makefile.deps
+++ b/tools/net/ynl/Makefile.deps
@@ -62,4 +62,5 @@ CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \
 	$(call get_hdr_inc,_TC_SKBEDIT_H,tc_act/tc_skbedit.h) \
 	$(call get_hdr_inc,_TC_TUNNEL_KEY_H,tc_act/tc_tunnel_key.h)
 CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)
+CFLAGS_vsock:=$(call get_hdr_inc,_LINUX_VSOCK_H,vsock.h)
 CFLAGS_wireguard:=$(call get_hdr_inc2,_LINUX_WIREGUARD_H,_WG_UAPI_WIREGUARD_H,wireguard.h)
diff --git a/tools/testing/selftests/vsock/Makefile b/tools/testing/selftests/vsock/Makefile
index c407c0afd9388..2b066ba91bcd7 100644
--- a/tools/testing/selftests/vsock/Makefile
+++ b/tools/testing/selftests/vsock/Makefile
@@ -13,5 +13,10 @@ $(VSOCK_TEST_DIR)/vsock_test: $(VSOCK_TEST_SRCS)
 TEST_PROGS += vmtest.sh
 TEST_GEN_FILES := vsock_test
 
+TEST_FILES := \
+	../../../net/ynl/pyynl/cli.py \
+	../../../net/ynl/pyynl/lib \
+	../../../../Documentation/netlink/specs/vsock.yaml
+
 include ../lib.mk
 
diff --git a/tools/testing/selftests/vsock/config b/tools/testing/selftests/vsock/config
index 5f0a4f17dfc96..4b31085558faa 100644
--- a/tools/testing/selftests/vsock/config
+++ b/tools/testing/selftests/vsock/config
@@ -109,3 +109,4 @@ CONFIG_FS_DAX=y
 CONFIG_MEMORY_HOTPLUG=y
 CONFIG_MEMORY_HOTREMOVE=y
 CONFIG_ZONE_DEVICE=y
+CONFIG_USER_NS=y
diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
index 310dfc2a39ad5..5bd90a0c23802 100755
--- a/tools/testing/selftests/vsock/vmtest.sh
+++ b/tools/testing/selftests/vsock/vmtest.sh
@@ -17,6 +17,20 @@ readonly KERNEL_CHECKOUT=$(realpath "${SCRIPT_DIR}"/../../../../)
 source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh
 
 readonly VSOCK_TEST="${SCRIPT_DIR}"/vsock_test
+
+# "make install" copies the ynl CLI, its lib package and the spec next to this
+# script (see TEST_FILES); in a source tree they stay where they are built.
+if [[ -r "${SCRIPT_DIR}"/cli.py ]]; then
+	readonly YNL_CLI="${SCRIPT_DIR}"/cli.py
+	readonly YNL_LIB="${SCRIPT_DIR}"/lib
+	readonly VSOCK_SPEC="${SCRIPT_DIR}"/vsock.yaml
+else
+	readonly YNL_DIR="${KERNEL_CHECKOUT}"/tools/net/ynl/pyynl
+	readonly YNL_CLI="${YNL_DIR}"/cli.py
+	readonly YNL_LIB="${YNL_DIR}"/lib
+	readonly SPEC_DIR="${KERNEL_CHECKOUT}"/Documentation/netlink/specs
+	readonly VSOCK_SPEC="${SPEC_DIR}"/vsock.yaml
+fi
 readonly TEST_GUEST_PORT=51000
 readonly TEST_HOST_PORT=50000
 readonly TEST_HOST_PORT_LISTENER=50001
@@ -73,6 +87,14 @@ readonly TEST_NAMES=(
 	ns_delete_vm_ok
 	ns_delete_host_ok
 	ns_delete_both_ok
+	ns_guest_local_connect_to_host_fails
+	ns_guest_assign_g2h_netns_connect_to_host_ok
+	ns_guest_assign_g2h_netns_init_ns_connect_fails
+	ns_guest_assign_g2h_netns_host_connect_ok
+	ns_guest_assign_g2h_netns_pins_ns_ok
+	ns_guest_assign_g2h_netns_old_conn_send_fails
+	ns_guest_assign_g2h_netns_no_cap_net_admin_fails
+	ns_guest_assign_g2h_netns_unpriv_user_ns_fails
 )
 readonly TEST_DESCS=(
 	# vm_server_host_client
@@ -149,12 +171,44 @@ readonly TEST_DESCS=(
 
 	# ns_delete_both_ok
 	"Check that deleting the VM and host's namespaces does not break the socket connection"
+
+	# ns_guest_local_connect_to_host_fails
+	"Check a guest process in a local ns cannot reach the host without the netns assign."
+
+	# ns_guest_assign_g2h_netns_connect_to_host_ok
+	"Check a guest process in a local ns reaches the host once the vsock device is assigned to it."
+
+	# ns_guest_assign_g2h_netns_init_ns_connect_fails
+	"Check the guest's initial ns loses vsock once the device is assigned to another ns."
+
+	# ns_guest_assign_g2h_netns_host_connect_ok
+	"Check the host reaches a guest listener in the ns the vsock device is assigned to."
+
+	# ns_guest_assign_g2h_netns_pins_ns_ok
+	"Check the assignment keeps the ns alive when deleted, and that reassigning releases it."
+
+	# ns_guest_assign_g2h_netns_old_conn_send_fails
+	"Check connections made before the assign stop sending once they lose the device."
+
+	# ns_guest_assign_g2h_netns_no_cap_net_admin_fails
+	"Check assigning the guest's vsock device to a namespace needs CAP_NET_ADMIN."
+
+	# ns_guest_assign_g2h_netns_unpriv_user_ns_fails
+	"Check an unprivileged user cannot claim the guest's vsock device via a user ns."
 )
 
 readonly USE_SHARED_VM=(
 	vm_server_host_client
 	vm_client_host_server
 	vm_loopback
+	ns_guest_local_connect_to_host_fails
+	ns_guest_assign_g2h_netns_connect_to_host_ok
+	ns_guest_assign_g2h_netns_init_ns_connect_fails
+	ns_guest_assign_g2h_netns_host_connect_ok
+	ns_guest_assign_g2h_netns_pins_ns_ok
+	ns_guest_assign_g2h_netns_old_conn_send_fails
+	ns_guest_assign_g2h_netns_no_cap_net_admin_fails
+	ns_guest_assign_g2h_netns_unpriv_user_ns_fails
 )
 readonly NS_MODES=("local" "global")
 
@@ -316,6 +370,34 @@ check_deps() {
 	fi
 }
 
+check_ns_guest_deps() {
+	local tname=$1
+	local dep
+
+	if [[ ! "${tname}" =~ ^ns_guest_ ]]; then
+		return 0
+	fi
+
+	for dep in unshare setpriv python3; do
+		if [[ ! -x $(command -v "${dep}") ]]; then
+			log_host "no ${dep} for test ${tname}"
+			return 1
+		fi
+	done
+
+	if ! python3 -c "import yaml" &>/dev/null; then
+		log_host "no python3 yaml module for test ${tname}"
+		return 1
+	fi
+
+	if [[ ! -r "${YNL_CLI}" ]] || [[ ! -r "${VSOCK_SPEC}" ]]; then
+		log_host "no ynl cli or vsock spec for test ${tname}"
+		return 1
+	fi
+
+	return 0
+}
+
 check_netns() {
 	local tname=$1
 
@@ -401,6 +483,19 @@ setup_home() {
 	mkdir -p "$(dirname "${SSH_KEY_PATH}")"
 	ssh-keygen -t ed25519 -f "${SSH_KEY_PATH}" -N "" -q
 	cp "${VSOCK_TEST}" "${TEST_HOME}"/vsock_test
+
+	mkdir -p "${TEST_HOME}"/ynl
+	if [[ -r "${YNL_CLI}" ]]; then
+		cp "${YNL_CLI}" "${TEST_HOME}"/ynl/
+		cp -r "${YNL_LIB}" "${TEST_HOME}"/ynl/
+		cp "${VSOCK_SPEC}" "${TEST_HOME}"/ynl/
+	fi
+
+	# One of the tests runs the CLI as an unprivileged user, so the CLI
+	# has to be reachable by one.
+	chmod -R a+rX "${TEST_HOME}"/ynl
+
+	chmod 755 "${TEST_HOME}"
 }
 
 create_pidfile() {
@@ -528,6 +623,82 @@ vm_wait_for_ssh() {
 	done
 }
 
+# Create a local mode namespace in the VM and echo the pid holding it open.
+vm_ns_start() {
+	local ns=$1
+	local initns
+	local nspid
+	local newns
+	local i
+
+	local mode=/proc/sys/net/vsock/child_ns_mode
+
+	if ! vm_ssh "${ns}" -- "echo local > ${mode}" &>/dev/null; then
+		return
+	fi
+
+	nspid=$(vm_ssh "${ns}" -- "unshare -n sleep infinity" \
+		'>/dev/null 2>&1 & echo $!')
+	if [[ -z "${nspid}" ]]; then
+		return
+	fi
+
+	initns=$(vm_ssh "${ns}" -- "readlink /proc/self/ns/net")
+	for ((i = 0; i < WAIT_PERIOD_MAX; i++)); do
+		newns=$(vm_ssh "${ns}" -- "readlink /proc/${nspid}/ns/net" \
+			2>/dev/null)
+		if [[ -n "${newns}" ]] && [[ "${newns}" != "${initns}" ]]; then
+			echo "${nspid}"
+			return
+		fi
+		sleep 1
+	done
+
+	vm_ns_stop "${ns}" "${nspid}"
+}
+
+# Returns once the holder is gone, so that the namespace is unreferenced and
+# the kernel can start tearing it down.
+vm_ns_stop() {
+	local ns=$1
+	local nspid=$2
+
+	vm_ssh "${ns}" <<-EOF &>/dev/null
+		kill ${nspid}
+		for ((i = 0; i < ${WAIT_PERIOD_MAX}; i++)); do
+			kill -0 ${nspid} 2>/dev/null || break
+			sleep 1
+		done
+	EOF
+}
+
+# Runs in the guest's initial namespace when <nspid> is empty. The command must
+# not contain single quotes.
+vm_ns_exec() {
+	local ns=$1
+	local nspid=$2
+	local cmd=$3
+
+	if [[ -z "${nspid}" ]]; then
+		vm_ssh "${ns}" -- "${cmd}"
+		return
+	fi
+
+	vm_ssh "${ns}" -- nsenter -t "${nspid}" -n sh -c "'${cmd}'"
+}
+
+vm_ns_assign_g2h() {
+	local ns=$1
+	local nspid=$2
+
+	vm_ns_exec "${ns}" "${nspid}" "python3 /root/ynl/cli.py --no-schema \
+		--spec /root/ynl/vsock.yaml --do dev-netns-set"
+}
+
+vm_reset_g2h() {
+	vm_ns_assign_g2h "init_ns" "" &>/dev/null
+}
+
 # derived from selftests/net/net_helper.sh
 wait_for_listener()
 {
@@ -564,17 +735,33 @@ wait_for_listener()
 	done
 }
 
-vm_wait_for_listener() {
+# Runs in the guest's initial namespace when <nspid> is empty.
+vm_ns_wait_for_listener() {
 	local ns=$1
-	local port=$2
-	local protocol=$3
+	local nspid=$2
+	local port=$3
+	local protocol=$4
+	local nsenter=
+	local args
+
+	[[ -n "${nspid}" ]] && nsenter="nsenter -t ${nspid} -n"
+	args="${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol}"
 
 	vm_ssh "${ns}" <<EOF
 $(declare -f wait_for_listener)
-wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol}
+export -f wait_for_listener
+${nsenter} bash -c "wait_for_listener ${args}"
 EOF
 }
 
+vm_wait_for_listener() {
+	local ns=$1
+	local port=$2
+	local protocol=$3
+
+	vm_ns_wait_for_listener "${ns}" "" "${port}" "${protocol}"
+}
+
 host_wait_for_listener() {
 	local ns=$1
 	local port=$2
@@ -1421,6 +1608,355 @@ test_ns_delete_both_ok() {
 	check_ns_delete_doesnt_break_connection "both"
 }
 
+# Send a string from the guest to a host listener and leave what the host
+# received in <outfile>.
+guest_send_to_host() {
+	local ns=$1
+	local nspid=$2
+	local port=$3
+	local outfile=$4
+	local cmd="echo TEST | socat -u STDIN VSOCK-CONNECT:2:${port}"
+	local pid
+
+	socat -u VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" 2>/dev/null &
+	pid=$!
+	host_wait_for_listener "${ns}" "${port}" "vsock"
+
+	vm_ns_exec "${ns}" "${nspid}" "${cmd}" 2>/dev/null
+
+	timeout "${WAIT_PERIOD}" \
+		bash -c 'while [[ ! -s '"${outfile}"' ]]; do sleep 1; done'
+
+	# Wait for the listener to actually go, or the next test binding the
+	# same port races it and silently reads nothing.
+	terminate_pids "${pid}"
+	wait "${pid}" 2>/dev/null
+}
+
+# Send a string from the host to a listener in the guest and leave what the
+# guest received in <outfile>.
+host_send_to_guest() {
+	local ns=$1
+	local nspid=$2
+	local port=$3
+	local outfile=$4
+	local cmd="socat -u VSOCK-LISTEN:${port} STDOUT"
+	local dst="VSOCK-CONNECT:${VSOCK_CID}:${port}"
+	local pid
+
+	vm_ns_exec "${ns}" "${nspid}" "${cmd}" > "${outfile}" 2>/dev/null &
+	pid=$!
+	vm_ns_wait_for_listener "${ns}" "${nspid}" "${port}" "vsock"
+
+	echo TEST | socat -u STDIN "${dst}" 2>/dev/null
+
+	timeout "${WAIT_PERIOD}" \
+		bash -c 'while [[ ! -s '"${outfile}"' ]]; do sleep 1; done'
+
+	vm_ns_exec "${ns}" "${nspid}" \
+		"pkill -f VSOCK-LISTEN:${port}" &>/dev/null
+	terminate_pids "${pid}"
+	wait "${pid}" 2>/dev/null
+}
+
+ns_old_conn_cleanup() {
+	local port=$1
+	local pid=$2
+	local sender=$3
+	local outfile=$4
+	local nspid=$5
+
+	vm_ns_exec "init_ns" "" "pkill -f VSOCK-CONNECT:2:${port}" &>/dev/null
+	terminate_pids "${pid}" "${sender}"
+	wait "${pid}" "${sender}" 2>/dev/null
+	rm -f "${outfile}"
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+}
+
+test_ns_guest_assign_g2h_netns_old_conn_send_fails() {
+	local gap=$(( WAIT_PERIOD * 3 ))
+	local port=12346
+	local elapsed
+	local started
+	local outfile
+	local result
+	local sender
+	local nspid
+	local pid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	socat -u VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" 2>/dev/null &
+	pid=$!
+	host_wait_for_listener "init_ns" "${port}" "vsock"
+
+	# Send a message, wait, then send another. While waiting, assign the
+	# device to a namespace. Confirm the second message does not arrive.
+	started=${SECONDS}
+	vm_ssh "init_ns" -- \
+		"(echo FIRST; sleep ${gap}; echo SECOND) |" \
+		"socat -u STDIN VSOCK-CONNECT:2:${port}" &>/dev/null &
+	sender=$!
+
+	timeout "$(( WAIT_PERIOD * WAIT_PERIOD_MAX ))" \
+		bash -c 'while [[ ! -s '"${outfile}"' ]]; do sleep 1; done'
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		ns_old_conn_cleanup "${port}" "${pid}" "${sender}" \
+			"${outfile}" "${nspid}"
+		return "${KSFT_FAIL}"
+	fi
+	elapsed=$(( SECONDS - started ))
+
+	sleep $(( gap + WAIT_PERIOD ))
+
+	result=$(cat "${outfile}")
+	ns_old_conn_cleanup "${port}" "${pid}" "${sender}" \
+		"${outfile}" "${nspid}"
+
+	if [[ "${result}" != *FIRST* ]]; then
+		log_host "no connection before the assign: [${result}]"
+		return "${KSFT_FAIL}"
+	fi
+
+	if [[ $(( elapsed + 1 )) -ge ${gap} ]]; then
+		log_host "connect and assign took ${elapsed}s of a ${gap}s gap"
+		return "${KSFT_SKIP}"
+	fi
+
+	if [[ "${result}" == *SECOND* ]]; then
+		log_host "old connection still delivered after the assign"
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_local_connect_to_host_fails() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "${nspid}" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" == TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_connect_to_host_ok() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "${nspid}" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" != TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_init_ns_connect_fails() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" == TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_host_connect_ok() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	outfile=$(mktemp)
+	host_send_to_guest "init_ns" "${nspid}" "${port}" "${outfile}"
+
+	vm_ns_stop "init_ns" "${nspid}"
+	vm_reset_g2h
+
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" != TEST ]]; then
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_pins_ns_ok() {
+	local port=12345
+	local outfile
+	local result
+	local nspid
+
+	nspid=$(vm_ns_start "init_ns")
+	if [[ -z "${nspid}" ]]; then
+		log_host "failed to create a namespace inside the guest"
+		return "${KSFT_FAIL}"
+	fi
+
+	if ! vm_ns_assign_g2h "init_ns" "${nspid}"; then
+		log_host "failed to assign the vsock device to the guest ns"
+		vm_ns_stop "init_ns" "${nspid}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	vm_ns_stop "init_ns" "${nspid}"
+	sleep "${WAIT_PERIOD}"
+
+	outfile=$(mktemp)
+	guest_send_to_host "init_ns" "" "${port}" "${outfile}"
+	result=$(cat "${outfile}")
+	if [[ "${result}" == TEST ]]; then
+		log_host "device returned to the init ns instead of pinning it"
+		rm -f "${outfile}"
+		vm_reset_g2h
+		return "${KSFT_FAIL}"
+	fi
+
+	# Assigning it back is what releases the deleted namespace.
+	vm_reset_g2h
+
+	guest_send_to_host "init_ns" "" "$(( port + 1 ))" "${outfile}"
+	result=$(cat "${outfile}")
+	rm -f "${outfile}"
+
+	if [[ "${result}" != TEST ]]; then
+		log_host "device did not come back on reassign: [${result}]"
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+check_assign_g2h_eperm() {
+	local wrapper=$1
+	local out
+
+	if out=$(vm_ssh "init_ns" -- "${wrapper}" python3 /root/ynl/cli.py \
+		--no-schema --spec /root/ynl/vsock.yaml \
+		--do dev-netns-set 2>&1); then
+		log_host "assign unexpectedly succeeded under '${wrapper}'"
+		return "${KSFT_FAIL}"
+	fi
+
+	if [[ "${out}" != *"Operation not permitted"* ]]; then
+		log_host "no EPERM under '${wrapper}': [${out}]"
+		return "${KSFT_FAIL}"
+	fi
+
+	return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_no_cap_net_admin_fails() {
+	check_assign_g2h_eperm "unshare -n setpriv --bounding-set=-net_admin"
+}
+
+test_ns_guest_assign_g2h_netns_unpriv_user_ns_fails() {
+	local unpriv_uid=65534
+	local unpriv
+
+	unpriv="setpriv --reuid=${unpriv_uid} --regid=${unpriv_uid}"
+	unpriv="${unpriv} --clear-groups"
+
+	if ! vm_ssh "init_ns" -- "${unpriv} unshare -Urn true"; then
+		log_host "unprivileged user namespaces unavailable, skipping"
+		return "${KSFT_SKIP}"
+	fi
+
+	check_assign_g2h_eperm "${unpriv} unshare -Urn"
+}
+
 shared_vm_test() {
 	local tname
 
@@ -1453,7 +1989,8 @@ run_shared_vm_tests() {
 			continue
 		fi
 
-		if ! check_netns "${arg}"; then
+		if ! check_netns "${arg}" ||
+		   ! check_ns_guest_deps "${arg}"; then
 			check_result "${KSFT_SKIP}" "${arg}"
 			continue
 		fi
@@ -1517,7 +2054,8 @@ run_ns_tests() {
 			continue
 		fi
 
-		if ! check_netns "${arg}"; then
+		if ! check_netns "${arg}" ||
+		   ! check_ns_guest_deps "${arg}"; then
 			check_result "${KSFT_SKIP}" "${arg}"
 			continue
 		fi

21/3 2026/10/21 10:17 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 24766
							output: 37
							thoughts: 1887
						

Thoughts:
**KMSAN Evaluation for vsock Patch Series**

Okay, let's break down whether this vsock patch series needs a dedicated KMSAN fuzzing session. The task at hand is to determine if any of these changes introduce or expose uninitialized memory risks that KMSAN is uniquely equipped to catch. I need to be thorough.

First, I review the changes: a `vsock.yaml` netlink spec, the `vsock` genetlink family, and associated commands and attributes, with code modifications spread across `af_vsock.c`, `vsock_nl_gen.c`, `virtio_transport.c`, `virtio_transport_common.c`, and `vhost/vsock.c`. The KMSAN documentation is clear: if a bug can be detected by KASAN, LOCKDEP, or other standard bug detectors, then there's no need for KMSAN. I should only set `NeedsKMSAN=true` if uninitialized memory risks are *exclusively* detectable by KMSAN.

My focus should be on areas where data is sent to user space because that is where kernel memory issues most often become exposed. I should be looking for potential problems with memory initialization, especially in the context of kernel structures, netlink attributes, ioctl output, and socket options. I need to be watching for conditional logic that might depend on uninitialized variables, complex data structure initialization with uninitialized fields, or potential buffer access that might expose uninitialized memory.

Specifically, I've zoomed in on the `vsock_nl_dev_netns_fill` function, where I see that the `id` variable is conditionally used, but it's *not* used if the `equal` is true. `nla_put_s32` is skipped, so no uninitialized data would leak. I then check the `vsock_g2h_net_assign` which replies with a header, no data is written. However, the YAML spec says that there's an expected `netns-id` return, but there's a disconnect in the C code, which is an netlink schema difference, which is not an uninitialized memory issue.

Then I check `vsock_dev_do_ioctl` because I see the potential for a `put_user` operation. But, `cid` is initialized before use via a call to `vsock_registered_transport_cid`, which means that it is safe, so it is not a problem. Even if I dig deeper, I can see that `cid` is always assigned. I check other places like `vhost_vsock_set_cid` and find no issues there either, as `ret` is initialized properly.

I should also use the `read-file` or `grepper` tool to search the context in the changed files. I will make a careful sweep through the code to eliminate any blind spots.

Based on the initial inspection, it seems unlikely that this patch series *introduces* new, exclusively KMSAN-detectable uninitialized memory vulnerabilities. Therefore, based on the findings, I am leaning toward *not* requiring a dedicated KMSAN fuzzing session. But I need to do a final review to confirm this and ensure I have not missed anything.



22/3 2026/10/21 10:17 tool grepper
0m Args:
{
  "Expression": "vsock_g2h_net",
  "PathPrefixes": [
    "net/vmw_vsock/"
  ]
}

Results:
{
  "Output": "net/vmw_vsock/af_vsock.c=236=EXPORT_SYMBOL_GPL(vsock_register_mutex);\n--\nnet/vmw_vsock/af_vsock.c-240- */\nnet/vmw_vsock/af_vsock.c:241:static struct net __rcu *vsock_g2h_net = RCU_INITIALIZER(\u0026init_net);\nnet/vmw_vsock/af_vsock.c:242:static netns_tracker vsock_g2h_net_tracker;\nnet/vmw_vsock/af_vsock.c-243-\n--\nnet/vmw_vsock/af_vsock.c=603=static bool vsock_g2h_unreachable(const struct vsock_transport *t,\n--\nnet/vmw_vsock/af_vsock.c-606-\treturn t == transport_g2h \u0026\u0026 vsock_net_assignable(t) \u0026\u0026\nnet/vmw_vsock/af_vsock.c:607:\t       !vsock_g2h_net_reachable(net);\nnet/vmw_vsock/af_vsock.c-608-}\n--\nnet/vmw_vsock/af_vsock.c=838=EXPORT_SYMBOL_GPL(vsock_find_cid);\n--\nnet/vmw_vsock/af_vsock.c-844- */\nnet/vmw_vsock/af_vsock.c:845:struct net *vsock_g2h_net_get(netns_tracker *tracker)\nnet/vmw_vsock/af_vsock.c-846-{\n--\nnet/vmw_vsock/af_vsock.c-849-\trcu_read_lock();\nnet/vmw_vsock/af_vsock.c:850:\tnet = rcu_dereference(vsock_g2h_net);\nnet/vmw_vsock/af_vsock.c-851-\tget_net_track(net, tracker, GFP_ATOMIC);\n--\nnet/vmw_vsock/af_vsock.c-855-}\nnet/vmw_vsock/af_vsock.c:856:EXPORT_SYMBOL_GPL(vsock_g2h_net_get);\nnet/vmw_vsock/af_vsock.c-857-\n--\nnet/vmw_vsock/af_vsock.c-860- */\nnet/vmw_vsock/af_vsock.c:861:bool vsock_g2h_net_eq(const struct net *net)\nnet/vmw_vsock/af_vsock.c-862-{\nnet/vmw_vsock/af_vsock.c:863:\treturn rcu_access_pointer(vsock_g2h_net) == net;\nnet/vmw_vsock/af_vsock.c-864-}\nnet/vmw_vsock/af_vsock.c:865:EXPORT_SYMBOL_GPL(vsock_g2h_net_eq);\nnet/vmw_vsock/af_vsock.c-866-\nnet/vmw_vsock/af_vsock.c:867:bool vsock_g2h_net_reachable(struct net *net)\nnet/vmw_vsock/af_vsock.c-868-{\n--\nnet/vmw_vsock/af_vsock.c-871-\trcu_read_lock();\nnet/vmw_vsock/af_vsock.c:872:\treachable = vsock_net_check_mode(net, rcu_dereference(vsock_g2h_net));\nnet/vmw_vsock/af_vsock.c-873-\trcu_read_unlock();\n--\nnet/vmw_vsock/af_vsock.c-876-}\nnet/vmw_vsock/af_vsock.c:877:EXPORT_SYMBOL_GPL(vsock_g2h_net_reachable);\nnet/vmw_vsock/af_vsock.c-878-\nnet/vmw_vsock/af_vsock.c=879=static bool vsock_g2h_reachable_sk(struct vsock_sock *vsk)\n--\nnet/vmw_vsock/af_vsock.c-883-\nnet/vmw_vsock/af_vsock.c:884:\treturn vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));\nnet/vmw_vsock/af_vsock.c-885-}\n--\nnet/vmw_vsock/af_vsock.c=948=static void vsock_g2h_reset_unreachable(const struct vsock_transport *t)\n--\nnet/vmw_vsock/af_vsock.c-985- */\nnet/vmw_vsock/af_vsock.c:986:static struct net *vsock_g2h_net_swap(struct net *net, netns_tracker *old)\nnet/vmw_vsock/af_vsock.c-987-{\n--\nnet/vmw_vsock/af_vsock.c-989-\nnet/vmw_vsock/af_vsock.c:990:\t*old = vsock_g2h_net_tracker;\nnet/vmw_vsock/af_vsock.c:991:\tget_net_track(net, \u0026vsock_g2h_net_tracker, GFP_KERNEL);\nnet/vmw_vsock/af_vsock.c-992-\nnet/vmw_vsock/af_vsock.c:993:\treturn rcu_replace_pointer(vsock_g2h_net, net,\nnet/vmw_vsock/af_vsock.c-994-\t\t\t\t   lockdep_is_held(\u0026vsock_register_mutex));\n--\nnet/vmw_vsock/af_vsock.c-997-/* Drop the namespace the device just left, once the readers that may still\nnet/vmw_vsock/af_vsock.c:998: * see it in vsock_g2h_net_get() are gone. Runs without the mutex held.\nnet/vmw_vsock/af_vsock.c-999- */\nnet/vmw_vsock/af_vsock.c:1000:static void vsock_g2h_net_release(struct net *old, netns_tracker *tracker)\nnet/vmw_vsock/af_vsock.c-1001-{\n--\nnet/vmw_vsock/af_vsock.c=1006=static int vsock_nl_dev_netns_fill(struct sk_buff *msg, struct genl_info *info)\n--\nnet/vmw_vsock/af_vsock.c-1016-\trcu_read_lock();\nnet/vmw_vsock/af_vsock.c:1017:\tassigned = rcu_dereference(vsock_g2h_net);\nnet/vmw_vsock/af_vsock.c-1018-\tequal = net_eq(net, assigned);\n--\nnet/vmw_vsock/af_vsock.c-1046- */\nnet/vmw_vsock/af_vsock.c:1047:static int vsock_g2h_net_assign(struct net *net, struct sk_buff *msg,\nnet/vmw_vsock/af_vsock.c-1048-\t\t\t\tstruct genl_info *info)\n--\nnet/vmw_vsock/af_vsock.c-1098-\nnet/vmw_vsock/af_vsock.c:1099:\told = vsock_g2h_net_swap(net, \u0026old_tracker);\nnet/vmw_vsock/af_vsock.c-1100-\tt = transport_g2h;\n--\nnet/vmw_vsock/af_vsock.c-1110-\nnet/vmw_vsock/af_vsock.c:1111:\tvsock_g2h_net_release(old, \u0026old_tracker);\nnet/vmw_vsock/af_vsock.c-1112-\n--\nnet/vmw_vsock/af_vsock.c=3261=int vsock_nl_dev_netns_set_doit(struct sk_buff *skb, struct genl_info *info)\n--\nnet/vmw_vsock/af_vsock.c-3268-\nnet/vmw_vsock/af_vsock.c:3269:\treturn vsock_g2h_net_assign(genl_info_net(info), msg, info);\nnet/vmw_vsock/af_vsock.c-3270-}\n--\nnet/vmw_vsock/af_vsock.c=3481=static int __init vsock_init(void)\n--\nnet/vmw_vsock/af_vsock.c-3486-\nnet/vmw_vsock/af_vsock.c:3487:\tget_net_track(\u0026init_net, \u0026vsock_g2h_net_tracker, GFP_KERNEL);\nnet/vmw_vsock/af_vsock.c-3488-\n--\nnet/vmw_vsock/af_vsock.c-3533-err_reset_transport:\nnet/vmw_vsock/af_vsock.c:3534:\tput_net_track(\u0026init_net, \u0026vsock_g2h_net_tracker);\nnet/vmw_vsock/af_vsock.c-3535-\treturn err;\n--\nnet/vmw_vsock/af_vsock.c=3538=static void __exit vsock_exit(void)\n--\nnet/vmw_vsock/af_vsock.c-3545-\nnet/vmw_vsock/af_vsock.c:3546:\tput_net_track(rcu_dereference_protected(vsock_g2h_net, true),\nnet/vmw_vsock/af_vsock.c:3547:\t\t      \u0026vsock_g2h_net_tracker);\nnet/vmw_vsock/af_vsock.c-3548-}\n--\nnet/vmw_vsock/af_vsock.c=3612=void vsock_core_unregister(const struct vsock_transport *t)\n--\nnet/vmw_vsock/af_vsock.c-3623-\t\ttransport_g2h = NULL;\nnet/vmw_vsock/af_vsock.c:3624:\t\told = vsock_g2h_net_swap(\u0026init_net, \u0026old_tracker);\nnet/vmw_vsock/af_vsock.c-3625-\t}\n--\nnet/vmw_vsock/af_vsock.c-3636-\tif (old)\nnet/vmw_vsock/af_vsock.c:3637:\t\tvsock_g2h_net_release(old, \u0026old_tracker);\nnet/vmw_vsock/af_vsock.c-3638-}\n--\nnet/vmw_vsock/virtio_transport.c=237=virtio_transport_send_pkt(struct sk_buff *skb, struct net *net)\n--\nnet/vmw_vsock/virtio_transport.c-261-\t */\nnet/vmw_vsock/virtio_transport.c:262:\tif (!vsock_g2h_net_reachable(net)) {\nnet/vmw_vsock/virtio_transport.c-263-\t\tkfree_skb(skb);\n--\nnet/vmw_vsock/virtio_transport.c=552=bool virtio_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port)\nnet/vmw_vsock/virtio_transport.c-553-{\nnet/vmw_vsock/virtio_transport.c:554:\treturn vsock_g2h_net_reachable(sock_net(sk_vsock(vsk)));\nnet/vmw_vsock/virtio_transport.c-555-}\n--\nnet/vmw_vsock/virtio_transport.c=624=virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid)\n--\nnet/vmw_vsock/virtio_transport.c-628-\nnet/vmw_vsock/virtio_transport.c:629:\tif (!vsock_g2h_net_reachable(sock_net(sk_vsock(vsk))))\nnet/vmw_vsock/virtio_transport.c-630-\t\treturn false;\n--\nnet/vmw_vsock/virtio_transport.c=642=static void virtio_transport_rx_work(struct work_struct *work)\n--\nnet/vmw_vsock/virtio_transport.c-650-\nnet/vmw_vsock/virtio_transport.c:651:\tnet = vsock_g2h_net_get(\u0026ns_tracker);\nnet/vmw_vsock/virtio_transport.c-652-\n--\nnet/vmw_vsock/virtio_transport.c-702-\t\t\t */\nnet/vmw_vsock/virtio_transport.c:703:\t\t\tif (unlikely(!vsock_g2h_net_eq(net))) {\nnet/vmw_vsock/virtio_transport.c-704-\t\t\t\tput_net_track(net, \u0026ns_tracker);\nnet/vmw_vsock/virtio_transport.c:705:\t\t\t\tnet = vsock_g2h_net_get(\u0026ns_tracker);\nnet/vmw_vsock/virtio_transport.c-706-\t\t\t}\n"
}

23/3 2026/10/21 10:17 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 30950
							output: 525
							thoughts: 2502
						

Thoughts:
**KMSAN Assessment: AF_VSOCK Network Namespace Support**

Okay, let's break this down. My primary concern here is always ensuring we aren't introducing any uninitialized memory reads or information leaks, especially in a network context where that could be exploited. This patch series is all about adding network namespace support for the guest-to-host vsock device (G2H transport). We're introducing a new `vsock` genetlink family, and the ability to move the vsock G2H transport to specific network namespaces. It's also modifying socket state to handle these moves.

First, I examined the netlink commands. The `VSOCK_CMD_DEV_NETNS_SET` command moves the vsock G2H transport, while `VSOCK_CMD_DEV_NETNS_GET` returns the assigned network namespace ID (nsid). My initial concern was about whether data sent back to userspace via the netlink attributes might include uninitialized memory. I drilled down into the code. In `vsock_nl_dev_netns_fill`, we have an `s32 id` used to send the namespace ID in netlink. The logic *guarantees* that `id` is *always* initialized before it is copied to userspace using `nla_put_s32`. When `equal` is true, nothing is sent at all. The `VSOCK_CMD_DEV_NETNS_SET` command sends back an empty response, so no chance of uninitialized memory. The IOCTL command `vsock_dev_do_ioctl` returns the connection ID, `cid` with `put_user(cid, p)`. `cid` is always initialized.

Next, I looked for conditional logic that could lead to uninitialized variables. In `vsock_nl_dev_netns_fill`, `assigned` and `equal` are properly assigned before any branching. `vsock_g2h_reset_unreachable` shows a similar pattern with local variables like `i`, `sk`, and `reset` - all initialized before use. Finally, I reviewed `vsock_g2h_net_assign`. The `old_tracker` is initialized before calling `vsock_g2h_net_swap`, and is returned from `rcu_replace_pointer`. Return values are also initialized in each branch.

I then considered whether new data structures are introduced or modified where uninitialized fields could be read later. I found that no new complex structures or allocations were introduced.

Finally, I checked for any potential buffer overruns or underflows. The code doesn't alter any buffer lengths or introduce any new bounds-checking logic that could cause problems. The only changes are associated with network namespace association.

I also looked carefully at the `netns_tracker` to make sure there are no subtle uninitialized memory issues. It appears properly initialized in `vsock_init`, `virtio_transport_rx_work`, and in the net namespace swap routines. Everything looked good here, with proper `get_net_track` and `put_net_track` calls.

This brings me to the core question: does this require KMSAN? I do not see any scenarios where KMSAN would be triggered. KASAN, LOCKDEP, and standard kernel debugging tools will likely catch any issues with concurrency bugs, locking, and netns refcounting. There are no uninitialized reads or information leaks. Therefore, I'm confident in setting `NeedsKMSAN=false`. We're good to go.
```set-results
NeedsKMSAN=false
```



24/3 2026/10/21 10:18 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET) allowing the guest-to-host vsock device to be assigned to specific network namespaces, along with reachability checks, namespace refcounting, and unreachable socket teardown.\n\nEvaluation of uninitialized memory and info-leak risks:\n1. User-space data transfers:\n   - In `vsock_nl_dev_netns_fill()`, the only netlink attribute emitted is `VSOCK_A_NETNS_ID` via `nla_put_s32(msg, VSOCK_A_NETNS_ID, id)`. The scalar variable `id` is explicitly assigned (`NETNSA_NSID_NOT_ASSIGNED` or `peernet2id_alloc()`) before emission and guarded by `!equal`. If `equal` is true, no attribute is sent.\n   - `vsock_nl_dev_netns_set_doit()` sends an empty Netlink reply containing only the generic netlink header.\n   - In `vsock_dev_do_ioctl()`, scalar `u32 cid` is returned by `vsock_registered_transport_cid()` and written with `put_user(cid, p)`.\n   - No uninitialized struct padding or partially initialized buffers are exposed to user space.\n2. Variables and control flow:\n   - All local variables, flags, and netns_tracker instances (`id`, `reset`, `old_tracker`, `ret`, `cid`) are initialized prior to branching or consumption.\n3. Complex allocations / buffer lengths:\n   - No new variable-length buffers or complex uninitialized data structures are introduced or modified.\n\nKASAN vs KMSAN applicability:\n- The predominant risk surface in this series relates to concurrency, locking hierarchies (`vsock_g2h_assign_mutex`, `vsock_register_mutex`, `vsock_table_lock`, `rx_lock`), netns reference counting, and socket lifecycle / use-after-free during device movement. These are detected by KASAN, LOCKDEP, and refcount tracking.\n- There are no uninitialized memory usage or info-leak risks requiring a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET) allowing the guest-to-host vsock device to be assigned to specific network namespaces, along with reachability checks, namespace refcounting, and unreachable socket teardown.\n\nEvaluation of uninitialized memory and info-leak risks:\n1. User-space data transfers:\n   - In `vsock_nl_dev_netns_fill()`, the only netlink attribute emitted is `VSOCK_A_NETNS_ID` via `nla_put_s32(msg, VSOCK_A_NETNS_ID, id)`. The scalar variable `id` is explicitly assigned (`NETNSA_NSID_NOT_ASSIGNED` or `peernet2id_alloc()`) before emission and guarded by `!equal`. If `equal` is true, no attribute is sent.\n   - `vsock_nl_dev_netns_set_doit()` sends an empty Netlink reply containing only the generic netlink header.\n   - In `vsock_dev_do_ioctl()`, scalar `u32 cid` is returned by `vsock_registered_transport_cid()` and written with `put_user(cid, p)`.\n   - No uninitialized struct padding or partially initialized buffers are exposed to user space.\n2. Variables and control flow:\n   - All local variables, flags, and netns_tracker instances (`id`, `reset`, `old_tracker`, `ret`, `cid`) are initialized prior to branching or consumption.\n3. Complex allocations / buffer lengths:\n   - No new variable-length buffers or complex uninitialized data structures are introduced or modified.\n\nKASAN vs KMSAN applicability:\n- The predominant risk surface in this series relates to concurrency, locking hierarchies (`vsock_g2h_assign_mutex`, `vsock_register_mutex`, `vsock_table_lock`, `rx_lock`), netns reference counting, and socket lifecycle / use-after-free during device movement. These are detected by KASAN, LOCKDEP, and refcount tracking.\n- There are no uninitialized memory usage or info-leak risks requiring a dedicated KMSAN fuzzing session.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)