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 DFA7D606A68B for ; Sat, 5 Dec 2026 03:29:03 +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 kanga.kvack.org (Postfix) id A96006B05CE; Fri, 4 Dec 2026 22:27:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A489B6B05CF; Fri, 4 Dec 2026 22:27:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C0526B05D0; Fri, 4 Dec 2026 22:27:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 565A16B05CE for ; Fri, 4 Dec 2026 22:27:47 -0500 (EST) Received: from smtpin02.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C2136A0282 for ; Sat, 5 Dec 2026 03:27:46 +0000 (UTC) X-FDA: 85508494932.02.B3D2059 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf31.hostedemail.com (Postfix) with ESMTP id 12C6220002 for ; Sat, 5 Dec 2026 03:27:44 +0000 (UTC) Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=WShve7JH; spf=pass (imf31.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796441265; b=M+t6e5QITIhjfeL09shGxo0xDt1HW1r962tIae2ZcPmskd91bIHDF4IiTssyUaVc+iFz/+ fo0j8jnjpyCS28/IIcAMCXD+MKTgRaSVes8gTxdFFOMkQgu4Mar424czaQAii0sZMYj4nb GENWPlBxxLkkl9Hc64KoFjh4YDxH+BE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796441265; 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=VWt39zEwKeJxYiKGWW/HTDRbJweYW8nnOi04VcBydo4=; b=rACuhCeKuK7HbtFs0HuHxmL03N5y+pBUZAG/ejaNaXrh8X4ktB+inH913+fodbBRz4Co8D md9cgCHdcVgw5fQpVhftiZyvw7iFIon3VGVbS2LjqOm+H29UOF2lmHtaE/i5tz6x3FSRRP e/BDIAZKdXeaDORW6s38UwwKgeeQ19I= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=WShve7JH; spf=pass (imf31.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with UTF8SMTP id 5200A437E2; Sat, 5 Dec 2026 03:27:44 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: 4ymqr873y8s4jw7ab99gkr79cptfqk67 X-Rspam-User: X-Spam-Status: No, score=-19.1 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-Queue-Id: 12C6220002 X-Rspamd-Server: rspam01 X-HE-Tag: 1796441264-705072 X-HE-Meta: U2FsdGVkX1/uVK3tFLOiSeTBKwpUxtyp0ua3AHe5/rEf/+L4X8us+MXrfWw3ME65l735Cm3bc/dnwtwNdN+460fQ4zDBJ7cDvbSUZcmpCawltW4byPAWmqx3UIZbjy1hAJoGnBMsP21EFdxz07CUxM/EH5ABddyDhEbNPuHrF+nPJ8o+6oycMBdlpxFW7LGvgLyvJXLvpVN4WUYpyRNncmC9+0VJIofan9OJoFbowwapuYK0VE9gYnnxU/egBjVWmnm86t5Pncf0CTSHhCh6Y27hd2OFDUnlCZZXh6ttNxhIX+tbpSWmFIQ3jcR2Z/CoD+L6jbi2vTS8/UJ8PjI7O3DcboXRFXO1N132D4/bG2he/g2WpwwnsK/sfC9Xcwx0WIpMhpxk32d5KYIDDeqBQSvmaNNMkdXUBthy7jJ/A+HGnwUJLdc5o9Xg2RuP6wMTg+/Ko2DxRf/5/0HO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: 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 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