mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhan Xusheng <zhanxusheng1024@gmail.com>
To: almaz.alexandrovich@paragon-software.com
Cc: ntfs3@lists.linux.dev, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, zhanxusheng@xiaomi.com
Subject: [PATCH] fs/ntfs3: don't prepare private mappings for writing
Date: Thu, 19 Nov 2026 23:12:59 +0800	[thread overview]
Message-ID: <20261119151259.3965463-1-zhanxusheng1024@gmail.com> (raw)

From: Zhan Xusheng <zhanxusheng@xiaomi.com>

ntfs_file_mmap_prepare() allocates the clusters of a sparse file and
zeroes the file up to the end of the mapping when the mapping has
VMA_WRITE.  A MAP_PRIVATE mapping never writes the file, but it gets
the same treatment, so any user who can read a file can make ntfs3
write to the volume, also when it is mounted read-only.

With -o ro, a MAP_PRIVATE mapping with PROT_WRITE by uid 1000 of a
4 MiB sparse file zeroes 4 MiB on the device and marks 1024 clusters
used in the bitmap.  The runlist is not updated, so the clusters are
lost.  The same mapping of a 4 MiB file with 4096 initialized bytes
writes 4190208 bytes of zeroes.  On a writable mount, it allocates all
clusters of a sparse file that the user cannot write.

Only prepare shared writable mappings.

Fixes: 4342306f0f0d ("fs/ntfs3: Add file operations and implementation")
Cc: stable@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
Tested in a VM on 7.4-rc3 (20eef29b7c84): with the patch, both read-only
cases leave the device unchanged and the sparse file is not allocated.
fsx with mmap on 4K and 64K cluster volumes and on sparse and compressed
files gives the same results as before.

 fs/ntfs3/file.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs3/file.c b/fs/ntfs3/file.c
index 6c84a3e1c7fe..3da8b65793e1 100644
--- a/fs/ntfs3/file.c
+++ b/fs/ntfs3/file.c
@@ -388,6 +388,8 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
 	struct inode *inode = file_inode(file);
 	struct ntfs_inode *ni = ntfs_i(inode);
 	const bool rw = vma_desc_test(desc, VMA_WRITE_BIT);
+	/* A private mapping never writes the file. */
+	const bool shared_rw = rw && vma_desc_test(desc, VMA_SHARED_BIT);
 	int err;
 
 	/* Avoid any operation if inode is bad. */
@@ -417,7 +419,7 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
 		file->f_ra.ra_pages = 0;
 	}
 
-	if (rw) {
+	if (shared_rw) {
 		u64 from = (u64)desc->pgoff << PAGE_SHIFT;
 		u64 to = min_t(u64, i_size_read(inode),
 			       from + vma_desc_size(desc));
@@ -452,7 +454,7 @@ static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
 	}
 
 	err = generic_file_mmap_prepare(desc);
-	if (!err && rw)
+	if (!err && shared_rw)
 		desc->vm_ops = &ntfs_file_vm_ops;
 out:
 	return err;

base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
-- 
2.43.0


                 reply	other threads:[~2026-11-19 15:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261119151259.3965463-1-zhanxusheng1024@gmail.com \
    --to=zhanxusheng1024@gmail.com \
    --cc=almaz.alexandrovich@paragon-software.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ntfs3@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®