Linux block layer
 help / color / mirror / Atom feed
* [PATCH] blk-mq: fix null-ptr-deref in blk_mq_run_hw_queues
@ 2026-12-02 15:13 Peiyang He
  2026-12-02 17:41 ` Bart Van Assche
  2026-12-02 22:48 ` Jens Axboe
  0 siblings, 2 replies; 3+ messages in thread
From: Peiyang He @ 2026-12-02 15:13 UTC (permalink / raw)
  To: axboe; +Cc: ming.lei, fengnanchang, linux-block, stable, linux-kernel,
	Peiyang He

blk_mq_run_hw_queues() iterates over q->queue_hw_ctx via
queue_for_each_hw_ctx() without synchronizing against concurrent
__blk_mq_update_nr_hw_queues(). While the queue is frozen,
__blk_mq_realloc_hw_ctxs() can set entries in q->queue_hw_ctx to NULL
when shrinking nr_hw_queues or when hctx reallocation fails. A
concurrent caller such as blk_mq_unquiesce_queue() can then get a NULL
hctx from queue_hctx() and dereference hctx->state in
blk_mq_hctx_stopped(), causing a NULL pointer dereference.

Acquire q->q_usage_counter via percpu_ref_tryget() in
blk_mq_run_hw_queues() and release it with blk_queue_exit(), similar
to blk_mq_queue_tag_busy_iter(). This serializes the hctx iteration
with queue freezing and hctx reallocation. If the queue has been fully
frozen and q_usage_counter has reached zero, blk_mq_run_hw_queues()
returns early without accessing q->queue_hw_ctx.

Fixes: d0c98769ee7d ("blk-mq: use array manage hctx map instead of xarray")
Cc: stable@vger.kernel.org
Signed-off-by: Peiyang He <peiyang_he@smail.nju.edu.cn>
---
Found by our research tool based on Syzkaller when fuzzing the nbd driver;
we manually write the PoC, confirm this bug exists in the latest Linux
and the fix is valid:

BUG: KASAN: null-ptr-deref in instrument_atomic_read include/linux/instrumented.h:82 [inline]
BUG: KASAN: null-ptr-deref in _test_bit include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline]
BUG: KASAN: null-ptr-deref in blk_mq_hctx_stopped block/blk-mq.h:242 [inline]
BUG: KASAN: null-ptr-deref in blk_mq_run_hw_queues+0x1bd/0x390 block/blk-mq.c:2426
Read of size 8 at addr 0000000000000050 by task syz.0.132/9244

CPU: 1 UID: 0 PID: 9244 Comm: syz.0.132 Not tainted 7.1.0 #14 PREEMPT(full) 
Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 kasan_report+0x117/0x150 mm/kasan/report.c:595
 check_region_inline mm/kasan/generic.c:-1 [inline]
 kasan_check_range+0x3c7/0x3f0 mm/kasan/generic.c:200
 instrument_atomic_read include/linux/instrumented.h:82 [inline]
 _test_bit include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline]
 blk_mq_hctx_stopped block/blk-mq.h:242 [inline]
 blk_mq_run_hw_queues+0x1bd/0x390 block/blk-mq.c:2426
 nbd_clear_que drivers/block/nbd.c:1054 [inline]
 nbd_clear_sock+0xb6/0x170 drivers/block/nbd.c:1428
 nbd_clear_sock_ioctl drivers/block/nbd.c:1575 [inline]
 __nbd_ioctl drivers/block/nbd.c:1603 [inline]
 nbd_ioctl+0x341/0xe60 drivers/block/nbd.c:1663
 blkdev_ioctl+0x5e8/0x750 block/ioctl.c:797
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]
 __se_sys_ioctl+0x101/0x170 fs/ioctl.c:583
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0x15f/0x560 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f5c289a6dcd
Code: 04 25 28 00 00 00 48 89 45 c8 31 c0 48 8d 45 10 c7 45 b0 10 00 00 00 48 89 45 b8 48 8d 45 d0 48 89 45 c0 b8 10 00 00 00 0f 05 <89> c2 3d 00 f0 ff ff 77 1a 48 8b 45 c8 64 48 2b 04 25 28 00 00 00
RSP: 002b:00007f5c273f70a0 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007f5c2979ffe0 RCX: 00007f5c289a6dcd
RDX: 0000000000000000 RSI: 000000000000ab04 RDI: 0000000000000008
RBP: 00007f5c273f70f0 R08: 0000000000000000 R09: 00007f5c273f76c0
R10: 0000000000000000 R11: 0000000000000246 R12: 00007f5c273f76c0
R13: ffffffffffffffb0 R14: 000000000000000e R15: 00007f5c2979fdd0
 </TASK>
==================================================================

 block/blk-mq.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index 54ccfc43c27c..bf129a6a77b5 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -2412,6 +2412,14 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async)
 	struct blk_mq_hw_ctx *hctx, *sq_hctx;
 	unsigned long i;
 
+	/*
+	 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and queue_hw_ctx
+	 * while the queue is frozen. So we can use q_usage_counter to avoid
+	 * racing with it.
+	 */
+	if (!percpu_ref_tryget(&q->q_usage_counter))
+		return;
+
 	sq_hctx = NULL;
 	if (blk_queue_sq_sched(q))
 		sq_hctx = blk_mq_get_sq_hctx(q);
@@ -2427,6 +2435,7 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async)
 		    !list_empty_careful(&hctx->dispatch))
 			blk_mq_run_hw_queue(hctx, async);
 	}
+	blk_queue_exit(q);
 }
 EXPORT_SYMBOL(blk_mq_run_hw_queues);
 

base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 22:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 15:13 [PATCH] blk-mq: fix null-ptr-deref in blk_mq_run_hw_queues Peiyang He
2026-12-02 17:41 ` Bart Van Assche
2026-12-02 22:48 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox