From nobody Fri Apr 3 04:58:20 2026 Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B19B1F30A4 for ; Wed, 18 Feb 2026 01:45:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771379119; cv=none; b=nimubI9vUP9yeGwZDmWj64pVJ4kAq2qWLRDgHVSShwGOCFwK/Qnx1WDcLq0eCsbnepREh2i+yPr6O7CpkjzVAHGl84bWhrAJivDGecusybWzGXf9sfwkISpqpiGvOmszurDic6YvTBaDbKyBFuMiGD6cJ3NPSFIoQ0Ikb09aj0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771379119; c=relaxed/simple; bh=3AT9Jcju4MsV/M4OfDSUtW23PxNc6ImHVjzXYS6HH4U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=eMn9F0OKJgBDJnO7oYKOu2EmKdjrUXp/fP4l6FcfOtMnCqwTjeq8DB72upQ307tiJbf2WI0BWuTZj1N8oDIKArE+aJ578gndO6bi/aZrgOpbD5ZKqWuVGUxvAfFNFOKhmTipop9wmF9Or22ztGEP9j0CXERNmK0nkItfYswb6VM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iOn8/iSy; arc=none smtp.client-ip=209.85.128.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iOn8/iSy" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-794fe16d032so40106137b3.3 for ; Tue, 17 Feb 2026 17:45:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771379116; x=1771983916; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=NJw3RoD55rqXpHAojvOHyDwhmqotW0ZND3DwuoSWa18=; b=iOn8/iSyVCbgKzva3Z2w1//iMGcbnuC5yZ0qWt8X3winpZ9wvrFQn8qsjrpPLw29Eu aFL95n9ZVVGpKoeB3emUJMQ+tYuOGdsRnQgcxyNjIf1C53qL97inW8LziMlFrW0fv8Vz UeQZ0Ag0AznInCpSioNQ0NLBAumPFrpUnOZW2xgUu/NWIWGiN3JHAIQLGPVxQ2DqIVfq Z93ywxACQqphbySqRDBpCuE8QvL++Dr+xeeRW9UsQyzXgN0VxRJc5/F5293YwGAefhJn rY5U8QnikmdVfWcKiAGz6EEaKqmmQmYMuKbICi2WNPE1TMKXHgA3AuUnvaS3bGMSRByn EaoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771379116; x=1771983916; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NJw3RoD55rqXpHAojvOHyDwhmqotW0ZND3DwuoSWa18=; b=I0RTY4hqnDvR1SAL8ho48Qc7vajhllpVBwE5rDD9rtA2j7I/ACTQ1QwjO3oac4ZiJu yEQiPZ4+u8Aa0fE5FNvjow1Wlh2mNP84oVgtJmRHGWVirRkV4OOVKCuZ62ngogzlC+PV fr/VtlUNKUty77v1mVF7B+WVZ6qV3C/ykPzZdQParBw+P5l6fcuRFo2WKsUAZpjYK6Ww /a6oozL93haINh2HoyOp4HqyZR9BWiqRXGljTuDTyTUqU0JrFHjUgjjFx79n93CDD0ch 0cULQDiDg1v5tArJ1KwTL2Zry6uJTjREoSG3+2Zfiv6Cag3DQ6WfXBUAhzcgECqbplL/ KM+g== X-Forwarded-Encrypted: i=1; AJvYcCVg7TUqzgmNOf+4vO3hYnjyA+oR0sRhzVB99xZ5JV9FtTcofNmZivJVAR/rJNm4dRoDKvtbt7OKMi97R/o=@vger.kernel.org X-Gm-Message-State: AOJu0YwR02Pe8CpgHrB9yyDGQ8wvghimHrEIOz04ZCWNOCfTeBuXzEaS r5ZBVOAijDWruiAiVshvM1JK0q29RHjnOtw8XwkOWA47zshSKgByA7tJ X-Gm-Gg: AZuq6aIJQQLWyV0Jjbd/y/O2uLIxoR+ng1CWQ9Vz0VjWUCvW4Pwjw9S9B2Lf4QOEo4N 8WXW4anFla5W8uReKngUZoQl8f0Ehp+siXpXGthGKtlb9pl/L21I28bFMce1YnYJtPZ7b3JbFF0 W+yAfQPoDEwe4BdS8Xg4dAls6piHL+dIG/EKapvzEjeh1E9t76z0GtZJtZMFPUiTqcc0gsrBqI6 7GEcKNmSKFldCktzi/PhPWlv6jONOAMs9Yemvn4BNkZXo/kft8ljklixawqqE1tdmm3E/ppbYqj ZetNwi26iZFs6ecV4GWT3xqnF0zPE21kK2SGcKnhTy5bvyG7SaBaATxUB0MPN9HnXovnqNIKkst bLN2YC/wZtygT7Ptc9yyOXKDBZyLpNLgvmtL+GpzXLXkEwFvqvgPTkKQ8ACG5sKpDm+d9RjFCay eAdrKNq3Ovx8JZJs/p4BHcdw== X-Received: by 2002:a05:690c:87:b0:792:7236:9708 with SMTP id 00721157ae682-797f71e85acmr2766167b3.20.1771379116125; Tue, 17 Feb 2026 17:45:16 -0800 (PST) Received: from localhost ([2a03:2880:25ff:71::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-7966c18ffc5sm116481207b3.18.2026.02.17.17.45.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Feb 2026 17:45:15 -0800 (PST) From: Bobby Eshleman Date: Tue, 17 Feb 2026 17:45:10 -0800 Subject: [PATCH net] vsock: lock down child_ns_mode as write-once Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260217-vsock-ns-write-once-v1-1-a1fb30f289a9@meta.com> X-B4-Tracking: v=1; b=H4sIAKUZlWkC/x3MUQqDMAwG4KuE/9lA7cSVXmXsQWq2hUE6GtGBe HfB7wDfDpem4si0o8mqrtWQqe8I5TPZW1hnZEIMcQyxv/PqtXzZnLemi3C1IpzSbZjHNEiYIjr Cr8lL/9f6gMmC53Gcz98HGmoAAAA= X-Change-ID: 20260217-vsock-ns-write-once-8834d684e0a2 To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , Shuah Khan , Bobby Eshleman , "Michael S. Tsirkin" Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Daan De Meyer X-Mailer: b4 0.14.3 From: Bobby Eshleman To improve the security posture of vsock namespacing, this patch locks down the vsock child_ns_mode sysctl setting with a write-once policy. The user may write to child_ns_mode only once in each namespace, making changes to either local or global mode be irreversible. This avoids security breaches where a process in a local namespace may attempt to jailbreak into the global vsock ns space by setting child_ns_mode to "global", creating a new namespace, and accessing the global space through the new namespace. Additionally, fix the test functions that this change would otherwise break by adding "global-parent" and "local-parent" namespaces and using them as intermediaries to spawn namespaces in the given modes. This avoids the need to change "child_ns_mode" in the init_ns. nsenter must be used because ip netns unshares the mount namespace so nested "ip netns add" breaks exec calls from the init ns. Test run: 1..25 ok 1 vm_server_host_client ok 2 vm_client_host_server ok 3 vm_loopback ok 4 ns_host_vsock_ns_mode_ok ok 5 ns_host_vsock_child_ns_mode_ok ok 6 ns_global_same_cid_fails ok 7 ns_local_same_cid_ok ok 8 ns_global_local_same_cid_ok ok 9 ns_local_global_same_cid_ok ok 10 ns_diff_global_host_connect_to_global_vm_ok ok 11 ns_diff_global_host_connect_to_local_vm_fails ok 12 ns_diff_global_vm_connect_to_global_host_ok ok 13 ns_diff_global_vm_connect_to_local_host_fails ok 14 ns_diff_local_host_connect_to_local_vm_fails ok 15 ns_diff_local_vm_connect_to_local_host_fails ok 16 ns_diff_global_to_local_loopback_local_fails ok 17 ns_diff_local_to_global_loopback_fails ok 18 ns_diff_local_to_local_loopback_fails ok 19 ns_diff_global_to_global_loopback_ok ok 20 ns_same_local_loopback_ok ok 21 ns_same_local_host_connect_to_local_vm_ok ok 22 ns_same_local_vm_connect_to_local_host_ok ok 23 ns_delete_vm_ok ok 24 ns_delete_host_ok ok 25 ns_delete_both_ok SUMMARY: PASS=3D25 SKIP=3D0 FAIL=3D0 Fixes: eafb64f40ca4 ("vsock: add netns to vsock core") Signed-off-by: Bobby Eshleman Suggested-by: Daan De Meyer Suggested-by: Stefano Garzarella --- include/net/af_vsock.h | 6 +++++- include/net/netns/vsock.h | 1 + net/vmw_vsock/af_vsock.c | 10 ++++++---- tools/testing/selftests/vsock/vmtest.sh | 35 +++++++++++++++--------------= ---- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index d3ff48a2fbe0..c7de33039907 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -276,10 +276,14 @@ static inline bool vsock_net_mode_global(struct vsock= _sock *vsk) return vsock_net_mode(sock_net(sk_vsock(vsk))) =3D=3D VSOCK_NET_MODE_GLOB= AL; } =20 -static inline void vsock_net_set_child_mode(struct net *net, +static inline bool vsock_net_set_child_mode(struct net *net, enum vsock_net_mode mode) { + if (xchg(&net->vsock.child_ns_mode_locked, 1)) + return false; + WRITE_ONCE(net->vsock.child_ns_mode, mode); + return true; } =20 static inline enum vsock_net_mode vsock_net_child_mode(struct net *net) diff --git a/include/net/netns/vsock.h b/include/net/netns/vsock.h index b34d69a22fa8..8c855fff8039 100644 --- a/include/net/netns/vsock.h +++ b/include/net/netns/vsock.h @@ -17,5 +17,6 @@ struct netns_vsock { =20 enum vsock_net_mode mode; enum vsock_net_mode child_ns_mode; + int child_ns_mode_locked; }; #endif /* __NET_NET_NAMESPACE_VSOCK_H */ diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 9880756d9eff..35e097f4fde8 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -90,14 +90,15 @@ * * - /proc/sys/net/vsock/ns_mode (read-only) reports the current namespa= ce's * mode, which is set at namespace creation and immutable thereafter. - * - /proc/sys/net/vsock/child_ns_mode (writable) controls what mode fut= ure + * - /proc/sys/net/vsock/child_ns_mode (write-once) controls what mode f= uture * child namespaces will inherit when created. The initial value match= es * the namespace's own ns_mode. * * Changing child_ns_mode only affects newly created namespaces, not the * current namespace or existing children. A "local" namespace cannot set - * child_ns_mode to "global". At namespace creation, ns_mode is inherited - * from the parent's child_ns_mode. + * child_ns_mode to "global". child_ns_mode is write-once, so that it may + * be configured and locked down by a namespace manager. At namespace + * creation, ns_mode is inherited from the parent's child_ns_mode. * * The init_net mode is "global" and cannot be modified. * @@ -2853,7 +2854,8 @@ static int vsock_net_child_mode_string(const struct c= tl_table *table, int write, new_mode =3D=3D VSOCK_NET_MODE_GLOBAL) return -EPERM; =20 - vsock_net_set_child_mode(net, new_mode); + if (!vsock_net_set_child_mode(net, new_mode)) + return -EPERM; } =20 return 0; diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selfte= sts/vsock/vmtest.sh index dc8dbe74a6d0..e1e78b295e41 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -210,16 +210,17 @@ check_result() { } =20 add_namespaces() { - local orig_mode - orig_mode=3D$(cat /proc/sys/net/vsock/child_ns_mode) + ip netns add "global-parent" 2>/dev/null + echo "global" | ip netns exec "global-parent" \ + tee /proc/sys/net/vsock/child_ns_mode &>/dev/null + ip netns add "local-parent" 2>/dev/null + echo "local" | ip netns exec "local-parent" \ + tee /proc/sys/net/vsock/child_ns_mode &>/dev/null =20 - for mode in "${NS_MODES[@]}"; do - echo "${mode}" > /proc/sys/net/vsock/child_ns_mode - ip netns add "${mode}0" 2>/dev/null - ip netns add "${mode}1" 2>/dev/null - done - - echo "${orig_mode}" > /proc/sys/net/vsock/child_ns_mode + nsenter --net=3D/var/run/netns/global-parent ip netns add "global0" 2>/de= v/null + nsenter --net=3D/var/run/netns/global-parent ip netns add "global1" 2>/de= v/null + nsenter --net=3D/var/run/netns/local-parent ip netns add "local0" 2>/dev/= null + nsenter --net=3D/var/run/netns/local-parent ip netns add "local1" 2>/dev/= null } =20 init_namespaces() { @@ -237,6 +238,8 @@ del_namespaces() { log_host "removed ns ${mode}0" log_host "removed ns ${mode}1" done + ip netns del "global-parent" &>/dev/null + ip netns del "local-parent" &>/dev/null } =20 vm_ssh() { @@ -287,7 +290,7 @@ check_args() { } =20 check_deps() { - for dep in vng ${QEMU} busybox pkill ssh ss socat; do + for dep in vng ${QEMU} busybox pkill ssh ss socat nsenter; do if [[ ! -x $(command -v "${dep}") ]]; then echo -e "skip: dependency ${dep} not found!\n" exit "${KSFT_SKIP}" @@ -1231,12 +1234,8 @@ test_ns_local_same_cid_ok() { } =20 test_ns_host_vsock_child_ns_mode_ok() { - local orig_mode - local rc - - orig_mode=3D$(cat /proc/sys/net/vsock/child_ns_mode) + local rc=3D"${KSFT_PASS}" =20 - rc=3D"${KSFT_PASS}" for mode in "${NS_MODES[@]}"; do local ns=3D"${mode}0" =20 @@ -1246,15 +1245,13 @@ test_ns_host_vsock_child_ns_mode_ok() { continue fi =20 - if ! echo "${mode}" > /proc/sys/net/vsock/child_ns_mode; then - log_host "child_ns_mode should be writable to ${mode}" + if ! echo "${mode}" | ip netns exec "${ns}" \ + tee /proc/sys/net/vsock/child_ns_mode &>/dev/null; then rc=3D"${KSFT_FAIL}" continue fi done =20 - echo "${orig_mode}" > /proc/sys/net/vsock/child_ns_mode - return "${rc}" } =20 --- base-commit: 77c5e3fdd2793f478e6fdae55c9ea85b21d06f8f change-id: 20260217-vsock-ns-write-once-8834d684e0a2 Best regards, --=20 Bobby Eshleman