Skip to contentSkip to conversation

exfat: fix bitmap lock ordering and inode cache reinit

PATCH New Breno Rodrigues Alves 1 participant 2 messages Oct 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover exfat: fix bitmap lock ordering and inode cache reinit

Two correctness fixes for fs/exfat:

- exfat_alloc_cluster() checks used_clusters and updates clu_srch_ptr before taking bitmap_lock, allowing concurrent allocations to observe an inconsistent bitmap and potentially double-allocate clusters. Moving the lock to the top and routing early errors through the existing 'unlock' label closes the window.

- exfat_alloc_inode() relied on exfat_inode_init_once() to seed the cache_lru fields, but slab objects are reused without re-running the constructor. Reinitializing the fields on every allocation prevents a crash in __exfat_cache_inval_inode() when an inode is recycled.

Breno Rodrigues Alves (1):
exfat: fix bitmap lock ordering and inode cache reinitialization

 fs/exfat/fatent.c | 13 ++++++++-----
 fs/exfat/super.c |  5 +++++
 2 files changed, 13 insertions(+), 5 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/1 exfat: fix bitmap lock ordering and inode cache reinitialization

Two correctness fixes for the exFAT allocator and inode cache.

1) exfat_alloc_cluster() checks sbi->used_clusters and updates sbi->clu_srch_ptr before acquiring sbi->bitmap_lock. Concurrent allocations can therefore observe an inconsistent bitmap state and double-allocate clusters. Take the lock before the checks and route the early error paths through the existing 'unlock' label.

2) exfat_alloc_inode() relies on exfat_inode_init_once() to seed the cache_lru fields, but slab objects are reused without re-running the constructor. A stale cache_valid_id or a non-empty cache_lru list can crash __exfat_cache_inval_inode() when an inode is recycled. Reinitialize the fields on every allocation.

Tested with 5000 create/delete cycles on a loop-mounted 100M exFAT image; fsck.exfat reports clean afterwards and dmesg shows no exFAT-related warnings.

Fixes 31023864e67a ("exfat: add fat entry operations")
Fixes 719c1e182916 ("exfat: add super block operations")
---
 fs/exfat/fatent.c | 13 ++++++++-----
 fs/exfat/super.c  |  5 +++++
 2 files changed, 13 insertions(+), 5 deletions(-)
fs/exfat/fatent.c +8 -5
index a8b11e2ce43f..7dcb2819849d 100644
@@ -427,19 +427,22 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
 	struct super_block *sb = inode->i_sb;
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 
+	mutex_lock(&sbi->bitmap_lock);
+
 	total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
 
 	if (unlikely(total_cnt < sbi->used_clusters)) {
 		exfat_fs_error_ratelimit(sb,
 			"%s: invalid used clusters(t:%u,u:%u)\n",
 			__func__, total_cnt, sbi->used_clusters);
-		return -EIO;
+		ret = -EIO;
+		goto unlock;
 	}
 
-	if (num_alloc > total_cnt - sbi->used_clusters)
-		return -ENOSPC;
-
-	mutex_lock(&sbi->bitmap_lock);
+	if (num_alloc > total_cnt - sbi->used_clusters) {
+		ret = -ENOSPC;
+		goto unlock;
+	}
 
 	hint_clu = p_chain->dir;
 	/* find new cluster */
fs/exfat/super.c +5 -0
index a9ea36ba2693..2d142bf68b63 100644
@@ -195,6 +195,11 @@ static struct inode *exfat_alloc_inode(struct super_block *sb)
 	if (!ei)
 		return NULL;
 
+	spin_lock_init(&ei->cache_lru_lock);
+	ei->nr_caches = 0;
+	ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
+	INIT_LIST_HEAD(&ei->cache_lru);
+
 	return &ei->vfs_inode;
 }
 
Signature · 1 line
2.56.0