From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 70D13606A6BB for ; Sat, 28 Nov 2026 23:59:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=I/2knj/m Received: by kanga.kvack.org (Postfix) id 9C5A16B05CA; Sat, 28 Nov 2026 18:59:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 928386B05CB; Sat, 28 Nov 2026 18:59:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C96C6B05CC; Sat, 28 Nov 2026 18:59:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 292616B05CA for ; Sat, 28 Nov 2026 18:59:31 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9D456407C2 for ; Sat, 28 Nov 2026 23:59:25 +0000 (UTC) X-FDA: 85486197090.09.C2CC190 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf23.hostedemail.com (Postfix) with ESMTP id F0240140003 for ; Sat, 28 Nov 2026 23:59:23 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="I/2knj/m"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795910364; b=Ar68nGOX/Uh0EnrlKJR7zneBMLHe/XTtcfXbh3wZBsUrG8YlvESoLA573yNhKSHe12MB+U mS7VAhnA8hF3ZPtg3Q4wYuO6tAFzkCjo4T6BmEkWVcYtKOYorKlFvszXIX22YkTxD3/LA5 VmvAiBdDytmJlbo2C7eOVRO5ZB6rVfE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795910364; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4d/7P/hYUnZkexiHBmkwXWZpbJqlPFLkV5QFYsSL3b0=; b=BGkJuA1iuNfxnbSPlQ02PLqvpLnzXMprgFVRlyQCb8MU+DD6ZZ+ZEZgtt7woKrX7zkYRnU ISQ65bHrwGWjV/bOvilyNFQLhInQKevbzs8ePQAZayl6AbYQu5JYCMNrC5MFDSgULnYljR KVOys/8AfHE+PTE4I0ihaa8zx8cZ/pw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="I/2knj/m"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with UTF8SMTP id 587CA41247; Sat, 28 Nov 2026 23:59:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 6B5841F00A2A; Sat, 28 Nov 2026 23:59:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795910363; bh=4d/7P/hYUnZkexiHBmkwXWZpbJqlPFLkV5QFYsSL3b0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=I/2knj/mCLVivVHXzBcD55NH/PzXNaPrZs3z/9reL0oYO/fnikOis+QTU4Sit0Ytg 1Ygh5g8pMetzrVYs5ZMWHLX+jWSMvtnirSpQzBDit9HnVWHTPyYglRvXCwbR9S+vNN Nry25SmMRN8Ls21yI0EuaooTr73xMZCbbpEqO2Bw5sU2IGGs5PcZW4Knf3K7wWZrTw mLN0oKzBzYhvSIhwtKFYBj8p0zJFviShFOV6wL0EFJuUAsMtS7CtLVWByRUwiQN9cA /KRF0Zjf93fhaeoj/PhEu1eRp3GTn+bcW2qd+jo9dr37g2tUjJ2/YKWIEfwbhXt+XA FMxwL6ZBEDz7g== From: Jarkko Sakkinen To: fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , bpf@vger.kernel.org, containers@lists.linux.dev, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Aleksa Sarai , Alexander Mikhalitsyn , Alexander Viro , Andy Lutomirski , Arnd Bergmann , Christian Brauner , David Howells , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , Hugh Dickins , James Morris , Jan Kara , Jeff Layton , John Johansen , Jonathan Corbet , Josef Bacik , Kees Cook , KP Singh , Lennart Poettering , Matt Bobrowski , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Serge Hallyn , Shuah Khan , Stephen Smalley , Zygmunt Krynicki Subject: [RFC PATCH v8 16/29] userland: Add USERLAND_SHARE_NSFD ioctl Date: Sun, 29 Nov 2026 01:57:55 +0200 Message-ID: <20261128235808.2369970-17-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261128235808.2369970-1-jarkko@kernel.org> References: <20261128235808.2369970-1-jarkko@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: F0240140003 X-Stat-Signature: m4oc4hjdw3orpsm8sw779i9wz398wzia X-Rspam-User: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam07 X-HE-Tag: 1795910363-11933 X-HE-Meta: U2FsdGVkX1/3UTjb07L+hs2an02Nz7HHd0b50xkN0Od9pAaNJWmdxGQD+OVUV+cLHXi9zjJvzJGz0hfUbEE5yqZL5hQiAhukWVvp3FPuFfb0lsSa+kimSswpJCnhKWdkB0ztwv25l7QvS1wtNIWCli1sw4Z/2x7dyxcWL5y5QgoHhm/jVP9/vGyGKYUvBXcNb9bzp46FOmFQL1Th0SRB0BnGXpIcUVwDlIE28gTAXrIS9/Tf+nTKFOwm7eeXuvx8X64vzR9qnbJatPDtYF6fzeDa+C4e76f4P0rsV0WJwvxw0q+3z1s+0cZHiEGVTq49wwxOHZvW8nG0plTOAJDc1LdmpP/ogzMlcdhkGE8KsBQMjXunkf7YPic53FkYfoLcIGfccqBI7oB0TRkmlQuFIsGDIHaKJ5lgSPnDPSYdsWKIXTnpsgKYsP41uj675Gx45p4hcio/tQQzBcc1NWafUimbgax4x4z2h7KAyBAR61tWAPMRpxIhbdJ7dfwzTcXq6tDY51ZLDE2LlygbgxuZM3YTolBM3gc3e379Wo4y4/vvisdPO7c74Mo1qWMBg5xwxIZqs5f5S0Y0OL1G5vLjtfH0vHdf827SKkqvYgKHIPGWtg0zSBe+purSrQmSan5vXckRM+grPvEpM7kLdX5SVfqeU50nW1b+zxe655u9ZVRros/TLAEAxIB7WqMW71JHfuZYLbqaZ+6CydHltT56SdwdfU0YK6UDSCob/G1so0KzkZ65tioLNUq5Yh8aEhbVp+tN1CVUG8QrhqQMMiKB679Xenjk5ldYalsFkTefGdFlBTpVd0MJ7wolA1vDaAYTiaS5YyCkCI7A56mlpbUprIN1PpUklURB6A/P87Wnmw+usaqkrMkMLVIQQXCNMfTtsC4w8U6SxD2qC29SBXB7z+zdNponeOZHjiYm/4m0LbyedKDQMZ6AUOHX1DBNBbABsS5egsUHTkw8dAzK4Sl UYIn+C+N RkHEnzqCgvCpJUbouj8FeLxqWOiH9i2gR7qN1rQL8agnWVVFDFuGi981I+hfo5uaCal3PJot7EUkIsUidZmRYuHKNnc+j+d3Jxh01EUz2XBijDxhbz35Uru5E6rNNjxQ+C6h7xZhFDoXiGGUMxHDWdAn98glgaDnI5gTB+6bBGM6rPHZDhQ8aqpjEsplYG8iYvscfkEAWHwYivFRXo3dRILJqol3bVjG+qCDaXUFnDmM8VqMeiuZG0yP4iFkNZ/n0T0q/8vsFuqAhVf7YCgQGBc4xx7sWfJQRMPniRrgbgq2eCKCXEfPY6x4uflUB7aUv4SyMY/6l5ZaIpZuAcz7xaZtpsCib3Abs3mBU7meXUAWywrUdzPWoWwfKJ3AtfXGiqCIbrwzmADnCz9x15/WQpBSWaph8v4GRTEGtcdyTdFdKLU4dMjVc1KBkrrjZzBoTFSsezbeIUTaMSwz1bCHsytjeSoXoyS/bRkt1I02drkdVEVygiN2J12xFxKdCHTXx1QkSQUiMQCdNzluIS1pS2eK5q37Pzd+JR7d2Pl/57BuIA8uSoG8+kZFNgcYMy9+VVSJBeXflafZDC/o= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 userland_open() isolates every namespace by default rather than relying on unshare() deny-listing, which prevents workloads such as host-networked sandboxes or sidecars sharing a network or IPC namespace from opting into specific existing namespaces. Add the build-phase USERLAND_SHARE_NSFD ioctl to replace the userland's cgroup, IPC, UTS, network, or time namespace with a supplied nsfs file descriptor in-place under userland->lock while the userland is still in USERLAND_INIT, rejecting calls with -EBUSY once CLONE_INTO_USERLAND begins. Require CAP_SYS_ADMIN in the userland's user namespace, and require CAP_SYS_ADMIN in the target namespace's owning user namespace when the caller is not already a member of that namespace. Signed-off-by: Jarkko Sakkinen --- include/uapi/linux/userland.h | 14 ++++++ kernel/userland.c | 90 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/include/uapi/linux/userland.h b/include/uapi/linux/userland.h index 16b540fe80c9..bf325b572f5d 100644 --- a/include/uapi/linux/userland.h +++ b/include/uapi/linux/userland.h @@ -40,4 +40,18 @@ enum userland_flags { */ #define USERLAND_GET_NSFD _IO(USERLAND_IOCTL_MAGIC, 0x01) +/** + * DOC: USERLAND_SHARE_NSFD + * + * ``ioctl(userland_fd, USERLAND_SHARE_NSFD, ns_fd)`` replaces the corresponding + * member namespace of ``userland_fd`` with ``ns_fd`` (``CLONE_NEWCGROUP``, + * ``CLONE_NEWIPC``, ``CLONE_NEWUTS``, ``CLONE_NEWNET``, or ``CLONE_NEWTIME``) + * while ``userland_fd`` is in ``USERLAND_INIT``. + * + * Requires ``CAP_SYS_ADMIN`` in the userland's user namespace, plus + * ``CAP_SYS_ADMIN`` in ``ns_fd``'s owning user namespace when the caller is + * not already a member of ``ns_fd``. + */ +#define USERLAND_SHARE_NSFD _IO(USERLAND_IOCTL_MAGIC, 0x02) + #endif /* _UAPI_LINUX_USERLAND_H */ diff --git a/kernel/userland.c b/kernel/userland.c index 8e4b527fa0ea..90931e3a0bf6 100644 --- a/kernel/userland.c +++ b/kernel/userland.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include #include @@ -125,6 +127,92 @@ static long userland_get_nsfd(struct userland *userland, unsigned long nstype) return open_namespace(ns); } +static long userland_share_nsfd(struct userland *c, unsigned long arg) +{ + struct ns_common *old_time_children = NULL; + struct ns_common *old_ns = NULL; + struct ns_common *ns; + + if (arg > INT_MAX) + return -EBADF; + + CLASS(fd, f)((int)arg); + if (fd_empty(f)) + return -EBADF; + if (!proc_ns_file(fd_file(f))) + return -EINVAL; + + ns = get_proc_ns(file_inode(fd_file(f))); + switch (ns->ns_type) { + case CLONE_NEWCGROUP: + case CLONE_NEWIPC: + case CLONE_NEWUTS: + case CLONE_NEWNET: + break; + case CLONE_NEWTIME: + if (!IS_ENABLED(CONFIG_TIME_NS)) + return -EOPNOTSUPP; + break; + default: + return -EINVAL; + } + + if (!ns_capable(c->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + if (!is_current_namespace(ns) && + !ns_capable(ns->ops->owner(ns), CAP_SYS_ADMIN)) + return -EPERM; + + scoped_guard(spinlock, &c->lock) { + if (c->state != USERLAND_INIT) + return -EBUSY; + + __ns_ref_inc(ns); + __ns_ref_active_get(ns); + + switch (ns->ns_type) { + case CLONE_NEWCGROUP: + old_ns = to_ns_common(c->ns->cgroup_ns); + c->ns->cgroup_ns = to_cg_ns(ns); + break; + case CLONE_NEWIPC: + old_ns = to_ns_common(c->ns->ipc_ns); + c->ns->ipc_ns = to_ipc_ns(ns); + break; + case CLONE_NEWUTS: + old_ns = to_ns_common(c->ns->uts_ns); + c->ns->uts_ns = to_uts_ns(ns); + break; + case CLONE_NEWNET: + old_ns = to_ns_common(c->ns->net_ns); + c->ns->net_ns = to_net_ns(ns); + break; +#ifdef CONFIG_TIME_NS + case CLONE_NEWTIME: + __ns_ref_inc(ns); + __ns_ref_active_get(ns); + old_ns = to_ns_common(c->ns->time_ns); + old_time_children = + to_ns_common(c->ns->time_ns_for_children); + c->ns->time_ns = to_time_ns(ns); + c->ns->time_ns_for_children = to_time_ns(ns); + break; +#endif + default: + WARN_ON_ONCE(1); + break; + } + } + + __ns_ref_active_put(old_ns); + old_ns->ops->put(old_ns); + if (old_time_children) { + __ns_ref_active_put(old_time_children); + old_time_children->ops->put(old_time_children); + } + return 0; +} + static long userland_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct userland *userland = file->private_data; @@ -132,6 +220,8 @@ static long userland_ioctl(struct file *file, unsigned int cmd, unsigned long ar switch (cmd) { case USERLAND_GET_NSFD: return userland_get_nsfd(userland, arg); + case USERLAND_SHARE_NSFD: + return userland_share_nsfd(userland, arg); default: return -ENOIOCTLCMD; } -- 2.47.3