Skip to contentSkip to conversation

fs/btrfs: init csum_list before possible free

PATCH In review Dan Robertson 3 participants 6 messages Feb 19, 2019 – Feb 22, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/btrfs: init csum_list before possible free

The scrub_ctx csum_list member must be initialized before scrub_free_ctx is called. If the csum_list is not initialized beforehand, the list_empty call in scrub_free_csums will result in a null deref.

---
 fs/btrfs/scrub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/scrub.c +1 -1
index 6dcd36d7b849..1e08ad1fe2aa 100644
@@ -584,6 +584,7 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
 	sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
 	sctx->curr = -1;
 	sctx->fs_info = fs_info;
+	INIT_LIST_HEAD(&sctx->csum_list);
 	for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
 		struct scrub_bio *sbio;
 
@@ -608,7 +609,6 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
 	atomic_set(&sctx->workers_pending, 0);
 	atomic_set(&sctx->cancel_req, 0);
 	sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
-	INIT_LIST_HEAD(&sctx->csum_list);
 
 	spin_lock_init(&sctx->list_lock);
 	spin_lock_init(&sctx->stat_lock);
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Dan Robertson

The scrub_ctx csum_list member must be initialized before scrub_free_ctx is called. If the csum_list is not initialized beforehand, the list_empty call in scrub_free_csums will result in a null deref.

Quoted 25 lines
---
 fs/btrfs/scrub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/scrub.c +1 -1
index 6dcd36d7b849..1e08ad1fe2aa 100644
@@ -584,6 +584,7 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
 	sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
 	sctx->curr = -1;
 	sctx->fs_info = fs_info;
+	INIT_LIST_HEAD(&sctx->csum_list);
 	for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
 		struct scrub_bio *sbio;
 
@@ -608,7 +609,6 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
 	atomic_set(&sctx->workers_pending, 0);
 	atomic_set(&sctx->cancel_req, 0);
 	sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
-	INIT_LIST_HEAD(&sctx->csum_list);
 
 	spin_lock_init(&sctx->list_lock);
 	spin_lock_init(&sctx->stat_lock);
#3 lore raw
Quoted 1 line

Thanks, it's been there since the first merge of scrub, but hard to hit.

Yeah, very hard to hit. Debated submitting a patch, but the fix seemed simple enough, so figured I'd send one in.

Side note: I noticed most of the time ENOMEM is hit in fs/btrfs BUG or BUG_ON is called. Would it be better (more in line with the fs/btrfs style) to call BUG or BUG_ON e.g. BUG_ON(!sbio) instead of goto nomem at fs/btrfs/scrub.c:592?

Cheers,

Dan

#4 lore raw
Quoted 8 lines · Dan Robertson
Quoted 1 line

Thanks, it's been there since the first merge of scrub, but hard to hit.

Yeah, very hard to hit. Debated submitting a patch, but the fix seemed simple enough, so figured I'd send one in.

Side note: I noticed most of the time ENOMEM is hit in fs/btrfs BUG or BUG_ON is called. Would it be better (more in line with the fs/btrfs style) to call BUG or BUG_ON e.g. BUG_ON(!sbio) instead of goto nomem at fs/btrfs/scrub.c:592?

No, we want to get rid of the BUG_ONs as a poor substitute for error handling. The ENOMEM case is usually a hard one to fix so you still can see a lot of them, but there are places where it's simple enough to rollback and return ENOMEM up to the original caller. Scrub in this case is restartable and if scrub_setup_ctx fails, it's at the time of start and the user is properly notified.