mount_too_revealing() skips visibility checks whenever current->nsproxy->mnt_ns belongs to init_user_ns, assuming the caller holds CAP_SYS_ADMIN in its current mount namespace. That assumption does not hold when an unprivileged caller creates mounts for a child user namespace while running in init_mnt_ns or another foreign mount namespace. Require CAP_SYS_ADMIN in init_user_ns before bypassing mount_too_revealing() for init_mnt_ns, reject chrooted callers lacking CAP_SYS_ADMIN in their current mount namespace, and treat existing mounts and child overmounts in a foreign mount namespace as locked during mnt_already_visible() checks. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- fs/namespace.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index 781380945d78..1bb9f90a6523 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -6580,7 +6580,7 @@ bool current_chrooted(void) static bool mnt_already_visible(struct mnt_namespace *ns, const struct super_block *sb, - int *new_mnt_flags) + int *new_mnt_flags, bool foreign_ns) { int new_flags = *new_mnt_flags; struct mount *mnt; @@ -6606,11 +6606,16 @@ static bool mnt_already_visible(struct mnt_namespace *ns, continue; /* A local view of the mount flags */ - mnt_flags = mnt->mnt.mnt_flags; + mnt_flags = READ_ONCE(mnt->mnt.mnt_flags); /* Don't miss readonly hidden in the superblock flags */ if (sb_rdonly(mnt->mnt.mnt_sb)) mnt_flags |= MNT_LOCK_READONLY; + if (foreign_ns) { + if (mnt_flags & MNT_READONLY) + mnt_flags |= MNT_LOCK_READONLY; + mnt_flags |= MNT_LOCK_ATIME; + } /* Verify the mount flags are equal to or more permissive * than the proposed new mount. @@ -6629,7 +6634,7 @@ static bool mnt_already_visible(struct mnt_namespace *ns, list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { struct inode *inode = child->mnt_mountpoint->d_inode; /* Only worry about locked mounts */ - if (!(child->mnt.mnt_flags & MNT_LOCKED)) + if (!foreign_ns && !(READ_ONCE(child->mnt.mnt_flags) & MNT_LOCKED)) continue; /* Is the directory permanently empty? */ if (!is_empty_dir_inode(inode)) @@ -6649,14 +6654,19 @@ static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; struct mnt_namespace *ns = current->nsproxy->mnt_ns; unsigned long s_iflags; - - if (ns->user_ns == &init_user_ns) - return false; + bool foreign_ns; /* Can this filesystem be too revealing? */ if (!(sb->s_type->fs_flags & FS_USERNS_MOUNT_RESTRICTED)) return false; + foreign_ns = !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN); + if (ns->user_ns == &init_user_ns && !foreign_ns) + return false; + + if (foreign_ns && current_chrooted()) + return true; + s_iflags = sb->s_iflags; if ((s_iflags & required_iflags) != required_iflags) { WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", @@ -6671,7 +6681,7 @@ static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags if (s_iflags & SB_I_RESTRICTED_VARIANT) return false; - return !mnt_already_visible(ns, sb, new_mnt_flags); + return !mnt_already_visible(ns, sb, new_mnt_flags, foreign_ns); } bool mnt_may_suid(struct vfsmount *mnt) -- 2.47.3