fs/ntfs3: don't prepare private mappings for writing
Subsystems (2)
PATCH 1/1 fs/ntfs3: don't prepare private mappings for writing
From: Zhan Xusheng <[email protected]>
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.
---
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(-)
fs/ntfs3/file.c +4 -2
@@ -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
Signature · 1 line
2.43.0