Re: [RFC PATCH v3 08/13] fs: Support FSCONFIG_SET_ROOTNS in fsconfig()

From
sashiko-bot@kernel.org
Date
2026-11-19 19:37:03
Also in
linux-mm
Reply to
Jarkko Sakkinen, 2026-11-18
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] fs: unprivileged devpts mount via fsconfig_set_rootns()
  bypasses pty_reserve
- [High] fs: alloc_mnt_ns(fc->user_ns) in fsmount() breaks submount
  MNT_LOCKED
- [Medium] fs: missing FS_CONTEXT_FAILED transition on set_rootns()
  error
- [Medium] fs: fsconfig_set_rootns() loses cpuset context state on
  re-init
- [Medium] fs: may_mount() in fsmount() bypasses fc->user_ns capability
  check
- [Low] fs: missing @init kernel-doc entry in alloc_fs_context()
--

commit fada980b534067d9740aa79ea2751dbe127d6be7
Author: Jarkko Sakkinen [off-list ref]

fs: Support FSCONFIG_SET_ROOTNS in fsconfig()

This commit adds FSCONFIG_SET_ROOTNS to bind a newly opened fs_context to
a target rootns before supplying parameters, allowing unprivileged callers
to open FS_USERNS_MOUNT contexts and configure mounts for a rootns where
they hold CAP_SYS_ADMIN.
quoted hunk ↗ jump to hunk
diff --git a/fs/fs_context.c b/fs/fs_context.c
index a50584df97bea..542dede2f8643 100644
--- a/fs/fs_context.c
+++ b/fs/fs_context.c
@@ -259,7 +259,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)
 {
[Severity: Low]
The kernel-doc comment above alloc_fs_context() is missing an @init entry
for the new parameter.

[ ... ]
quoted hunk ↗ jump to hunk
diff --git a/fs/fsopen.c b/fs/fsopen.c
index 70024870fefa2..3e36270baadb3 100644
--- a/fs/fsopen.c
+++ b/fs/fsopen.c
@@ -41,6 +46,76 @@ static inline const char *fetch_message_locked(struct fc_log *log, size_t len,
 	return p;
 }
 
+/**
+ * DOC: FSCONFIG_SET_ROOTNS
+ *
+ * ``fsconfig(fsfd, FSCONFIG_SET_ROOTNS, NULL, NULL, rootnsfd)`` binds a newly
+ * opened filesystem context to ``rootnsfd`` once before any parameters are
+ * supplied (subsequent calls fail with ``-EBUSY``).
+ *
+ * The caller must hold ``CAP_SYS_ADMIN`` in the target ``rootns`` user
+ * namespace, allowing unprivileged callers to configure ``FS_USERNS_MOUNT``
+ * filesystems without entering the user namespace first.
+ *
+ * The context is initialized against the ``rootns`` network namespace,
+ * ``fc->user_ns`` is delegated to the ``rootns`` user namespace for
+ * ``FS_USERNS_MOUNT`` and ``FS_USERNS_DELEGATABLE`` filesystems when the
+ * mounter ``fsuid``/``fsgid`` map into it or the caller lacks
+ * ``CAP_SYS_ADMIN`` in its own user namespace, and ``proc`` and ``mqueue``
+ * contexts bind to the target ``rootns`` PID and IPC namespaces.
+ */
+static int fsconfig_set_rootns(struct fs_context *fc, struct fs_parameter *param)
+{
+	struct user_namespace *user_ns;
+	struct rootns *c;
+	int ret;
+
+	if (!is_rootns_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->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);
+
+	put_net(fc->net_ns);
+	fc->net_ns = get_net(c->ns->net_ns);
+	put_user_ns(fc->user_ns);
+	if ((fc->fs_type->fs_flags & (FS_USERNS_MOUNT | FS_USERNS_DELEGATABLE)) &&
+	    ((kuid_has_mapping(user_ns, fc->cred->fsuid) &&
+	      kgid_has_mapping(user_ns, fc->cred->fsgid)) ||
+	     !ns_capable(fc->cred->user_ns, CAP_SYS_ADMIN)))
+		fc->user_ns = get_user_ns(user_ns);
+	else
+		fc->user_ns = get_user_ns(fc->cred->user_ns);
+
+	ret = fc->fs_type->init_fs_context(fc);
[Severity: Medium]
Does re-calling fc->fs_type->init_fs_context(fc) here lose the cpuset
configuration when fsopen("cpuset", 0) was opened by a caller with
may_mount() == true?

During the initial fs_context_for_mount() in fsopen(),
cpuset_init_fs_context() configures cpuset_fs_context_ops,
ctx->subsys_mask, and CGRP_ROOT_NOPREFIX, and then replaces fc->fs_type
with &cgroup_fs_type:

kernel/cgroup/cgroup.c:cpuset_init_fs_context() {
    ...
	get_filesystem(&cgroup_fs_type);
	put_filesystem(fc->fs_type);
	fc->fs_type = &cgroup_fs_type;
    ...
}

When fsconfig_set_rootns() frees the initial context and calls
fc->fs_type->init_fs_context(fc) again, it invokes
cgroup_init_fs_context() instead of cpuset_init_fs_context(), leaving
ctx->subsys_mask as 0 and fc->ops as &cgroup1_fs_context_ops so cpuset
parameter parsing or superblock creation fails.
quoted hunk ↗ jump to hunk
+	if (ret < 0) {
+		fc->phase = FS_CONTEXT_FAILED;
+		return ret;
+	}
+	fc->need_free = true;
+
+	if (fc->ops->set_rootns) {
+		ret = fc->ops->set_rootns(fc, c);
+		if (ret < 0)
+			return ret;
+	}
[Severity: Medium]
Should fc->phase be set to FS_CONTEXT_FAILED when fc->ops->set_rootns()
fails?

By the time set_rootns() runs, the prior filesystem-private state has been
freed, fc->net_ns and fc->user_ns have been overwritten, and
init_fs_context() has re-run in the fsconfig() caller's task context.  If
proc_set_rootns() returns -EINVAL, leaving fc->phase in
FS_CONTEXT_CREATE_NS (or FS_CONTEXT_AWAITING_NS) with fc->rootns_bound
still false leaves a partially mutated context usable for subsequent
parameter parsing, superblock creation, or repeated FSCONFIG_SET_ROOTNS
calls.

[ ... ]
quoted hunk ↗ jump to hunk
@@ -137,17 +214,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_USERNS_MOUNT)) {
+		put_filesystem(fs_type);
+		return -EPERM;
+	}
+
+	fc = priv ? fs_context_for_mount(fs_type, 0) :
+		    fs_context_for_unpriv_mount(fs_type);
[Severity: High]
Does allowing an unprivileged caller in the initial mount namespace to
open and configure a devpts context via FSCONFIG_SET_ROOTNS bypass
pty_reserve?

devpts has FS_USERNS_MOUNT set, and devpts_init_fs_context() grants access
to the reserved PTY pool solely based on whether current is in the initial
mount namespace:

fs/devpts/inode.c:devpts_init_fs_context() {
    ...
	if (fc->purpose == FS_CONTEXT_FOR_MOUNT &&
	    current->nsproxy->mnt_ns == init_task.nsproxy->mnt_ns)
		fsi->mount_opts.reserve = true;
    ...
}

Previously, fsopen() required may_mount(), forcing unprivileged callers to
unshare a mount namespace first.  With this regression, an unprivileged
user in init_task.nsproxy->mnt_ns can open devpts, bind it to a userns via
FSCONFIG_SET_ROOTNS (which runs devpts_init_fs_context() while still in
the initial mount namespace, setting fsi->mount_opts.reserve = true), and
allocate PTYs from the host emergency reserve up to pty_limit.

[ ... ]
quoted hunk ↗ jump to hunk
diff --git a/fs/namespace.c b/fs/namespace.c
index 781380945d784..a6c0a31e275ae 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4658,6 +4659,16 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
 	if (ret)
 		return ret;
 
+	if (!(flags & FSMOUNT_NAMESPACE)) {
+		if (fc->rootns_bound) {
+			if (!ns_capable(fc->user_ns, CAP_SYS_ADMIN) &&
+			    !may_mount())
+				return -EPERM;
+		} else if (!may_mount()) {
+			return -EPERM;
+		}
+	}
[Severity: Medium]
When fc->rootns_bound is true, can a caller lacking CAP_SYS_ADMIN in
fc->user_ns bypass the fc->user_ns capability check by satisfying
may_mount() in its own unprivileged mount namespace (or by passing
FSMOUNT_NAMESPACE)?

Because alloc_mnt_ns() below unconditionally uses fc->user_ns whenever
fc->rootns_bound is true, a task that passes this check via may_mount() in
an unrelated user namespace still allocates the anonymous mount namespace
in fc->user_ns and charges UCOUNT_MNT_NAMESPACES against fc->user_ns.
quoted hunk ↗ jump to hunk
+
 	/* There must be a valid superblock or we can't mount it */
 	ret = -EINVAL;
 	if (!fc->root)
@@ -4699,7 +4710,8 @@ 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->rootns_bound ? fc->user_ns :
+			  current->nsproxy->mnt_ns->user_ns, true);
[Severity: High]
Does allocating fsmount()'s anonymous mount namespace in fc->user_ns leave
host submounts without MNT_LOCKED when an external manager in init_user_ns
assembles a mount tree for an unprivileged rootns?

When a manager in init_user_ns creates a rootns-bound anonymous root mount
(so dest_mnt->mnt_ns is anonymous with user_ns == fc->user_ns) and attaches
a host submount from init_user_ns onto it via move_mount(),
attach_recursive_mnt() compares child->mnt_parent->mnt_ns->user_ns
(fc->user_ns) against current->nsproxy->mnt_ns->user_ns (&init_user_ns):

fs/namespace.c:attach_recursive_mnt() {
    ...
	if (is_anon_ns(ns))
		user_ns = current->nsproxy->mnt_ns->user_ns;
	else
		user_ns = ns->user_ns;
    ...
		/* Notice when we are propagating across user namespaces */
		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
			lock_mnt_tree(child);
    ...
}

lock_mnt_tree(child) only sets MNT_LOCKED on descendants p != child,
leaving child itself unlocked, and commit_tree(child) then moves child into
the anonymous mount namespace owned by fc->user_ns.  When the assembled
anonymous tree is later moved into the target mount namespace (also owned
by fc->user_ns), the user namespaces match, so attach_recursive_mnt() does
not lock the tree and unprivileged users in the target namespace can
unmount the host submount to expose the underlying directory.  Conversely,
attaching a submount that was also created with FSCONFIG_SET_ROOTNS for
fc->user_ns onto the anonymous root mount sees
child->mnt_parent->mnt_ns->user_ns != user_ns and spuriously runs
lock_mnt_tree().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118193539.240065-1-jarkko@kernel.org?part=8
Thread (33 messages) 33 messages, 3 authors, last reply 1d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help