From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.7 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796147626; b=hC2fuR+ZmmyrgotqetHDOes01udygXokCIC0aDRWnL3LW346OynjqgdvLzIJQA/FGKh+ /wJgoo/63fZMVLxMaW2rXGG3gm4au1CBJo6aiQ0vWVEQMsKGFDR5vdJXm/7s2FYbXNUlg xjzftrrdfI0FKbotndW68WYI4ImdP2GdgFlZRiFYXzft3TkiE4Ny1pMYdxMEUjZ3yspkn RpE7NQEkBETeYWLqgGL9+ze4WN/ukAsLRGSQHY6y+C5bu80QaqGPiuHvaCcpkz/eRhyIC g48nDbUw7exdOcodIlEuv12r0T8zsEC3wSUVBdt5KLTOVt7AP3TEKEgOx92Bp6dvHkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147626; h=DMARC-Filter:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID: Content-Type:Content-Transfer-Encoding:MIME-Version:X-CM-TRANSID: X-Coremail-Antispam:X-CM-SenderInfo; bh=V4XeJt7nlopOa68Q6v1jmt01h1mZINUV0iW54lGmduc=; b=dDZZ90F9P/hxPaN2UamHd1t9GASqtUZ5a93AMaydmNlFwMcBG+Fx+Jz+XgqLyuM6bRmO ZEJWTG9OL26GZEKYDMI/v68bU06hb2bL7q0wlNzQ/Nwpw30Lm5Qpt7/rvQ7aJtzjBjXHf 5v6dIuGBvIiGvi3Rzv+sCRP4iBEw+3EmKkF+bG0jTVUEmyYTzyqV8M5Qrbb4w4gVWsW+I jzyUXdFnpsC7H90ICwrj+V7i/wEQXFRxTvfhIBycsO5w7F+72eQqoEHlRIdUB3rLyLzm0 nDcJxWpJrtcD2LNhiBo68TMXkT6LOZ5KbjOFBGIZPRSeGtoBD+KxaCQ1UT1X08NMi9g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=126.com; spf=pass smtp.mailfrom=126.com; dkim=pass header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=Frpe+ocB; arc=none smtp.remote-ip=220.197.31.7 Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65B38606A31A; Tue, 1 Dec 2026 17:53:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 65B38606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=126.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=126.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=Frpe+ocB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:To:Subject:Date:Message-ID:Content-Type: MIME-Version; bh=V4XeJt7nlopOa68Q6v1jmt01h1mZINUV0iW54lGmduc=; b=Frpe+ocBHF3wYUt1Dy5XlcnW5c92jdbTYq2n3yq4MQZuthjGJEbBEm87CV84pC seT6x3FVEEB5OiV9cTFcJhyd9d1GpwyfrwJwAkIV1mH4ueNCufltwm6OFA8qUWv4 7uAm/S10yyteuXFuvUgpuhPDVTynEzLU8jFQnI6EdSMe8= From: Bilal Hashim To: Damien Le Moal , Christoph Hellwig , Jens Axboe 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 Message-ID: <179614753959.27176.11073306063101756949@126.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-CM-TRANSID:_____wD3F2RVCQ9rYPQ1Dw--.12754S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZFWkJFyfGFy7tFy3KFWktFb_yoW5Aw47pF 4YgFyUKFWrta1kXF47Zw4xuFykKa18Wa97G348Cw1a9FyYyFsIvFWIvFy3WryI9r93ArWq vFyDXw17WF1jk37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07ULyCXUUUUU= X-CM-SenderInfo: 5dhh2vbf6rjloofrz/xtbBnwH4k2sPCWEnGAAA3G 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 --- 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: