[RFC PATCH v4 08/14] fs: Support FSCONFIG_SET_ROOTNS in fsconfig()
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Fri Nov 20 22:44:43 UTC 2026
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] fs: user_ns delegation and init_fs_context() mismatch in
fsconfig_set_rootns()
- [High] fs: condition anon mnt_ns user_ns and seq_origin on
rootns_bound in fsmount()
- [Medium] proc: allow pidns option to overwrite rootns binding in
proc_parse_param()
--
commit 08909a29107ff970d2d4b5a71eaa424398baf6f2
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 callers
holding CAP_SYS_ADMIN in a target rootns user namespace can bind a newly
opened fs_context to that rootns before configuring parameters and mounting.
> diff --git a/fs/fsopen.c b/fs/fsopen.c
> index 70024870fefa2..85b31af68a4d6 100644
> --- a/fs/fsopen.c
> +++ b/fs/fsopen.c
[ ... ]
> +static int fsconfig_set_rootns(struct fs_context *fc, struct fs_parameter *param)
> +{
[ ... ]
> + 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);
> +
> + fc->rootns_bound = true;
> + fc->rootns_mnt_id = c->ns->mnt_ns->ns.ns_id;
> +
> + ret = fc->fs_type->init_fs_context(fc);
[Severity: High]
Can fsconfig_set_rootns() leave fc->user_ns pointing to a user namespace
other than c->user_ns while still marking fc->rootns_bound = true and
fc->rootns_mnt_id = c->ns->mnt_ns->ns.ns_id?
First, fsopen() sets priv from may_mount() (CAP_SYS_ADMIN in
current->nsproxy->mnt_ns->user_ns), whereas the fallback here tests
!ns_capable(fc->cred->user_ns, CAP_SYS_ADMIN). If a task calls
unshare(CLONE_NEWUSER) without CLONE_NEWNS (so priv is false in
init_mnt_ns, but the caller has CAP_SYS_ADMIN in its new cred->user_ns U1)
and binds to a child rootns (U2) before writing uid_map/gid_map,
fc->user_ns falls back to U1 while fc->rootns_bound is true. Subsequent
FSCONFIG_CMD_CREATE and fsmount() calls then authorize against U1 despite
!may_mount(), and fsmount() allocates an anonymous mount namespace in U1
with ns->seq_origin set to U2's mount namespace ID.
Second, a nested container manager with CAP_SYS_ADMIN in U1 that binds a
context to a child rootns U2 before U2's uid_map/gid_map are written (or
when U2 maps subordinate IDs excluding fc->cred->fsuid/fsgid) also hits
the else branch, leaving the superblock in U1 instead of delegating it to
U2.
Third, calling fc->fs_type->init_fs_context(fc) after setting fc->user_ns
allows filesystems without a ->set_rootns() hook to overwrite fc->user_ns
from current->nsproxy (for example, cgroup_init_fs_context() resets
fc->user_ns to current->nsproxy->cgroup_ns->user_ns).
> diff --git a/fs/namespace.c b/fs/namespace.c
> index 781380945d784..dfe0ee9ecdcd0 100644
> --- a/fs/namespace.c
> +++ b/fs/namespace.c
[ ... ]
> @@ -4658,6 +4661,15 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
> if (ret)
> return ret;
>
> + if (fc->rootns_bound) {
> + if (flags & FSMOUNT_NAMESPACE)
> + return -EINVAL;
> + if (!ns_capable(fc->user_ns, CAP_SYS_ADMIN))
> + return -EPERM;
> + } else if (!(flags & FSMOUNT_NAMESPACE) && !priv) {
> + return -EPERM;
> + }
> +
> /* There must be a valid superblock or we can't mount it */
> ret = -EINVAL;
> if (!fc->root)
> @@ -4699,9 +4711,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(priv ? current->nsproxy->mnt_ns->user_ns :
> + fc->user_ns, true);
> if (IS_ERR(ns))
> return PTR_ERR(ns);
> + if (!priv)
> + ns->seq_origin = fc->rootns_mnt_id;
[Severity: High]
Should alloc_mnt_ns() and ns->seq_origin be conditioned on fc->rootns_bound
rather than priv?
When a manager process already has CAP_SYS_ADMIN in its own mount
namespace (priv == true) and uses FSCONFIG_SET_ROOTNS to prepare a mount
for a target rootns, testing priv allocates the anonymous mount namespace
in current->nsproxy->mnt_ns->user_ns instead of fc->user_ns (causing
attach_recursive_mnt() to lock the mount with MNT_LOCKED when moved into
the target rootns) and leaves ns->seq_origin at 0 instead of restricting
the detached mount to fc->rootns_mnt_id.
> diff --git a/fs/proc/root.c b/fs/proc/root.c
> index 7fbbe92bf73a7..323296b030892 100644
> --- a/fs/proc/root.c
> +++ b/fs/proc/root.c
[ ... ]
> @@ -323,6 +325,21 @@ static int proc_get_tree(struct fs_context *fc)
> return get_tree_nodev(fc, proc_fill_super);
> }
>
> +static int proc_set_rootns(struct fs_context *fc, struct rootns *rootns)
> +{
> + struct proc_fs_context *ctx = fc->fs_private;
> + struct pid_namespace *target = rootns->ns->pid_ns_for_children;
> +
> + if (!pidns_is_ancestor(target, task_active_pid_ns(current)))
> + return invalfc(fc, "cannot set pidns to non-descendant pidns");
> +
> + put_pid_ns(ctx->pid_ns);
> + ctx->pid_ns = get_pid_ns(target);
> + put_user_ns(fc->user_ns);
> + fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
> + return 0;
> +}
[Severity: Medium]
After proc_set_rootns() binds a context to a rootns, can a subsequent
"pidns" mount option overwrite ctx->pid_ns and fc->user_ns?
fs/proc/root.c:proc_parse_param() {
...
case Opt_pidns:
if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
errorfc(fc, "cannot reconfigure pidns for existing procfs");
return -EBUSY;
}
err = proc_parse_pidns_param(fc, param, &result);
...
}
proc_parse_pidns_param() replaces ctx->pid_ns and sets fc->user_ns to the
new PID namespace's user_ns while fc->rootns_bound remains true and
fc->rootns_mnt_id retains the original rootns mount namespace ID.
fsmount() then checks CAP_SYS_ADMIN and allocates the anonymous mount
namespace against the overwritten fc->user_ns while pairing it with
ns->seq_origin = fc->rootns_mnt_id from the original rootns. Should
Opt_pidns also be rejected when fc->rootns_bound is set?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119224312.1037966-1-jarkko@kernel.org?part=8
More information about the Linux-security-module-archive
mailing list