Skip to contentSkip to conversation

i3c: dw: check the response TID against the command count

PATCH New Maoyi Xie 1 participant 2 messages Nov 19

Subsystems (3)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

PATCH 1/2 i3c: dw: check the response TID against the command count

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")

---
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(-)
drivers/i3c/master/dw-i3c-master.c +12 -3
index 34aa8f8e1a0a26d..1e0fdebf58d654c 100644
@@ -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

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/2 i3c: dw: check the response data length against the receive buffer

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")

---
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(-)
drivers/i3c/master/dw-i3c-master.c +12 -2
index 1e0fdebf58d654c..895cbded5b8129c 100644
@@ -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);
Signature · 1 line
2.34.1