Skip to contentSkip to conversation

bcache: fix uninitialized closure object

PATCH In review [email protected] 2 participants 2 messages Apr 22 – Apr 23

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

PATCH 1/1 bcache: fix uninitialized closure object

From: Mingzhe Zou <[email protected]>

In the previous patch ("bcache: fix cached_dev.sb_bio use-after-free and crash"), we adopted a simple modification suggestion from AI to fix the use-after-free.

But in actual testing, we found an extreme case where the device is stopped before calling bch_write_bdev_super().

At this point, struct closure sb_write has not been initialized yet. For this patch, we ensure that sb_bio has been completed via sb_write_mutex.

Fixes fec114a98b87 ("bcache: fix cached_dev.sb_bio use-after-free and crash")
---
 drivers/md/bcache/super.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/bcache/super.c +2 -1
index 6627a381f65a..97d9adb0bf96 100644
@@ -1376,11 +1376,12 @@ static CLOSURE_CALLBACK(cached_dev_free)
 	/*
 	 * Wait for any pending sb_write to complete before free.
 	 * The sb_bio is embedded in struct cached_dev, so we must
 	 * ensure no I/O is in progress.
 	 */
-	closure_sync(&dc->sb_write);
+	down(&dc->sb_write_mutex);
+	up(&dc->sb_write_mutex);
 
 	if (dc->sb_disk)
 		folio_put(virt_to_folio(dc->sb_disk));
 
 	if (dc->bdev_file)
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 45 lines · On Wed, Apr 22, 2026 at 11:21:13PM +0800, [email protected] wrote:

From: Mingzhe Zou <[email protected]>

In the previous patch ("bcache: fix cached_dev.sb_bio use-after-free and crash"), we adopted a simple modification suggestion from AI to fix the use-after-free.

But in actual testing, we found an extreme case where the device is stopped before calling bch_write_bdev_super().

At this point, struct closure sb_write has not been initialized yet. For this patch, we ensure that sb_bio has been completed via sb_write_mutex.

Fixes fec114a98b87 ("bcache: fix cached_dev.sb_bio use-after-free and crash")
---
 drivers/md/bcache/super.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/bcache/super.c +2 -1
index 6627a381f65a..97d9adb0bf96 100644
@@ -1376,11 +1376,12 @@ static CLOSURE_CALLBACK(cached_dev_free)
 	/*
 	 * Wait for any pending sb_write to complete before free.
 	 * The sb_bio is embedded in struct cached_dev, so we must
 	 * ensure no I/O is in progress.
 	 */
-	closure_sync(&dc->sb_write);
+	down(&dc->sb_write_mutex);
+	up(&dc->sb_write_mutex);
 
 	if (dc->sb_disk)
 		folio_put(virt_to_folio(dc->sb_disk));
 
 	if (dc->bdev_file)
Signature · 1 line
2.47.3

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>