fuse: zero the partial EOF page when extending a file
Subsystems (2)
cover fuse: zero the partial EOF page when extending a file
Extending a fuse file past a non-page-aligned EOF does not zero the tail of the old last page. When that page is cached and has been mmap-dirtied beyond the old EOF, the now in-bounds tail is served to later reads as stale data rather than zeros, which violates POSIX file-extension semantics.
This is the same class of bug that commit b1817b18ff20 ("NFS: Protect against 'eof page pollution'") fixed for the NFS client; fuse has the equivalent gap on its non-writeback_cache, FOPEN_KEEP_CACHE path. pagecache_isize_extended() cannot be reused because it is a no-op when i_blocksize() >= PAGE_SIZE, which always holds for a non-fuseblk mount, so patch 1 open-codes the zeroing and calls it from all three paths that extend a file: buffered write, setattr/truncate, and fallocate. Patch 2 adds a self-contained raw /dev/fuse selftest covering each path; every case fails without patch 1 and passes with it.
Tested by booting the patched kernel under User-Mode Linux and running the new selftest (4/4 pass; 4/4 fail without patch 1), and reproduced against libfuse's passthrough_ll example daemon (-o cache=always,no_writeback).
Jimmy Zuber (2):
fuse: zero the partial EOF page when extending a file selftests/fuse: test post-EOF page zeroing when a file is extended
fs/fuse/dir.c | 3 + fs/fuse/file.c | 56 +++ fs/fuse/fuse_i.h | 1 + .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 3 + .../filesystems/fuse/write_extend_eof_test.c | 368 ++++++++++++++++++ 6 files changed, 432 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c
base-commit: 7d87a5a284bb34edb3f4e7e312ef403b3385a7b7
Signature · 1 line
2.50.1
PATCH 1/2 fuse: zero the partial EOF page when extending a file
Extending a fuse file past a non-page-aligned EOF does not zero the tail of the old last page. If that page is cached and was dirtied beyond the old EOF -- e.g. an application mmap()ed the EOF page and stored into the region past EOF, which is undefined until the file grows -- the now in-bounds tail is exposed to subsequent reads as stale data instead of zeros, in violation of POSIX file-extension semantics.
Other filesystems zero this via pagecache_isize_extended(), but that helper is a no-op for fuse: it returns early when i_blocksize() >= PAGE_SIZE, and a non-fuseblk fuse mount has s_blocksize == PAGE_SIZE (the server-supplied st_blksize only sets fi->cached_i_blkbits, not i_blkbits). The NFS client hit the same problem and open-codes the zeroing in nfs_truncate_last_folio(); add the equivalent fuse_zero_partial_eof_folio() and call it from the three paths that extend a file: a buffered write, a size-extending setattr/truncate, and a size-extending fallocate (fuse_write_update_attr(), fuse_do_setattr() and fuse_file_fallocate()).
writeback_cache connections are unaffected, as their writes go through iomap_file_buffered_write(), which zeroes post-EOF folios. The bug is observable on a non-writeback_cache server that returns FOPEN_KEEP_CACHE on writable files (without FOPEN_DIRECT_IO), and is caught by the new write_extend_eof fuse selftest.
--- fs/fuse/dir.c | 3 +++ fs/fuse/file.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/fuse_i.h | 1 + 3 files changed, 60 insertions(+)
fs/fuse/dir.c +3 -0
@@ -2282,6 +2282,9 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,*/ if ((is_truncate || !is_wb) &&S_ISREG (inode->i_mode) && oldsize != outarg.attr.size) { +if (outarg.attr.size > oldsize) +fuse_zero_partial_eof_folio (inode, oldsize, + outarg.attr.size);truncate_pagecache (inode, outarg.attr.size);invalidate_inode_pages2 (mapping); }
fs/fuse/file.c +56 -0
90 lines not on this page · raw message
fs/fuse/fuse_i.h +1 -0
@@ -1183,6 +1183,7 @@ long fuse_ioctl_common(struct file *file, unsigned int cmd,__poll_t fuse_file_poll (struct file *file,poll_table *wait);bool fuse_write_update_attr (struct inode *inode,loff_t pos,ssize_t written); +void fuse_zero_partial_eof_folio (struct inode *inode,loff_t from,loff_t to);int fuse_flush_times (struct inode *inode,struct fuse_file *ff);int fuse_write_inode (struct inode *inode,struct writeback_control *wbc);
Signature · 1 line
2.50.1
Miklos Szeredi
maintainer[Cc: Jan + Joanne]
Quoted 90 lines · Jimmy Zuber
Extending a fuse file past a non-page-aligned EOF does not zero the tail of the old last page. If that page is cached and was dirtied beyond the old EOF -- e.g. an application mmap()ed the EOF page and stored into the region past EOF, which is undefined until the file grows -- the now in-bounds tail is exposed to subsequent reads as stale data instead of zeros, in violation of POSIX file-extension semantics.
Other filesystems zero this via
pagecache_isize_extended(), but that helper is a no-op for fuse: it returns early wheni_blocksize()>=PAGE_SIZE, and a non-fuseblk fuse mount hass_blocksize==PAGE_SIZE(the server-suppliedst_blksizeonly setsfi->cached_i_blkbits, noti_blkbits). The NFS client hit the same problem and open-codes the zeroing innfs_truncate_last_folio(); add the equivalentfuse_zero_partial_eof_folio()and call it from the three paths that extend a file: a buffered write, a size-extending setattr/truncate, and a size-extending fallocate (fuse_write_update_attr(),fuse_do_setattr()andfuse_file_fallocate()).
writeback_cacheconnections are unaffected, as their writes go throughiomap_file_buffered_write(), which zeroes post-EOF folios. The bug is observable on a non-writeback_cache server that returnsFOPEN_KEEP_CACHEon writable files (withoutFOPEN_DIRECT_IO), and is caught by the newwrite_extend_eoffuse selftest.Signed-off-by Jimmy Zuber <[email protected]>--- fs/fuse/dir.c | 3 +++ fs/fuse/file.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/fuse_i.h | 1 + 3 files changed, 60 insertions(+)fs/fuse/dir.c +3
-0@@ -2282,6 +2282,9 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,*/ if ((is_truncate || !is_wb) &&S_ISREG (inode->i_mode) && oldsize != outarg.attr.size) { +if (outarg.attr.size > oldsize) +fuse_zero_partial_eof_folio (inode, oldsize, + outarg.attr.size);truncate_pagecache (inode, outarg.attr.size);invalidate_inode_pages2 (mapping); }fs/fuse/file.c +30
-0@@ -21,6 +21,8 @@#include <linux/splice.h> #include <linux/task_io_accounting_ops.h> #include <linux/iomap.h> +#include <linux/highmem.h> +#include <linux/rmap.h> static int fuse_send_open(struct fuse_mount *fm, u64 nodeid, unsigned int open_flags, int opcode,@@ -1200,20 +1202,64 @@ static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,return err ?: ia->write.out.size; } +/* +* An operation extended i_size past a non-folio-aligned old EOF at @from, +* turning [@from, @to) into a hole that must read back as zero. If the old +* last folio is cached and was dirtied beyond the old EOF (e.g. mmap stores +* into the post-EOF region, which are undefined until the file grows), zero +* that tail so it is not exposed as stale data (xfstests generic/363). +* +* pagecache_isize_extended() cannot be used: it bails out for +* i_blocksize() >= PAGE_SIZE, and a non-fuseblk mount has +* s_blocksize == PAGE_SIZE, so the zeroing has to be done here. +* Callers hold i_rwsem, serialising this against concurrent writes and +* truncates; it must not run under fi->lock, as it locks the folio. +*/ +void fuse_zero_partial_eof_folio (struct inode *inode,loff_t from,loff_t to) +{ +struct folio *folio; +size_t offset, end; + +if (from >= to) +return ; + + folio =filemap_lock_folio (inode->i_mapping, from >>PAGE_SHIFT ); +if (IS_ERR (folio)) +return ; + +if (folio_mkclean (folio)) +folio_mark_dirty (folio); +
fs/fuse/file.c static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos, · line 1242
Quoted 10 lines
fs/fuse/file.c +10
-0+if (folio_test_dirty (folio)) { + offset =offset_in_folio (folio, from); + end =min_t (loff_t, to -folio_pos (folio),folio_size (folio)); +folio_zero_segment (folio, offset, end); + } + +folio_unlock (folio); +folio_put (folio); +} +
While the fix is probably correct, I'm not happy with it.
Why does pagecache_isize_extended() not handle blocksize >= PAGE_SIZE?
Where does straddling folio zeroing happen for other filesystems?
Is it done by the buffer head code? Is it done by iomap? What
about cases that use neither?
Thanks,
Miklos
Hello!
Quoted 103 lines · Miklos Szeredi
Quoted 89 lines · Jimmy Zuber
Extending a fuse file past a non-page-aligned EOF does not zero the tail of the old last page. If that page is cached and was dirtied beyond the old EOF -- e.g. an application mmap()ed the EOF page and stored into the region past EOF, which is undefined until the file grows -- the now in-bounds tail is exposed to subsequent reads as stale data instead of zeros, in violation of POSIX file-extension semantics.
Other filesystems zero this via
pagecache_isize_extended(), but that helper is a no-op for fuse: it returns early wheni_blocksize()>=PAGE_SIZE, and a non-fuseblk fuse mount hass_blocksize==PAGE_SIZE(the server-suppliedst_blksizeonly setsfi->cached_i_blkbits, noti_blkbits). The NFS client hit the same problem and open-codes the zeroing innfs_truncate_last_folio(); add the equivalentfuse_zero_partial_eof_folio()and call it from the three paths that extend a file: a buffered write, a size-extending setattr/truncate, and a size-extending fallocate (fuse_write_update_attr(),fuse_do_setattr()andfuse_file_fallocate()).
writeback_cacheconnections are unaffected, as their writes go throughiomap_file_buffered_write(), which zeroes post-EOF folios. The bug is observable on a non-writeback_cache server that returnsFOPEN_KEEP_CACHEon writable files (withoutFOPEN_DIRECT_IO), and is caught by the newwrite_extend_eoffuse selftest.Signed-off-by Jimmy Zuber <[email protected]>--- fs/fuse/dir.c | 3 +++ fs/fuse/file.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/fuse_i.h | 1 + 3 files changed, 60 insertions(+)fs/fuse/dir.c +3
-0@@ -2282,6 +2282,9 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,*/ if ((is_truncate || !is_wb) &&S_ISREG (inode->i_mode) && oldsize != outarg.attr.size) { +if (outarg.attr.size > oldsize) +fuse_zero_partial_eof_folio (inode, oldsize, + outarg.attr.size);truncate_pagecache (inode, outarg.attr.size);invalidate_inode_pages2 (mapping); }fs/fuse/file.c +30
-0@@ -21,6 +21,8 @@#include <linux/splice.h> #include <linux/task_io_accounting_ops.h> #include <linux/iomap.h> +#include <linux/highmem.h> +#include <linux/rmap.h> static int fuse_send_open(struct fuse_mount *fm, u64 nodeid, unsigned int open_flags, int opcode,@@ -1200,20 +1202,64 @@ static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,return err ?: ia->write.out.size; } +/* +* An operation extended i_size past a non-folio-aligned old EOF at @from, +* turning [@from, @to) into a hole that must read back as zero. If the old +* last folio is cached and was dirtied beyond the old EOF (e.g. mmap stores +* into the post-EOF region, which are undefined until the file grows), zero +* that tail so it is not exposed as stale data (xfstests generic/363). +* +* pagecache_isize_extended() cannot be used: it bails out for +* i_blocksize() >= PAGE_SIZE, and a non-fuseblk mount has +* s_blocksize == PAGE_SIZE, so the zeroing has to be done here. +* Callers hold i_rwsem, serialising this against concurrent writes and +* truncates; it must not run under fi->lock, as it locks the folio. +*/ +void fuse_zero_partial_eof_folio (struct inode *inode,loff_t from,loff_t to) +{ +struct folio *folio; +size_t offset, end; + +if (from >= to) +return ; + + folio =filemap_lock_folio (inode->i_mapping, from >>PAGE_SHIFT ); +if (IS_ERR (folio)) +return ; + +if (folio_mkclean (folio)) +folio_mark_dirty (folio); +fs/fuse/file.c static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos, · line 1242
Quoted 10 lines
fs/fuse/file.c +10
-0+if (folio_test_dirty (folio)) { + offset =offset_in_folio (folio, from); + end =min_t (loff_t, to -folio_pos (folio),folio_size (folio)); +folio_zero_segment (folio, offset, end); + } + +folio_unlock (folio); +folio_put (folio); +} +While the fix is probably correct, I'm not happy with it.
Quoted 1 line
Why does
pagecache_isize_extended()not handle blocksize >=PAGE_SIZE?
Because originally it didn't do any zeroing. It was only used to writeprotect the tail page so that the filesystem could allocate missing blocks under mmaped page after i_size extension which makes sense only when blocksize < pagesize. Then Brian added the zeroing in commit 52aecaee1c264 ("mm: zero range of eof folio exposed by inode size extension") mostly because it was a convenient place where to add it. For standard disk-based filesystems the zeroing is needed only when bs < PAGE_SIZE because the zeroing of the tail block gets handled by the filesystem itself in a different place.
Quoted 1 line
Where does straddling folio zeroing happen for other filesystems?
It is generally done by the filesystem. E.g. for extending writes XFS zeroes from xfs_file_write_checks() -> xfs_file_write_zero_eof(), ext4 from ext4_write_checks() -> ext4_block_zero_eof(). Similarly for extending truncate e.g. ext4_setattr() calls ext4_block_zero_eof().
I agree the zeroing is kind of messy and could use some standardization (especially because it's a corner case people rarely think about and as a result we had several issues in this area over the years in various filesystems). But it's a bit tricky because of fs-specific needs (locking, journalling) for the tail block update and also because this is somewhat performance sensitive path so you don't want to do too much needless work due to abstraction.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH 2/2 selftests/fuse: test post-EOF page zeroing when a file is extended
In reply to Jimmy ZuberAdd a regression test for the bug where extending a file left the tail of the old partial EOF page exposing stale mmap-dirtied data instead of zeros.
The test is a self-contained raw /dev/fuse server (no libfuse dependency) that runs without writeback_cache and returns FOPEN_KEEP_CACHE, the configuration in which the bug is visible. Its backing data is always zero in the hole, so any non-zero byte a read sees is stale page-cache data. All offsets are relative to the runtime page size. Four cases:
- write_extend: pollute the post-EOF tail, extend past it by writing into
a later page, and verify the tail reads back as zero;
- ftruncate_extend: same, but extend via ftruncate();
- fallocate_extend: same, but extend via fallocate() at the old EOF;
- extend_into_eof_page_preserves_data: an extending write landing inside the old EOF page must not be clobbered by the zeroing.
Each case fails without the fix and passes with it.
--- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 3 + .../filesystems/fuse/write_extend_eof_test.c | 368 ++++++++++++++++++ 3 files changed, 372 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only fuse_mnt fusectl_test +write_extend_eof_test
tools/testing/selftests/filesystems/fuse/Makefile +3 -0
@@ -3,10 +3,13 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test +TEST_GEN_PROGS += write_extend_eof_test TEST_GEN_FILES := fuse_mnt include ../../lib.mk +$(OUTPUT)/write_extend_eof_test: LDLIBS += -lpthread + VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null) ifeq ($(VAR_CFLAGS),) VAR_CFLAGS := -D_FILE_OFFSET_BITS=64 -I/usr/include/fuse
tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c new file +368 -0
368 lines not on this page · raw message
Signature · 1 line
2.50.1