From: Zhan Xusheng A page fault that makes a page of a shared mapping writable moves the initialized size to the end of that page. If the page starts past the initialized size, nothing zeroes the range in between, and the allocated tail of the cluster holding the old initialized size becomes readable with whatever the disk held before. This needs clusters larger than a page. For example, with 64K clusters, write 4K to a new file, map it 4M MAP_SHARED, grow it to 4M with ftruncate() and store 8 bytes at 2M through the mapping. After msync() and a remount, [4K, 64K) reads back the old contents of the cluster. Zero the gap with ntfs_extend_initialized_size() before the fault takes the invalidate lock, as a write past the initialized size does, and write the zeroes back before the initialized size covers them, or an O_DIRECT read that has already flushed the page cache reads the gap from disk. A truncate can shrink the file between the fault and the zeroing, so have ntfs_extend_initialized_size() check the file size under that lock, or the initialized size ends up past the end of the file. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Signed-off-by: Zhan Xusheng --- v3: - Write the zeroes back before the initialized size covers them; a direct read that flushed the page cache before the zeroing read the gap from disk (Namjae). - Fail the fault on a shut-down volume before zeroing (Sashiko). - Not changed: truncate_pagecache_range() in __ntfs_extend_initialized_size() drops the faulting folio, so the fault is retried once (Sashiko). The retry reads the page again and finds the initialized size at the page, so it does not extend again. - Not changed either: a direct read of the faulting page itself can still see disk contents, as iomap_page_mkwrite() moves the initialized size to the end of the page in ->iomap_begin() before it dirties the folio. This is the same without the patch. - Baolin's Reviewed-by on v2 is dropped, as the code changed. v2: https://lore.kernel.org/all/20261201125126.1651071-1-zhanxusheng1024@gmail.com/ v1: https://lore.kernel.org/all/20261130030220.1100209-1-zhanxusheng1024@gmail.com/ Tested in a VM on ntfs-next 660cb7022966 with 64K clusters, all free space filled with a known pattern first. Bytes of old data read: ntfs-next v3 the example above, [0, 64K) after remount 61440 0 direct read of [4K, 64K) racing the store 61440 0 direct read of [2M, 2M + 4K) racing it 4096 4096 The races use a test-only 300ms sleep in ->iomap_begin() of the direct read, 3 runs each; v2 reads the same 61440 bytes as ntfs-next from [4K, 64K). With 4K clusters the example reads 0 in both. The truncate race of v2 still leaves 65536 for both sizes. mmap, fsx (buffered and O_DIRECT), insert/collapse range, aio, partial O_DIRECT and the short write cases give the same results with and without the patch. No lockdep or KASAN reports. fs/ntfs/file.c | 17 +++++++++++++++++ fs/ntfs/inode.c | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index 31ff35176e05..1289fc9a18e2 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -1177,6 +1177,7 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf) struct inode *inode = file_inode(vmf->vma->vm_file); struct address_space *mapping = inode->i_mapping; bool stream = ntfs_inode_is_named_stream(NTFS_I(inode)); + loff_t pos; int err; vm_fault_t ret; @@ -1195,6 +1196,22 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf) file_update_time(vmf->vma->vm_file); } + /* + * Making the page writable moves the initialized size up to its end. + * If the page starts past the initialized size, zero what lies between + * first, as a write there would, or the tail of the last initialized + * cluster becomes readable with whatever the disk held. + */ + pos = (loff_t)vmf->pgoff << PAGE_SHIFT; + if (NInoNonResident(NTFS_I(inode)) && + pos > NTFS_I(inode)->initialized_size) { + err = -EIO; + if (!NVolShutdown(NTFS_I(inode)->vol)) + err = ntfs_extend_initialized_size(inode, pos, pos); + if (err) + goto out_error; + } + /* * Serialize against truncate/fallocate which hold the lock * exclusively while invalidating pagecache and changing extents. diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c index 3aac1d7d310f..209f340f6dfb 100644 --- a/fs/ntfs/inode.c +++ b/fs/ntfs/inode.c @@ -2634,6 +2634,16 @@ static int __ntfs_extend_initialized_size(struct inode *vi, &ntfs_iomap_folio_ops, NULL); if (err) goto out; + /* + * A direct read that has already flushed the page cache + * would read the gap from disk once the initialized size + * covers it: write the zeroes back first. + */ + err = filemap_write_and_wait_range(vi->i_mapping, + old_init_size, + offset - 1); + if (err) + goto out; } } @@ -2647,10 +2657,15 @@ static int __ntfs_extend_initialized_size(struct inode *vi, int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, const loff_t new_size) { - int err; + int err = 0; filemap_invalidate_lock(vi->i_mapping); - err = __ntfs_extend_initialized_size(vi, offset, new_size); + /* + * A page fault or mmap() looks at the file size without the lock, so + * a truncate may have shrunk the file since: don't initialize past it. + */ + if (new_size <= i_size_read(vi)) + err = __ntfs_extend_initialized_size(vi, offset, new_size); filemap_invalidate_unlock(vi->i_mapping); return err; } base-commit: 660cb70229665c241b4ea5537491d7cd01c25ad8 -- 2.43.0