ublk: fix null-ptr-deref in ublk_queue_cmd
Subsystems (2)
PATCH 1/1 ublk: fix null-ptr-deref in ublk_queue_cmd
ublk_queue_cmd() dereferences ios[tag].cmd without NULL check. The cmd pointer can be NULL when ublk_cancel_cmd() races with IO dispatch during server teardown:
CPU0 (partition scan work) CPU1 (io_uring cancel callback)
ublk_queue_rq()
ublk_prep_req() -> OK
check canceling -> false
ublk_start_cancel()
quiesce, set canceling, unquiesce
ublk_cancel_cmd()
io->cmd = NULL
ublk_queue_cmd()
cmd = ios[tag].cmd -> NULL
ublk_get_uring_cmd_pdu(cmd) -> null-ptr-derefThe race window exists because ublk_cancel_cmd() can execute between the canceling flag check and the cmd dereference in ublk_queue_cmd(). This cannot be closed with simple synchronization since blk_mq_quiesce_queue only waits for in-flight dispatches, not requests already past the canceling check.
Fix by checking cmd for NULL before dereferencing. When NULL, abort the request via __ublk_abort_rq() which handles both recovery (requeue) and non-recovery (end with IOERR) cases.
--- drivers/block/ublk_drv.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +7 -1
@@ -2090,8 +2090,14 @@ static void ublk_batch_queue_cmd(struct ublk_queue *ubq, struct request *rq, boostatic void ublk_queue_cmd (struct ublk_queue *ubq,struct request *rq) {struct io_uring_cmd *cmd = ubq->ios[rq->tag].cmd; -struct ublk_uring_cmd_pdu *pdu =ublk_get_uring_cmd_pdu (cmd); +struct ublk_uring_cmd_pdu *pdu; + +if (unlikely (!cmd)) { +__ublk_abort_rq (ubq, rq); +return ; + } + pdu =ublk_get_uring_cmd_pdu (cmd); pdu->req = rq;io_uring_cmd_complete_in_task (cmd, ublk_cmd_tw_cb); }
Signature · 1 line
2.43.0
Ming Lei
maintainerQuoted 23 lines · Yun Zhou
ublk_queue_cmd()dereferences ios[tag].cmd without NULL check. The cmd pointer can be NULL whenublk_cancel_cmd()races with IO dispatch during server teardown:CPU0 (partition scan work) CPU1 (
io_uringcancel callback)
ublk_queue_rq()ublk_prep_req() -> OK check canceling -> false ublk_start_cancel() quiesce, set canceling, unquiesce ublk_cancel_cmd() io->cmd = NULL ublk_queue_cmd() cmd = ios[tag].cmd -> NULL ublk_get_uring_cmd_pdu(cmd) -> null-ptr-derefThe race window exists because
ublk_cancel_cmd()can execute between the canceling flag check and the cmd dereference inublk_queue_cmd(). This cannot be closed with simple synchronization sinceblk_mq_quiesce_queueonly waits for in-flight dispatches, not requests already past the canceling check.
Quoted 8 lines
Fix by checking cmd for NULL before dereferencing. When NULL, abort the request via
__ublk_abort_rq()which handles both recovery (requeue) and non-recovery (end with IOERR) cases.Fixes 71f28f3136af ("ublk_drv: add io_uring based userspace block driver")Reported-by [email protected]Signed-off-by Yun Zhou <[email protected]>
Hello Yun,
It should have been addressed by the following commit:
Thanks,
Ming Lei