mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhan Xusheng <zhanxusheng1024@gmail.com>
To: linkinjeon@kernel.org, hyc.lee@gmail.com
Cc: liubaolin12138@163.com, ernest.borowski.git@proton.me,
	ntfs@lists.linux.dev, linux-kernel@vger.kernel.org,
	zhanxusheng@xiaomi.com, stable@vger.kernel.org
Subject: [PATCH v2] ntfs: zero up to a page that a shared mapping makes writable
Date: Tue,  1 Dec 2026 20:51:26 +0800	[thread overview]
Message-ID: <20261201125126.1651071-1-zhanxusheng1024@gmail.com> (raw)

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.  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
---
v2: check the file size under the invalidate lock, as a truncate
between the fault and the zeroing left the initialized size past the
end of the file (Baolin).  The check is in
ntfs_extend_initialized_size(), so it also covers
ntfs_file_mmap_prepare(), which reads the file size without the lock as
well; that path is not tested.

v1: https://lore.kernel.org/all/20261130030220.1100209-1-zhanxusheng1024@gmail.com/

Tested in a VM on ntfs-next 329758f9d73b, with all free space filled
with a known pattern first.  The example above, bytes of old data in
[0, 64K) after remount:

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

With a test-only msleep() before the zeroing, the store at 2M waiting
there and the file shrunk to 64K meanwhile, the store gets SIGBUS in
both versions.  After remount, v1 leaves "initialized size 2097152,
data size 65536" and the inode cannot be read; v2 leaves 65536 for
both.

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.

 fs/ntfs/file.c  | 15 +++++++++++++++
 fs/ntfs/inode.c |  9 +++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 31ff35176e05..213a867dd2a4 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,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.
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 3aac1d7d310f..84762691046e 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -2647,10 +2647,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;
 }
-- 
2.43.0


             reply	other threads:[~2026-12-01 12:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:51 Zhan Xusheng [this message]
2026-12-01 13:23 ` liubaolin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201125126.1651071-1-zhanxusheng1024@gmail.com \
    --to=zhanxusheng1024@gmail.com \
    --cc=ernest.borowski.git@proton.me \
    --cc=hyc.lee@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liubaolin12138@163.com \
    --cc=ntfs@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=zhanxusheng@xiaomi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®