Skip to contentSkip to conversation

hw/i3c/dw-i3c: Fix uninitialized data use in short transfer

PATCH Applied Jamin Lin 3 participants 4 messages Mar 11 – Mar 17

lore
Share & tools
lore

Jamin Lin

author
#0 lore raw

PATCH 1/1 hw/i3c/dw-i3c: Fix uninitialized data use in short transfer

Coverity reports that dw_i3c_short_transfer() may pass an uninitialized buffer to dw_i3c_send().

The immediate cause is the use of data[len] += arg.byte0, which reads from an uninitialized element of the buffer. Replace this with a simple assignment.

Additionally, avoid calling dw_i3c_send() when the constructed payload length is zero. In that case the transfer has no data phase, so the controller can transition to the idle state directly.

This resolves the Coverity UNINIT warning and clarifies the handling of zero-length short transfers.

Resolves: Coverity CID 1645555

---
 hw/i3c/dw-i3c.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
hw/i3c/dw-i3c.c +10 -4
index 3d8b95a14c..aa6c27c8de 100644
@@ -1211,7 +1211,7 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
          * ignored.
          */
         if (cmd.dbp) {
-            data[len] += arg.byte0;
+            data[len] = arg.byte0;
             len++;
         }
     }
@@ -1226,10 +1226,16 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
         len++;
     }
 
-    if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
-        err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+    if (len > 0) {
+        if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
+            err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+        } else {
+            /* Only go to an idle state on a successful transfer. */
+            ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
+                             DW_I3C_TRANSFER_STATE_IDLE);
+        }
     } else {
-        /* Only go to an idle state on a successful transfer. */
+        /* No payload bytes for this short transfer. */
         ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
                          DW_I3C_TRANSFER_STATE_IDLE);
     }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jamin Lin

Coverity reports that dw_i3c_short_transfer() may pass an uninitialized buffer to dw_i3c_send().

The immediate cause is the use of data[len] += arg.byte0, which reads from an uninitialized element of the buffer. Replace this with a simple assignment.

Additionally, avoid calling dw_i3c_send() when the constructed payload length is zero. In that case the transfer has no data phase, so the controller can transition to the idle state directly.

Quoted 5 lines

This resolves the Coverity UNINIT warning and clarifies the handling of zero-length short transfers.

Resolves: Coverity CID 1645555

Quoted 37 lines
---
  hw/i3c/dw-i3c.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
hw/i3c/dw-i3c.c +10 -4
index 3d8b95a14c..aa6c27c8de 100644
@@ -1211,7 +1211,7 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
           * ignored.
           */
          if (cmd.dbp) {
-            data[len] += arg.byte0;
+            data[len] = arg.byte0;
              len++;
          }
      }
@@ -1226,10 +1226,16 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
          len++;
      }
 
-    if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
-        err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+    if (len > 0) {
+        if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
+            err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+        } else {
+            /* Only go to an idle state on a successful transfer. */
+            ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
+                             DW_I3C_TRANSFER_STATE_IDLE);
+        }
      } else {
-        /* Only go to an idle state on a successful transfer. */
+        /* No payload bytes for this short transfer. */
          ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
                           DW_I3C_TRANSFER_STATE_IDLE);
      }
Reviewed-by on v1 1/1
#2 lore raw
In reply to Jamin Lin

Nabih Estefan (he/him) | Software Engineer | [email protected]

Quoted 12 lines · Jamin Lin

Coverity reports that dw_i3c_short_transfer() may pass an uninitialized buffer to dw_i3c_send().

The immediate cause is the use of data[len] += arg.byte0, which reads from an uninitialized element of the buffer. Replace this with a simple assignment.

Additionally, avoid calling dw_i3c_send() when the constructed payload length is zero. In that case the transfer has no data phase, so the controller can transition to the idle state directly.

Quoted 5 lines

This resolves the Coverity UNINIT warning and clarifies the handling of zero-length short transfers.

Resolves: Coverity CID 1645555

Thanks!
- Nabih

Quoted 39 lines
---
 hw/i3c/dw-i3c.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
hw/i3c/dw-i3c.c +10 -5
index 3d8b95a14c..aa6c27c8de 100644
@@ -1211,7 +1211,7 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
          * ignored.
          */
         if (cmd.dbp) {
-            data[len] += arg.byte0;
+            data[len] = arg.byte0;
             len++;
         }
     }
@@ -1226,10 +1226,16 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
         len++;
     }
 
-    if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
-        err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+    if (len > 0) {
+        if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
+            err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+        } else {
+            /* Only go to an idle state on a successful transfer. */
+            ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
+                             DW_I3C_TRANSFER_STATE_IDLE);
+        }
     } else {
-        /* Only go to an idle state on a successful transfer. */
+        /* No payload bytes for this short transfer. */
         ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
                          DW_I3C_TRANSFER_STATE_IDLE);
     }
--

2.43.0

#3 lore raw
In reply to Jamin Lin
Quoted 43 lines · Jamin Lin

Coverity reports that dw_i3c_short_transfer() may pass an uninitialized buffer to dw_i3c_send().

The immediate cause is the use of data[len] += arg.byte0, which reads from an uninitialized element of the buffer. Replace this with a simple assignment.

Additionally, avoid calling dw_i3c_send() when the constructed payload length is zero. In that case the transfer has no data phase, so the controller can transition to the idle state directly.

This resolves the Coverity UNINIT warning and clarifies the handling of zero-length short transfers.

Resolves: Coverity CID 1645555

---
  hw/i3c/dw-i3c.c | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
hw/i3c/dw-i3c.c +5 -3
index 3d8b95a14c..aa6c27c8de 100644
@@ -1211,7 +1211,7 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
           * ignored.
           */
          if (cmd.dbp) {
-            data[len] += arg.byte0;
+            data[len] = arg.byte0;
              len++;
          }
      }
@@ -1226,10 +1226,16 @@ static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd,
          len++;
      }
 
-    if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
-        err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+    if (len > 0) {
+        if (dw_i3c_send(s, data, len, &bytes_sent, is_i2c)) {
+            err = DW_I3C_RESP_QUEUE_ERR_I2C_NACK;
+        } else {

hw/i3c/dw-i3c.c static void dw_i3c_short_transfer(DWI3C *s, DWI3CTransferCmd cmd, · line 1241

Quoted 10 lines
hw/i3c/dw-i3c.c +5 -1
+            /* Only go to an idle state on a successful transfer. */
+            ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
+                             DW_I3C_TRANSFER_STATE_IDLE);
+        }
      } else {
-        /* Only go to an idle state on a successful transfer. */
+        /* No payload bytes for this short transfer. */
          ARRAY_FIELD_DP32(s->regs, PRESENT_STATE, CM_TFR_ST_STATUS,
                           DW_I3C_TRANSFER_STATE_IDLE);
      }

Applied to aspeed-next.

Thanks,

C.