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=1796094691; b=siHEdNL7Y5uSRH/pzZ5SNxm2MSEho5+Hm8+EkRx1+E3nnPyb1HaOpK0k6RkF/F20x6LJ Au3skf2u0faJoVKnuUwIZ4ad6w33nAb/wzFdASheCLGSdr6BLDIUI0jOFJzNGS5YnUfmC 7Fo+opsnjxNCrN171mVd0PVx5FX1+HVysD3w4IENGUxpQ3L4ClU5xcuc/v5kRwupq19Xf UQT8DiT6rL+H3c5Fm8L1YjjYwqo3eSWH6pLJdCBSJCKRlaHrMw5UWVzrd+armlrE/mLyA dwVtTDuTBCVv/wjq8hMWNEzFSIDI/5LE2/JOeQQOQfmITa2TuVBDbg7kIBY1ElWz8DQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796094691; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=wPla872AF6CTwh/F7vWcrR1xA9JS0SEB5lFdw5djXTM=; b=NNPzXEaF5TcSF1t5wOYxQhP5y0AiG3+dt21ajGoHeVQ8HvHRe6P6YVCFg6jSAd2ZVfb8 J94jyjk12nFqKo1gBvhgVVk7DQEMkyQ5VyhZ8IPbsEu8UqQry2yVdLvkZ/uAcz8eXqBzr JGDeLbBDLCjZnE7Hp0cJJZuJ4HQC053IkDJtxEFruSYrFe9I/JPFwIDuLOb3putymfYQ2 BjVpdZ2uEAQ/X8NwBF+0m75AjcCLhbV3mw+NUJ2w4+8R12kNRJi/evOPjnFfZX+RtYl5d 2ME3LB/ar6Xx9mLbEkixKHUAIi/h7GeYGRn+drNqCiV4SAI+OUdIMukMYQ1tV6qhMZQ== 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="Q/rXNxRK"; 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 88921606A325; Tue, 1 Dec 2026 03:11:30 +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=Q/rXNxRK Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 42D491F000C1; Tue, 1 Dec 2026 03:11:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796094690; bh=wPla872AF6CTwh/F7vWcrR1xA9JS0SEB5lFdw5djXTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Q/rXNxRKlFoH0zgmpbDRvKLfwzs3Voa8BgxiiILI1Ty2w0UxqpVdcDM8KgMxp0xFN cyXHlLGXR/4EfJmNbvuHzbbwYof1gMQ6HS81d0I1w7drQjBJ6187cmgV9D578WZaf5 suM1ABeOVxRVuhGpdoomixSPusEM0+7oXmn3QVqW9Wn9/HmuWZ0IcnHrgyLQh8yskx BDZqsWL0S3YHlYpYf7svPwT/ZEGUt8fp5QWt3x51v9V+OT+h9ZOJqanqOhmLFfjqsJ bcRnDdVoTe8RHdUc/f0cGNzhY6ZwaJP9TnVtIy3FblKYdyF1WBW1PFUEAkUvOoByAu L3cMH1618CrHw== 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-integrity@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 , Alexei Starovoitov , Andrew Morton , Andrii Nakryiko , Andy Lutomirski , Arnd Bergmann , Baolin Wang , Ben Segall , Christian Brauner , Daniel Borkmann , David Hildenbrand , David Howells , David Woodhouse , Davidlohr Bueso , Dietmar Eggemann , Eduard Zingerman , Emil Tsalapatis , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , =?UTF-8?q?G=C3=BCnther=20Noack?= , Hugh Dickins , Ihor Solodrai , Ingo Molnar , James Morris , Jan Kara , Jason Gunthorpe , Jeff Layton , Jiri Olsa , John Johansen , Jonathan Corbet , Josef Bacik , Juri Lelli , Kees Cook , K Prateek Nayak , KP Singh , Kumar Kartikeya Dwivedi , Lennart Poettering , "Liam R. Howlett" , Manfred Spraul , Martin KaFai Lau , Matt Bobrowski , Mel Gorman , Michal Hocko , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Mike Rapoport , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Peter Zijlstra , Randy Dunlap , "Serge E. Hallyn" , Serge Hallyn , Shuah Khan , Song Liu , Stephen Smalley , Steven Rostedt , Valentin Schneider , Vincent Guittot , Yonghong Song , Zygmunt Krynicki Subject: [RFC PATCH v9 15/29] nsexec: Add NSEXEC_GET_NSFD ioctl Date: Tue, 1 Dec 2026 05:09:47 +0200 Message-ID: <20261201031002.593830-16-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201031002.593830-1-jarkko@kernel.org> References: <20261201031002.593830-1-jarkko@kernel.org> X-Mailing-List: linux-api@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 an nsexec file descriptor before any process has entered it. Add the NSEXEC_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 nsexec'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/nsexec.h | 19 ++++++++++ kernel/nsexec.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/nsexec.h b/include/uapi/linux/nsexec.h index 704eb206de7e..af8e287c229d 100644 --- a/include/uapi/linux/nsexec.h +++ b/include/uapi/linux/nsexec.h @@ -7,6 +7,8 @@ #ifndef _UAPI_LINUX_NSEXEC_H #define _UAPI_LINUX_NSEXEC_H +#include + /** * enum nsexec_flags - Flags for nsexec_open() * @NSEXEC_AUTOKILL: Send %SIGKILL to nsexec init on final fd release. @@ -19,4 +21,21 @@ enum nsexec_flags { NSEXEC_AUTOKILL = 0x00000001, }; +#define NSEXEC_IOCTL_MAGIC 0xB9 + +/** + * DOC: NSEXEC_GET_NSFD + * + * ``ioctl(nsexec_fd, NSEXEC_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 nsexec's user namespace. + * Opening ``CLONE_NEWPID`` additionally requires running in an ancestor + * active PID namespace and that nsexec init is running. + */ +#define NSEXEC_GET_NSFD _IO(NSEXEC_IOCTL_MAGIC, 0x01) + #endif /* _UAPI_LINUX_NSEXEC_H */ diff --git a/kernel/nsexec.c b/kernel/nsexec.c index 3277822386f6..383fcc7112d4 100644 --- a/kernel/nsexec.c +++ b/kernel/nsexec.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include #include @@ -32,6 +34,8 @@ #include #include #include +#include +#include #include #include @@ -64,6 +68,76 @@ static __poll_t nsexec_poll(struct file *file, poll_table *wait) return mask; } +static long nsexec_get_nsfd(struct nsexec *nsexec, unsigned long nstype) +{ + struct ns_common *ns = NULL; + + if (nstype > UINT_MAX) + return -EINVAL; + if (!ns_capable(nsexec->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + + if (nstype == CLONE_NEWPID) { + if (!pidns_is_ancestor(nsexec->ns->pid_ns_for_children, + task_active_pid_ns(current))) + return -EXDEV; + if (READ_ONCE(nsexec->state) != NSEXEC_RUNNING) + return -ESRCH; + } + + scoped_guard(spinlock, &nsexec->lock) { + switch (nstype) { + case CLONE_NEWNS: + ns = from_mnt_ns(nsexec->ns->mnt_ns); + break; + case CLONE_NEWCGROUP: + ns = to_ns_common(nsexec->ns->cgroup_ns); + break; + case CLONE_NEWUTS: + ns = to_ns_common(nsexec->ns->uts_ns); + break; + case CLONE_NEWIPC: + ns = to_ns_common(nsexec->ns->ipc_ns); + break; + case CLONE_NEWUSER: + ns = to_ns_common(nsexec->user_ns); + break; + case CLONE_NEWPID: + ns = to_ns_common(nsexec->ns->pid_ns_for_children); + break; + case CLONE_NEWNET: + ns = to_ns_common(nsexec->ns->net_ns); + break; + case CLONE_NEWTIME: +#ifdef CONFIG_TIME_NS + ns = to_ns_common(nsexec->ns->time_ns); + break; +#else + return -EOPNOTSUPP; +#endif + default: + return -EINVAL; + } + + if (!__ns_ref_get(ns)) + return -ESRCH; + } + + return open_namespace(ns); +} + +static long nsexec_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct nsexec *nsexec = file->private_data; + + switch (cmd) { + case NSEXEC_GET_NSFD: + return nsexec_get_nsfd(nsexec, arg); + default: + return -ENOIOCTLCMD; + } +} + static int nsexec_release(struct inode *inode, struct file *file) { struct nsexec *nsexec = file->private_data; @@ -86,6 +160,8 @@ static int nsexec_release(struct inode *inode, struct file *file) static const struct file_operations nsexec_fops = { .poll = nsexec_poll, + .unlocked_ioctl = nsexec_ioctl, + .compat_ioctl = nsexec_ioctl, .release = nsexec_release, }; -- 2.47.3