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

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Nov 18 23:15:19 UTC 2026


Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Critical] fs: fsconfig_set_rootns() replaces fc->net_ns after
  init_fs_context()
- [High] fs: proc: missing pidns_is_ancestor() check in
  proc_set_rootns()
- [High] fs: broken fc->user_ns delegation in fsconfig_set_rootns()
- [Medium] fs: missing net_ns and ipc_ns capability checks in
  fsconfig_set_rootns()
--

commit 5cbef69c784337269ad86c164ca92e045e18c429
Author: Jarkko Sakkinen <jarkko at kernel.org>

fs: Support FSCONFIG_SET_ROOTNS in fsconfig()

This commit adds the FSCONFIG_SET_ROOTNS command to fsconfig() so a newly
opened fs_context can be bound to a target root namespace before parsing
parameters.

> diff --git a/fs/fsopen.c b/fs/fsopen.c
> index 70024870fefa2..ca3234c8c4e47 100644
> --- a/fs/fsopen.c
> +++ b/fs/fsopen.c
[ ... ]
> +static int fsconfig_set_rootns(struct fs_context *fc, struct fs_parameter *param)
> +{
> +	struct user_namespace *user_ns;
> +	struct rootns *c;
> +
> +	if (!is_rootns_file(param->file))
> +		return -EINVAL;
> +
> +	c = param->file->private_data;
> +	user_ns = c->cred->user_ns;
> +	if (!ns_capable(user_ns, CAP_SYS_ADMIN))
> +		return -EPERM;

[Severity: Medium]
Does checking CAP_SYS_ADMIN only against c->cred->user_ns allow rebinding
fc->net_ns or mqueue's ctx->ipc_ns to an ancestor namespace where the
caller lacks privileges?

In create_rootns(), when ROOTNS_USER is passed without ROOTNS_NET or
ROOTNS_IPC, c->ns->net_ns and c->ns->ipc_ns are inherited from
current->nsproxy and remain owned by an ancestor user namespace.  A caller
in an isolated network or IPC namespace that holds CAP_SYS_ADMIN in
c->cred->user_ns can then use a rootns fd to switch fc->net_ns or
ctx->ipc_ns back to that ancestor namespace without holding CAP_SYS_ADMIN
in c->ns->net_ns->user_ns or c->ns->ipc_ns->user_ns.

> +
> +	put_net(fc->net_ns);
> +	fc->net_ns = get_net(c->ns->net_ns);

[Severity: Critical]
Can replacing and dropping fc->net_ns here after init_fs_context() has
already run cause a use-after-free in filesystems that cache per-net state
during fsopen()?

During fsopen(), alloc_fs_context() calls fc->fs_type->init_fs_context(fc)
with the caller's current fc->net_ns.  In kAFS, afs_init_fs_context()
stores an unreferenced pointer into that netns:

fs/afs/super.c:afs_init_fs_context() {
	...
	ctx->net = afs_net(fc->net_ns);

	/* Default to the workstation cell. */
	cell = afs_find_cell(ctx->net, NULL, 0, afs_cell_trace_use_fc);
	if (IS_ERR(cell))
		cell = NULL;
	ctx->cell = cell;
	...
}

When fsconfig_set_rootns() calls put_net(fc->net_ns), the initial netns
can be freed once the caller exits it, leaving ctx->net dangling when a
subsequent fsconfig() call parses "source" via afs_parse_source() ->
afs_lookup_cell(ctx->net, ...).  If ctx->cell was non-NULL, it instead
holds a cell reference on the old netns and hangs afs_cell_purge() in
wait_var_event(&net->cells_outstanding) on netns exit.

Other filesystems also derive state from fc->net_ns during
init_fs_context() that becomes inconsistent after fc->net_ns changes:
nfsd_init_fs_context() and rpc_init_fs_context() set fc->user_ns from the
initial fc->net_ns->user_ns (and nfsd_fill_super() writes
nn->nfsd_client_dir into current->nsproxy->net_ns while keying the
superblock to fc->net_ns), while nfs_init_fs_context() sets
NFS_MOUNT_NETUNREACH_FATAL from fc->net_ns != &init_net.

> +	if (!fc->global &&
> +	    (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)) {
> +		put_user_ns(fc->user_ns);
> +		fc->user_ns = get_user_ns(user_ns);
> +	}

[Severity: High]
Does delegating fc->user_ns this way work for unentered ROOTNS_USER root
namespaces or for FS_USERNS_MOUNT filesystems?

For an unentered ROOTNS_USER rootns, create_user_ns() leaves uid_map and
gid_map empty until a process exists in the namespace, and
rootns_prepare_fork() rejects fork with -ENOENT while the rootns is in
ROOTNS_EMPTY before "/" is mounted.  Because kuid_has_mapping() and
kgid_has_mapping() are false, fsconfig_set_rootns() silently leaves
fc->user_ns unchanged while returning 0, so a privileged manager creates
an init_user_ns superblock (without SB_I_NODEV) and an unprivileged caller
later fails mount_capable(fc) with -EPERM.  Meanwhile, mqueue_set_rootns()
unconditionally overwrites fc->user_ns with ctx->ipc_ns->user_ns, so
mqueue_get_inode() assigns unmapped current_fsuid()/current_fsgid() values
to the root inode.

Conversely, when uid_map and gid_map are populated and fc->user_ns is
switched to a target userns different from current_user_ns(),
FS_USERNS_MOUNT filesystems that require fc->user_ns == current_user_ns()
(such as ovl_fill_super(), ovl_parse_param(), and bm_fill_super()) reject
the mount with -EINVAL:

fs/overlayfs/super.c:ovl_fill_super() {
	...
	/* The fscontext fd may have been passed to another user namespace. */
	if (fc->user_ns != current_user_ns())
		goto out_err;
	...
}

> +	if (fc->ops->set_rootns)
> +		fc->ops->set_rootns(fc, c);
> +	fc->phase = FS_CONTEXT_CREATE_PARAMS;
> +	return 0;
> +}
[ ... ]
> diff --git a/fs/proc/root.c b/fs/proc/root.c
> index 7fbbe92bf73a7..5976029ad3f80 100644
> --- a/fs/proc/root.c
> +++ b/fs/proc/root.c
[ ... ]
> @@ -323,6 +325,16 @@ static int proc_get_tree(struct fs_context *fc)
>  	return get_tree_nodev(fc, proc_fill_super);
>  }
>  
> +static void proc_set_rootns(struct fs_context *fc, struct rootns *rootns)
> +{
> +	struct proc_fs_context *ctx = fc->fs_private;
> +
> +	put_pid_ns(ctx->pid_ns);
> +	ctx->pid_ns = get_pid_ns(rootns->ns->pid_ns_for_children);
> +	put_user_ns(fc->user_ns);
> +	fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
> +}

[Severity: High]
Should proc_set_rootns() verify that rootns->ns->pid_ns_for_children is a
descendant of task_active_pid_ns(current)?

Both proc_parse_pidns_param() and rootns_prepare_fork() enforce
pidns_is_ancestor(target, task_active_pid_ns(current)).  Without that
check here (and a return value from ->set_rootns()), a process confined to
a child or sibling PID namespace within the same user namespace can use a
rootns fd to mount procfs for an ancestor or sibling PID namespace outside
its active PID namespace hierarchy.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117231352.3816603-1-jarkko@kernel.org?part=8



More information about the Linux-security-module-archive mailing list