Re: [PATCH 5/6] ntfs: serialize the mmap write fault initialized size update

From: liubaolin

Date: Wed Nov 25 2026 - 09:10:48 EST




在 2026/11/25 15:35, Hongling Zeng 写道:
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>
---
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 081ce3e829f2..56df3049a386 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 afc4494822e1..39772e9e5f32 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -676,10 +676,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;

Looks good to me.

Reviewed-by: Baolin Liu <liubaolin@xxxxxxxxxx>