Skip to contentSkip to conversation

exfat: zero invalid tail on partial overwrites

PATCH In review Namjae Jeon 2 participants 3 messages Oct 7 – Oct 8 exFAT file system

lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

PATCH 1/1 exfat: zero invalid tail on partial overwrites

Buffered overwrite that ends before valid_size in the same filesystem block reads the entire block without IOMAP_F_ZERO_TAIL. If an existing cluster contains stale bytes beyond valid_size, the synchronous read loads them into the page cache and marks the block uptodate. Subsequent buffered reads can return these bytes while i_size exceeds valid_size.

For example, with 512-byte blocks, valid_size of 1000 and i_size of 2048, a cold-cache overwrite of [900, 950) exposes stale data in [1000, 1024). Setting ZERO_TAIL for this write would also erase valid data in [950, 1000).

Use iomap_truncate_page() to zero only the invalid tail at valid_size before preparing such buffered overwrites. Call it after dropping s_lock and before iomap locks the write folio. The inode lock held by buffered writes serializes valid_size against truncation and page_mkwrite.

Skip direct I/O, fault mappings and new allocations. Writes that reach valid_size continue to use IOMAP_F_ZERO_TAIL.

---
 fs/exfat/iomap.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
fs/exfat/iomap.c +21 -1
index 291367e10c1e..bec0b35b9420 100644
@@ -149,7 +149,27 @@ static int exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 static int exfat_write_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
 {
-	return __exfat_iomap_begin(inode, offset, length, flags, iomap, true);
+	struct exfat_inode_info *ei = EXFAT_I(inode);
+	loff_t valid_size = exfat_get_valid_size(ei);
+	loff_t end;
+	int err;
+
+	err = __exfat_iomap_begin(inode, offset, length, flags, iomap, true);
+	if (err || flags & (IOMAP_DIRECT | IOMAP_FAULT) ||
+	    iomap->flags & IOMAP_F_NEW)
+		return err;
+
+	end = iomap->offset + iomap->length;
+	if (end >= valid_size ||
+	    round_up(end, i_blocksize(inode)) <= valid_size)
+		return 0;
+
+	/*
+	 * Zero the invalid bytes from valid_size to the end of the block
+	 * to prevent stale data from being exposed through the page cache.
+	 */
+	return iomap_truncate_page(inode, valid_size, NULL, &exfat_iomap_ops,
+				   NULL, NULL);
 }
 
 static DEFINE_IOMAP_ITER_NEXT(exfat_iomap_next, exfat_iomap_begin);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Namjae Jeon

Buffered overwrite that ends before valid_size in the same filesystem block reads the entire block without IOMAP_F_ZERO_TAIL. If an existing cluster contains stale bytes beyond valid_size, the synchronous read
loads them into the page cache and marks the block uptodate.
Subsequent buffered reads can return these bytes while i_size exceeds
valid_size.

Quoted 8 lines

For example, with 512-byte blocks, valid_size of 1000 and i_size of 2048, a cold-cache overwrite of [900, 950) exposes stale data in [1000, 1024). Setting ZERO_TAIL for this write would also erase valid data in [950, 1000).

Use iomap_truncate_page() to zero only the invalid tail at valid_size before preparing such buffered overwrites. Call it after dropping s_lock and before iomap locks the write folio. The inode lock held by buffered
writes serializes valid_size against truncation and page_mkwrite.

I’m not saying that this patch will introduce a bug. I think it would be easier to handle if we perform the zeroing in iomap_write_ops.put_folio.

Also, we may remove the inode lock from exfat_page_mkwrite soon. Once it’s removed, we won’t be able to use valid_size without holding the folio lock.

Thanks.

author maintainer
#2 lore raw
Quoted 14 lines · Chi Zhiling
Quoted 12 lines · Namjae Jeon

Buffered overwrite that ends before valid_size in the same filesystem block reads the entire block without IOMAP_F_ZERO_TAIL. If an existing cluster contains stale bytes beyond valid_size, the synchronous read loads them into the page cache and marks the block uptodate. Subsequent buffered reads can return these bytes while i_size exceeds valid_size.

For example, with 512-byte blocks, valid_size of 1000 and i_size of 2048, a cold-cache overwrite of [900, 950) exposes stale data in [1000, 1024). Setting ZERO_TAIL for this write would also erase valid data in [950, 1000).

Quoted 7 lines
Quoted 4 lines

Use iomap_truncate_page() to zero only the invalid tail at valid_size before preparing such buffered overwrites. Call it after dropping s_lock and before iomap locks the write folio. The inode lock held by buffered writes serializes valid_size against truncation and page_mkwrite.

I’m not saying that this patch will introduce a bug. I think it would be easier to handle if we perform the zeroing in iomap_write_ops.put_folio.

Christoph did not want to handle them in ->put_folio. That is why IOMAP_F_ZERO_TAIL was created. ntfs also handles this in put_folio, but I recently submit a patch to change it to handle it like exfat.

Quoted 4 lines

Also, we may remove the inode lock from exfat_page_mkwrite soon. Once it’s removed, we won’t be able to use valid_size without holding the folio lock.

Okay, Thanks!

Quoted 4 lines

Thanks.