fs: reintroduce SB_I_MULTIROOT
Subsystems (5)
Christian Brauner
maintainercover 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
Christian Brauner
maintainerPATCH 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.
--- 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
@@ -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
@@ -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
@@ -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
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 commit397d425dc26d("vfs: Test for and handle paths that are unreachable from theirmnt_root") for directories that get renamed out from under a bind mount. To avoid callingis_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_rootis simply the root of whatever subtree got mounted first. For that mountmnt_root==s_rootandpath_connected()never callsis_subdir().This was fixed in commit
95dd77580ccd("fs: Teachpath_connectedto handle nfs filesystems with multiple roots."). That addedSB_I_MULTIROOTwhich nfs raised on every superblock it might share, andpath_connected()stopped taking the shortcut for such superblocks.When nfs was converted to the new mount api in commit
f2aedb713c28("NFS: Addfs_contextsupport.")nfs_fs_mount_common()was rewritten intonfs_get_tree_common()and the two lines raisingSB_I_MULTIROOTgot lost on the way. The changelog doesn't mention it. Nothing else raised the flag so from v5.6 onwardspath_connected()took the shortcut for nfs again.Then commit
402dd2cf46b1("fs: remove the unusedSB_I_MULTIROOTflag") noticed that the flag was dead, named thefs_contextconversion as the commit that removed its last user, and removed the flag together with the check inpath_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/amount -t nfs -o vers=3 srv:/export/b/sub/mnt/bBoth mounts share a superblock and
s_rootis 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/Dafterwards.nfs_lookup()finds the cached directory inode andd_splice_alias()moves the existing dentry under the root of/mnt/b.Now ".." from the old working directory lands in
/mnt/bwhich isn't reachable from/mnt/aat all. That works from inside a chroot to/mnt/aas 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/bgets 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_rootare 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_MULTIROOTand raise it again innfs_get_tree_common()wherenfs_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 throughis_subdir()again.Fixes f2aedb713c28 ("NFS: Add fs_context support.")Signed-off-by Christian Brauner (Amutable) <[email protected]>--- 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@@ -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
-0return is_subdir (dentry, mnt->mnt_root);fs/nfs/super.c +0
-0
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@@ -359,5 +359,6 @@ struct super_block {#define SB_I_NOIDMAP 0x00002000/* No idmapped mounts on this superblock */
#defineSB_I_ALLOW_HSM0x00004000/* Allow HSM events on this
superblock */
#defineSB_I_NO_DATA_INTEGRITY0x00008000/* fs cannot guarantee data
persistence on sync */
+#defineSB_I_MULTIROOT0x00010000/* Multiple roots to the dentry tree
*/#endif /*
_LINUX_FS_SUPER_TYPES_H*/Signature · 1 line
2.53.0
Christian Brauner
maintainerQuoted 1 line
I assume this is going through the VFS tree?
Yeah, I got it on vfs.fixes.
Quoted 1 line
Acked-by Anna Schumaker <[email protected]>
Thanks!
Christian Brauner
maintainerPATCH 2/2 fhandle: don't warn about multi-root filesystems
In reply to Christian BraunerWhen 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.
--- fs/fhandle.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/fhandle.c +4 -2
@@ -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
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()foropen_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.Signed-off-by Christian Brauner (Amutable) <[email protected]>---
Christian Brauner (2):
fs: reintroduceSB_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.