[PATCH] fs/ntfs3: accept $Bad covering the last partial cluster

From: Zhan Xusheng

Date: Thu Nov 19 2026 - 02:03:32 EST


From: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>

ntfs3 refuses a volume that Windows, chkdsk and the ntfs driver accept:

ntfs3: ino=8, mi_enum_attr
ntfs3: Mark volume as dirty due to NTFS errors
ntfs3: Failed to load $BadClus (-22)

The volume has 64 KiB clusters and 7337721855 sectors of 512 bytes,
one sector short of a cluster multiple, so sbi->used.bitmap.nbits is
57325951. Its $BadClus:$Bad covers 57325952 clusters: evcn is 57325951
and alloc_size is 512 bytes over sbi->volume.size. mi_enum_attr()
rejects both values.

To reproduce, run mkntfs -c 65536 on a 256 MiB image (524287 sectors,
4095 whole clusters), then extend the $Bad run to 4096 clusters and set
evcn, alloc_size and data_size to match, in both $MFT and $MFTMirr.
ntfsfix accepts the image; ntfs3 does not mount it.

Compare alloc_size and evcn with the volume size rounded up to a whole
cluster. run_unpack() still checks real LCNs against nbits, so only a
sparse run can reach the extra cluster.

Reported-by: Jonathan Pitre <jakeld@xxxxxxxxx>
Closes: https://lore.kernel.org/all/CADjMKxV2F-vgD6kVoo58j83p9Cc-xn1U_L6T7V5Y+FNNHaGUNQ@xxxxxxxxxxxxxx/
Fixes: c4a8ba334262 ("fs/ntfs3: Add rough attr alloc_size check")
Fixes: 20fd9f64c005 ("fs/ntfs3: reject out-of-range evcn in mi_enum_attr()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
---
Tested in a VM on 7.4-rc3 (20eef29b7c84) with images forged as above,
64K and 4K clusters: before, mount fails with -EINVAL; after, mount,
write, fill to ENOSPC and remount work, and ntfsfix -n stays clean. An
image whose $Bad extends one cluster further is still rejected.

fs/ntfs3/record.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
index d7785f9e8e09..22c5a2c56064 100644
--- a/fs/ntfs3/record.c
+++ b/fs/ntfs3/record.c
@@ -317,10 +317,10 @@ struct ATTRIB *mi_enum_attr(struct ntfs_inode *ni, struct mft_inode *mi,
* since "(u64)-1 + 1" is 0 and "0 > 0" is false.
*
* For a non-empty attribute evcn is a cluster index and must lie
- * within the volume (sbi->used.bitmap.nbits, set up in
- * ntfs_init_from_boot() before any caller of mi_enum_attr() runs).
- * Bounding evcn also prevents a malformed value close to U64_MAX
- * from slipping through the near-wrap "evcn + 1" upper bound.
+ * within the volume rounded up to a whole cluster: $BadClus:$Bad
+ * may cover the partial cluster at the end. Bounding evcn also
+ * prevents a malformed value close to U64_MAX from slipping through
+ * the near-wrap "evcn + 1" upper bound.
*/
svcn = le64_to_cpu(attr->nres.svcn);
evcn = le64_to_cpu(attr->nres.evcn);
@@ -338,7 +338,9 @@ struct ATTRIB *mi_enum_attr(struct ntfs_inode *ni, struct mft_inode *mi,
#endif
} else {
/* Check out of volume for normal attribute. */
- if (evcn != U64_MAX && evcn >= mi->sbi->used.bitmap.nbits)
+ if (evcn != U64_MAX &&
+ evcn >= ntfs_up_cluster(mi->sbi, mi->sbi->volume.size) >>
+ mi->sbi->cluster_bits)
goto out;
}

@@ -370,7 +372,7 @@ struct ATTRIB *mi_enum_attr(struct ntfs_inode *ni, struct mft_inode *mi,
if (attr->nres.c_unit)
goto out;

- if (alloc_size > mi->sbi->volume.size)
+ if (alloc_size > ntfs_up_cluster(mi->sbi, mi->sbi->volume.size))
goto out;
}


base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
--
2.43.0