This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8cfcbebd13a882071a79329e61e40d8c844d1363
Author: Bowen Wang <[email protected]>
AuthorDate: Mon Jul 13 20:45:01 2026 +0800

    drivers/rpmsg/rpmsg_port_spi: validate the rx CRC of a zero crc field
    
    The rx path guarded CRC validation with "if (rxhdr->crc != 0)", so any
    frame arriving with a zero CRC field skipped the check entirely. When
    RETX is enabled the CRC is compiled in (Kconfig selects
    RPMSG_PORT_SPI_CRC for RETX) and rpmsg_port_spi_crc16() folds a zero
    result to 1, so a genuine frame's CRC is never zero. A crc==0 frame is
    therefore always a torn/garbage frame, yet the old guard waved it
    straight through into the retx state machine, where a bogus min_seq
    could drag rx_next backwards and stall the rx channel.
    
    Drop the outer crc!=0 guard and compute the CRC unconditionally. The
    inner "crc != 0" test still preserves the CRC-disabled build
    (rpmsg_port_spi_crc16() returns 0), so behaviour is unchanged when CRC
    is off; when CRC is on, torn crc==0 frames now fail "0 != calculated"
    and are dropped.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/rpmsg_port_spi.c       | 36 +++++++++++++++++++-----------------
 drivers/rpmsg/rpmsg_port_spi_slave.c | 36 +++++++++++++++++++-----------------
 2 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/drivers/rpmsg/rpmsg_port_spi.c b/drivers/rpmsg/rpmsg_port_spi.c
index fc8e2b00aa6..43beeea73a5 100644
--- a/drivers/rpmsg/rpmsg_port_spi.c
+++ b/drivers/rpmsg/rpmsg_port_spi.c
@@ -403,6 +403,7 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg)
 {
   FAR struct rpmsg_port_spi_s *rpspi = arg;
   irqstate_t flags;
+  uint16_t crc;
 
   SPI_SELECT(rpspi->spi, rpspi->devid, false);
 
@@ -446,27 +447,28 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg)
       goto out;
     }
 
-  if (rpspi->rxhdr->crc != 0)
-    {
-      uint16_t crc = rpmsg_port_spi_crc16(rpspi->rxhdr);
+  /* When RETX is enabled the CRC is compiled in and a valid frame's CRC is
+   * never zero (the crc16 macro folds a zero result to 1), so validate
+   * unconditionally: a torn frame carrying crc==0 would otherwise bypass
+   * the check and poison the retx state machine.
+   */
 
-      if (crc != 0 && rpspi->rxhdr->crc != crc)
-        {
-          rpmsgerr("crc check fail received: %u calculated: %u "
-                   "len=%u cmd=%u\n",
-                   rpspi->rxhdr->crc, crc, rpspi->rxhdr->len,
-                   rpspi->rxhdr->cmd);
+  crc = rpmsg_port_spi_crc16(rpspi->rxhdr);
+  if (crc != 0 && rpspi->rxhdr->crc != crc)
+    {
+      rpmsgerr("crc check fail received: %u calculated: %u "
+               "len=%u cmd=%u\n",
+               rpspi->rxhdr->crc, crc, rpspi->rxhdr->len,
+               rpspi->rxhdr->cmd);
 #ifdef CONFIG_RPMSG_PORT_RETX
-          rpmsgerr("retx crc check fail received: seq=%u\n",
-                   rpspi->rxhdr->seqno);
+      rpmsgerr("crc check fail retx state: seq=%u\n", rpspi->rxhdr->seqno);
 #endif
-          if (rpspi->rxerr_count != UINT16_MAX)
-            {
-              rpspi->rxerr_count++;
-            }
-
-          goto out;
+      if (rpspi->rxerr_count != UINT16_MAX)
+        {
+          rpspi->rxerr_count++;
         }
+
+      goto out;
     }
 
   if (rpspi->rxerr_count != 0)
diff --git a/drivers/rpmsg/rpmsg_port_spi_slave.c 
b/drivers/rpmsg/rpmsg_port_spi_slave.c
index 74569041530..897ba5ac742 100644
--- a/drivers/rpmsg/rpmsg_port_spi_slave.c
+++ b/drivers/rpmsg/rpmsg_port_spi_slave.c
@@ -480,6 +480,7 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
   FAR struct rpmsg_port_spi_s *rpspi =
     container_of(dev, struct rpmsg_port_spi_s, spislv);
   irqstate_t flags;
+  uint16_t crc;
 
   IOEXP_WRITEPIN(rpspi->ioe, rpspi->sreq, 0);
   SPIS_CTRLR_QPOLL(rpspi->spictrlr);
@@ -524,27 +525,28 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
       goto out;
     }
 
-  if (rpspi->rxhdr->crc != 0)
-    {
-      uint16_t crc = rpmsg_port_spi_crc16(rpspi->rxhdr);
+  /* When RETX is enabled the CRC is compiled in and a valid frame's CRC is
+   * never zero (the crc16 macro folds a zero result to 1), so validate
+   * unconditionally: a torn frame carrying crc==0 would otherwise bypass
+   * the check and poison the retx state machine.
+   */
 
-      if (crc != 0 && rpspi->rxhdr->crc != crc)
-        {
-          rpmsgerr("crc check fail received: %u calculated: %u "
-                   "len=%u cmd=%u\n",
-                   rpspi->rxhdr->crc, crc, rpspi->rxhdr->len,
-                   rpspi->rxhdr->cmd);
+  crc = rpmsg_port_spi_crc16(rpspi->rxhdr);
+  if (crc != 0 && rpspi->rxhdr->crc != crc)
+    {
+      rpmsgerr("crc check fail received: %u calculated: %u "
+               "len=%u cmd=%u\n",
+               rpspi->rxhdr->crc, crc, rpspi->rxhdr->len,
+               rpspi->rxhdr->cmd);
 #ifdef CONFIG_RPMSG_PORT_RETX
-          rpmsgerr("retx crc check fail received: seq=%u\n",
-                   rpspi->rxhdr->seqno);
+      rpmsgerr("crc check fail retx state: seq=%u\n", rpspi->rxhdr->seqno);
 #endif
-          if (rpspi->rxerr_count != UINT16_MAX)
-            {
-              rpspi->rxerr_count++;
-            }
-
-          goto out;
+      if (rpspi->rxerr_count != UINT16_MAX)
+        {
+          rpspi->rxerr_count++;
         }
+
+      goto out;
     }
 
   if (rpspi->rxerr_count != 0)

Reply via email to