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=1796441264; b=Aa08dg36+8DoRRg+aGRW+Tnu/beAQDQO5PfweIbUrWI7SSlsvqRbjuCsDv9GMxSuyE+A /ilhDpFA16zb2LPXDiH4qVEulL8seHggStXuVU1JJRuPG+2v2OvPM/G+2T5k2ppXBavN5 WHrdc5/ahgh4Y2W8kpSo07bu3Nz6Nz+35CvFqyH4pJVRiEsbeUCWoLWuEoDzVAMECACBT 65M0rwlOelBshoW/A6KGq5qs5O98jiNMeCLKWegq5ZveYRUNEy5cHJ3VPIyElzHYwQoru ae5QeTBjvP2KvmCbwRkwd2IcdycyNNl05gZ+M3UJ5UzNQ295kPuHXmZ7QuOLvGpDgYg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796441264; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=VWt39zEwKeJxYiKGWW/HTDRbJweYW8nnOi04VcBydo4=; b=fXu0Iz4WQCMaOlo7SEQ7mORASP+VmzeMFmJNjeTAyiK/n1M5EpMtn6Jsc1Fs9llRb4sK Nm+Kgrdt/Y+ax2j93srAZTEauSqRcVqHF3MyPMtLG+t8qbyzLTAVjSrQPUYwVnrQA8//N FKFmqsbHEkpavsQIvJ/ogi7k9l0G29ngo1lHFaGPtKGK+nyIpRozs6dAs2QsPekT1nlGM wvCgkuwVyD2t2BLti39wP4Dg/DHw05nG5lPQEPaCvEuj/+ts1FTurWxd0BJ4MFNKwfMSW Tu6VAn82DqdonkSZLHcOTWyLTIQ/6BAYXL5ltNy9xm7a13zlYiJRRiREfie/uh9Dxbg== 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=WShve7JH; 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 5962160A2610; Sat, 5 Dec 2026 03:27:44 +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=WShve7JH Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 7F34A1F00A13; Sat, 5 Dec 2026 03:27:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796441264; bh=VWt39zEwKeJxYiKGWW/HTDRbJweYW8nnOi04VcBydo4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WShve7JH34rOeM7ETl32ykkqt/2BV51ta6zWV9OOQUB44f0s/A8ccmB7qdaomdaQg 3vuHJy5C31lDORvWiCdX/tee/CnfQMlvukn0GtcInuNTuWCcy6pwFNP9Zlb/YKWicB xz9PSpLAI9xwRqTM2SMt8pXDL55qm6NzBno7bCN3T0TZr7Tx3PKu8YrpRNvu/i7744 L7V0D7UubM/kcO1lCoGBBHmw42n6vSTQ7EPS1xFOzBlCMD21S+by4BNvP+8vJ3Pd7T SyVofXzq2BgkJjB0cpl76iGmPYlooN4W60QwrQqFayFgraoc5ZC7xPQPoBVLb2PNy1 /U711/Vcr4RWg== 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 v10 14/28] runfd: Add RUNFD_GET_NSFD ioctl Date: Sat, 5 Dec 2026 05:26:19 +0200 Message-ID: <20261205032633.1702772-15-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205032633.1702772-1-jarkko@kernel.org> References: <20261205032633.1702772-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 NS_SET_IDMAP and move_mount(..., MOVE_MOUNT_T_MNTNS) operate on nsfs file descriptors, requiring a way to obtain member namespace fds from a run file descriptor before any process has entered it. Add the RUNFD_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 run'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/runfd.h | 19 ++++++++++ kernel/runfd.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/runfd.h b/include/uapi/linux/runfd.h index f46a757b1159..7c4ecc5e4f1c 100644 --- a/include/uapi/linux/runfd.h +++ b/include/uapi/linux/runfd.h @@ -7,6 +7,8 @@ #ifndef _UAPI_LINUX_RUNFD_H #define _UAPI_LINUX_RUNFD_H +#include + /** * enum runfd_flags - Flags for runfd() * @RUNFD_AUTOKILL: Send %SIGKILL to run init on final fd release. @@ -19,4 +21,21 @@ enum runfd_flags { RUNFD_AUTOKILL = 0x00000001, }; +#define RUNFD_IOCTL_MAGIC 0xB9 + +/** + * DOC: RUNFD_GET_NSFD + * + * ``ioctl(run_fd, RUNFD_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 run's user namespace. + * Opening ``CLONE_NEWPID`` additionally requires running in an ancestor + * active PID namespace and that run init is running. + */ +#define RUNFD_GET_NSFD _IO(RUNFD_IOCTL_MAGIC, 0x01) + #endif /* _UAPI_LINUX_RUNFD_H */ diff --git a/kernel/runfd.c b/kernel/runfd.c index 6b250796c24e..b0dd7666f5d7 100644 --- a/kernel/runfd.c +++ b/kernel/runfd.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include #include @@ -33,6 +35,8 @@ #include #include #include +#include +#include #include #include @@ -65,6 +69,76 @@ static __poll_t runfd_poll(struct file *file, poll_table *wait) return mask; } +static long runfd_get_nsfd(struct run *run, unsigned long nstype) +{ + struct ns_common *ns = NULL; + + if (nstype > UINT_MAX) + return -EINVAL; + if (!ns_capable(run->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + + if (nstype == CLONE_NEWPID) { + if (!pidns_is_ancestor(run->ns->pid_ns_for_children, + task_active_pid_ns(current))) + return -EXDEV; + if (READ_ONCE(run->state) != RUNFD_RUNNING) + return -ESRCH; + } + + scoped_guard(spinlock, &run->lock) { + switch (nstype) { + case CLONE_NEWNS: + ns = from_mnt_ns(run->ns->mnt_ns); + break; + case CLONE_NEWCGROUP: + ns = to_ns_common(run->ns->cgroup_ns); + break; + case CLONE_NEWUTS: + ns = to_ns_common(run->ns->uts_ns); + break; + case CLONE_NEWIPC: + ns = to_ns_common(run->ns->ipc_ns); + break; + case CLONE_NEWUSER: + ns = to_ns_common(run->user_ns); + break; + case CLONE_NEWPID: + ns = to_ns_common(run->ns->pid_ns_for_children); + break; + case CLONE_NEWNET: + ns = to_ns_common(run->ns->net_ns); + break; + case CLONE_NEWTIME: +#ifdef CONFIG_TIME_NS + ns = to_ns_common(run->ns->time_ns); + break; +#else + return -EOPNOTSUPP; +#endif + default: + return -EINVAL; + } + + if (!__ns_ref_get(ns)) + return -ESRCH; + } + + return open_namespace(ns); +} + +static long runfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct run *run = file->private_data; + + switch (cmd) { + case RUNFD_GET_NSFD: + return runfd_get_nsfd(run, arg); + default: + return -ENOIOCTLCMD; + } +} + static int runfd_release(struct inode *inode, struct file *file) { struct run *run = file->private_data; @@ -87,6 +161,8 @@ static int runfd_release(struct inode *inode, struct file *file) static const struct file_operations runfd_fops = { .poll = runfd_poll, + .unlocked_ioctl = runfd_ioctl, + .compat_ioctl = runfd_ioctl, .release = runfd_release, }; -- 2.47.3