Skip to contentSkip to conversation

fs: reintroduce SB_I_MULTIROOT

PATCH In review Christian Brauner 3 participants 6 messages Nov 18 – Nov 19

Subsystems (5)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover fs: reintroduce SB_I_MULTIROOT

Welcome to 2018: Since the nfs conversion to the new mount api in v5.6 ".." can escape an NFSv2/NFSv3 mount again when the client shares the superblock between mounts of different subtrees of the same server filesystem.

I reproduced it in a VM with knfsd exporting a single ext4 filesystem and the client mounting two subdirectories of it over loopback. On v7.4-rc3 ".." from a directory that got moved into the other mount's subtree lands in that other mount, from inside a chroot as well. With the first patch it fails with ENOENT. The first patch just restores what we had so it can go to stable easily.

While testing this I noticed that the same setup trips the warning I added to vfs_dentry_acceptable() for open_by_handle_at(). The handle is correctly refused but an unprivileged user can trigger the warning from a user namespace if they own the directories. The second patch uses the flag to not warn on multi-root superblocks.

---
Christian Brauner (2):
fs: reintroduce SB_I_MULTIROOT
fhandle: don't warn about multi-root filesystems

 fs/fhandle.c                   | 6 ++++--
 fs/namei.c                     | 4 ++--
 fs/nfs/super.c                 | 2 ++
 include/linux/fs/super_types.h | 1 +
 4 files changed, 9 insertions(+), 4 deletions(-)

---
base-commit: 94ca49563acb36a19fb578499b04c84d5b0a5181
change-id: 20261118-work-nfs-multiroot-d82e334c72a2

author maintainer
#1 lore raw

PATCH 1/2 fs: reintroduce SB_I_MULTIROOT

On NFSv2 and NFSv3 ".." can walk out of a mount and out of a chroot as well (again).

path_connected() makes ".." fail with ENOENT when the parent isn't reachable from the root of the mount anymore. It was added in commit 397d425dc26d ("vfs: Test for and handle paths that are unreachable from their mnt_root") for directories that get renamed out from under a bind mount. To avoid calling is_subdir() for every ".." it takes a shortcut. If the root of the mount is the root of the superblock every dentry is assumed to be reachable and the check is skipped.

That doesn't hold for nfs. By default the client shares a single superblock between mounts of the same server filesystem, i.e. mounts that report the same filesystem identifier and use the same mount options. Those mounts can be of disjoint subtrees, e.g. two subdirectories of one export. NFSv2 and NFSv3 provide no way to find the common root of these trees so s_root is simply the root of whatever subtree got mounted first. For that mount mnt_root == s_root and path_connected() never calls is_subdir().

This was fixed in commit 95dd77580ccd ("fs: Teach path_connected to handle nfs filesystems with multiple roots."). That added SB_I_MULTIROOT which nfs raised on every superblock it might share, and path_connected() stopped taking the shortcut for such superblocks.

When nfs was converted to the new mount api in commit f2aedb713c28 ("NFS: Add fs_context support.") nfs_fs_mount_common() was rewritten into nfs_get_tree_common() and the two lines raising SB_I_MULTIROOT got lost on the way. The changelog doesn't mention it. Nothing else raised the flag so from v5.6 onwards path_connected() took the shortcut for nfs again.

Then commit 402dd2cf46b1 ("fs: remove the unused SB_I_MULTIROOT flag") noticed that the flag was dead, named the fs_context conversion as the commit that removed its last user, and removed the flag together with the check in path_connected().

But the flag was only unused because the conversion had dropped it by accident. Et voila, we're back in 2018. Mount two disjoint subdirectories of the same export with vers=3 and without nosharecache:

mount -t nfs -o vers=3 srv:/export/a /mnt/a mount -t nfs -o vers=3 srv:/export/b/sub /mnt/b

Both mounts share a superblock and s_root is the root of /mnt/a. A process sits in /mnt/a/D1/D. Someone with write access to both trees renames D into b/sub, either on the server, through another client or through a mount of a common parent on the same client. For a rename on the server it's enough that anyone looks up /mnt/b/D afterwards. nfs_lookup() finds the cached directory inode and d_splice_alias() moves the existing dentry under the root of /mnt/b.

Now ".." from the old working directory lands in /mnt/b which isn't reachable from /mnt/a at all. That works from inside a chroot to /mnt/a as well. And if the root of the export is mounted too and someone walks down to b/sub through it then the root of /mnt/b gets spliced in below it and ".." climbs all the way up to the root of the exported filesystem.

This reproduces on v7.4-rc3 with a single ext4 filesystem exported by knfsd and both subdirectories mounted over loopback. With this patch applied ".." fails with ENOENT instead.

Only mounts whose root is s_root are affected, i.e. the subtree that got mounted first and bind mounts of its root. Mounts with nosharecache get their own superblock.

Reintroduce SB_I_MULTIROOT and raise it again in nfs_get_tree_common() where nfs_fs_mount_common() used to raise it. Like before nfs raises it on every superblock it may share, independent of the protocol version. That means ".." on such superblocks always goes through is_subdir() again.

Fixes f2aedb713c28 ("NFS: Add fs_context support.")
---
 fs/namei.c                     | 4 ++--
 fs/nfs/super.c                 | 2 ++
 include/linux/fs/super_types.h | 1 +
 3 files changed, 5 insertions(+), 2 deletions(-)
fs/namei.c +2 -2
index 925aeb63bfa1d..a08c93369f4f1 100644
@@ -822,8 +822,8 @@ static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
 {
 	struct super_block *sb = mnt->mnt_sb;
 
-	/* Bind mounts can have disconnected paths */
-	if (mnt->mnt_root == sb->s_root)
+	/* Bind mounts and multi-root filesystems can have disconnected paths */
+	if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
 		return true;
 
 	return is_subdir(dentry, mnt->mnt_root);
fs/nfs/super.c +2 -0
index 23292680adbd5..99e883e7a4890 100644
@@ -1315,6 +1315,8 @@ int nfs_get_tree_common(struct fs_context *fc)
 		error = nfs_get_cache_cookie(s, ctx);
 		if (error < 0)
 			goto error_splat_super;
+		if (!(server->flags & NFS_MOUNT_UNSHARED))
+			s->s_iflags |= SB_I_MULTIROOT;
 	}
 
 	error = nfs_get_root(s, fc);
include/linux/fs/super_types.h +1 -0
index 6ce105fe8359c..b09e8b2087252 100644
@@ -359,5 +359,6 @@ struct super_block {
 #define SB_I_NOIDMAP	0x00002000	/* No idmapped mounts on this superblock */
 #define SB_I_ALLOW_HSM	0x00004000	/* Allow HSM events on this superblock */
 #define SB_I_NO_DATA_INTEGRITY	0x00008000 /* fs cannot guarantee data persistence on sync */
+#define SB_I_MULTIROOT	0x00010000	/* Multiple roots to the dentry tree */
 
 #endif /* _LINUX_FS_SUPER_TYPES_H */
Signature · 1 line
2.53.0
maintainer Acked-by on v1 1/2
#2 lore raw

Hi Christian,

Quoted 103 lines · Christian Brauner

On NFSv2 and NFSv3 ".." can walk out of a mount and out of a chroot as well (again).

path_connected() makes ".." fail with ENOENT when the parent isn't reachable from the root of the mount anymore. It was added in commit 397d425dc26d ("vfs: Test for and handle paths that are unreachable from their mnt_root") for directories that get renamed out from under a bind mount. To avoid calling is_subdir() for every ".." it takes a shortcut. If the root of the mount is the root of the superblock every dentry is assumed to be reachable and the check is skipped.

That doesn't hold for nfs. By default the client shares a single superblock between mounts of the same server filesystem, i.e. mounts that report the same filesystem identifier and use the same mount options. Those mounts can be of disjoint subtrees, e.g. two subdirectories of one export. NFSv2 and NFSv3 provide no way to find the common root of these trees so s_root is simply the root of whatever subtree got mounted first. For that mount mnt_root == s_root and path_connected() never calls is_subdir().

This was fixed in commit 95dd77580ccd ("fs: Teach path_connected to handle nfs filesystems with multiple roots."). That added SB_I_MULTIROOT which nfs raised on every superblock it might share, and path_connected() stopped taking the shortcut for such superblocks.

When nfs was converted to the new mount api in commit f2aedb713c28 ("NFS: Add fs_context support.") nfs_fs_mount_common() was rewritten into nfs_get_tree_common() and the two lines raising SB_I_MULTIROOT got lost on the way. The changelog doesn't mention it. Nothing else raised the flag so from v5.6 onwards path_connected() took the shortcut for nfs again.

Then commit 402dd2cf46b1 ("fs: remove the unused SB_I_MULTIROOT flag") noticed that the flag was dead, named the fs_context conversion as the commit that removed its last user, and removed the flag together with the check in path_connected().

But the flag was only unused because the conversion had dropped it by accident. Et voila, we're back in 2018. Mount two disjoint subdirectories of the same export with vers=3 and without nosharecache:

mount -t nfs -o vers=3 srv:/export/a /mnt/a mount -t nfs -o vers=3 srv:/export/b/sub /mnt/b

Both mounts share a superblock and s_root is the root of /mnt/a. A process sits in /mnt/a/D1/D. Someone with write access to both trees renames D into b/sub, either on the server, through another client or through a mount of a common parent on the same client. For a rename on the server it's enough that anyone looks up /mnt/b/D afterwards. nfs_lookup() finds the cached directory inode and d_splice_alias() moves the existing dentry under the root of /mnt/b.

Now ".." from the old working directory lands in /mnt/b which isn't reachable from /mnt/a at all. That works from inside a chroot to /mnt/a as well. And if the root of the export is mounted too and someone walks down to b/sub through it then the root of /mnt/b gets spliced in below it and ".." climbs all the way up to the root of the exported filesystem.

This reproduces on v7.4-rc3 with a single ext4 filesystem exported by knfsd and both subdirectories mounted over loopback. With this patch applied ".." fails with ENOENT instead.

Only mounts whose root is s_root are affected, i.e. the subtree that got mounted first and bind mounts of its root. Mounts with nosharecache get their own superblock.

Reintroduce SB_I_MULTIROOT and raise it again in nfs_get_tree_common() where nfs_fs_mount_common() used to raise it. Like before nfs raises it on every superblock it may share, independent of the protocol version. That means ".." on such superblocks always goes through is_subdir() again.

Fixes f2aedb713c28 ("NFS: Add fs_context support.")
---
 fs/namei.c                     | 4 ++--
 fs/nfs/super.c                 | 2 ++
 include/linux/fs/super_types.h | 1 +
 3 files changed, 5 insertions(+), 2 deletions(-)
fs/namei.c +2 -2
index 925aeb63bfa1d..a08c93369f4f1 100644
@@ -822,8 +822,8 @@ static bool path_connected(struct vfsmount *mnt, 
 struct dentry *dentry)
 {
 	struct super_block *sb = mnt->mnt_sb;
 
-	/* Bind mounts can have disconnected paths */
-	if (mnt->mnt_root == sb->s_root)
+	/* Bind mounts and multi-root filesystems can have disconnected paths */
+	if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
 		return true;
 
fs/namei.c +0 -0
 	return is_subdir(dentry, mnt->mnt_root);
fs/nfs/super.c +0 -0
index 23292680adbd5..99e883e7a4890 100644

fs/nfs/super.c int nfs_get_tree_common(struct fs_context *fc) · line 1319

Quoted 5 lines
fs/nfs/super.c +2 -0
@@ -1315,6 +1315,8 @@ int nfs_get_tree_common(struct fs_context *fc)
 		error = nfs_get_cache_cookie(s, ctx);
 		if (error < 0)
 			goto error_splat_super;
+		if (!(server->flags & NFS_MOUNT_UNSHARED))
+			s->s_iflags |= SB_I_MULTIROOT;

I assume this is going through the VFS tree?

Quoted 22 lines
fs/nfs/super.c +0 -0
 	}
 
 	error = nfs_get_root(s, fc);
include/linux/fs/super_types.h +0 -0
index 6ce105fe8359c..b09e8b2087252 100644
@@ -359,5 +359,6 @@ struct super_block {
 #define SB_I_NOIDMAP	0x00002000	/* No idmapped mounts on this 

superblock */
#define SB_I_ALLOW_HSM 0x00004000 /* Allow HSM events on this
superblock */
#define SB_I_NO_DATA_INTEGRITY 0x00008000 /* fs cannot guarantee data
persistence on sync */
+#define SB_I_MULTIROOT 0x00010000 /* Multiple roots to the dentry tree
*/

#endif /* _LINUX_FS_SUPER_TYPES_H */

Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 2/2 fhandle: don't warn about multi-root filesystems

In reply to Christian Brauner

When I relaxed the permission checks for open_by_handle_at() in commit 620c266f3949 ("fhandle: relax open_by_handle_at() permission checks") I added a warning for when walking up from a decoded directory ends neither at the root of the mount fd nor at the root of the superblock. But a superblock can have more than one root. nfs shares a superblock between mounts of disjoint subtrees of the same server filesystem and each of them has its own root dentry. If the decoded directory lives below one of the other roots the walk ends there and we warn.

Mount two subdirectories of the same export with vers=3, encode a directory below the second mount with name_to_handle_at() and decode it from a new user and mount namespace with open_by_handle_at() and the first mount as the mount fd:

  WARNING: fs/fhandle.c:260 at vfs_dentry_acceptable+0x2e3/0x4b0
  Call Trace:
   <TASK>
   exportfs_decode_fh_raw+0x1de/0x840
   do_handle_open+0x46e/0xb20
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
   </TASK>

The handle is still refused because the directory isn't reachable from the mount fd. But an unprivileged user can trigger the warning if the directories involved belong to them, e.g. if their home directory is one of the mounts.

Don't warn for superblocks with multiple roots. This depends on the previous patch reintroducing SB_I_MULTIROOT.

Fixes 620c266f3949 ("fhandle: relax open_by_handle_at() permission checks")
---
 fs/fhandle.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/fhandle.c +4 -2
index 32fcff14f88fd..8e6110215469d 100644
@@ -254,9 +254,11 @@ static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
 	/*
 	 * exportfs_decode_fh_raw() does not call acceptable() callback with
 	 * a disconnected directory dentry, so we should have reached either
-	 * mount fd directory or sb root.
+	 * mount fd directory or sb root. Multi-root filesystems have more
+	 * than one root.
 	 */
-	if (ctx->fh_flags & EXPORT_FH_DIR_ONLY)
+	if ((ctx->fh_flags & EXPORT_FH_DIR_ONLY) &&
+	    !(root->d_sb->s_iflags & SB_I_MULTIROOT))
 		WARN_ON_ONCE(d != root && d != root->d_sb->s_root);
 	dput(d);
 	return retval;
Signature · 1 line
2.53.0
maintainer Reviewed-by on the v1 series
#5 lore raw
In reply to Christian Brauner
Quoted 24 lines · Christian Brauner

Welcome to 2018: Since the nfs conversion to the new mount api in v5.6 ".." can escape an NFSv2/NFSv3 mount again when the client shares the superblock between mounts of different subtrees of the same server filesystem.

I reproduced it in a VM with knfsd exporting a single ext4 filesystem and the client mounting two subdirectories of it over loopback. On v7.4-rc3 ".." from a directory that got moved into the other mount's subtree lands in that other mount, from inside a chroot as well. With the first patch it fails with ENOENT. The first patch just restores what we had so it can go to stable easily.

While testing this I noticed that the same setup trips the warning I added to vfs_dentry_acceptable() for open_by_handle_at(). The handle is correctly refused but an unprivileged user can trigger the warning from a user namespace if they own the directories. The second patch uses the flag to not warn on multi-root superblocks.

---
Christian Brauner (2):
fs: reintroduce SB_I_MULTIROOT
fhandle: don't warn about multi-root filesystems

Quoted 8 lines
 fs/fhandle.c                   | 6 ++++--
 fs/namei.c                     | 4 ++--
 fs/nfs/super.c                 | 2 ++
 include/linux/fs/super_types.h | 1 +
 4 files changed, 9 insertions(+), 4 deletions(-)

---
base-commit: 94ca49563acb36a19fb578499b04c84d5b0a5181
change-id: 20261118-work-nfs-multiroot-d82e334c72a2

Seems sane.