From nobody Sat Feb 7 09:29:23 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19E5C34A781; Wed, 22 Oct 2025 16:11:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761149485; cv=none; b=srXeIIoAqjFQPg4l9UNyjIODH3zAszwXXz0tnfkBLmZ94jpXRAA66QTwnXshinjfZ2pHz0z6S29Fe97laC1hr3v33mIi3DSYeafwGn/HCN8ZarxPB3THsSG6s/1VXjGmyENBDVshvHIV9N3FJCENkTsB3Qz7nYf55lCtmd1yE14= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761149485; c=relaxed/simple; bh=9gt1tGf0xzLbqqMQlOif9Kfq8cZPXlqC1fDbDaJd8HY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XcegZvEpSPgRRqoSxp6tQV6L8fspdhTlT/097NLV96CEiys5lo9Dh1+gUBDafuPUHH3pmot2Gg2Uozm6DFOYosQ6YTxZJSwcyK1UZjS7wKSKkjibmNmdI+bKAfz9IdveHf1udIzCPuoqBM92/1d6fWF+k3bsZ6rBM7mK+8yfFfo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TK5GFEEn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TK5GFEEn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29C98C4CEFD; Wed, 22 Oct 2025 16:11:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1761149485; bh=9gt1tGf0xzLbqqMQlOif9Kfq8cZPXlqC1fDbDaJd8HY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=TK5GFEEnmZrfv6GsUBTQz62dn/rSmX2tK5q7zHxzNNDif6hvZo3xBdfrdk/uKqzJB 86TjnIxufVb6avZlalC38ubTu0DUAfF/uF3MZ9yJhyYISlQQ2Oul6TPgqpGRjphdsU KsgiTQCpTfSdIhV+SDJ3mL23G1pgfxTV7xMvzRqKxeuOzL4ZwOaNC1AMqaLEUqxkCr nY1PUiE9FMjBJCJvmYHZsa+rKx3bWm1tQAtbfftCaX0O4SqINkhG6ZLEgE6MbKFFDO ziioRYG7FGA2bhXhcGJE1oJzw/g82omBkhvhU5Z+PRyuWSyMnkdVv4YBaj/l3tJzYh /zRM7mwuBvP6w== From: Christian Brauner Date: Wed, 22 Oct 2025 18:06:38 +0200 Subject: [PATCH v2 60/63] selftests/namespaces: ninth inactive namespace resurrection test 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: <20251022-work-namespace-nstree-listns-v2-60-71a588572371@kernel.org> References: <20251022-work-namespace-nstree-listns-v2-0-71a588572371@kernel.org> In-Reply-To: <20251022-work-namespace-nstree-listns-v2-0-71a588572371@kernel.org> To: linux-fsdevel@vger.kernel.org, Josef Bacik , Jeff Layton Cc: Jann Horn , Mike Yuan , =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= , Lennart Poettering , Daan De Meyer , Aleksa Sarai , Amir Goldstein , Tejun Heo , Johannes Weiner , Thomas Gleixner , Alexander Viro , Jan Kara , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, bpf@vger.kernel.org, Eric Dumazet , Jakub Kicinski , netdev@vger.kernel.org, Arnd Bergmann , Christian Brauner X-Mailer: b4 0.15-dev-96507 X-Developer-Signature: v=1; a=openpgp-sha256; l=5959; i=brauner@kernel.org; h=from:subject:message-id; bh=9gt1tGf0xzLbqqMQlOif9Kfq8cZPXlqC1fDbDaJd8HY=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWT8ZHi+48Ub8awebSYZqQBe41V3p6yYwf3qv9T+9nsJ8 62E10w931HKwiDGxSArpsji0G4SLrecp2KzUaYGzBxWJpAhDFycAjCRI2cZ/spKfFRXWnv9gbBi 83GvVafCZKNWv7rbe1R83Yznt/gY5DcxMlw+vetx0uX+VTNu1X/b8N9vCze/yvlXK+Sjshw5n/9 yi+cCAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Test that socket-kept netns appears in listns() output. Verify that a network namespace kept alive by a socket FD appears in listns() output even after the creating process exits, and that it disappears when the socket is closed. Signed-off-by: Christian Brauner --- .../testing/selftests/namespaces/siocgskns_test.c | 204 +++++++++++++++++= ++++ 1 file changed, 204 insertions(+) diff --git a/tools/testing/selftests/namespaces/siocgskns_test.c b/tools/te= sting/selftests/namespaces/siocgskns_test.c index 1c40c7772ad4..7b3133f3898c 100644 --- a/tools/testing/selftests/namespaces/siocgskns_test.c +++ b/tools/testing/selftests/namespaces/siocgskns_test.c @@ -577,4 +577,208 @@ TEST(siocgskns_ipv6) close(current_netns_fd); } =20 +/* + * Test that socket-kept netns appears in listns() output. + * Verify that a network namespace kept alive by a socket FD appears in + * listns() output even after the creating process exits, and that it + * disappears when the socket is closed. + */ +TEST(siocgskns_listns_visibility) +{ + int sock_fd, netns_fd, owner_fd; + int ipc_sockets[2]; + pid_t pid; + int status; + __u64 netns_id, owner_id; + struct ns_id_req req =3D { + .size =3D sizeof(req), + .spare =3D 0, + .ns_id =3D 0, + .ns_type =3D CLONE_NEWNET, + .spare2 =3D 0, + .user_ns_id =3D 0, + }; + __u64 ns_ids[256]; + int ret, i; + bool found_netns =3D false; + + EXPECT_EQ(socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets= ), 0); + + pid =3D fork(); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + /* Child: create new netns and socket */ + close(ipc_sockets[0]); + + if (unshare(CLONE_NEWNET) < 0) { + close(ipc_sockets[1]); + exit(1); + } + + sock_fd =3D socket(AF_INET, SOCK_DGRAM, 0); + if (sock_fd < 0) { + close(ipc_sockets[1]); + exit(1); + } + + /* Send socket FD to parent via SCM_RIGHTS */ + struct msghdr msg =3D {0}; + struct iovec iov =3D {0}; + char buf[1] =3D {'X'}; + char cmsg_buf[CMSG_SPACE(sizeof(int))]; + + iov.iov_base =3D buf; + iov.iov_len =3D 1; + msg.msg_iov =3D &iov; + msg.msg_iovlen =3D 1; + msg.msg_control =3D cmsg_buf; + msg.msg_controllen =3D sizeof(cmsg_buf); + + struct cmsghdr *cmsg =3D CMSG_FIRSTHDR(&msg); + cmsg->cmsg_level =3D SOL_SOCKET; + cmsg->cmsg_type =3D SCM_RIGHTS; + cmsg->cmsg_len =3D CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cmsg), &sock_fd, sizeof(int)); + + if (sendmsg(ipc_sockets[1], &msg, 0) < 0) { + close(sock_fd); + close(ipc_sockets[1]); + exit(1); + } + + close(sock_fd); + close(ipc_sockets[1]); + exit(0); + } + + /* Parent: receive socket FD */ + close(ipc_sockets[1]); + + struct msghdr msg =3D {0}; + struct iovec iov =3D {0}; + char buf[1]; + char cmsg_buf[CMSG_SPACE(sizeof(int))]; + + iov.iov_base =3D buf; + iov.iov_len =3D 1; + msg.msg_iov =3D &iov; + msg.msg_iovlen =3D 1; + msg.msg_control =3D cmsg_buf; + msg.msg_controllen =3D sizeof(cmsg_buf); + + ssize_t n =3D recvmsg(ipc_sockets[0], &msg, 0); + close(ipc_sockets[0]); + ASSERT_EQ(n, 1); + + struct cmsghdr *cmsg =3D CMSG_FIRSTHDR(&msg); + ASSERT_NE(cmsg, NULL); + memcpy(&sock_fd, CMSG_DATA(cmsg), sizeof(int)); + + /* Wait for child to exit */ + waitpid(pid, &status, 0); + ASSERT_TRUE(WIFEXITED(status)); + if (WEXITSTATUS(status) !=3D 0) + SKIP(close(sock_fd); return, "Child failed to create namespace"); + + /* Get network namespace from socket */ + netns_fd =3D ioctl(sock_fd, SIOCGSKNS); + if (netns_fd < 0) { + close(sock_fd); + if (errno =3D=3D ENOTTY || errno =3D=3D EINVAL) + SKIP(return, "SIOCGSKNS not supported"); + ASSERT_GE(netns_fd, 0); + } + + /* Get namespace ID */ + ret =3D ioctl(netns_fd, NS_GET_ID, &netns_id); + if (ret < 0) { + close(sock_fd); + close(netns_fd); + if (errno =3D=3D ENOTTY || errno =3D=3D EINVAL) + SKIP(return, "NS_GET_ID not supported"); + ASSERT_EQ(ret, 0); + } + + /* Get owner user namespace */ + owner_fd =3D ioctl(netns_fd, NS_GET_USERNS); + if (owner_fd < 0) { + close(sock_fd); + close(netns_fd); + if (errno =3D=3D ENOTTY || errno =3D=3D EINVAL) + SKIP(return, "NS_GET_USERNS not supported"); + ASSERT_GE(owner_fd, 0); + } + + /* Get owner namespace ID */ + ret =3D ioctl(owner_fd, NS_GET_ID, &owner_id); + if (ret < 0) { + close(owner_fd); + close(sock_fd); + close(netns_fd); + ASSERT_EQ(ret, 0); + } + close(owner_fd); + + /* Namespace should appear in listns() output */ + ret =3D sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0); + if (ret < 0) { + close(sock_fd); + close(netns_fd); + if (errno =3D=3D ENOSYS) + SKIP(return, "listns() not supported"); + TH_LOG("listns failed: %s", strerror(errno)); + ASSERT_GE(ret, 0); + } + + /* Search for our network namespace in the list */ + for (i =3D 0; i < ret; i++) { + if (ns_ids[i] =3D=3D netns_id) { + found_netns =3D true; + break; + } + } + + ASSERT_TRUE(found_netns); + TH_LOG("Found netns %llu in listns() output (kept alive by socket)", netn= s_id); + + /* Now verify with owner filtering */ + req.user_ns_id =3D owner_id; + found_netns =3D false; + + ret =3D sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0); + ASSERT_GE(ret, 0); + + for (i =3D 0; i < ret; i++) { + if (ns_ids[i] =3D=3D netns_id) { + found_netns =3D true; + break; + } + } + + ASSERT_TRUE(found_netns); + TH_LOG("Found netns %llu owned by userns %llu", netns_id, owner_id); + + /* Close socket - namespace should become inactive and disappear from lis= tns() */ + close(sock_fd); + close(netns_fd); + + /* Verify it's no longer in listns() output */ + req.user_ns_id =3D 0; + found_netns =3D false; + + ret =3D sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0); + ASSERT_GE(ret, 0); + + for (i =3D 0; i < ret; i++) { + if (ns_ids[i] =3D=3D netns_id) { + found_netns =3D true; + break; + } + } + + ASSERT_FALSE(found_netns); + TH_LOG("Netns %llu correctly disappeared from listns() after socket close= d", netns_id); +} + TEST_HARNESS_MAIN --=20 2.47.3