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 1F134606A335 for ; Tue, 1 Dec 2026 03:12:07 +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 kanga.kvack.org (Postfix) id B0DEA6B05CD; Mon, 30 Nov 2026 22:11:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ABE5B6B05CF; Mon, 30 Nov 2026 22:11:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 910B16B05D0; Mon, 30 Nov 2026 22:11:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 34CE36B05CD for ; Mon, 30 Nov 2026 22:11:34 -0500 (EST) Received: from smtpin16.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9A4301401D7 for ; Tue, 1 Dec 2026 03:11:32 +0000 (UTC) X-FDA: 85493938824.16.950E922 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf19.hostedemail.com (Postfix) with ESMTP id F19811A000B for ; Tue, 1 Dec 2026 03:11:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="Q/rXNxRK"; spf=pass (imf19.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=1796094691; b=CpGM4e8FLn7/ycunyvsTMMgrePNfkHcgwA7VOXl1nPOXw64Nq6hd+QTGwtIdPs+xKVlWF+ uzrRYYJL2l0FQGDJ/DN74g//q+NodG81v2FM5boEUiV9X39OrIdwJFVM6zqoEct8qdx37f YefKJCZWAOaHxul1iF+opCbQLnDxhn8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796094691; 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=wPla872AF6CTwh/F7vWcrR1xA9JS0SEB5lFdw5djXTM=; b=xXyUaG4qK/jPZ6UhT/mMW+nNh7efttmm5wCTxZdt0KyEw6JCDcL2JcaLYkYNvR0bdSSzNI YIy+5zigFWe2g/LNlrE7w0GbNG3TbhSxDZ8o9OTLG9AKIgMbqk5swbED+qcxwwRP/wHgjP ila1gNLdzU41JRal1hbvFELmlTHT1Hg= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="Q/rXNxRK"; spf=pass (imf19.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 43C9A43EF8; Tue, 1 Dec 2026 03:11:30 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: F19811A000B 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-Stat-Signature: peupafmtjh17xbbi89zhc8tf7oj1occo X-HE-Tag: 1796094690-483054 X-HE-Meta: U2FsdGVkX1+grChMQdII6k0HOXoAXLqrjHy5tjzVswJSL4NmwRw1j/gyipAC4d7DmAXbiA2wfTuAgvNqiUeK4QSpWslPNs8tc/ru1ef3AoiFuAFYYz8pnzVPd12AAbU784tnw1+cpH3w62WZEUvMjRJ12r/W/D+8bGobaVrOSkRRH7LNd0JMgpZIoJBpsjnaHy09Bh9e+5xxfNHE4UtgmPBeQcKfYmc2IRtUzbR+viLt+YrvjkKNmsdr0lP22M0Ep0s+n/LwqcO33CtD/391fCxx1Hz8WQ+HHAhnmrBBizBcSyNa9owx0QjDrdER4avPjz1otHgA4/ebV23pn7U9XZQbkOdnK7d/FhRriEOYUa/BZjUhH1xAlpLppB48+WWxa/uZSwVVm3chwM0ynZ8USVVtYYqEOcLazNciXF2g3hP+2/GSepWp/0qlV1iYIIf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lxi2BKJal8g/pCOR4tc1swk= 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 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