Skip to contentSkip to conversation

bcachefs: fix invalid memory access in bch2_fs_alloc() error path

PATCH In review Thomas Bertschinger 2 participants 2 messages Dec 14, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: fix invalid memory access in bch2_fs_alloc() error path

When bch2_fs_alloc() gets an error before calling bch2_fs_btree_iter_init(), bch2_fs_btree_iter_exit() makes an invalid memory access because btree_trans_list is uninitialized.

Fixes 6bd68ec266ad ("bcachefs: Heap allocate btree_trans")
---
 fs/bcachefs/btree_iter.c | 8 ++++++--
 fs/bcachefs/btree_iter.h | 1 +
 fs/bcachefs/super.c      | 1 +
 3 files changed, 8 insertions(+), 2 deletions(-)
fs/bcachefs/btree_iter.c +6 -2
index f6c41a8924dd..569a087f6a65 100644
@@ -3142,10 +3142,9 @@ void bch2_fs_btree_iter_exit(struct bch_fs *c)
 	mempool_exit(&c->btree_trans_pool);
 }
 
-int bch2_fs_btree_iter_init(struct bch_fs *c)
+void bch2_fs_btree_iter_init_early(struct bch_fs *c)
 {
 	struct btree_transaction_stats *s;
-	int ret;
 
 	for (s = c->btree_transaction_stats;
 	     s < c->btree_transaction_stats + ARRAY_SIZE(c->btree_transaction_stats);
@@ -3156,6 +3155,11 @@ int bch2_fs_btree_iter_init(struct bch_fs *c)
 
 	INIT_LIST_HEAD(&c->btree_trans_list);
 	seqmutex_init(&c->btree_trans_lock);
+}
+
+int bch2_fs_btree_iter_init(struct bch_fs *c)
+{
+	int ret;
 
 	c->btree_trans_bufs = alloc_percpu(struct btree_trans_buf);
 	if (!c->btree_trans_bufs)
fs/bcachefs/btree_iter.h +1 -0
index 90e54545afff..a9277a1564b3 100644
@@ -876,6 +876,7 @@ unsigned bch2_trans_get_fn_idx(const char *);
 void bch2_btree_trans_to_text(struct printbuf *, struct btree_trans *);
 
 void bch2_fs_btree_iter_exit(struct bch_fs *);
+void bch2_fs_btree_iter_init_early(struct bch_fs *);
 int bch2_fs_btree_iter_init(struct bch_fs *);
 
 #endif /* _BCACHEFS_BTREE_ITER_H */
fs/bcachefs/super.c +1 -0
index 893b52eda519..3a72df8a8334 100644
@@ -769,6 +769,7 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
 
 	bch2_fs_copygc_init(c);
 	bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
+	bch2_fs_btree_iter_init_early(c);
 	bch2_fs_btree_interior_update_init_early(c);
 	bch2_fs_allocator_background_init(c);
 	bch2_fs_allocator_foreground_init(c);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 6 lines · Thomas Bertschinger

When bch2_fs_alloc() gets an error before calling bch2_fs_btree_iter_init(), bch2_fs_btree_iter_exit() makes an invalid memory access because btree_trans_list is uninitialized.

Fixes 6bd68ec266ad ("bcachefs: Heap allocate btree_trans")

Perfectly follows the conventions for this code, nice!

Applied :)