[PATCH 3/6] ntfs: read initialized_size under size_lock in the iomap paths
From: Hongling Zeng
Date: Wed Nov 25 2026 - 02:36:20 EST
The iomap read paths read ni->initialized_size without size_lock.
ntfs_read_iomap_begin_non_resident() reads it twice in the length
adjustment condition and once more in round_up(), so a concurrent
update in between lets the condition pass with one value while the
length is computed from another, which can yield a length matching
neither boundary.
Read initialized_size once under size_lock in the resident clamp,
the non-resident unwritten mapping, the length adjustment and
ntfs_iomap_put_folio_non_resident(), so each decision is made on one
value.
The buffered write and direct I/O paths read initialized_size
unlocked too, when deciding whether the head or the tail of a newly
allocated cluster has to be zeroed first. The mmap write fault path
can extend initialized_size while holding neither the inode lock nor
the MFT record lock, so snapshot the field under size_lock there as
well.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hongling Zeng <zenghongling@xxxxxxxxxx>
---
fs/ntfs/iomap.c | 50 +++++++++++++++++++++++++++++++++++++++----------
1 file changed, 40 insertions(+), 10 deletions(-)
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 8933f432aa68..afc4494822e1 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -17,11 +17,14 @@ static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
{
struct ntfs_inode *ni = NTFS_I(inode);
unsigned long sector_size = 1UL << inode->i_blkbits;
+ unsigned long flags;
loff_t start_down, end_up, init;
start_down = round_down(pos, sector_size);
end_up = (pos + len - 1) | (sector_size - 1);
+ read_lock_irqsave(&ni->size_lock, flags);
init = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
if (init >= start_down && init <= end_up) {
if (init < pos) {
@@ -88,6 +91,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
loff_t i_size;
u32 attr_len;
int err = 0;
+ unsigned long irqflags;
char *kattr;
base_ni = ntfs_base_inode(ni);
@@ -106,8 +110,10 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
goto out;
attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
+ read_lock_irqsave(&ni->size_lock, irqflags);
if (unlikely(attr_len > ni->initialized_size))
attr_len = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
i_size = i_size_read(inode);
if (unlikely(attr_len > i_size)) {
@@ -200,6 +206,7 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
struct ntfs_volume *vol = ni->vol;
loff_t vcn_ofs;
loff_t rl_length;
+ unsigned long irqflags;
vcn = ntfs_bytes_to_cluster(vol, offset);
vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset);
@@ -231,7 +238,12 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
} else {
- if (need_unwritten && offset >= ni->initialized_size)
+ loff_t initialized_size;
+
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
+ if (need_unwritten && offset >= initialized_size)
iomap->type = IOMAP_UNWRITTEN;
else
iomap->type = IOMAP_MAPPED;
@@ -255,11 +267,17 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
up_write(&ni->runlist.lock);
if (!(flags & IOMAP_ZERO) &&
- iomap->type == IOMAP_MAPPED &&
- iomap->offset < ni->initialized_size &&
- iomap->offset + iomap->length > ni->initialized_size) {
- iomap->length = round_up(ni->initialized_size, 1 << inode->i_blkbits) -
- iomap->offset;
+ iomap->type == IOMAP_MAPPED) {
+ loff_t initialized_size;
+
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
+ if (iomap->offset < initialized_size &&
+ iomap->offset + iomap->length > initialized_size) {
+ iomap->length = round_up(initialized_size, 1 << inode->i_blkbits) -
+ iomap->offset;
+ }
}
iomap->flags |= IOMAP_F_MERGED;
@@ -383,6 +401,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
struct runlist_element *rl, *rlc;
bool is_retry = false;
int err = 0, map_err = 0;
+ unsigned long flags;
s64 vcn, lcn;
s64 max_clu_count =
ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
@@ -511,9 +530,14 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
iomap->flags = IOMAP_F_NEW;
if (lcn <= LCN_HOLE) {
loff_t end = offset + length;
+ loff_t initialized_size;
+
+ read_lock_irqsave(&ni->size_lock, flags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
- end < ni->initialized_size)) {
+ end < initialized_size)) {
loff_t z_start, z_end;
z_start = vcn << vol->cluster_size_bits;
@@ -527,7 +551,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
if ((!err || err == -EPERM) &&
max_clu_count > 1 &&
(iomap->length & vol->cluster_size_mask &&
- end < ni->initialized_size)) {
+ end < initialized_size)) {
loff_t z_start, z_end;
z_start = (vcn + max_clu_count - 1) <<
@@ -581,6 +605,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
s64 vcn, start_lcn, lcn_count;
bool balloc = false, update_mp;
int err;
+ unsigned long irqflags;
s64 max_clu_count =
ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
@@ -622,16 +647,21 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
if (flags & IOMAP_DIRECT ||
ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
loff_t end = offset + length;
+ loff_t initialized_size;
+
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
- end < ni->initialized_size))
+ end < initialized_size))
err = ntfs_dio_zero_range(inode,
start_lcn <<
vol->cluster_size_bits,
vol->cluster_size);
if (!err && lcn_count > 1 &&
(iomap->length & vol->cluster_size_mask &&
- end < ni->initialized_size))
+ end < initialized_size))
err = ntfs_dio_zero_range(inode,
(start_lcn + lcn_count - 1) <<
vol->cluster_size_bits,
--
2.25.1