mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ntfs: zero up to a page that a shared mapping makes writable
@ 2026-11-30  3:02 Zhan Xusheng
  0 siblings, 0 replies; only message in thread
From: Zhan Xusheng @ 2026-11-30  3:02 UTC (permalink / raw)
  To: linkinjeon, hyc.lee
  Cc: liubaolin12138, ernest.borowski.git, ntfs, linux-kernel,
	zhanxusheng, stable

From: Zhan Xusheng <zhanxusheng@xiaomi.com>

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.

Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
---
Tested in a VM on ntfs-next 8b42a85768e9, with all free space filled
with a known pattern first.  The example above, bytes of old data in
[0, 64K) after remount:

                   ntfs-next  patched
  4K clusters      0          0
  64K clusters     61440      0

The 4K written first and the bytes stored at 2M are kept in both.  A
pwrite() of one byte at 2M instead of the store gives 0 with and without
the patch.  Shared mappings made writable with mprotect(), the killed
and stalled writer cases, fsx (buffered and O_DIRECT), insert/collapse
range and the partial O_DIRECT cases give the same results with and
without the patch.  No lockdep or KASAN reports.

This only runs for a page past the initialized size.
ntfs_file_mmap_prepare() already moves it to the end of the mapping, so
that takes a file grown after mmap().

 fs/ntfs/file.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 612498b45fbb..d0da022d0860 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -993,6 +993,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;
 
@@ -1011,6 +1012,20 @@ 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 = 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.
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30  3:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30  3:02 [PATCH] ntfs: zero up to a page that a shared mapping makes writable Zhan Xusheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®