Skip to content

Commit e1c5ae5

Browse files
sforsheebrauner
authored andcommitted
fs: don't allow non-init s_user_ns for filesystems without FS_USERNS_MOUNT
Christian noticed that it is possible for a privileged user to mount most filesystems with a non-initial user namespace in sb->s_user_ns. When fsopen() is called in a non-init namespace the caller's namespace is recorded in fs_context->user_ns. If the returned file descriptor is then passed to a process priviliged in init_user_ns, that process can call fsconfig(fd_fs, FSCONFIG_CMD_CREATE), creating a new superblock with sb->s_user_ns set to the namespace of the process which called fsopen(). This is problematic. We cannot assume that any filesystem which does not set FS_USERNS_MOUNT has been written with a non-initial s_user_ns in mind, increasing the risk for bugs and security issues. Prevent this by returning EPERM from sget_fc() when FS_USERNS_MOUNT is not set for the filesystem and a non-initial user namespace will be used. sget() does not need to be updated as it always uses the user namespace of the current context, or the initial user namespace if SB_SUBMOUNT is set. Fixes: cb50b34 ("convenience helpers: vfs_get_super() and sget_fc()") Reported-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Seth Forshee (DigitalOcean) <sforshee@kernel.org> Link: https://lore.kernel.org/r/20240724-s_user_ns-fix-v1-1-895d07c94701@kernel.org Reviewed-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent c33ffdb commit e1c5ae5

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

fs/super.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,17 @@ struct super_block *sget_fc(struct fs_context *fc,
736736
struct user_namespace *user_ns = fc->global ? &init_user_ns : fc->user_ns;
737737
int err;
738738

739+
/*
740+
* Never allow s_user_ns != &init_user_ns when FS_USERNS_MOUNT is
741+
* not set, as the filesystem is likely unprepared to handle it.
742+
* This can happen when fsconfig() is called from init_user_ns with
743+
* an fs_fd opened in another user namespace.
744+
*/
745+
if (user_ns != &init_user_ns && !(fc->fs_type->fs_flags & FS_USERNS_MOUNT)) {
746+
errorfc(fc, "VFS: Mounting from non-initial user namespace is not allowed");
747+
return ERR_PTR(-EPERM);
748+
}
749+
739750
retry:
740751
spin_lock(&sb_lock);
741752
if (test) {

0 commit comments

Comments
 (0)