Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: fsdevel@vger.kernel.org
Cc: "Jarkko Sakkinen" <jarkko@kernel.org>,
	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" <aleksa@amutable.com>,
	"Alexander Mikhalitsyn" <alex@amutable.com>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Ben Segall" <bsegall@google.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"David Hildenbrand" <david@kernel.org>,
	"David Howells" <dhowells@redhat.com>,
	"David Woodhouse" <dwmw2@infradead.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"Georgia Garcia" <georgia.garcia@canonical.com>,
	"Giuseppe Scrivano" <giuseppe@scrivano.org>,
	"Günther Noack" <gnoack@google.com>,
	"Hugh Dickins" <hughd@google.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Ingo Molnar" <mingo@redhat.com>,
	"James Morris" <jmorris@namei.org>, "Jan Kara" <jack@suse.cz>,
	"Jason Gunthorpe" <jgg@nvidia.com>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"John Johansen" <john.johansen@canonical.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Josef Bacik" <josef@toxicpanda.com>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	"Kees Cook" <kees@kernel.org>,
	"K Prateek Nayak" <kprateek.nayak@amd.com>,
	"KP Singh" <kpsingh@kernel.org>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Lennart Poettering" <lennart@poettering.net>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Manfred Spraul" <manfred@colorfullife.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Matt Bobrowski" <matt@bobrowski.net>,
	"Mel Gorman" <mgorman@suse.de>, "Michal Hocko" <mhocko@suse.com>,
	"Mickaël Salaün" <mic@digikod.net>,
	"Mike Rapoport" <rppt@kernel.org>, "Neal Gompa" <neal@gompa.dev>,
	"Oleg Nesterov" <oleg@redhat.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"Pavel Tikhomirov" <ptikhomirov@virtuozzo.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Serge Hallyn" <sergeh@kernel.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Song Liu" <song@kernel.org>,
	"Stephen Smalley" <stephen.smalley.work@gmail.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Valentin Schneider" <vschneid@redhat.com>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Zygmunt Krynicki" <zygmunt.krynicki@canonical.com>
Subject: [RFC PATCH v9 20/29] fs: Support FSCONFIG_SET_NSEXEC in fsconfig()
Date: Tue,  1 Dec 2026 05:09:52 +0200	[thread overview]
Message-ID: <20261201031002.593830-21-jarkko@kernel.org> (raw)
In-Reply-To: <20261201031002.593830-1-jarkko@kernel.org>

fsopen() and fsmount() gate on CAP_SYS_ADMIN in the caller's current
mount namespace and initialize filesystem contexts from current's user
and network namespaces, preventing an unprivileged external manager from
creating superblocks and detached mounts for an unentered nsexec.

Add FSCONFIG_SET_NSEXEC and the ->set_nsexec() fs_context operation
so callers holding CAP_SYS_ADMIN in the target nsexec's user namespace
can bind a newly opened fs_context to that nsexec once before offering
parameters. Gate nsexec context binding on explicit FS_NSEXEC_MOUNT
opt-in (or FS_USERNS_DELEGATABLE), mark tmpfs, ramfs, and devpts with
FS_NSEXEC_MOUNT, refresh fc->cred when FSCONFIG_SET_NSEXEC
authorizes and initializes the context, and keep devpts pty_reserve
restricted to host mounts.

Co-developed-by: David Howells <dhowells@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 Documentation/filesystems/mount_api.rst |  35 ++++++-
 fs/devpts/inode.c                       |   8 +-
 fs/filesystems.c                        |  15 +++
 fs/fs_context.c                         |  21 +++-
 fs/fsopen.c                             | 131 ++++++++++++++++++++++--
 fs/internal.h                           |   6 ++
 fs/namespace.c                          |  18 +++-
 fs/ramfs/inode.c                        |   2 +-
 include/linux/fs.h                      |   1 +
 include/linux/fs_context.h              |   6 ++
 include/uapi/linux/mount.h              |   1 +
 mm/shmem.c                              |   5 +-
 12 files changed, 230 insertions(+), 19 deletions(-)

diff --git a/Documentation/filesystems/mount_api.rst b/Documentation/filesystems/mount_api.rst
index e8b94357b4df..a4e9cfd13955 100644
--- a/Documentation/filesystems/mount_api.rst
+++ b/Documentation/filesystems/mount_api.rst
@@ -80,6 +80,7 @@ context.  This is represented by the fs_context structure::
 		unsigned int		sb_flags_mask;
 		unsigned int		s_iflags;
 		enum fs_context_purpose	purpose:8;
+		bool			nsexec_bound:1;
 		...
 	};
 
@@ -123,7 +124,8 @@ The fs_context fields are as follows:
      There are a subset of the namespaces in use by the invoking process.  They
      retain references on each namespace.  The subscribed namespaces may be
      replaced by the filesystem to reflect other sources, such as the parent
-     mount superblock on an automount.
+     mount superblock on an automount, or to the target nsexec's
+     namespaces when bound via FSCONFIG_SET_NSEXEC.
 
    * ::
 
@@ -186,6 +188,15 @@ The fs_context fields are as follows:
 	FS_CONTEXT_FOR_RECONFIGURE	Change an existing mount
 	==========================	======================================
 
+   * ::
+
+       bool nsexec_bound:1
+
+     Set when the filesystem context has been bound to a target nsexec via
+     FSCONFIG_SET_NSEXEC.  Filesystems may inspect this flag (for example in
+     ->init_fs_context() or ->parse_param()) to distinguish nsexec-delegated
+     mounts from host mounts.
+
 The mount context is created by calling vfs_new_fs_context() or
 vfs_dup_fs_context() and is destroyed with put_fs_context().  Note that the
 structure is not refcounted.
@@ -214,6 +225,8 @@ The filesystem context points to a table of operations::
 		int (*parse_param)(struct fs_context *fc,
 				   struct fs_parameter *param);
 		int (*parse_monolithic)(struct fs_context *fc, void *data);
+		int (*set_nsexec)(struct fs_context *fc,
+				   struct nsexec *nsexec);
 		int (*get_tree)(struct fs_context *fc);
 		int (*reconfigure)(struct fs_context *fc);
 	};
@@ -272,6 +285,26 @@ manage the filesystem context.  They are as follows:
      finds it's the standard key-val list then it may pass it off to
      generic_parse_monolithic().
 
+   * ::
+
+	int (*set_nsexec)(struct fs_context *fc,
+			   struct nsexec *nsexec);
+
+     Called by FSCONFIG_SET_NSEXEC after updating fc->user_ns, fc->net_ns,
+     and fc->cred, setting fc->nsexec_bound, and re-running
+     ->init_fs_context().  Filesystems opting into FS_NSEXEC_MOUNT (or
+     FS_USERNS_DELEGATABLE) that capture additional namespaces from current in
+     ->init_fs_context() (such as PID or IPC namespaces) can implement this
+     operation to rebind their private context to the target nsexec's
+     namespaces.  It may update fc->user_ns (for example to a shared
+     namespace's owning user namespace), in which case the caller must hold
+     CAP_SYS_ADMIN in fc->user_ns or the bind fails with -EPERM.  When
+     ->set_nsexec is NULL, fc->user_ns must equal nsexec->user_ns after
+     ->init_fs_context() or the bind fails with -EOPNOTSUPP.
+
+     On success it should return 0.  In the case of an error, it should return
+     a negative error code and the context transitions to FS_CONTEXT_FAILED.
+
    * ::
 
 	int (*get_tree)(struct fs_context *fc);
diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c
index bd1e8eb26edb..aa3c454ee2bf 100644
--- a/fs/devpts/inode.c
+++ b/fs/devpts/inode.c
@@ -231,10 +231,14 @@ static int devpts_parse_param(struct fs_context *fc, struct fs_parameter *param)
 
 	switch (opt) {
 	case Opt_uid:
+		if (!kuid_has_mapping(fc->user_ns, result.uid))
+			return invalf(fc, "Invalid uid");
 		opts->uid = result.uid;
 		opts->setuid = 1;
 		break;
 	case Opt_gid:
+		if (!kgid_has_mapping(fc->user_ns, result.gid))
+			return invalf(fc, "Invalid gid");
 		opts->gid = result.gid;
 		opts->setgid = 1;
 		break;
@@ -434,6 +438,8 @@ static int devpts_init_fs_context(struct fs_context *fc)
 	fsi->mount_opts.max     = NR_UNIX98_PTY_MAX;
 
 	if (fc->purpose == FS_CONTEXT_FOR_MOUNT &&
+	    !fc->nsexec_bound &&
+	    fc->user_ns == &init_user_ns &&
 	    current->nsproxy->mnt_ns == init_task.nsproxy->mnt_ns)
 		fsi->mount_opts.reserve = true;
 
@@ -457,7 +463,7 @@ static struct file_system_type devpts_fs_type = {
 	.init_fs_context = devpts_init_fs_context,
 	.parameters	= devpts_param_specs,
 	.kill_sb	= devpts_kill_sb,
-	.fs_flags	= FS_USERNS_MOUNT,
+	.fs_flags	= FS_USERNS_MOUNT | FS_NSEXEC_MOUNT,
 };
 
 /*
diff --git a/fs/filesystems.c b/fs/filesystems.c
index 3083d904df9b..9429844bf6bf 100644
--- a/fs/filesystems.c
+++ b/fs/filesystems.c
@@ -18,6 +18,7 @@
 #include <linux/uaccess.h>
 #include <linux/fs_parser.h>
 #include <linux/rculist.h>
+#include "internal.h"
 
 /*
  * Read-mostly filesystem drivers list.
@@ -390,6 +391,20 @@ static struct file_system_type *__get_fs_type(const char *name, int len)
 	return fs;
 }
 
+struct file_system_type *get_fs_type_noload(const char *name)
+{
+	struct file_system_type *fs;
+	const char *dot = strchr(name, '.');
+	int len = dot ? dot - name : strlen(name);
+
+	fs = __get_fs_type(name, len);
+	if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
+		put_filesystem(fs);
+		fs = NULL;
+	}
+	return fs;
+}
+
 struct file_system_type *get_fs_type(const char *name)
 {
 	struct file_system_type *fs;
diff --git a/fs/fs_context.c b/fs/fs_context.c
index a50584df97be..cd004750b99c 100644
--- a/fs/fs_context.c
+++ b/fs/fs_context.c
@@ -249,6 +249,7 @@ EXPORT_SYMBOL(generic_parse_monolithic);
  * @sb_flags: Filesystem/superblock flags (SB_*)
  * @sb_flags_mask: Applicable members of @sb_flags
  * @purpose: The purpose that this configuration shall be used for.
+ * @init: Whether to initialise the filesystem context immediately.
  *
  * Open a filesystem and create a mount context.  The mount context is
  * initialised with the supplied flags and, if a submount/automount from
@@ -259,7 +260,8 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
 				      struct dentry *reference,
 				      unsigned int sb_flags,
 				      unsigned int sb_flags_mask,
-				      enum fs_context_purpose purpose)
+				      enum fs_context_purpose purpose,
+				      bool init)
 {
 	struct fs_context *fc;
 	int ret = -ENOMEM;
@@ -292,6 +294,9 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
 		break;
 	}
 
+	if (!init)
+		return fc;
+
 	ret = fc->fs_type->init_fs_context(fc);
 	if (ret < 0)
 		goto err_fc;
@@ -307,10 +312,16 @@ struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
 					unsigned int sb_flags)
 {
 	return alloc_fs_context(fs_type, NULL, sb_flags, 0,
-					FS_CONTEXT_FOR_MOUNT);
+				FS_CONTEXT_FOR_MOUNT, true);
 }
 EXPORT_SYMBOL(fs_context_for_mount);
 
+struct fs_context *fs_context_for_unpriv_mount(struct file_system_type *fs_type)
+{
+	return alloc_fs_context(fs_type, NULL, 0, 0,
+				FS_CONTEXT_FOR_MOUNT, false);
+}
+
 struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 					unsigned int sb_flags,
 					unsigned int sb_flags_mask)
@@ -320,7 +331,8 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 		return ERR_PTR(-EINVAL);
 
 	return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
-				sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
+				sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE,
+				true);
 }
 
 /**
@@ -337,7 +349,8 @@ struct fs_context *fs_context_for_submount(struct file_system_type *type,
 	struct fs_context *fc;
 	int ret;
 
-	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT,
+			      true);
 	if (IS_ERR(fc))
 		return fc;
 
diff --git a/fs/fsopen.c b/fs/fsopen.c
index 70024870fefa..04c3f16079ae 100644
--- a/fs/fsopen.c
+++ b/fs/fsopen.c
@@ -13,9 +13,14 @@
 #include <linux/security.h>
 #include <linux/anon_inodes.h>
 #include <linux/namei.h>
+#include <linux/capability.h>
+#include <linux/nsexec.h>
+#include <linux/nsproxy.h>
+#include <linux/user_namespace.h>
 #include <linux/file.h>
 #include <uapi/linux/mount.h>
 #include <linux/audit.h>
+#include <net/net_namespace.h>
 #include "internal.h"
 #include "mount.h"
 
@@ -41,6 +46,84 @@ static inline const char *fetch_message_locked(struct fc_log *log, size_t len,
 	return p;
 }
 
+/**
+ * DOC: FSCONFIG_SET_NSEXEC
+ *
+ * ``fsconfig(fsfd, FSCONFIG_SET_NSEXEC, NULL, NULL, nsexec_fd)`` binds a newly
+ * opened ``FS_NSEXEC_MOUNT`` or ``FS_USERNS_DELEGATABLE`` filesystem context
+ * to ``nsexec_fd`` once before parameters are supplied. Requires
+ * ``CAP_SYS_ADMIN`` in the nsexec's user namespace and binds the context to the
+ * nsexec's user, network, PID (``proc``), and IPC (``mqueue``) namespaces.
+ */
+static int fsconfig_set_nsexec(struct fs_context *fc, struct fs_parameter *param)
+{
+	struct user_namespace *user_ns;
+	struct net *net;
+	struct nsexec *c;
+	int ret;
+
+	if (!nsexec_is_file(param->file))
+		return -EINVAL;
+
+	c = param->file->private_data;
+	user_ns = c->user_ns;
+	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
+		return -EPERM;
+	if (!(fc->fs_type->fs_flags &
+	      (FS_NSEXEC_MOUNT | FS_USERNS_DELEGATABLE)))
+		return -EOPNOTSUPP;
+
+	if (fc->need_free && fc->ops && fc->ops->free)
+		fc->ops->free(fc);
+	fc->need_free = false;
+	fc->fs_private = NULL;
+	fc->s_fs_info = NULL;
+	fc->ops = NULL;
+	fc->sb_flags = 0;
+	fc->global = false;
+	security_free_mnt_opts(&fc->security);
+
+	scoped_guard(spinlock, &c->lock)
+		net = get_net(c->ns->net_ns);
+	put_cred(fc->cred);
+	fc->cred = get_current_cred();
+	put_net(fc->net_ns);
+	fc->net_ns = net;
+	put_user_ns(fc->user_ns);
+	fc->user_ns = get_user_ns(user_ns);
+
+	fc->nsexec_bound = true;
+	fc->nsexec_mnt_id = c->ns->mnt_ns->ns.ns_id;
+
+	ret = fc->fs_type->init_fs_context(fc);
+	if (ret < 0) {
+		fc->nsexec_bound = false;
+		fc->phase = FS_CONTEXT_FAILED;
+		return ret;
+	}
+	fc->need_free = true;
+
+	if (fc->ops->set_nsexec) {
+		ret = fc->ops->set_nsexec(fc, c);
+		if (ret < 0) {
+			fc->nsexec_bound = false;
+			fc->phase = FS_CONTEXT_FAILED;
+			return ret;
+		}
+		if (!ns_capable(fc->user_ns, CAP_SYS_ADMIN)) {
+			fc->nsexec_bound = false;
+			fc->phase = FS_CONTEXT_FAILED;
+			return -EPERM;
+		}
+	} else if (fc->user_ns != user_ns) {
+		fc->nsexec_bound = false;
+		fc->phase = FS_CONTEXT_FAILED;
+		return -EOPNOTSUPP;
+	}
+	fc->phase = FS_CONTEXT_CREATE_PARAMS;
+	return 0;
+}
+
 /*
  * Allow the user to read back any error, warning or informational messages.
  * Only one message is returned for each read(2) call.
@@ -123,9 +206,11 @@ SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
 	struct file_system_type *fs_type;
 	struct fs_context *fc;
 	const char *fs_name;
+	bool priv;
 	int ret;
 
-	if (!may_mount())
+	priv = may_mount();
+	if (!priv && !IS_ENABLED(CONFIG_NSEXEC))
 		return -EPERM;
 
 	if (flags & ~FSOPEN_CLOEXEC)
@@ -137,17 +222,23 @@ SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
 
 	audit_log_fsopen(fs_name);
 
-	fs_type = get_fs_type(fs_name);
+	fs_type = priv ? get_fs_type(fs_name) : get_fs_type_noload(fs_name);
 	kfree(fs_name);
 	if (!fs_type)
-		return -ENODEV;
+		return priv ? -ENODEV : -EPERM;
 
-	fc = fs_context_for_mount(fs_type, 0);
+	if (!priv && !(fs_type->fs_flags & FS_NSEXEC_MOUNT)) {
+		put_filesystem(fs_type);
+		return -EPERM;
+	}
+
+	fc = priv ? fs_context_for_mount(fs_type, 0) :
+		    fs_context_for_unpriv_mount(fs_type);
 	put_filesystem(fs_type);
 	if (IS_ERR(fc))
 		return PTR_ERR(fc);
 
-	fc->phase = FS_CONTEXT_CREATE_PARAMS;
+	fc->phase = priv ? FS_CONTEXT_CREATE_NS : FS_CONTEXT_AWAITING_NS;
 
 	ret = fscontext_alloc_log(fc);
 	if (ret < 0)
@@ -222,7 +313,8 @@ static int vfs_cmd_create(struct fs_context *fc, bool exclusive)
 	struct super_block *sb;
 	int ret;
 
-	if (fc->phase != FS_CONTEXT_CREATE_PARAMS)
+	if (fc->phase != FS_CONTEXT_CREATE_NS &&
+	    fc->phase != FS_CONTEXT_CREATE_PARAMS)
 		return -EBUSY;
 
 	if (!mount_capable(fc))
@@ -291,6 +383,8 @@ static int vfs_fsconfig_locked(struct fs_context *fc, int cmd,
 	ret = finish_clean_context(fc);
 	if (ret)
 		return ret;
+	if (fc->phase == FS_CONTEXT_AWAITING_NS && cmd != FSCONFIG_SET_NSEXEC)
+		return -EPERM;
 	switch (cmd) {
 	case FSCONFIG_CMD_CREATE:
 		return vfs_cmd_create(fc, false);
@@ -298,9 +392,17 @@ static int vfs_fsconfig_locked(struct fs_context *fc, int cmd,
 		return vfs_cmd_create(fc, true);
 	case FSCONFIG_CMD_RECONFIGURE:
 		return vfs_cmd_reconfigure(fc);
+	case FSCONFIG_SET_NSEXEC:
+		if (fc->phase != FS_CONTEXT_AWAITING_NS &&
+		    fc->phase != FS_CONTEXT_CREATE_NS)
+			return -EBUSY;
+		return fsconfig_set_nsexec(fc, param);
+
 	default:
-		if (fc->phase != FS_CONTEXT_CREATE_PARAMS &&
-		    fc->phase != FS_CONTEXT_RECONF_PARAMS)
+		if (fc->phase == FS_CONTEXT_CREATE_NS)
+			fc->phase = FS_CONTEXT_CREATE_PARAMS;
+		else if (fc->phase != FS_CONTEXT_CREATE_PARAMS &&
+			 fc->phase != FS_CONTEXT_RECONF_PARAMS)
 			return -EBUSY;
 
 		return vfs_parse_fs_param(fc, param);
@@ -390,6 +492,12 @@ SYSCALL_DEFINE5(fsconfig,
 		if (!_key || _value || aux < 0)
 			return -EINVAL;
 		break;
+	case FSCONFIG_SET_NSEXEC:
+		if (!IS_ENABLED(CONFIG_NSEXEC))
+			return -EOPNOTSUPP;
+		if (_key || _value || aux < 0)
+			return -EINVAL;
+		break;
 	case FSCONFIG_CMD_CREATE:
 	case FSCONFIG_CMD_CREATE_EXCL:
 	case FSCONFIG_CMD_RECONFIGURE:
@@ -457,6 +565,12 @@ SYSCALL_DEFINE5(fsconfig,
 			goto out_key;
 		param.dirfd = aux;
 		break;
+	case FSCONFIG_SET_NSEXEC:
+		ret = -EBADF;
+		param.file = fget(aux);
+		if (!param.file)
+			goto out_key;
+		break;
 	default:
 		break;
 	}
@@ -482,6 +596,7 @@ SYSCALL_DEFINE5(fsconfig,
 			putname(param.name);
 		break;
 	case FSCONFIG_SET_FD:
+	case FSCONFIG_SET_NSEXEC:
 		if (param.file)
 			fput(param.file);
 		break;
diff --git a/fs/internal.h b/fs/internal.h
index ee3f796241ac..c7b57263ac83 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -40,12 +40,18 @@ int __block_write_begin_int(struct folio *folio, loff_t pos, unsigned len,
  */
 extern void __init chrdev_init(void);
 
+/*
+ * filesystems.c
+ */
+struct file_system_type *get_fs_type_noload(const char *name);
+
 /*
  * fs_context.c
  */
 extern int parse_monolithic_mount_data(struct fs_context *, void *);
 extern void vfs_clean_context(struct fs_context *fc);
 extern int finish_clean_context(struct fs_context *fc);
+struct fs_context *fs_context_for_unpriv_mount(struct file_system_type *fs_type);
 
 /*
  * namei.c
diff --git a/fs/namespace.c b/fs/namespace.c
index 535a22f59e73..020585171e41 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4723,7 +4723,8 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (!(flags & FSMOUNT_NAMESPACE) && !may_mount())
+	if (!(flags & FSMOUNT_NAMESPACE) && !IS_ENABLED(CONFIG_NSEXEC) &&
+	    !may_mount())
 		return -EPERM;
 
 	if (attr_flags & ~FSMOUNT_VALID_FLAGS)
@@ -4758,6 +4759,16 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
 	if (ret)
 		return ret;
 
+	if (fc->nsexec_bound) {
+		if (flags & FSMOUNT_NAMESPACE)
+			return -EINVAL;
+		if (!ns_capable(fc->user_ns, CAP_SYS_ADMIN))
+			return -EPERM;
+	} else if (!(flags & FSMOUNT_NAMESPACE) &&
+		   IS_ENABLED(CONFIG_NSEXEC) && !may_mount()) {
+		return -EPERM;
+	}
+
 	/* There must be a valid superblock or we can't mount it */
 	ret = -EINVAL;
 	if (!fc->root)
@@ -4799,9 +4810,12 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
 		return FD_ADD((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
 			      open_new_namespace(&new_path, MOUNT_COPY_NEW));
 
-	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
+	ns = alloc_mnt_ns(fc->nsexec_bound ? fc->user_ns :
+			  current->nsproxy->mnt_ns->user_ns, true);
 	if (IS_ERR(ns))
 		return PTR_ERR(ns);
+	if (fc->nsexec_bound)
+		ns->seq_origin = fc->nsexec_mnt_id;
 	mnt = real_mount(new_path.mnt);
 	ns->root = mnt;
 	ns->nr_mounts = 1;
diff --git a/fs/ramfs/inode.c b/fs/ramfs/inode.c
index ef91b933d4e6..f46df1d5b852 100644
--- a/fs/ramfs/inode.c
+++ b/fs/ramfs/inode.c
@@ -319,7 +319,7 @@ static struct file_system_type ramfs_fs_type = {
 	.init_fs_context = ramfs_init_fs_context,
 	.parameters	= ramfs_fs_parameters,
 	.kill_sb	= ramfs_kill_sb,
-	.fs_flags	= FS_USERNS_MOUNT,
+	.fs_flags	= FS_USERNS_MOUNT | FS_NSEXEC_MOUNT,
 };
 
 static int __init init_ramfs_fs(void)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5b3a100aba77..7069cc13a848 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2306,6 +2306,7 @@ struct file_system_type {
 #define FS_USERNS_MOUNT_RESTRICTED 512	/* Restrict mount in userns if not already visible */
 #define FS_USERNS_DELEGATABLE	1024	/* Can be mounted inside userns from outside */
 #define FS_DISALLOW_NOTIFY	2048	/* No fsnotify marks on its objects */
+#define FS_NSEXEC_MOUNT	4096	/* Supports FSCONFIG_SET_NSEXEC */
 #define FS_RENAME_DOES_D_MOVE	32768	/* FS will handle d_move() during rename() internally. */
 	int (*init_fs_context)(struct fs_context *);
 	const struct fs_parameter_spec *parameters;
diff --git a/include/linux/fs_context.h b/include/linux/fs_context.h
index d1727dffecc5..69be2527da41 100644
--- a/include/linux/fs_context.h
+++ b/include/linux/fs_context.h
@@ -23,6 +23,7 @@ struct net;
 struct pid_namespace;
 struct super_block;
 struct user_namespace;
+struct nsexec;
 struct vfsmount;
 struct path;
 
@@ -36,6 +37,8 @@ enum fs_context_purpose {
  * Userspace usage phase for fsopen/fspick.
  */
 enum fs_context_phase {
+	FS_CONTEXT_AWAITING_NS,		/* Unprivileged context awaiting target ns */
+	FS_CONTEXT_CREATE_NS,		/* Set namespaces for sb creation */
 	FS_CONTEXT_CREATE_PARAMS,	/* Loading params for sb creation */
 	FS_CONTEXT_CREATING,		/* A superblock is being created */
 	FS_CONTEXT_AWAITING_MOUNT,	/* Superblock created, awaiting fsmount() */
@@ -101,6 +104,7 @@ struct fs_context {
 	const char		*source;	/* The source name (eg. dev path) */
 	void			*security;	/* LSM options */
 	void			*s_fs_info;	/* Proposed s_fs_info */
+	u64			nsexec_mnt_id;	/* Target nsexec mnt_ns ID */
 	unsigned int		sb_flags;	/* Proposed superblock flags (SB_*) */
 	unsigned int		sb_flags_mask;	/* Superblock flags that were changed */
 	unsigned int		s_iflags;	/* OR'd with sb->s_iflags */
@@ -110,6 +114,7 @@ struct fs_context {
 	bool			global:1;	/* Goes into &init_user_ns */
 	bool			oldapi:1;	/* Coming from mount(2) */
 	bool			exclusive:1;    /* create new superblock, reject existing one */
+	bool			nsexec_bound:1;	/* Bound via FSCONFIG_SET_NSEXEC */
 };
 
 struct fs_context_operations {
@@ -117,6 +122,7 @@ struct fs_context_operations {
 	int (*dup)(struct fs_context *fc, struct fs_context *src_fc);
 	int (*parse_param)(struct fs_context *fc, struct fs_parameter *param);
 	int (*parse_monolithic)(struct fs_context *fc, void *data);
+	int (*set_nsexec)(struct fs_context *fc, struct nsexec *nsexec);
 	int (*get_tree)(struct fs_context *fc);
 	int (*reconfigure)(struct fs_context *fc);
 };
diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h
index cb4f7175db02..9308b382477b 100644
--- a/include/uapi/linux/mount.h
+++ b/include/uapi/linux/mount.h
@@ -107,6 +107,7 @@ enum fsconfig_command {
 	FSCONFIG_CMD_CREATE	= 6,	/* Create new or reuse existing superblock */
 	FSCONFIG_CMD_RECONFIGURE = 7,	/* Invoke superblock reconfiguration */
 	FSCONFIG_CMD_CREATE_EXCL = 8,	/* Create new superblock, fail if reusing existing superblock */
+	FSCONFIG_SET_NSEXEC	= 9,	/* Set target nsexec, supplied by fd */
 };
 
 /*
diff --git a/mm/shmem.c b/mm/shmem.c
index a5713109c843..06ac4434a8d8 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -5632,7 +5632,8 @@ static struct file_system_type shmem_fs_type = {
 	.parameters	= shmem_fs_parameters,
 #endif
 	.kill_sb	= kill_anon_super,
-	.fs_flags	= FS_USERNS_MOUNT | FS_ALLOW_IDMAP | FS_MGTIME,
+	.fs_flags	= FS_USERNS_MOUNT | FS_NSEXEC_MOUNT |
+			  FS_ALLOW_IDMAP | FS_MGTIME,
 };
 
 #if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS)
@@ -6061,7 +6062,7 @@ static struct file_system_type shmem_fs_type = {
 	.init_fs_context = ramfs_init_fs_context,
 	.parameters	= ramfs_fs_parameters,
 	.kill_sb	= ramfs_kill_sb,
-	.fs_flags	= FS_USERNS_MOUNT,
+	.fs_flags	= FS_USERNS_MOUNT | FS_NSEXEC_MOUNT,
 };
 
 void __init shmem_init(void)
-- 
2.47.3


  parent reply	other threads:[~2026-12-01  3:11 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  3:09 [RFC PATCH v9 00/29] nsexec: Staged namespace execution Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 01/29] userns: Factor out idmap validation and commit helpers Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 02/29] userns: Add userns_set_idmap() system call Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 03/29] arch: Wire up " Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 04/29] mount: Add mnt_ns_get_root() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 05/29] mount: Enforce mount_too_revealing() for foreign mntns Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 06/29] mount: Support MOVE_MOUNT_T_MNTNS in move_mount() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 07/29] security: Pass flags to security_move_mount() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 08/29] mount: Support MOVE_MOUNT_CLONE with MOVE_MOUNT_T_MNTNS Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 09/29] apparmor: Mediate MOVE_MOUNT_CLONE as recursive bind Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 10/29] userns: Factor out install_user_ns_cred() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 11/29] nsproxy: Expose create_new_namespaces() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 12/29] nsexec: Add core infrastructure Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 13/29] nsexec: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 14/29] nsexec: Add nsexec_open() system call Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 15/29] nsexec: Add NSEXEC_GET_NSFD ioctl Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 16/29] nsexec: Add NSEXEC_SHARE_NSFD ioctl Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 17/29] exit: Support P_NSEXEC in waitid() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 18/29] signal: Support nsexec fds in pidfd_send_signal() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 19/29] fork: Support CLONE_INTO_NSEXEC in clone3() Jarkko Sakkinen
2026-12-01  3:09 ` Jarkko Sakkinen [this message]
2026-12-01  3:09 ` [RFC PATCH v9 21/29] proc: Implement ->set_nsexec() fs_context operation Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 22/29] ipc/mqueue: " Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 23/29] security: Add nsexec LSM hooks Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 24/29] landlock: Enforce namespace use restrictions on nsexec Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 25/29] bpf: Register nsexec LSM hooks Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 26/29] arch: Wire up nsexec_open() system call Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 27/29] docs: Document nsexec uAPI Jarkko Sakkinen
2026-12-01  3:10 ` [RFC PATCH v9 28/29] selftests/nsexec: Add nsexec test suite Jarkko Sakkinen
2026-12-01  3:10 ` [RFC PATCH v9 29/29] MAINTAINERS: Add entry for NSEXEC Jarkko Sakkinen
2026-12-01  4:02 ` [RFC PATCH v9 00/29] nsexec: Staged namespace execution Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201031002.593830-21-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=aleksa@amutable.com \
    --cc=alex@amutable.com \
    --cc=andrii@kernel.org \
    --cc=arnd@arndb.de \
    --cc=ast@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=containers@lists.linux.dev \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=dave@stgolabs.net \
    --cc=david@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=dwmw2@infradead.org \
    --cc=ebiggers@kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=fsdevel@vger.kernel.org \
    --cc=georgia.garcia@canonical.com \
    --cc=giuseppe@scrivano.org \
    --cc=gnoack@google.com \
    --cc=hughd@google.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jgg@nvidia.com \
    --cc=jlayton@kernel.org \
    --cc=jmorris@namei.org \
    --cc=john.johansen@canonical.com \
    --cc=jolsa@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=kprateek.nayak@amd.com \
    --cc=kpsingh@kernel.org \
    --cc=lennart@poettering.net \
    --cc=liam@infradead.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=manfred@colorfullife.com \
    --cc=martin.lau@linux.dev \
    --cc=matt@bobrowski.net \
    --cc=memxor@gmail.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=mic@digikod.net \
    --cc=mingo@redhat.com \
    --cc=neal@gompa.dev \
    --cc=oleg@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=peterz@infradead.org \
    --cc=ptikhomirov@virtuozzo.com \
    --cc=rdunlap@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=serge@hallyn.com \
    --cc=sergeh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=song@kernel.org \
    --cc=stephen.smalley.work@gmail.com \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vschneid@redhat.com \
    --cc=yonghong.song@linux.dev \
    --cc=zygmunt.krynicki@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox