[PATCH] zloop: fix double-free and use-after-free in zloop_ctl_add()

From: Bilal Hashim

Date: Tue Dec 01 2026 - 12:53:49 EST


In zloop_ctl_add(), after blk_mq_alloc_disk() succeeds and
zlo->disk->fops is set to &zloop_fops (whose .free_disk callback is
zloop_free_disk()), if blk_revalidate_disk_zones() or add_disk() fails,
the error path jumps to out_cleanup_disk and calls put_disk(zlo->disk).

put_disk() drops the last reference on the gendisk and invokes
zloop_free_disk(), which frees zlo->tag_set, puts every zone->file and
zlo->data_dir, destroys zlo->workqueue, frees zlo->base_dir, and frees
zlo via kvfree(). Execution then falls through into out_cleanup_tags,
out_close_files, out_free_base_dir, out_destroy_workqueue, out_free_idr,
and out_free_dev, dereferencing and freeing all of these objects a second
time (use-after-free and double-free).

In addition, on the success path, zloop_ctl_add() sets zlo->state to
Zlo_live and drops zloop_ctl_mutex before logging zlo fields via
pr_info(). A concurrent zloop_ctl_remove() can remove the device and
free zlo via put_disk() before pr_info() executes, causing a
use-after-free.

Fix both issues by:
1. Removing zlo->id from zloop_index_idr under zloop_ctl_mutex before
calling put_disk(zlo->disk) at out_cleanup_disk, and jumping directly
to out so zloop_free_disk() performs the sole cleanup.
2. Moving the pr_info() calls before transitioning zlo->state to
Zlo_live.

Found by manual code inspection. Verified in a 4-vCPU QEMU guest with
CONFIG_BLK_DEV_ZONED_LOOP=y, CONFIG_PROVE_LOCKING=y, and
CONFIG_DEBUG_ATOMIC_SLEEP=y across normal add/IO/remove and concurrent
add/remove rounds with zero lockdep, RCU, or sleeping-in-atomic warnings.

Fixes: eb0570c7df23 ("block: new zoned loop block device driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Bilal Hashim <AeeSec@xxxxxxx>
---
drivers/block/zloop.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 9666674..f5a7fca 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -1360,10 +1360,6 @@ static int zloop_ctl_add(struct zloop_options *opts)
goto out_cleanup_disk;
}

- mutex_lock(&zloop_ctl_mutex);
- WRITE_ONCE(zlo->state, Zlo_live);
- mutex_unlock(&zloop_ctl_mutex);
-
pr_info("zloop: device %d, %u zones of %llu MiB, %u B block size\n",
zlo->id, zlo->nr_zones,
((sector_t)zlo->zone_size << SECTOR_SHIFT) >> 20,
@@ -1373,10 +1369,18 @@ static int zloop_ctl_add(struct zloop_options *opts)
zlo->ordered_zone_append ? "ordered " : "",
zlo->zone_append ? "native" : "emulated");

+ mutex_lock(&zloop_ctl_mutex);
+ WRITE_ONCE(zlo->state, Zlo_live);
+ mutex_unlock(&zloop_ctl_mutex);
+
return 0;

out_cleanup_disk:
+ mutex_lock(&zloop_ctl_mutex);
+ idr_remove(&zloop_index_idr, zlo->id);
+ mutex_unlock(&zloop_ctl_mutex);
put_disk(zlo->disk);
+ goto out;
out_cleanup_tags:
blk_mq_free_tag_set(&zlo->tag_set);
out_close_files: