libfs: do not extend i_size when no data was copied
Subsystems (2)
PATCH 1/1 libfs: do not extend i_size when no data was copied
simple_write_end() updates i_size to pos + copied even when copied is zero. A failed buffered write past EOF can therefore extend a ramfs file without writing any data.
Since prefaulting was moved to the slow write path, this is reproducible with a one-byte pwrite() at offset 4096 into an empty ramfs file using an unreadable source buffer. The call fails with EFAULT but leaves the file size at 4096. generic_perform_write() calls ->write_end() with copied == 0 before trying to fault in the source buffer.
Only extend i_size when data was copied. Keep the existing folio handling so that zero-progress retries and successful writes retain their current behavior.
--- fs/libfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/libfs.c +1 -1
@@ -1007,7 +1007,7 @@ static int simple_write_end(const struct kiocb *iocb,* No need to use i_size_read() here, the i_size * cannot change under us because we hold the i_rwsem. */ -if (last_pos > inode->i_size) +if (copied && last_pos > inode->i_size)i_size_write (inode, last_pos);folio_mark_dirty (folio);
Signature · 1 line
2.43.0
Quoted 10 lines · Hungyu Lin
simple_write_end()updatesi_sizeto pos + copied even when copied is zero. A failed buffered write past EOF can therefore extend a ramfs file without writing any data.Since prefaulting was moved to the slow write path, this is reproducible with a one-byte
pwrite()at offset 4096 into an empty ramfs file using an unreadable source buffer. The call fails with EFAULT but leaves the file size at 4096.generic_perform_write()calls ->write_end() with copied == 0 before trying to fault in the source buffer.
Quoted 7 lines
Only extend
i_sizewhen data was copied. Keep the existing folio handling so that zero-progress retries and successful writes retain their current behavior.Fixes 665575cff098 ("filemap: move prefaulting out of hot write path")Signed-off-by Hungyu Lin <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 20 lines
--- fs/libfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/libfs.c +1
-1@@ -1007,7 +1007,7 @@ static int simple_write_end(const struct kiocb *iocb,* No need to use i_size_read() here, the i_size * cannot change under us because we hold the i_rwsem. */ -if (last_pos > inode->i_size) +if (copied && last_pos > inode->i_size)i_size_write (inode, last_pos);folio_mark_dirty (folio);Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 11 lines · Hungyu Lin
simple_write_end()updatesi_sizeto pos + copied even when copied is zero. A failed buffered write past EOF can therefore extend a ramfs file without writing any data.Since prefaulting was moved to the slow write path, this is reproducible with a one-byte
pwrite()at offset 4096 into an empty ramfs file using an unreadable source buffer. The call fails with EFAULT but leaves the file size at 4096.generic_perform_write()calls ->write_end() with copied == 0 before trying to fault in the source buffer.[...]
Applied to the vfs-7.5.misc branch of the vfs/vfs.git tree. Patches in the vfs-7.5.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.5.misc
[1/1] libfs: do not extend i_size when no data was copied
https://git.kernel.org/vfs/vfs/c/4a1edc38b617