fuse: fix readdir with long names
Subsystems (4)
Christian Brauner
maintainercover fuse: fix readdir with long names
This fixes directory listings on fuse silently ending right before a long name.
Since FUSE_NAME_MAX was raised to PATH_MAX - 1 a READDIR entry can be up to 4120 bytes and a READDIRPLUS entry up to 4248 bytes. But the READDIR request is only as large as the getdents() buffer and never smaller than a page. The server can't send more than we asked for so it sends nothing. And an empty reply means end of directory. So getdents() returns 0 and the long name and everything after it are gone. Userspace never gets the EINVAL that would tell it to retry with a larger buffer.
That hits getdents() with a buffer of a page or less, the old readdir() system call, and in-kernel callers like nfsd that don't pass a size. And since we fall back to whatever alloc_pages_bulk() hands us it can hit glibc's 32k buffer under memory pressure as well. Falling back to a shorter read is fine (cf. [1]) but it must still fit the largest entry.
This came up with a userspace directory walker that starts with a small getdents() buffer and doubles it on EINVAL.
The first patch makes the request large enough for the largest entry the server may send and makes sure we always have the pages for it. The second patch adds a selftest. It fails on v7.4-rc1 and passes with the fix. I also reproduced all of it in a VM on v7.4-rc1, including the memory pressure case.
The request is sized by fc->name_max, not FUSE_NAME_MAX, as that's what the connection negotiated. But parse_dirfile() and parse_dirplusfile() accept names up to FUSE_NAME_MAX no matter what was negotiated. So a server that doesn't negotiate FUSE_MAX_PAGES and returns long names anyway still loses them. I don't think that's worth caring about but I'm happy to size by FUSE_NAME_MAX instead.
[1]: https://lore.kernel.org/CAJfpegtcsWnXh-uDh1Qu83dFF9nJji1gytk2y9Wk5p9+JDTZ+w@mail.gmail.com
---
Christian Brauner (2):
fuse: make readdir requests big enough for the largest dirent selftests/fuse: test listing directories with long names
fs/fuse/readdir.c | 22 ++- .../testing/selftests/filesystems/fuse/.gitignore | 1 + tools/testing/selftests/filesystems/fuse/Makefile | 2 + .../filesystems/fuse/fuse_readdir_long_name_test.c | 197 +++++++++++++++++++++ 4 files changed, 215 insertions(+), 7 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261124-work-fuse-readdir-0fed3f05a430
Christian Brauner
maintainerPATCH 1/2 fuse: make readdir requests big enough for the largest dirent
Since commit 27992ef80770 ("fuse: Increase FUSE_NAME_MAX to PATH_MAX") a dirent can be larger than a page. But the readdir request can still be a single page, for a small getdents() buffer or when alloc_pages_bulk() didn't give us more. The server can't fit the dirent and sends an empty reply which we take as end of directory. So the long name and all entries after it silently disappear.
Make the request big enough for the largest dirent the server may send.
--- fs/fuse/readdir.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
fs/fuse/readdir.c +15 -7
@@ -333,17 +333,23 @@ static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,return 0 ; } -static struct page **fuse_readdir_alloc_buf (struct fuse_args_pages *ap,size_t *bufsize) +static struct page **fuse_readdir_alloc_buf (struct fuse_args_pages *ap,size_t *bufsize, +size_t minsize) {unsigned int i, nr_alloc, nr_pages =DIV_ROUND_UP (*bufsize,PAGE_SIZE ); +unsigned int nr_min =DIV_ROUND_UP (minsize,PAGE_SIZE );struct page **pages =kzalloc_objs (*pages, nr_pages);if (!pages)return NULL ; nr_alloc =alloc_pages_bulk (GFP_KERNEL , nr_pages, pages); -if (!nr_alloc) - goto free_array; +/* The bulk allocator may stop early, but the largest entry must fit. */ +for (; nr_alloc < nr_min; nr_alloc++) { + pages[nr_alloc] =alloc_page (GFP_KERNEL ); +if (!pages[nr_alloc]) + goto release_pages; + }if (nr_alloc < nr_pages) { nr_pages = nr_alloc;
@@ -364,8 +370,7 @@ static struct page **fuse_readdir_alloc_buf(struct fuse_args_pages *ap, size_t *return pages; release_pages: -release_pages (pages, nr_pages); -free_array: +release_pages (pages, nr_alloc);kfree (pages);return NULL ; }
@@ -380,10 +385,13 @@ static int fuse_readdir_uncached(struct file *file, struct dir_context *ctx)struct fuse_io_args ia = {};struct fuse_args_pages *ap = &ia.ap;void *buf; -size_t bufsize =clamp ((unsigned int ) ctx->count,PAGE_SIZE , fc->max_pages <<PAGE_SHIFT ); +/* Room for the largest entry the server may send, a direntplus. */ +size_t minsize =max (PAGE_SIZE , +FUSE_DIRENT_ALIGN (FUSE_NAME_OFFSET_DIRENTPLUS + fc->name_max)); +size_t bufsize =clamp ((unsigned int ) ctx->count, minsize, fc->max_pages <<PAGE_SHIFT );u64 attr_version =0 , evict_ctr =0 ;bool locked; -struct page **pages =fuse_readdir_alloc_buf (ap, &bufsize); +struct page **pages =fuse_readdir_alloc_buf (ap, &bufsize, minsize);if (!pages)return -ENOMEM ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH 2/2 selftests/fuse: test listing directories with long names
In reply to Christian BraunerAdd a test that lists a directory whose first entry has a name too long for a one page READDIR or READDIRPLUS reply:
- READDIR with the shortest such name and with FUSE_NAME_MAX
- READDIRPLUS with the shortest such name and with FUSE_NAME_MAX
It starts out with a 4k getdents64() buffer, doubles it on EINVAL and checks that both the long name and the entry after it show up.
--- .../testing/selftests/filesystems/fuse/.gitignore | 1 + tools/testing/selftests/filesystems/fuse/Makefile | 2 + .../filesystems/fuse/fuse_readdir_long_name_test.c | 197 +++++++++++++++++++++ 3 files changed, 200 insertions(+)
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
@@ -6,3 +6,4 @@ write_extend_eof_test fuse_acl_cache_test fuse_symlink_cache_test fuse_readdir_cache_test +fuse_readdir_long_name_test
tools/testing/selftests/filesystems/fuse/Makefile +2 -0
@@ -7,6 +7,7 @@ TEST_GEN_PROGS += write_extend_eof_test TEST_GEN_PROGS += fuse_acl_cache_test TEST_GEN_PROGS += fuse_symlink_cache_test TEST_GEN_PROGS += fuse_readdir_cache_test +TEST_GEN_PROGS += fuse_readdir_long_name_test TEST_GEN_FILES := fuse_mnt include ../../lib.mk
@@ -32,5 +33,6 @@ $(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS) $(OUTPUT)/fuse_acl_cache_test: fuse_common.c fuse_acl_cache_test.c $(OUTPUT)/fuse_symlink_cache_test: fuse_common.c fuse_symlink_cache_test.c $(OUTPUT)/fuse_readdir_cache_test: fuse_common.c fuse_readdir_cache_test.c +$(OUTPUT)/fuse_readdir_long_name_test: fuse_common.c fuse_readdir_long_name_test.c EXTRA_CLEAN := fuse_common.o
tools/testing/selftests/filesystems/fuse/fuse_readdir_long_name_test.c new file +197 -0
197 lines not on this page · raw message
Signature · 1 line
2.53.0