From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795910359; b=5sZeRo/MNl2FBD/dc1IGwNrP29dYsH4XMpm4uviL2T0HG8dTfX7Cr7uCJU/Dn/kR2aSY yzff0a10hPaRbuUDQd9pUlFp+IyYbNGoQ+hmvIrGWwwcLVy8pkjBWDZ+nciqUsCBY0LO8 +fkCfPE82yufElJeZ+O7AeY6UhDzkREGshAo9XDeubdQMRLqANXvhSKp+86iEw5iYMKn9 uLjh6ZI/7eR9O6QKw3ZmIOYH1M2XX7j+evw9fXGG/zYW+yQT9HCprHbvtC3nGOqDqDcTA /YNNn8dWHWjajl5ZFNETm8GLCw5e6e4Ttwfb+aTW7Cc5Zf3UBq71WIw/L8zVqdwR3tg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795910359; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=E45sgLkJnX7CjJ8+6VXA6+9n7kEiy6+dPnXjwnle4Yo=; b=1zP4OPH0REORXHaR6n3FrwaQY+ag5sFTrAUx+l1APq0Mi67hzwwvbFt+LiVl4zyJ7ift 1+00DK7lp4LoJAzjW6fQTXcond9QbJtACfyOGNLaRFa+9HOoqvDV00saoIdsbU1rOhLU/ FUoSPI23PI6+f8bjPmUSYQu+2381dFocF1lQ/y3P7s0PZkPI1YNzTVpOsBwpEVVvP7wZR 6S07Hpx/+jTwam6C7oXQZSTou2bdEE1DD/1Y9W1dIjcEpuMpTpyjYwSbAF9MGVsoIhbHA 7CAgPfLw5C7+lHLDhnxtew82Xdca3p/XjO8MDEEnzVotTWpWkZC1rAmDEbBdN/gxXAA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kAbSxrPO; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 36006606A68F; Sat, 28 Nov 2026 23:59:19 +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=kAbSxrPO Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 1A56D1F00A2D; Sat, 28 Nov 2026 23:59:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795910358; bh=E45sgLkJnX7CjJ8+6VXA6+9n7kEiy6+dPnXjwnle4Yo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kAbSxrPO35BchiQ8mgri6etd7GU8hblA6Hbli5TPWmk0NklFouuXPsGAyZ+8r4TRR fl7rEYALaSUomnStxhHAiIJbVDeULfyeERyF8gFfGS0RRJ0h13CsIjuTqLNpBcj4Cj kfMfLLc4bSb5cZFyR+Rm0tFGx2p0a5tiqs/PtTI7m6ekzo1JBPSBkLfZzmdRhMEp48 zSPGi7TksQeI7W3CMsSLpZtaIhGorFP75vFzs1vnXVyg8RL0y2drIJEDn8MH6v3DAE +10NSvT8DxOlDOqgFkMCNxc/zCmNa0syrQeqpeOB0J2tli+jOl7JUq2rnWbBf0dTRI GwnYX48hRi/ug== 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 15/29] userland: Add USERLAND_GET_NSFD ioctl Date: Sun, 29 Nov 2026 01:57:54 +0200 Message-ID: <20261128235808.2369970-16-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-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Standalone namespace operations such as userns_set_idmap() and move_mount(..., MOVE_MOUNT_T_MNTNS) operate on nsfs file descriptors, requiring a way to obtain member namespace fds from a userland file descriptor before any process has entered it. Add the USERLAND_GET_NSFD ioctl to open an O_CLOEXEC nsfs file descriptor for a requested member namespace when the caller holds CAP_SYS_ADMIN in the userland's user namespace, additionally requiring an ancestor active PID namespace and a running init process for CLONE_NEWPID. Signed-off-by: Jarkko Sakkinen --- fs/internal.h | 1 - include/linux/nsfs.h | 1 + include/uapi/linux/userland.h | 19 +++++++++ kernel/userland.c | 76 +++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/fs/internal.h b/fs/internal.h index 3f2fbac40f8e..ee3f796241ac 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -250,7 +250,6 @@ extern void mnt_pin_kill(struct mount *m); * fs/nsfs.c */ extern const struct dentry_operations ns_dentry_operations; -int open_namespace(struct ns_common *ns); struct file *open_namespace_file(struct ns_common *ns); /* diff --git a/include/linux/nsfs.h b/include/linux/nsfs.h index 731b67fc2fec..40912c1eb5ff 100644 --- a/include/linux/nsfs.h +++ b/include/linux/nsfs.h @@ -23,6 +23,7 @@ bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino); int ns_get_name(char *buf, size_t size, struct task_struct *task, const struct proc_ns_operations *ns_ops); void nsfs_init(void); +int open_namespace(struct ns_common *ns); #define __current_namespace_from_type(__ns) \ _Generic((__ns), \ diff --git a/include/uapi/linux/userland.h b/include/uapi/linux/userland.h index ecc9cc3fb539..16b540fe80c9 100644 --- a/include/uapi/linux/userland.h +++ b/include/uapi/linux/userland.h @@ -7,6 +7,8 @@ #ifndef _UAPI_LINUX_USERLAND_H #define _UAPI_LINUX_USERLAND_H +#include + /** * enum userland_flags - Flags for userland_open() * @USERLAND_KILL_ON_CLOSE: Send %SIGKILL to userland init on final fd @@ -21,4 +23,21 @@ enum userland_flags { USERLAND_CLOEXEC = 0x00000002, }; +#define USERLAND_IOCTL_MAGIC 0xB9 + +/** + * DOC: USERLAND_GET_NSFD + * + * ``ioctl(userland_fd, USERLAND_GET_NSFD, nstype)`` opens an ``nsfs`` file + * descriptor (with ``O_CLOEXEC``) for the member namespace identified by + * ``nstype`` (one of ``CLONE_NEWNS``, ``CLONE_NEWCGROUP``, + * ``CLONE_NEWUTS``, ``CLONE_NEWIPC``, ``CLONE_NEWUSER``, ``CLONE_NEWPID``, + * ``CLONE_NEWNET``, or ``CLONE_NEWTIME``). + * + * Requires ``CAP_SYS_ADMIN`` in the userland's user namespace. + * Opening ``CLONE_NEWPID`` additionally requires running in an ancestor + * active PID namespace and that userland init is running. + */ +#define USERLAND_GET_NSFD _IO(USERLAND_IOCTL_MAGIC, 0x01) + #endif /* _UAPI_LINUX_USERLAND_H */ diff --git a/kernel/userland.c b/kernel/userland.c index 22575f60e682..8e4b527fa0ea 100644 --- a/kernel/userland.c +++ b/kernel/userland.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -31,6 +33,8 @@ #include #include #include +#include +#include #include #include @@ -63,6 +67,76 @@ static __poll_t userland_poll(struct file *file, poll_table *wait) return mask; } +static long userland_get_nsfd(struct userland *userland, unsigned long nstype) +{ + struct ns_common *ns = NULL; + + if (nstype > UINT_MAX) + return -EINVAL; + if (!ns_capable(userland->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + + if (nstype == CLONE_NEWPID) { + if (!pidns_is_ancestor(userland->ns->pid_ns_for_children, + task_active_pid_ns(current))) + return -EXDEV; + if (READ_ONCE(userland->state) != USERLAND_RUNNING) + return -ESRCH; + } + + scoped_guard(spinlock, &userland->lock) { + switch (nstype) { + case CLONE_NEWNS: + ns = from_mnt_ns(userland->ns->mnt_ns); + break; + case CLONE_NEWCGROUP: + ns = to_ns_common(userland->ns->cgroup_ns); + break; + case CLONE_NEWUTS: + ns = to_ns_common(userland->ns->uts_ns); + break; + case CLONE_NEWIPC: + ns = to_ns_common(userland->ns->ipc_ns); + break; + case CLONE_NEWUSER: + ns = to_ns_common(userland->user_ns); + break; + case CLONE_NEWPID: + ns = to_ns_common(userland->ns->pid_ns_for_children); + break; + case CLONE_NEWNET: + ns = to_ns_common(userland->ns->net_ns); + break; + case CLONE_NEWTIME: +#ifdef CONFIG_TIME_NS + ns = to_ns_common(userland->ns->time_ns); + break; +#else + return -EOPNOTSUPP; +#endif + default: + return -EINVAL; + } + + if (!__ns_ref_get(ns)) + return -ESRCH; + } + + return open_namespace(ns); +} + +static long userland_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct userland *userland = file->private_data; + + switch (cmd) { + case USERLAND_GET_NSFD: + return userland_get_nsfd(userland, arg); + default: + return -ENOIOCTLCMD; + } +} + static int userland_release(struct inode *inode, struct file *file) { struct userland *userland = file->private_data; @@ -85,6 +159,8 @@ static int userland_release(struct inode *inode, struct file *file) static const struct file_operations userland_fops = { .poll = userland_poll, + .unlocked_ioctl = userland_ioctl, + .compat_ioctl = userland_ioctl, .release = userland_release, }; -- 2.47.3