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

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Nov 18 05:09:51 UTC 2026


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

commit c40a472d45a1ca48a5976af47a79bdbf753d63b0
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 filesystem context can be bound to a target rootns file descriptor
before parameters are parsed.

> diff --git a/fs/fsopen.c b/fs/fsopen.c
> index 70024870fefa2..98d407fd249bc 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;
> +
> +	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 fc->fs_type->init_fs_context()
has already run during fsopen() cause a use-after-free on ctx->net in kAFS?

alloc_fs_context() invokes init_fs_context() during fsopen(), where
afs_init_fs_context() caches an unreferenced net_generic pointer from the
initial fc->net_ns:

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), an unprivileged caller
in a user and network namespace can let that initial network namespace exit so
cleanup_net() frees struct net and its pernet struct afs_net, and then call
fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "#cell:vol", 0) to trigger a
use-after-free in afs_parse_source() -> afs_lookup_cell(ctx->net, ...).

In addition, nfsd_init_fs_context() and rpc_init_fs_context() set fc->user_ns
from fc->net_ns->user_ns during fsopen(), so switching fc->net_ns afterwards
leaves fc->user_ns bound to the fsopen() caller's user namespace and bypasses
the sget_fc() non-initial user_ns check.  nfs_init_fs_context() also evaluates
fc->net_ns != &init_net during fsopen() when setting NFS_MOUNT_NETUNREACH_FATAL,
which becomes stale once fc->net_ns changes.

> +	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);
> +	}
> +	if (fc->ops->set_rootns)
> +		fc->ops->set_rootns(fc, c);
> +	fc->phase = FS_CONTEXT_CREATE_PARAMS;
> +	return 0;
> +}

[Severity: High]
Does gating the fc->user_ns update on kuid_has_mapping() and
kgid_has_mapping() silently skip user namespace delegation for an unentered
ROOTNS_USER rootns?

When rootns_create() creates a rootns with ROOTNS_USER, create_user_ns()
initializes user_ns->uid_map and user_ns->gid_map with zero extents, and
sys_rootns_enter() rejects entering the rootns with -ENOENT while its state is
ROOTNS_EMPTY before "/" is mounted.  Because no ID mappings can exist yet in
that unentered user namespace (or when a host manager's fsuid/fsgid lies
outside the container's subordinate ID range), kuid_has_mapping() and
kgid_has_mapping() return false and fsconfig_set_rootns() returns 0 without
updating fc->user_ns, causing privileged managers to create init_user_ns-owned
superblocks and unprivileged callers to fail mount_capable(fc) with -EPERM.

Meanwhile, mqueue_set_rootns() unconditionally sets fc->user_ns to
ctx->ipc_ns->user_ns even when current_fsuid() and current_fsgid() are
unmapped there, leaving the root inode created by mqueue_fill_super() ->
mqueue_get_inode() with unmapped i_uid and i_gid.  Conversely, when ID
mappings do exist and fc->user_ns is switched to a target user_ns that differs
from current_user_ns(), FS_USERNS_MOUNT filesystems such as overlayfs
(ovl_fill_super() and ovl_parse_param()) and binfmt_misc (bm_fill_super())
explicitly check fc->user_ns != current_user_ns() and fail with -EINVAL.

[ ... ]
> 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
[ ... ]
> +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]
Does proc_set_rootns() allow a task in a child or sibling PID namespace to
mount procfs for a non-descendant PID namespace by omitting a
pidns_is_ancestor() check?

proc_parse_pidns_param(), pidns_install(), and sys_rootns_enter() all require
pidns_is_ancestor(target, task_active_pid_ns(current)) so a process confined
to a sub-PID namespace cannot access or mount procfs for an ancestor or
sibling PID namespace.  Because fsconfig_set_rootns() only checks
ns_capable(user_ns, CAP_SYS_ADMIN) and proc_set_rootns() unconditionally binds
ctx->pid_ns to rootns->ns->pid_ns_for_children, a process in a child PID
namespace within the same user namespace that receives a rootnsfd can mount
procfs for that outer or sibling PID namespace.

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



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