[PATCH v5 5/6] ntfs: serialize the mmap write fault initialized size update
From: Hongling Zeng
Date: Tue Dec 01 2026 - 06:14:12 EST
The mmap write fault path checks whether the faulting range extends
past ni->initialized_size with an unlocked read and then calls
ntfs_attr_set_initialized_size(), which rewrites the field and the
MFT record with its argument unconditionally. The call is made
without the MFT record lock that every other caller of the function
holds, and a buffered write or a second fault extending the
initialized size between the read and the rewrite can make the
rewrite shrink the field again, which exposes initialized data as
uninitialized.
Hold the MFT record lock across the check and the update and read
initialized_size under size_lock inside the critical section, so the
decision is made on the value that is current for the record and the
field can no longer regress through the fault path.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hongling Zeng <zenghongling@xxxxxxxxxx>
Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
fs/ntfs/attrib.c | 1 +
fs/ntfs/iomap.c | 15 +++++++++++----
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 77ee1972ef68..27aeaa66da29 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2431,6 +2431,7 @@ int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)
return ret;
}
+/* The caller must hold the base inode's mft record lock. */
int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
{
struct ntfs_attr_search_ctx *ctx;
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 39a31855e389..de41fed5f6c8 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -669,10 +669,17 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
return err;
}
- if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
- iomap->offset + iomap->length > ni->initialized_size) {
- err = ntfs_attr_set_initialized_size(ni,
- iomap->offset + iomap->length);
+ if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
+ loff_t initialized_size;
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
+ if (iomap->offset + iomap->length > initialized_size)
+ err = ntfs_attr_set_initialized_size(ni,
+ iomap->offset + iomap->length);
+ mutex_unlock(&mrec_ni->mrec_lock);
}
return err;
--
2.25.1