From: Bilal Hashim <AeeSec@126.com>
To: Damien Le Moal <dlemoal@kernel.org>,
Christoph Hellwig <hch@lst.de>, Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] zloop: fix double-free and use-after-free in zloop_ctl_add()
Date: Wed, 02 Dec 2026 01:52:19 +0800 [thread overview]
Message-ID: <179614753959.27176.11073306063101756949@126.com> (raw)
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@vger.kernel.org
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
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:
next reply other threads:[~2026-12-01 17:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 17:52 Bilal Hashim [this message]
2026-12-01 18:55 ` Bilal Hashim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179614753959.27176.11073306063101756949@126.com \
--to=aeesec@126.com \
--cc=axboe@kernel.dk \
--cc=dlemoal@kernel.org \
--cc=hch@lst.de \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®