[PATCH 2/2] i3c: dw: check the response data length against the receive buffer
Maoyi Xie
maoyixie.tju at gmail.com
Thu Nov 19 04:26:37 PST 2026
dw_i3c_master_end_xfer_locked() reads as many bytes from the RX FIFO as
the data length of the response says. It does not compare this length
with the read length of the command. With a longer length, it writes
past cmd->rx_buf. Data for a command without rx_buf, such as a write or
DAA, is written through a NULL pointer.
Skip such a response and fail the transfer with -EIO, as for a bad TID.
A response with an error status still sets cmd->rx_len, unless the
length is above the size of rx_buf. DAA reads the number of unassigned
addresses from cmd->rx_len.
Found with a CodeQL-based static analysis tool that we are developing.
These cases reproduce in a userspace harness under AddressSanitizer and
UBSan, which report none of them 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 at vger.kernel.org
Signed-off-by: Maoyi Xie <maoyixie.tju at gmail.com>
---
I used an LLM to help write the harness and this patch.
drivers/i3c/master/dw-i3c-master.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c
index 1e0fdebf58d654c..895cbded5b8129c 100644
--- a/drivers/i3c/master/dw-i3c-master.c
+++ b/drivers/i3c/master/dw-i3c-master.c
@@ -478,7 +478,7 @@ 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, tid;
+ u32 resp, tid, len;
resp = readl(master->regs + RESPONSE_QUEUE_PORT);
@@ -492,8 +492,18 @@ static void dw_i3c_master_end_xfer_locked(struct dw_i3c_master *master, u32 isr)
}
cmd = &xfer->cmds[tid];
- cmd->rx_len = RESPONSE_PORT_DATA_LEN(resp);
+ len = RESPONSE_PORT_DATA_LEN(resp);
cmd->error = RESPONSE_PORT_ERR_STATUS(resp);
+ if (len && !cmd->error && (!cmd->rx_buf || len > cmd->rx_len)) {
+ dev_err_ratelimited(&master->base.dev,
+ "response len=%u, rx_len=%u\n",
+ len, cmd->rx_len);
+ ret = -EIO;
+ continue;
+ }
+
+ if (!cmd->rx_buf || len <= cmd->rx_len)
+ cmd->rx_len = len;
if (cmd->rx_len && !cmd->error)
dw_i3c_master_read_rx_fifo(master, cmd->rx_buf,
cmd->rx_len);
--
2.34.1
More information about the linux-i3c
mailing list