dw_i3c_master_end_xfer_locked() uses the 4-bit TID of each response as an index into xfer->cmds[] without a check. For a TID at or above xfer->ncmds, the stores to cmd->rx_len and cmd->error write past the array. If that response has data and no error, the driver copies the FIFO data to a cmd->rx_buf pointer that it reads from past the array. The error scan runs to nresp, the number of responses that the controller reports. It reads past the array when nresp is above ncmds. Skip a response with such a TID, fail the transfer with -EIO and let the existing error path reset the queues. Scan the ncmds commands for errors. Found with a CodeQL-based static analysis tool that we are developing. Both cases reproduce in a userspace harness under AddressSanitizer, which reports neither with this patch. Not tested on hardware, only compile-tested with W=1 on next-20261116. Fixes: 1dd728f5d4d4 ("i3c: master: Add driver for Synopsys DesignWare IP") Cc: stable@vger.kernel.org Signed-off-by: Maoyi Xie --- I used an LLM to help write the harness and this patch. drivers/i3c/master/dw-i3c-master.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c index 34aa8f8e1a0a26d..1e0fdebf58d654c 100644 --- a/drivers/i3c/master/dw-i3c-master.c +++ b/drivers/i3c/master/dw-i3c-master.c @@ -478,11 +478,20 @@ static void dw_i3c_master_end_xfer_locked(struct dw_i3c_master *master, u32 isr) for (i = 0; i < nresp; i++) { struct dw_i3c_cmd *cmd; - u32 resp; + u32 resp, tid; resp = readl(master->regs + RESPONSE_QUEUE_PORT); - cmd = &xfer->cmds[RESPONSE_PORT_TID(resp)]; + tid = RESPONSE_PORT_TID(resp); + if (tid >= xfer->ncmds) { + dev_err_ratelimited(&master->base.dev, + "response tid=%u, ncmds=%u\n", + tid, xfer->ncmds); + ret = -EIO; + continue; + } + + cmd = &xfer->cmds[tid]; cmd->rx_len = RESPONSE_PORT_DATA_LEN(resp); cmd->error = RESPONSE_PORT_ERR_STATUS(resp); if (cmd->rx_len && !cmd->error) @@ -490,7 +499,7 @@ static void dw_i3c_master_end_xfer_locked(struct dw_i3c_master *master, u32 isr) cmd->rx_len); } - for (i = 0; i < nresp; i++) { + for (i = 0; i < xfer->ncmds; i++) { switch (xfer->cmds[i].error) { case RESPONSE_NO_ERROR: break; base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea -- 2.34.1