| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/10/12 23:19 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"VSOCKETS",
"VIRTIO_VSOCKETS",
"VHOST_VSOCK"
],
"FocusSymbols": [
"vsock_nl_dev_netns_set_doit",
"vsock_nl_dev_netns_get_doit",
"vsock_maybe_set_connected",
"vsock_g2h_net_reachable",
"vsock_g2h_net_get",
"vhost_vsock_set_cid",
"__vsock_find_cid"
],
"KMSANReasoning": "The patch series implements network namespace assignment for the guest-to-host (G2H) vsock device via new genetlink commands (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET), updates the IOCTL_VM_SOCKETS_GET_LOCAL_CID ioctl to respect network namespaces, and updates socket reachability and teardown logic when namespaces change.\n\nFrom a memory sanitization perspective:\n1. User-space copies / info-leaks: The netlink GET handler sends only a single 32-bit signed scalar attribute (VSOCK_A_NETNS_ID via nla_put_s32), which is deterministically initialized before transmission and has no struct padding or uninitialized field issues. The SET handler returns an empty genetlink response header. The ioctl handler copies a primitive u32 value ('cid') via put_user(), which is explicitly assigned on all execution paths. No uninitialized memory or structure padding is leaked to user space.\n2. Uninitialized memory in kernel logic: No new complex data structures or uninitialized buffers are introduced. All local variables used in branching logic are fully initialized before use. Packet buffers (sk_buff) are not parsed with modified lengths or bounds checks.\n3. Relevant bug classes: The potential risks introduced by this change relate to network namespace reference tracking (get_net_track/put_net_track), RCU synchronization (rcu_dereference/rcu_replace_pointer), socket state machine transitions, socket lifetime (use-after-free during reset sweeps), and mutex locking (vsock_register_mutex and vsock_table_lock). These failure modes are comprehensively covered by standard KASAN, LOCKDEP, and refcount tracking.\n\nBecause the changes introduce no risk of uninitialized memory reads or leaks detectable exclusively by KMSAN, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK ('vsock' family) enabling dynamic network namespace assignment for the guest-to-host vsock device. It adds reachability checks, CID conflict validation, and connection reset sweeps across network namespaces in vsock core, virtio-vsock, and vhost-vsock. This modifies reachable socket and netlink logic that can be exercised in standard virtualized environments.",
"WorthFuzzing": true
} |
| 1/1 | 2026/10/12 23:19 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 07e9cb1df64854ae931106c7f7246c9482607b56\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Mon Oct 12 23:19:09 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..9276479004c0f 100644\n--- a/Documentation/admin-guide/sysctl/net.rst\n+++ b/Documentation/admin-guide/sysctl/net.rst\n@@ -529,6 +529,43 @@ 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+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 72ca3aab21066..f11423727449f 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -29163,10 +29163,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 44cee74519555..e663be3bbfa0b 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,15 @@ 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 /**** UTILS ****/\n \n@@ -480,7 +513,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 +586,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@@ -564,7 +632,12 @@ static bool vsock_use_local_transport(unsigned int remote_cid)\n \t\treturn true;\n \n \tif (transport_g2h) {\n-\t\treturn remote_cid == transport_g2h-\u003eget_local_cid();\n+\t\tu32 cid = __vsock_registered_transport_cid(\u0026transport_g2h, net);\n+\n+\t\t/* The device may be unreachable from @net, in which case\n+\t\t * @remote_cid is not the local CID.\n+\t\t */\n+\t\treturn cid != VMADDR_CID_ANY \u0026\u0026 remote_cid == cid;\n \t} else {\n \t\treturn remote_cid == VMADDR_CID_HOST;\n \t}\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,267 @@ 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+/* 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, *tmp;\n+\tstruct sock *sk;\n+\tint i;\n+\n+\tlockdep_assert_held(\u0026vsock_register_mutex);\n+\n+\tspin_lock_bh(\u0026vsock_table_lock);\n+\n+\tfor (i = 0; i \u003c ARRAY_SIZE(vsock_connected_table); i++) {\n+\t\tlist_for_each_entry_safe(vsk, tmp, \u0026vsock_connected_table[i],\n+\t\t\t\t\t connected_table) {\n+\t\t\tsk = sk_vsock(vsk);\n+\n+\t\t\tif (vsk-\u003etransport != t ||\n+\t\t\t sk-\u003esk_state == TCP_CLOSE ||\n+\t\t\t vsock_g2h_reachable_sk(vsk))\n+\t\t\t\tcontinue;\n+\n+\t\t\t/* If close has already been scheduled for this socket,\n+\t\t\t * we can skip.\n+\t\t\t */\n+\t\t\tif (vsk-\u003eclose_work_scheduled)\n+\t\t\t\tcontinue;\n+\n+\t\t\tsk-\u003esk_state = TCP_CLOSE;\n+\t\t\tsk-\u003esk_err = ECONNRESET;\n+\t\t\tsk_error_report(sk);\n+\n+\t\t\t__vsock_remove_connected(vsk);\n+\t\t}\n+\t}\n+\n+\tspin_unlock_bh(\u0026vsock_table_lock);\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+\tnetns_tracker old_tracker;\n+\tstruct net *old;\n+\tvoid *hdr;\n+\tint ret;\n+\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+\n+\t/* See vsock_maybe_set_connected() comment about synchronizing with\n+\t * connecting sockets.\n+\t */\n+\tvsock_g2h_reset_unreachable(transport_g2h);\n+\n+\tmutex_unlock(\u0026vsock_register_mutex);\n+\n+\tvsock_g2h_net_release(old, \u0026old_tracker);\n+\n+\treturn 0;\n+\n+unlock:\n+\tmutex_unlock(\u0026vsock_register_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 +1250,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@@ -2795,6 +3138,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@@ -2802,11 +3146,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@@ -2847,6 +3194,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@@ -3036,7 +3421,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@@ -3046,14 +3431,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@@ -3062,6 +3447,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@@ -3083,15 +3470,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@@ -3099,15 +3494,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@@ -3174,13 +3574,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@@ -3189,6 +3594,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..4123924536dd6 100644\n--- a/net/vmw_vsock/virtio_transport.c\n+++ b/net/vmw_vsock/virtio_transport.c\n@@ -542,13 +542,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 +587,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 +617,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 +634,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 +688,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 +705,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 f225f53ed4bab..0234379c14a15 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 1e746e25e2bc9..fa51108c43d43 100644\n--- a/tools/net/ynl/Makefile.deps\n+++ b/tools/net/ynl/Makefile.deps\n@@ -55,4 +55,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..d5d7b0660c5da 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,359 @@ 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+\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+\tstarted=${SECONDS}\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} -ge ${gap} ]]; then\n+\t\tlog_host \"assign took ${elapsed}s, longer than the ${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+test_ns_guest_assign_g2h_netns_no_cap_net_admin_fails() {\n+\tlocal cmd=\"unshare -n setpriv --bounding-set=-net_admin\"\n+\tlocal rc\n+\n+\tvm_ssh \"init_ns\" -- \"${cmd}\" python3 /root/ynl/cli.py --no-schema \\\n+\t\t--spec /root/ynl/vsock.yaml --do dev-netns-set \u0026\u003e/dev/null\n+\trc=$?\n+\n+\tif [[ \"${rc}\" -eq 0 ]]; then\n+\t\tlog_host \"assign unexpectedly succeeded without CAP_NET_ADMIN\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n+test_ns_guest_assign_g2h_netns_unpriv_user_ns_fails() {\n+\tlocal unpriv_uid=65534\n+\tlocal unpriv\n+\tlocal rc\n+\n+\tunpriv=\"setpriv --reuid=${unpriv_uid} --regid=${unpriv_uid}\"\n+\tunpriv=\"${unpriv} --clear-groups\"\n+\n+\tif ! vm_ssh \"init_ns\" -- \"${unpriv} unshare -U true\"; then\n+\t\tlog_host \"unprivileged user namespaces unavailable, skipping\"\n+\t\treturn \"${KSFT_SKIP}\"\n+\tfi\n+\n+\tvm_ssh \"init_ns\" -- \"${unpriv} unshare -Urn\" \\\n+\t\tpython3 /root/ynl/cli.py --no-schema \\\n+\t\t--spec /root/ynl/vsock.yaml --do dev-netns-set \u0026\u003e/dev/null\n+\trc=$?\n+\n+\tif [[ \"${rc}\" -eq 0 ]]; then\n+\t\tlog_host \"assign unexpectedly succeeded for unprivileged user\"\n+\t\treturn \"${KSFT_FAIL}\"\n+\tfi\n+\n+\treturn \"${KSFT_PASS}\"\n+}\n+\n shared_vm_test() {\n \tlocal tname\n \n@@ -1453,7 +1993,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 +2058,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/12 23:19 | agent | patch-evaluator |
0mModel:core Args: null Results: {
"EnableConfigs": [
"VSOCKETS",
"VIRTIO_VSOCKETS",
"VHOST_VSOCK"
],
"FocusSymbols": [
"vsock_nl_dev_netns_set_doit",
"vsock_nl_dev_netns_get_doit",
"vsock_maybe_set_connected",
"vsock_g2h_net_reachable",
"vsock_g2h_net_get",
"vhost_vsock_set_cid",
"__vsock_find_cid"
],
"Reasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK ('vsock' family) enabling dynamic network namespace assignment for the guest-to-host vsock device. It adds reachability checks, CID conflict validation, and connection reset sweeps across network namespaces in vsock core, virtio-vsock, and vhost-vsock. This modifies reachable socket and netlink logic that can be exercised in standard virtualized 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 07e9cb1df64854ae931106c7f7246c9482607b56
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Oct 12 23:19:09 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..9276479004c0f 100644
--- a/Documentation/admin-guide/sysctl/net.rst
+++ b/Documentation/admin-guide/sysctl/net.rst
@@ -529,6 +529,43 @@ 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.
+
+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 72ca3aab21066..f11423727449f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -29163,10 +29163,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 44cee74519555..e663be3bbfa0b 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,15 @@ 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;
/**** UTILS ****/
@@ -480,7 +513,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 +586,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);
@@ -564,7 +632,12 @@ static bool vsock_use_local_transport(unsigned int remote_cid)
return true;
if (transport_g2h) {
- return remote_cid == transport_g2h->get_local_cid();
+ u32 cid = __vsock_registered_transport_cid(&transport_g2h, net);
+
+ /* The device may be unreachable from @net, in which case
+ * @remote_cid is not the local CID.
+ */
+ return cid != VMADDR_CID_ANY && remote_cid == cid;
} else {
return remote_cid == VMADDR_CID_HOST;
}
@@ -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,267 @@ 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);
+
+/* 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, *tmp;
+ struct sock *sk;
+ int i;
+
+ lockdep_assert_held(&vsock_register_mutex);
+
+ spin_lock_bh(&vsock_table_lock);
+
+ for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
+ list_for_each_entry_safe(vsk, tmp, &vsock_connected_table[i],
+ connected_table) {
+ sk = sk_vsock(vsk);
+
+ if (vsk->transport != t ||
+ sk->sk_state == TCP_CLOSE ||
+ vsock_g2h_reachable_sk(vsk))
+ continue;
+
+ /* If close has already been scheduled for this socket,
+ * we can skip.
+ */
+ if (vsk->close_work_scheduled)
+ continue;
+
+ sk->sk_state = TCP_CLOSE;
+ sk->sk_err = ECONNRESET;
+ sk_error_report(sk);
+
+ __vsock_remove_connected(vsk);
+ }
+ }
+
+ spin_unlock_bh(&vsock_table_lock);
+}
+
+/* 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;
+ netns_tracker old_tracker;
+ struct net *old;
+ void *hdr;
+ int ret;
+
+ mutex_lock(&vsock_register_mutex);
+
+ ret = -ENODEV;
+ if (!transport_g2h) {
+ NL_SET_ERR_MSG(extack, "no guest-to-host transport is loaded");
+ goto unlock;
+ }
+
+ ret = -EOPNOTSUPP;
+ if (!vsock_net_assignable(transport_g2h)) {
+ NL_SET_ERR_MSG(extack,
+ "the loaded guest-to-host transport does not support namespace assignment");
+ goto unlock;
+ }
+
+ if (transport_h2g && !transport_h2g->has_remote_cid) {
+ NL_SET_ERR_MSG(extack,
+ "the loaded host-to-guest transport cannot check the device's CID");
+ goto unlock;
+ }
+
+ ret = -EADDRINUSE;
+ if (transport_h2g &&
+ transport_h2g->has_remote_cid(net,
+ transport_g2h->get_local_cid())) {
+ NL_SET_ERR_MSG(extack,
+ "a guest in the target namespace already uses the device's CID");
+ goto unlock;
+ }
+
+ ret = -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);
+
+ /* See vsock_maybe_set_connected() comment about synchronizing with
+ * connecting sockets.
+ */
+ vsock_g2h_reset_unreachable(transport_g2h);
+
+ mutex_unlock(&vsock_register_mutex);
+
+ vsock_g2h_net_release(old, &old_tracker);
+
+ return 0;
+
+unlock:
+ mutex_unlock(&vsock_register_mutex);
+ nlmsg_free(msg);
+
+ return ret;
+}
+
static struct sock *vsock_dequeue_accept(struct sock *listener)
{
struct vsock_sock *vlistener;
@@ -908,7 +1250,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) {
@@ -2795,6 +3138,7 @@ static long vsock_dev_do_ioctl(struct file *filp,
{
u32 __user *p = ptr;
int retval = 0;
+ struct net *net;
u32 cid;
switch (cmd) {
@@ -2802,11 +3146,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;
@@ -2847,6 +3194,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,
@@ -3036,7 +3421,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);
@@ -3046,14 +3431,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)
@@ -3062,6 +3447,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);
@@ -3083,15 +3470,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:
@@ -3099,15 +3494,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)
@@ -3174,13 +3574,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;
@@ -3189,6 +3594,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..4123924536dd6 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -542,13 +542,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 +587,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 +617,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 +634,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 +688,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 +705,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 f225f53ed4bab..0234379c14a15 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 1e746e25e2bc9..fa51108c43d43 100644
--- a/tools/net/ynl/Makefile.deps
+++ b/tools/net/ynl/Makefile.deps
@@ -55,4 +55,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..d5d7b0660c5da 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,359 @@ 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.
+ 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'
+
+ started=${SECONDS}
+ 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} -ge ${gap} ]]; then
+ log_host "assign took ${elapsed}s, longer than the ${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}"
+}
+
+test_ns_guest_assign_g2h_netns_no_cap_net_admin_fails() {
+ local cmd="unshare -n setpriv --bounding-set=-net_admin"
+ local rc
+
+ vm_ssh "init_ns" -- "${cmd}" python3 /root/ynl/cli.py --no-schema \
+ --spec /root/ynl/vsock.yaml --do dev-netns-set &>/dev/null
+ rc=$?
+
+ if [[ "${rc}" -eq 0 ]]; then
+ log_host "assign unexpectedly succeeded without CAP_NET_ADMIN"
+ return "${KSFT_FAIL}"
+ fi
+
+ return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_unpriv_user_ns_fails() {
+ local unpriv_uid=65534
+ local unpriv
+ local rc
+
+ unpriv="setpriv --reuid=${unpriv_uid} --regid=${unpriv_uid}"
+ unpriv="${unpriv} --clear-groups"
+
+ if ! vm_ssh "init_ns" -- "${unpriv} unshare -U true"; then
+ log_host "unprivileged user namespaces unavailable, skipping"
+ return "${KSFT_SKIP}"
+ fi
+
+ vm_ssh "init_ns" -- "${unpriv} unshare -Urn" \
+ python3 /root/ynl/cli.py --no-schema \
+ --spec /root/ynl/vsock.yaml --do dev-netns-set &>/dev/null
+ rc=$?
+
+ if [[ "${rc}" -eq 0 ]]; then
+ log_host "assign unexpectedly succeeded for unprivileged user"
+ return "${KSFT_FAIL}"
+ fi
+
+ return "${KSFT_PASS}"
+}
+
shared_vm_test() {
local tname
@@ -1453,7 +1993,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 +2058,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/12 23:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24816 output: 61 thoughts: 1087 Thoughts: **Analyzing the vsock Netlink Patch Series** Okay, so I've got this Linux kernel patch series in front of me, and the user wants a thorough evaluation. My initial thought process is to break this down methodically, following the established triage guidelines. First, I need to understand what this patch series *does*. The instructions clearly outline the triage philosophy: reachability is the primary gate, and I should be skeptical of claims of "no functional change" or pure refactoring. Any new or modified assertions in reachable code are definitely worth fuzzing. Right, so according to the instructions, anything in Documentation, tools, selftests, or build systems is generally out unless it's *related* to reachable code. Specific unreachable hardware is also a no-go. Unreachable execution paths are similarly not a concern. The good stuff is any modification to reachable, executable code. Things like core subsystems, drivers, protocols are where the real vulnerabilities lurk. Refactorings, cleanups, runtime assertions—all potential areas for fuzzing if they interact with reachable code. Finally, I need to focus on identifying the most relevant code sections, keeping in mind specific feature logic and entrypoints. Now, let's look at the patch itself. The diff involves a lot of files: `Documentation/` and `tools/` directories, a `MAINTAINERS` update, and a bunch of files related to `vsock`. Specifically, I see changes to `drivers/vhost/vsock.c`, the `net/vmw_vsock/` directory, and various include files. The core of this seems to be introducing a new generic netlink family for `vsock` – `VSOCK_FAMILY_NAME`. This family has two new operations: `VSOCK_CMD_DEV_NETNS_SET` and `VSOCK_CMD_DEV_NETNS_GET`, with associated handler functions in `af_vsock.c`. There are also changes within other parts of `af_vsock.c`, as well as modifications to various core vsock files, and in `vhost/vsock.c`. Now, the critical question: is this reachable? Absolutely! VSOCK is a virtualized protocol, and therefore is readily emulated in QEMU and GCE. Moreover, generic netlink is user-space accessible via netlink sockets, so I can already assume it is. The documentation shows that netlink family "vsock" can be queried and commanded by unprivileged or privileged (with `CAP_NET_ADMIN`) user space. I see here that the `VSOCK_CMD_DEV_NETNS_GET` operation doesn't even require `CAP_NET_ADMIN`, unlike the SET command. User namespaces, or root access with `CAP_NET_ADMIN`, make the SET operation completely reachable, which is exactly how syzkaller runs. This seems like a juicy target! Socket operations on `AF_VSOCK` sockets are *definitely* reachable. Therefore, my initial assessment is that this is a *very* interesting patch series. I need to dig deeper into the code, and then check Kconfig requirements to evaluate just how easily it can be reached in a typical environment. I need to make sure to do this using our fuzzing tools to determine if it is worth fuzzing. |
| 4/2 | 2026/10/12 23:19 | tool | grepper |
0mArgs:{
"Expression": "config (VSOCKETS|VIRTIO_VSOCKETS|VHOST_VSOCK)",
"PathPrefixes": [
"net/vmw_vsock/Kconfig",
"drivers/vhost/Kconfig"
]
}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--\nnet/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--\nnet/vmw_vsock/Kconfig=41=config VMWARE_VMCI_VSOCKETS\n--\nnet/vmw_vsock/Kconfig-52-\nnet/vmw_vsock/Kconfig:53:config VIRTIO_VSOCKETS\nnet/vmw_vsock/Kconfig-54-\ttristate \"virtio transport for Virtual Sockets\"\n--\nnet/vmw_vsock/Kconfig-65-\nnet/vmw_vsock/Kconfig:66:config VIRTIO_VSOCKETS_COMMON\nnet/vmw_vsock/Kconfig-67-\ttristate\n"
} |
| 5/2 | 2026/10/12 23:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 27091 output: 36 thoughts: 48 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Examining Kconfig for VSOCKs & Related Drivers** Right, let's dive in. I need to refresh my mental map of how these components are wired together, especially now that I'm working with the latest codebase. I'm thinking specifically about `net/vmw_vsock/Kconfig` and `drivers/vhost/Kconfig`. These are the usual suspects. I want to double-check the dependencies and relationships around the various `VSOCKETS`, `VIRTIO_VSOCKETS`, and `VHOST_VSOCK` symbols. I need to make sure I haven't missed a new configuration variable or a potential conflict introduced in a recent merge or upstream update, especially since I'm concerned with a specific kernel functionality. I'll need to scrutinize any conditional dependencies and ensure the configuration matrix behaves as expected, especially when enabling and disabling various components. I need a clear picture of how these three relate, so I can ensure this system functions as desired. |
| 6/2 | 2026/10/12 23:19 | tool | read-file |
0mArgs:{
"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/12 23:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28331 output: 34 thoughts: 21 Thoughts: Here's my thought process, as requested: **Examining VHOST_VSOCK Configuration** Okay, so I need to understand how `VHOST_VSOCK` is configured. My first step is to dig into the `drivers/vhost/Kconfig` file. I'm assuming that's where the relevant configuration options related to vhost-vsock are defined. This is standard practice, so it's a logical place to start. I expect to find dependencies, default values, help text, and likely, a description of the functionality controlled by `VHOST_VSOCK`. I'll be looking for clues about the underlying features and how they integrate into the broader vhost framework. I'm hoping it's well-documented, because understanding the configuration is crucial to correctly deploying or troubleshooting any vhost-based virtualized environment. |
| 8/2 | 2026/10/12 23:19 | tool | read-file |
0mArgs:{
"File": "drivers/vhost/Kconfig",
"FirstLine": 55,
"LineCount": 25
}Results: {
"Contents": " 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 70:\tconfig VHOST_VDPA\n 71:\t\ttristate \"Vhost driver for vDPA-based backend\"\n 72:\t\tdepends on EVENTFD\n 73:\t\tselect VHOST\n 74:\t\tselect IRQ_BYPASS_MANAGER\n 75:\t\tdepends on VDPA\n 76:\t\thelp\n 77:\t\t This kernel module can be loaded in host kernel to accelerate\n 78:\t\t guest virtio devices with the vDPA-based backends.\n 79:\t\n"
} |
| 9/2 | 2026/10/12 23:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28908 output: 35 thoughts: 94 Thoughts: Here's my analysis of what I'm seeing in `net/vmw_vsock/Makefile`: **Examining the vsock Makefile** Okay, so I'm looking at the `net/vmw_vsock/Makefile`. First, I see the directive `vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o vsock_nl_gen.o`. This tells me these object files – `af_vsock.o`, `af_vsock_tap.o`, `vsock_addr.o`, and `vsock_nl_gen.o` – are going to be compiled and linked into the `vsock.o` module. This is expected. I want to check how these get built. Next, there's `obj-$(CONFIG_VSOCKETS) += vsock.o`. This is interesting; It says that if `CONFIG_VSOCKETS` is defined (likely in the kernel configuration), the `vsock.o` module itself will be included. Now, I'm going to quickly check the preceding lines in the makefile to see if that `CONFIG_VSOCKETS` is handled properly. That's a key detail to watch for; an undefined `CONFIG_VSOCKETS` will prevent the `vsock` module from being built, which is likely desired, but requires a certain configuration. Lines 1-15 should be enough to confirm this. |
| 10/2 | 2026/10/12 23:19 | tool | read-file |
0mArgs:{
"File": "net/vmw_vsock/Makefile",
"FirstLine": 1,
"LineCount": 20
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0\n 2:\tobj-$(CONFIG_VSOCKETS) += vsock.o\n 3:\tobj-$(CONFIG_VSOCKETS_DIAG) += vsock_diag.o\n 4:\tobj-$(CONFIG_VMWARE_VMCI_VSOCKETS) += vmw_vsock_vmci_transport.o\n 5:\tobj-$(CONFIG_VIRTIO_VSOCKETS) += vmw_vsock_virtio_transport.o\n 6:\tobj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o\n 7:\tobj-$(CONFIG_HYPERV_VSOCKETS) += hv_sock.o\n 8:\tobj-$(CONFIG_VSOCKETS_LOOPBACK) += vsock_loopback.o\n 9:\t\n 10:\tvsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o vsock_nl_gen.o\n 11:\tvsock-$(CONFIG_BPF_SYSCALL) += vsock_bpf.o\n 12:\t\n 13:\tvsock_diag-y += diag.o\n 14:\t\n 15:\tvmw_vsock_vmci_transport-y += vmci_transport.o vmci_transport_notify.o \\\n 16:\t\tvmci_transport_notify_qstate.o\n 17:\t\n 18:\tvmw_vsock_virtio_transport-y += virtio_transport.o\n 19:\t\n 20:\tvmw_vsock_virtio_transport_common-y += virtio_transport_common.o\n"
} |
| 11/2 | 2026/10/12 23:19 | llm | patch-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29789 output: 236 thoughts: 2497 Thoughts: **Fuzzing Target Identification: vsock Netlink and vhost-vsock for Vulnerabilities** Okay, so here's the breakdown of what I'm looking at. First, I need to understand the module dependencies, and it looks like `vsock.o` is built with `CONFIG_VSOCKETS`, while `virtio_transport.o` needs `CONFIG_VIRTIO_VSOCKETS` and `virtio_transport_common.o` is tied to `CONFIG_VIRTIO_VSOCKETS_COMMON`. Finally, `vhost_vsock.o` depends on `CONFIG_VHOST_VSOCK`. I need to ensure these are properly configured for the fuzzing environment; specifically `VSOCKETS`, `VIRTIO_VSOCKETS`, and `VHOST_VSOCK`. Now, the interesting part: I'm examining code changes and identifying critical functions. I've got a list from `net/vmw_vsock/af_vsock.c`, `drivers/vhost/vsock.c`, `net/vmw_vsock/virtio_transport.c`, and `net/vmw_vsock/virtio_transport_common.c`. I need to zero in on the relevant entry points and those functions that have been changed. I see that `vsock_nl_dev_netns_set_doit` and `vsock_nl_dev_netns_get_doit` are critical, as they're the direct netlink operations. These are exposed to user space. `vsock_maybe_set_connected` is also important, it's called by `virtio_transport_recv_connecting` and `virtio_transport_recv_listen`. Then there's `vhost_vsock_set_cid` called by ioctl on `/dev/vhost-vsock`. I need to double-check syzkaller support and confirm I have the right syscalls and ioctls set up. Good, syzkaller does support these. The core question is, is this code fuzzer-worthy? Absolutely, given the networking context, netlink interface, namespace switching, and state management. The code changes involve complex interaction with network namespaces, state transitions, and connection handling, which are all prime targets for fuzzing. It's definitely reachable code and will run in standard QEMU. Therefore, after a thorough review, I've identified the optimal functions for "FocusSymbols". I have to make sure I pick the right ones, avoiding the common and instead selecting functions that are specific to the changes, such as `vsock_nl_dev_netns_set_doit`, `vsock_nl_dev_netns_get_doit`, `vsock_maybe_set_connected`, `vsock_g2h_net_reachable`, `vsock_g2h_net_get`, `vhost_vsock_set_cid`, and `__vsock_find_cid`. These are all exported symbols except `vhost_vsock_set_cid`, which is called through the vhost ioctl. In short, there is a clear attack surface: new netlink commands for network namespace management and vhost-vsock ioctls combined with the fact that these modules need `CONFIG_VSOCKETS`, `CONFIG_VIRTIO_VSOCKETS`, and `CONFIG_VHOST_VSOCK` enabled. The code is reachable through AF_NETLINK sockets, AF_VSOCK sockets, and /dev/vhost-vsock ioctls. I can now `set-results`. |
| 12/2 | 2026/10/12 23:19 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"VSOCKETS",
"VIRTIO_VSOCKETS",
"VHOST_VSOCK"
],
"FocusSymbols": [
"vsock_nl_dev_netns_set_doit",
"vsock_nl_dev_netns_get_doit",
"vsock_maybe_set_connected",
"vsock_g2h_net_reachable",
"vsock_g2h_net_get",
"vhost_vsock_set_cid",
"__vsock_find_cid"
],
"Reasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK ('vsock' family) enabling dynamic network namespace assignment for the guest-to-host vsock device. It adds reachability checks, CID conflict validation, and connection reset sweeps across network namespaces in vsock core, virtio-vsock, and vhost-vsock. This modifies reachable socket and netlink logic that can be exercised in standard virtualized environments.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"VSOCKETS",
"VIRTIO_VSOCKETS",
"VHOST_VSOCK"
],
"FocusSymbols": [
"vsock_nl_dev_netns_set_doit",
"vsock_nl_dev_netns_get_doit",
"vsock_maybe_set_connected",
"vsock_g2h_net_reachable",
"vsock_g2h_net_get",
"vhost_vsock_set_cid",
"__vsock_find_cid"
],
"Reasoning": "The patch series introduces a Generic Netlink control interface for AF_VSOCK ('vsock' family) enabling dynamic network namespace assignment for the guest-to-host vsock device. It adds reachability checks, CID conflict validation, and connection reset sweeps across network namespaces in vsock core, virtio-vsock, and vhost-vsock. This modifies reachable socket and netlink logic that can be exercised in standard virtualized environments.",
"WorthFuzzing": true
} |
| 13/1 | 2026/10/12 23:19 | action | If |
1mArgs:{
"WorthFuzzing": true
}Results: null |
| 14/2 | 2026/10/12 23:19 | agent | kmsan-evaluator |
1mModel:core Args: null Results: {
"KMSANReasoning": "The patch series implements network namespace assignment for the guest-to-host (G2H) vsock device via new genetlink commands (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET), updates the IOCTL_VM_SOCKETS_GET_LOCAL_CID ioctl to respect network namespaces, and updates socket reachability and teardown logic when namespaces change.\n\nFrom a memory sanitization perspective:\n1. User-space copies / info-leaks: The netlink GET handler sends only a single 32-bit signed scalar attribute (VSOCK_A_NETNS_ID via nla_put_s32), which is deterministically initialized before transmission and has no struct padding or uninitialized field issues. The SET handler returns an empty genetlink response header. The ioctl handler copies a primitive u32 value ('cid') via put_user(), which is explicitly assigned on all execution paths. No uninitialized memory or structure padding is leaked to user space.\n2. Uninitialized memory in kernel logic: No new complex data structures or uninitialized buffers are introduced. All local variables used in branching logic are fully initialized before use. Packet buffers (sk_buff) are not parsed with modified lengths or bounds checks.\n3. Relevant bug classes: The potential risks introduced by this change relate to network namespace reference tracking (get_net_track/put_net_track), RCU synchronization (rcu_dereference/rcu_replace_pointer), socket state machine transitions, socket lifetime (use-after-free during reset sweeps), and mutex locking (vsock_register_mutex and vsock_table_lock). These failure modes are comprehensively covered by standard KASAN, LOCKDEP, and refcount tracking.\n\nBecause the changes introduce no risk of uninitialized memory reads or leaks detectable exclusively by KMSAN, a dedicated KMSAN fuzzing session is not warranted.",
"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 07e9cb1df64854ae931106c7f7246c9482607b56
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Oct 12 23:19:09 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..9276479004c0f 100644
--- a/Documentation/admin-guide/sysctl/net.rst
+++ b/Documentation/admin-guide/sysctl/net.rst
@@ -529,6 +529,43 @@ 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.
+
+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 72ca3aab21066..f11423727449f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -29163,10 +29163,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 44cee74519555..e663be3bbfa0b 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,15 @@ 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;
/**** UTILS ****/
@@ -480,7 +513,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 +586,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);
@@ -564,7 +632,12 @@ static bool vsock_use_local_transport(unsigned int remote_cid)
return true;
if (transport_g2h) {
- return remote_cid == transport_g2h->get_local_cid();
+ u32 cid = __vsock_registered_transport_cid(&transport_g2h, net);
+
+ /* The device may be unreachable from @net, in which case
+ * @remote_cid is not the local CID.
+ */
+ return cid != VMADDR_CID_ANY && remote_cid == cid;
} else {
return remote_cid == VMADDR_CID_HOST;
}
@@ -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,267 @@ 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);
+
+/* 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, *tmp;
+ struct sock *sk;
+ int i;
+
+ lockdep_assert_held(&vsock_register_mutex);
+
+ spin_lock_bh(&vsock_table_lock);
+
+ for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) {
+ list_for_each_entry_safe(vsk, tmp, &vsock_connected_table[i],
+ connected_table) {
+ sk = sk_vsock(vsk);
+
+ if (vsk->transport != t ||
+ sk->sk_state == TCP_CLOSE ||
+ vsock_g2h_reachable_sk(vsk))
+ continue;
+
+ /* If close has already been scheduled for this socket,
+ * we can skip.
+ */
+ if (vsk->close_work_scheduled)
+ continue;
+
+ sk->sk_state = TCP_CLOSE;
+ sk->sk_err = ECONNRESET;
+ sk_error_report(sk);
+
+ __vsock_remove_connected(vsk);
+ }
+ }
+
+ spin_unlock_bh(&vsock_table_lock);
+}
+
+/* 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;
+ netns_tracker old_tracker;
+ struct net *old;
+ void *hdr;
+ int ret;
+
+ mutex_lock(&vsock_register_mutex);
+
+ ret = -ENODEV;
+ if (!transport_g2h) {
+ NL_SET_ERR_MSG(extack, "no guest-to-host transport is loaded");
+ goto unlock;
+ }
+
+ ret = -EOPNOTSUPP;
+ if (!vsock_net_assignable(transport_g2h)) {
+ NL_SET_ERR_MSG(extack,
+ "the loaded guest-to-host transport does not support namespace assignment");
+ goto unlock;
+ }
+
+ if (transport_h2g && !transport_h2g->has_remote_cid) {
+ NL_SET_ERR_MSG(extack,
+ "the loaded host-to-guest transport cannot check the device's CID");
+ goto unlock;
+ }
+
+ ret = -EADDRINUSE;
+ if (transport_h2g &&
+ transport_h2g->has_remote_cid(net,
+ transport_g2h->get_local_cid())) {
+ NL_SET_ERR_MSG(extack,
+ "a guest in the target namespace already uses the device's CID");
+ goto unlock;
+ }
+
+ ret = -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);
+
+ /* See vsock_maybe_set_connected() comment about synchronizing with
+ * connecting sockets.
+ */
+ vsock_g2h_reset_unreachable(transport_g2h);
+
+ mutex_unlock(&vsock_register_mutex);
+
+ vsock_g2h_net_release(old, &old_tracker);
+
+ return 0;
+
+unlock:
+ mutex_unlock(&vsock_register_mutex);
+ nlmsg_free(msg);
+
+ return ret;
+}
+
static struct sock *vsock_dequeue_accept(struct sock *listener)
{
struct vsock_sock *vlistener;
@@ -908,7 +1250,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) {
@@ -2795,6 +3138,7 @@ static long vsock_dev_do_ioctl(struct file *filp,
{
u32 __user *p = ptr;
int retval = 0;
+ struct net *net;
u32 cid;
switch (cmd) {
@@ -2802,11 +3146,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;
@@ -2847,6 +3194,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,
@@ -3036,7 +3421,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);
@@ -3046,14 +3431,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)
@@ -3062,6 +3447,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);
@@ -3083,15 +3470,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:
@@ -3099,15 +3494,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)
@@ -3174,13 +3574,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;
@@ -3189,6 +3594,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..4123924536dd6 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -542,13 +542,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 +587,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 +617,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 +634,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 +688,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 +705,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 f225f53ed4bab..0234379c14a15 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 1e746e25e2bc9..fa51108c43d43 100644
--- a/tools/net/ynl/Makefile.deps
+++ b/tools/net/ynl/Makefile.deps
@@ -55,4 +55,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..d5d7b0660c5da 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,359 @@ 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.
+ 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'
+
+ started=${SECONDS}
+ 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} -ge ${gap} ]]; then
+ log_host "assign took ${elapsed}s, longer than the ${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}"
+}
+
+test_ns_guest_assign_g2h_netns_no_cap_net_admin_fails() {
+ local cmd="unshare -n setpriv --bounding-set=-net_admin"
+ local rc
+
+ vm_ssh "init_ns" -- "${cmd}" python3 /root/ynl/cli.py --no-schema \
+ --spec /root/ynl/vsock.yaml --do dev-netns-set &>/dev/null
+ rc=$?
+
+ if [[ "${rc}" -eq 0 ]]; then
+ log_host "assign unexpectedly succeeded without CAP_NET_ADMIN"
+ return "${KSFT_FAIL}"
+ fi
+
+ return "${KSFT_PASS}"
+}
+
+test_ns_guest_assign_g2h_netns_unpriv_user_ns_fails() {
+ local unpriv_uid=65534
+ local unpriv
+ local rc
+
+ unpriv="setpriv --reuid=${unpriv_uid} --regid=${unpriv_uid}"
+ unpriv="${unpriv} --clear-groups"
+
+ if ! vm_ssh "init_ns" -- "${unpriv} unshare -U true"; then
+ log_host "unprivileged user namespaces unavailable, skipping"
+ return "${KSFT_SKIP}"
+ fi
+
+ vm_ssh "init_ns" -- "${unpriv} unshare -Urn" \
+ python3 /root/ynl/cli.py --no-schema \
+ --spec /root/ynl/vsock.yaml --do dev-netns-set &>/dev/null
+ rc=$?
+
+ if [[ "${rc}" -eq 0 ]]; then
+ log_host "assign unexpectedly succeeded for unprivileged user"
+ return "${KSFT_FAIL}"
+ fi
+
+ return "${KSFT_PASS}"
+}
+
shared_vm_test() {
local tname
@@ -1453,7 +1993,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 +2058,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
|
| 15/3 | 2026/10/12 23:19 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 24028 output: 37 thoughts: 2638 Thoughts: **KMSAN Evaluation for VSOCK Netlink and IOCTL Changes**
Okay, I need to determine if this patch series necessitates a dedicated KMSAN fuzzing session. Let's break it down based on the established KMSAN rules. The core question is whether the changes introduce or modify any areas where uninitialized memory could be read, particularly when data is passed to user space or when conditional logic depends on potentially uninitialized variables. Standard KASAN should handle the usual suspects (OOB, UAF, etc.), so I'm focusing on KMSAN-specific concerns.
1. **Netlink (genetlink) `vsock` family:** The patch adds commands `dev-netns-set` and `dev-netns-get`. I need to review these carefully.
* `vsock_nl_dev_netns_fill`: I initially had a concern about the `id` variable within this function. The code path includes potentially skipping the setting of the netns id attribute and therefore not initializing `id`. However, upon closer inspection, the conditional logic ensures that `id` is properly initialized in all execution paths, either to `NETNSA_NSID_NOT_ASSIGNED` or the result of `peernet2id_alloc()`. The code doesn't read `id` if it's not initialized. So, no KMSAN issue there.
* `vsock_g2h_net_assign`: This handler for `dev-netns-set` merely returns an empty netlink message. The YAML configuration suggests a `netns-id` attribute, but the code doesn't actually put it in the message, so there's no data being passed to userspace.
* `vsock_nl_dev_netns_get_doit`: This function calls `vsock_nl_dev_netns_fill`, so the above analysis applies. The message itself is allocated with `genlmsg_new()`, so the buffer's initial state is known.
2. **IOCTL Command `vsock_dev_do_ioctl`:** Here, the code writes the `cid` (connection ID) to user space via `put_user()`.
* `cid` source is `vsock_registered_transport_cid()`. The function path ensures that `cid` always ends up being assigned a value: either `VMADDR_CID_ANY` or the result of `(*transport)->get_local_cid()`, which is a `u32`. This confirms the initialization of the variable before it is passed to the user.
* I need to be aware of the interaction of `struct net *net`, `vsock_g2h_net`, and `netns_tracker`.
* I check `vsock_g2h_net_swap`, `virtio_transport_rx_work`, `vsock_g2h_net_get`, and `virtio_transport_recv_pkt`. The `netns_tracker` is initialized within `vsock_g2h_net`, and so `net` is also initialized using `get_net_track()` when `vsock_g2h_net_get` is called.
Based on the above, I see no evidence of uninitialized memory being read from any of the changed code paths, especially from the netlink attributes or from `ioctl`. There is no data at risk of memory leaks due to the new code paths. KASAN/other standard bug detectors are enough to handle the usual suspects. I don't see any KMSAN requirements here. The patch does not introduce the risks that the KMSAN detector is designed to check.
|
| 16/3 | 2026/10/12 23:20 | tool | grepper |
0mArgs:{
"Expression": "virtio_transport_recv_pkt",
"PathPrefixes": [
"net/vmw_vsock/"
]
}Results: {
"Output": "net/vmw_vsock/virtio_transport.c=633=static void virtio_transport_rx_work(struct work_struct *work)\n--\nnet/vmw_vsock/virtio_transport.c-698-\nnet/vmw_vsock/virtio_transport.c:699:\t\t\tvirtio_transport_recv_pkt(t, skb, net);\nnet/vmw_vsock/virtio_transport.c-700-\t\t}\n--\nnet/vmw_vsock/virtio_transport_common.c=1779=static bool virtio_transport_valid_type(u16 type)\n--\nnet/vmw_vsock/virtio_transport_common.c-1787- */\nnet/vmw_vsock/virtio_transport_common.c:1788:void virtio_transport_recv_pkt(struct virtio_transport *t,\nnet/vmw_vsock/virtio_transport_common.c-1789-\t\t\t struct sk_buff *skb, struct net *net)\n--\nnet/vmw_vsock/virtio_transport_common.c-1801-\nnet/vmw_vsock/virtio_transport_common.c:1802:\ttrace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,\nnet/vmw_vsock/virtio_transport_common.c-1803-\t\t\t\t\tdst.svm_cid, dst.svm_port,\n--\nnet/vmw_vsock/virtio_transport_common.c-1896-}\nnet/vmw_vsock/virtio_transport_common.c:1897:EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);\nnet/vmw_vsock/virtio_transport_common.c-1898-\n--\nnet/vmw_vsock/vsock_loopback.c=123=static void vsock_loopback_work(struct work_struct *work)\n--\nnet/vmw_vsock/vsock_loopback.c-141-\t\tvirtio_transport_deliver_tap_pkt(skb);\nnet/vmw_vsock/vsock_loopback.c:142:\t\tvirtio_transport_recv_pkt(\u0026loopback_transport, skb,\nnet/vmw_vsock/vsock_loopback.c-143-\t\t\t\t\t sock_net(skb-\u003esk));\n"
} |
| 17/3 | 2026/10/12 23:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 28113 output: 45 thoughts: 42 Thoughts: Okay, let's dive into this. Here's my understanding, assuming I'm the one thinking these things: **Analyzing `virtio_transport_recv_pkt` for Net Usage** Right, so I need to understand how the `net` subsystem is utilized within the `vmw_vsock` implementation, specifically when receiving packets. The most logical place to start is `virtio_transport_recv_pkt` in `net/vmw_vsock/virtio_transport_common.c`. I'll need to dissect this function to see how it interacts with the networking stack. I'm looking for clues about how data is received, processed, and ultimately handled within the overall network framework. My goal is to map out the data flow from the virtio interface to the higher-level networking components. This should give me a clearer picture of potential bottlenecks, optimization opportunities, or any architectural assumptions made in the design. |
| 18/3 | 2026/10/12 23:20 | tool | read-file |
0mArgs:{
"File": "net/vmw_vsock/virtio_transport_common.c",
"FirstLine": 1785,
"LineCount": 70
}Results: {
"Contents": "1785:\t/* We are under the virtio-vsock's vsock-\u003erx_lock or vhost-vsock's vq-\u003emutex\n1786:\t * lock.\n1787:\t */\n1788:\tvoid virtio_transport_recv_pkt(struct virtio_transport *t,\n1789:\t\t\t\t struct sk_buff *skb, struct net *net)\n1790:\t{\n1791:\t\tstruct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);\n1792:\t\tstruct sockaddr_vm src, dst;\n1793:\t\tstruct vsock_sock *vsk;\n1794:\t\tstruct sock *sk;\n1795:\t\tbool space_available;\n1796:\t\n1797:\t\tvsock_addr_init(\u0026src, le64_to_cpu(hdr-\u003esrc_cid),\n1798:\t\t\t\tle32_to_cpu(hdr-\u003esrc_port));\n1799:\t\tvsock_addr_init(\u0026dst, le64_to_cpu(hdr-\u003edst_cid),\n1800:\t\t\t\tle32_to_cpu(hdr-\u003edst_port));\n1801:\t\n1802:\t\ttrace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,\n1803:\t\t\t\t\t\tdst.svm_cid, dst.svm_port,\n1804:\t\t\t\t\t\tle32_to_cpu(hdr-\u003elen),\n1805:\t\t\t\t\t\tle16_to_cpu(hdr-\u003etype),\n1806:\t\t\t\t\t\tle16_to_cpu(hdr-\u003eop),\n1807:\t\t\t\t\t\tle32_to_cpu(hdr-\u003eflags),\n1808:\t\t\t\t\t\tle32_to_cpu(hdr-\u003ebuf_alloc),\n1809:\t\t\t\t\t\tle32_to_cpu(hdr-\u003efwd_cnt));\n1810:\t\n1811:\t\tif (!virtio_transport_valid_type(le16_to_cpu(hdr-\u003etype))) {\n1812:\t\t\t(void)virtio_transport_reset_no_sock(t, skb, net);\n1813:\t\t\tgoto free_pkt;\n1814:\t\t}\n1815:\t\n1816:\t\t/* The socket must be in connected or bound table\n1817:\t\t * otherwise send reset back\n1818:\t\t */\n1819:\t\tsk = vsock_find_connected_socket_net(\u0026src, \u0026dst, net);\n1820:\t\tif (!sk) {\n1821:\t\t\tsk = vsock_find_bound_socket_net(\u0026dst, net);\n1822:\t\t\tif (!sk) {\n1823:\t\t\t\t(void)virtio_transport_reset_no_sock(t, skb, net);\n1824:\t\t\t\tgoto free_pkt;\n1825:\t\t\t}\n1826:\t\t}\n1827:\t\n1828:\t\tif (virtio_transport_get_type(sk) != le16_to_cpu(hdr-\u003etype)) {\n1829:\t\t\t(void)virtio_transport_reset_no_sock(t, skb, net);\n1830:\t\t\tsock_put(sk);\n1831:\t\t\tgoto free_pkt;\n1832:\t\t}\n1833:\t\n1834:\t\tif (!skb_set_owner_sk_safe(skb, sk)) {\n1835:\t\t\tWARN_ONCE(1, \"receiving vsock socket has sk_refcnt == 0\\n\");\n1836:\t\t\tgoto free_pkt;\n1837:\t\t}\n1838:\t\n1839:\t\tvsk = vsock_sk(sk);\n1840:\t\n1841:\t\tlock_sock(sk);\n1842:\t\n1843:\t\t/* Check if sk has been closed or assigned to another transport before\n1844:\t\t * lock_sock (note: listener sockets are not assigned to any transport)\n1845:\t\t */\n1846:\t\tif (sock_flag(sk, SOCK_DONE) ||\n1847:\t\t (sk-\u003esk_state != TCP_LISTEN \u0026\u0026\n1848:\t\t !vsock_check_source(vsk, \u0026t-\u003etransport, \u0026src))) {\n1849:\t\t\t(void)virtio_transport_reset_no_sock(t, skb, net);\n1850:\t\t\trelease_sock(sk);\n1851:\t\t\tsock_put(sk);\n1852:\t\t\tgoto free_pkt;\n1853:\t\t}\n1854:\t\n"
} |
| 19/3 | 2026/10/12 23:20 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 29583 output: 39 thoughts: 123 Thoughts: Okay, let me think this through. **Analyzing the `virtio_transport_recv_pkt` Change** I'm noticing a critical change in this `virtio_transport_recv_pkt` function. Previously, it was called with a `NULL` value, but now we're passing `net`. That's the key difference. The old code was: ```c virtio_transport_recv_pkt(&virtio_transport, skb, NULL); ``` While the new code is: ```c virtio_transport_recv_pkt(t, skb, net); ``` The difference is passing in `net`. I need to figure out the implications of this. My focus is now on how this affects `vsock_find_connected_socket_net` as it's the function that receives this new argument. The old call would look like `sk = vsock_find_connected_socket_net(&src, &dst, NULL);` but the new code is `sk = vsock_find_connected_socket_net(&src, &dst, net);` The vital question then is: what happens internally when `net` is `NULL` versus when it's *not* `NULL` in the `vsock_find_connected_socket_net` function? It's all about how that function handles the network namespace context. This is what I need to investigate to determine the impact of this change. |
| 20/3 | 2026/10/12 23:21 | tool | grepper |
0mArgs:{
"Expression": "vsock_find_connected_socket_net",
"PathPrefixes": [
"net/vmw_vsock/"
]
}Results: {
"Output": "net/vmw_vsock/af_vsock.c=352=static struct sock *\nnet/vmw_vsock/af_vsock.c:353:__vsock_find_connected_socket_net(struct sockaddr_vm *src,\nnet/vmw_vsock/af_vsock.c-354-\t\t\t\t struct sockaddr_vm *dst, struct net *net)\n--\nnet/vmw_vsock/af_vsock.c=438=EXPORT_SYMBOL_GPL(vsock_find_bound_socket);\n--\nnet/vmw_vsock/af_vsock.c-444- */\nnet/vmw_vsock/af_vsock.c:445:struct sock *vsock_find_connected_socket_net(struct sockaddr_vm *src,\nnet/vmw_vsock/af_vsock.c-446-\t\t\t\t\t struct sockaddr_vm *dst,\n--\nnet/vmw_vsock/af_vsock.c-451-\tspin_lock_bh(\u0026vsock_table_lock);\nnet/vmw_vsock/af_vsock.c:452:\tsk = __vsock_find_connected_socket_net(src, dst, net);\nnet/vmw_vsock/af_vsock.c-453-\tif (sk)\n--\nnet/vmw_vsock/af_vsock.c-459-}\nnet/vmw_vsock/af_vsock.c:460:EXPORT_SYMBOL_GPL(vsock_find_connected_socket_net);\nnet/vmw_vsock/af_vsock.c-461-\n--\nnet/vmw_vsock/af_vsock.c=467=struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,\n--\nnet/vmw_vsock/af_vsock.c-469-{\nnet/vmw_vsock/af_vsock.c:470:\treturn vsock_find_connected_socket_net(src, dst, NULL);\nnet/vmw_vsock/af_vsock.c-471-}\n--\nnet/vmw_vsock/virtio_transport_common.c=1788=void virtio_transport_recv_pkt(struct virtio_transport *t,\n--\nnet/vmw_vsock/virtio_transport_common.c-1818-\t */\nnet/vmw_vsock/virtio_transport_common.c:1819:\tsk = vsock_find_connected_socket_net(\u0026src, \u0026dst, net);\nnet/vmw_vsock/virtio_transport_common.c-1820-\tif (!sk) {\n"
} |
| 21/3 | 2026/10/12 23:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 30746 output: 42 thoughts: 34 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Diving into vsock Connection Discovery** Okay, I need to understand how these vsock connections are being managed at the network level, so let's start by digging into `__vsock_find_connected_socket_net` within `net/vmw_vsock/af_vsock.c`. That's likely where the connection tracking and lookups are happening, and it's the right level of abstraction to begin with. I'm expecting to find the key data structures used to index connections. I need to see how the connection establishment process is linked in there. I'm hoping I don't run into any unnecessary locking or per-CPU data structures early on. This should give me a good handle on how the kernel figures out which socket goes with which inbound connection request. I need to be sure that this function accounts for all the different potential states the socket can be in. |
| 22/3 | 2026/10/12 23:21 | tool | read-file |
0mArgs:{
"File": "net/vmw_vsock/af_vsock.c",
"FirstLine": 350,
"LineCount": 40
}Results: {
"Contents": " 350:\t}\n 351:\t\n 352:\tstatic struct sock *\n 353:\t__vsock_find_connected_socket_net(struct sockaddr_vm *src,\n 354:\t\t\t\t\t struct sockaddr_vm *dst, struct net *net)\n 355:\t{\n 356:\t\tstruct vsock_sock *vsk;\n 357:\t\n 358:\t\tlist_for_each_entry(vsk, vsock_connected_sockets(src, dst),\n 359:\t\t\t\t connected_table) {\n 360:\t\t\tstruct sock *sk = sk_vsock(vsk);\n 361:\t\n 362:\t\t\tif (vsock_addr_equals_addr(src, \u0026vsk-\u003eremote_addr) \u0026\u0026\n 363:\t\t\t dst-\u003esvm_port == vsk-\u003elocal_addr.svm_port \u0026\u0026\n 364:\t\t\t vsock_net_check_mode(sock_net(sk), net)) {\n 365:\t\t\t\treturn sk;\n 366:\t\t\t}\n 367:\t\t}\n 368:\t\n 369:\t\treturn NULL;\n 370:\t}\n 371:\t\n 372:\tstatic void vsock_insert_unbound(struct vsock_sock *vsk)\n 373:\t{\n 374:\t\tspin_lock_bh(\u0026vsock_table_lock);\n 375:\t\t__vsock_insert_bound(vsock_unbound_sockets, vsk);\n 376:\t\tspin_unlock_bh(\u0026vsock_table_lock);\n 377:\t}\n 378:\t\n 379:\tvoid vsock_insert_connected(struct vsock_sock *vsk)\n 380:\t{\n 381:\t\tstruct list_head *list = vsock_connected_sockets(\n 382:\t\t\t\u0026vsk-\u003eremote_addr, \u0026vsk-\u003elocal_addr);\n 383:\t\n 384:\t\tspin_lock_bh(\u0026vsock_table_lock);\n 385:\t\t__vsock_insert_connected(list, vsk);\n 386:\t\tspin_unlock_bh(\u0026vsock_table_lock);\n 387:\t}\n 388:\tEXPORT_SYMBOL_GPL(vsock_insert_connected);\n 389:\t\n"
} |
| 23/3 | 2026/10/12 23:21 | llm | kmsan-evaluator |
0mModel:gemini-3.8-flash Args: null Results: null Tokens: input: 31646 output: 429 thoughts: 3744 Thoughts: **KMSAN Analysis of AF_VSOCK Network Namespace Support** Okay, let's break this down systematically. I'm focusing on whether this patch series introduces any *unique* uninitialized memory bugs that KMSAN would catch and other tools like KASAN, LOCKDEP, or standard checks wouldn't. This patch significantly touches the AF_VSOCK code, so thoroughness is crucial. First, I examined the Netlink interaction, specifically the `dev-netns-set` and `dev-netns-get` commands defined in `vsock.yaml`. The corresponding C code in `vsock_nl_gen.c` was scrutinized (`vsock_nl_ops`, `vsock_nl_dev_netns_set_doit`, `vsock_nl_dev_netns_fill`). The focus was on data sent to user space via Netlink: I confirmed that `vsock_nl_dev_netns_fill` securely uses `nla_put_s32`, with the `id` being initialized before calling `nla_put_s32`. In the `dev-netns-set`, `vsock_g2h_net_assign` builds a genetlink reply header which contains no uninitialized data, and the `msg` is allocated using `genlmsg_new`. I ensured no uninitialized data leaks. In the `vsock_dev_do_ioctl` function, the `cid` variable, passed to user space via `put_user`, is properly initialized as a 32-bit integer. I moved on to looking at potential allocations of complex data structures and branching logic to find uninitialized fields. There is no new allocation of complex structures, and the patch deals mainly with socket lookup and network namespace handling through RCU and `vsock_g2h_net_tracker` which is correctly initialized. There is also a boolean `netns_assign_allow` which has an initialized value. The overall control flow changes don't seem to allow reads of uninitialized memory. The patch includes significant changes, including: new netlink commands (`dev-netns-set` and `dev-netns-get`) for network namespace assignment, modifications to `ioctl` handling and CID resolution, and updates to socket reset and reachability logic. However, I didn't see any changes that could lead to uninitialized memory. I considered potential bugs introduced such as locking issues, reference counting errors, and socket state races. Such bugs, while serious, would be caught by KASAN, LOCKDEP or standard checks. In summary, this patch does not introduce the type of uninitialized memory usage or data leaks that KMSAN is designed to detect. Any memory safety concerns would be out-of-bounds, use-after-free, or locking-related, falling under the purview of KASAN, refcount checks, and LOCKDEP. KMSAN is, therefore, not required. ```tool_code set_results(NeedsKMSAN=false) ``` |
| 24/3 | 2026/10/12 23:21 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series implements network namespace assignment for the guest-to-host (G2H) vsock device via new genetlink commands (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET), updates the IOCTL_VM_SOCKETS_GET_LOCAL_CID ioctl to respect network namespaces, and updates socket reachability and teardown logic when namespaces change.\n\nFrom a memory sanitization perspective:\n1. User-space copies / info-leaks: The netlink GET handler sends only a single 32-bit signed scalar attribute (VSOCK_A_NETNS_ID via nla_put_s32), which is deterministically initialized before transmission and has no struct padding or uninitialized field issues. The SET handler returns an empty genetlink response header. The ioctl handler copies a primitive u32 value ('cid') via put_user(), which is explicitly assigned on all execution paths. No uninitialized memory or structure padding is leaked to user space.\n2. Uninitialized memory in kernel logic: No new complex data structures or uninitialized buffers are introduced. All local variables used in branching logic are fully initialized before use. Packet buffers (sk_buff) are not parsed with modified lengths or bounds checks.\n3. Relevant bug classes: The potential risks introduced by this change relate to network namespace reference tracking (get_net_track/put_net_track), RCU synchronization (rcu_dereference/rcu_replace_pointer), socket state machine transitions, socket lifetime (use-after-free during reset sweeps), and mutex locking (vsock_register_mutex and vsock_table_lock). These failure modes are comprehensively covered by standard KASAN, LOCKDEP, and refcount tracking.\n\nBecause the changes introduce no risk of uninitialized memory reads or leaks detectable exclusively by KMSAN, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series implements network namespace assignment for the guest-to-host (G2H) vsock device via new genetlink commands (VSOCK_CMD_DEV_NETNS_SET and VSOCK_CMD_DEV_NETNS_GET), updates the IOCTL_VM_SOCKETS_GET_LOCAL_CID ioctl to respect network namespaces, and updates socket reachability and teardown logic when namespaces change.\n\nFrom a memory sanitization perspective:\n1. User-space copies / info-leaks: The netlink GET handler sends only a single 32-bit signed scalar attribute (VSOCK_A_NETNS_ID via nla_put_s32), which is deterministically initialized before transmission and has no struct padding or uninitialized field issues. The SET handler returns an empty genetlink response header. The ioctl handler copies a primitive u32 value ('cid') via put_user(), which is explicitly assigned on all execution paths. No uninitialized memory or structure padding is leaked to user space.\n2. Uninitialized memory in kernel logic: No new complex data structures or uninitialized buffers are introduced. All local variables used in branching logic are fully initialized before use. Packet buffers (sk_buff) are not parsed with modified lengths or bounds checks.\n3. Relevant bug classes: The potential risks introduced by this change relate to network namespace reference tracking (get_net_track/put_net_track), RCU synchronization (rcu_dereference/rcu_replace_pointer), socket state machine transitions, socket lifetime (use-after-free during reset sweeps), and mutex locking (vsock_register_mutex and vsock_table_lock). These failure modes are comprehensively covered by standard KASAN, LOCKDEP, and refcount tracking.\n\nBecause the changes introduce no risk of uninitialized memory reads or leaks detectable exclusively by KMSAN, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|