From: Ernest Borowski Since commit 20fd9f64c005 ("fs/ntfs3: reject out-of-range evcn in mi_enum_attr()"), mi_enum_attr() refuses a non-resident attribute segment whose end vcn lies past the end of the volume unless the segment itself is flagged sparse or compressed. Only the first segment reliably carries those flags, though. ntfs-3g copies the attribute's current flags into a new segment and, when the attribute later becomes sparse, updates only the first one; the ntfs driver creates every later segment with flags 0. A file that was written densely until it needed an attribute list and was then extended past a hole therefore has a sparse first segment and normal later segments whose vcns exceed the number of clusters of the volume, and ntfs3 now marks such a file bad: ntfs3(loop0): ino=7016, mi_enum_attr and every read returns -EINVAL, although ntfs-3g, the ntfs driver and ntfsck handle the file fine. Keep the volume bound for the first segment of a normal attribute. A later segment that is not flagged sparse or compressed may still belong to a sparse attribute, so bound its end vcn by the largest sparse file (sbi->maxbytes_sparse) instead. That still rejects a malformed evcn close to U64_MAX, also with CONFIG_NTFS3_64BIT_CLUSTER, so the "evcn + 1" wraparound stays defused. Reproduced on a 64 MiB volume with 512-byte clusters and fragmented free space: ntfs-3g writes 2 MiB densely into a new file (three MFT records) and then one byte at 200 MiB. Fixes: 20fd9f64c005 ("fs/ntfs3: reject out-of-range evcn in mi_enum_attr()") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs3/record.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c index d7785f9e8e..7669db5314 100644 --- a/fs/ntfs3/record.c +++ b/fs/ntfs3/record.c @@ -316,9 +316,12 @@ struct ATTRIB *mi_enum_attr(struct ntfs_inode *ni, struct mft_inode *mi, * clusters) and must be accepted: "svcn > evcn + 1" tolerates it, * 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). + * For a non-empty attribute evcn is a cluster index. The first + * segment of a normal attribute must lie within the volume + * (sbi->used.bitmap.nbits, set up in ntfs_init_from_boot() before + * any caller of mi_enum_attr() runs); later segments only by the + * largest sparse file, sparse or compressed attributes only by the + * width of a cluster number. * Bounding evcn also prevents a malformed value close to U64_MAX * from slipping through the near-wrap "evcn + 1" upper bound. */ @@ -336,6 +339,17 @@ struct ATTRIB *mi_enum_attr(struct ntfs_inode *ni, struct mft_inode *mi, if (evcn != U64_MAX && evcn >= (1ull << 32)) goto out; #endif + } else if (svcn) { + /* + * Later segment. Only the first segment reliably says whether + * the attribute is sparse. ntfs-3g and the ntfs driver leave + * the flags of segments created before the attribute became + * sparse as they were, so the vcns may lie past the end of the + * volume, but not past the largest sparse file. + */ + if (evcn > ((mi->sbi->maxbytes_sparse - 1) >> + mi->sbi->cluster_bits)) + goto out; } else { /* Check out of volume for normal attribute. */ if (evcn != U64_MAX && evcn >= mi->sbi->used.bitmap.nbits) -- 2.56.1