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 fc5166936d00a0f2092ddc2e0630f7ceff34088a
Author: Bowen Wang <[email protected]>
AuthorDate: Mon Jul 13 20:56:18 2026 +0800

    drivers/rpmsg/rpmsg_port_spi: validate rx cmd and bound min_seq advance
    
    Two receive-side sanity checks harden the retx state machine against
    corrupt frames that pass length/CRC but carry garbage fields.
    
    First, reject frames whose cmd is outside the known command range
    [CONNECT, SHUTDOWN] as malformed. An unknown cmd would otherwise fall
    through the "cmd != AVAIL" branch, skip rpmsg_port_retx_rx_valid() and
    be added to the rx queue directly, bypassing seqno validation.
    
    Second, bound how far a peer's min_seq may advance rx_next. min_seq
    only legitimately moves rx_next forward within the peer's in-flight
    window, which is at most the rx queue depth. RPMSG_PORT_SEQ_LT uses a
    16-bit signed wrap compare with a 32768 tolerance, so a torn frame
    carrying min_seq==0 was misread as "12616 frames ahead" and dragged
    rx_next back to 0, stalling the rx channel for ~23 minutes in the
    field. Only advance when the forward gap is within the rx queue depth;
    otherwise drop the min_seq and leave rx_next untouched so the link
    self-heals. This needs the total buffer count, so record it in a new
    rpmsg_port_queue_s::num field fixed at queue creation.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/rpmsg_port.c           | 26 ++++++++++++++++++++++----
 drivers/rpmsg/rpmsg_port.h           |  4 ++++
 drivers/rpmsg/rpmsg_port_spi.c       |  4 +++-
 drivers/rpmsg/rpmsg_port_spi_slave.c |  4 +++-
 4 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/drivers/rpmsg/rpmsg_port.c b/drivers/rpmsg/rpmsg_port.c
index 2d143d21ff5..51cdfe7b79e 100644
--- a/drivers/rpmsg/rpmsg_port.c
+++ b/drivers/rpmsg/rpmsg_port.c
@@ -191,6 +191,7 @@ static int rpmsg_port_create_queue(FAR struct 
rpmsg_port_queue_s *queue,
     }
 
   queue->len = len;
+  queue->num = count;
   queue->buf = buf;
 
   /* Init free list */
@@ -819,13 +820,30 @@ void rpmsg_port_retx_process_ack(FAR struct rpmsg_port_s 
*port,
             atomic_read(&port->retx.ackdup_count));
   port->retx.last_ackno = ackno;
 
-  /* Advance rx_next based on peer's min_seq to skip abandoned frames */
+  /* Advance rx_next based on peer's min_seq to skip abandoned frames.
+   * A valid min_seq only advances rx_next within the peer's in-flight
+   * window, bounded by the rx queue depth. A larger jump means the
+   * min_seq is corrupt (e.g. a torn frame carrying min_seq==0 that
+   * SEQ_LT misreads as a huge forward wrap), so drop it instead of
+   * dragging rx_next backwards and stalling the rx channel.
+   */
 
   if (RPMSG_PORT_SEQ_LT(port->retx.rx_next, min_seq))
     {
-      rpmsgerr("Peer abandoned seqno [%u, %u), advancing rx_next\n",
-               port->retx.rx_next, min_seq);
-      port->retx.rx_next = min_seq;
+      uint16_t gap = min_seq - port->retx.rx_next;
+
+      if (gap <= port->rxq.num)
+        {
+          rpmsgerr("Peer abandoned seqno [%u, %u), advancing rx_next\n",
+                   port->retx.rx_next, min_seq);
+          port->retx.rx_next = min_seq;
+        }
+      else
+        {
+          rpmsgerr("drop bogus min_seq %u: gap %u exceeds window %u, "
+                   "rx_next %u\n",
+                   min_seq, gap, port->rxq.num, port->retx.rx_next);
+        }
     }
 
   spin_unlock_irqrestore(&port->retx.lock, flags);
diff --git a/drivers/rpmsg/rpmsg_port.h b/drivers/rpmsg/rpmsg_port.h
index 630e46f39e6..624a8dbc112 100644
--- a/drivers/rpmsg/rpmsg_port.h
+++ b/drivers/rpmsg/rpmsg_port.h
@@ -116,6 +116,10 @@ struct rpmsg_port_queue_s
 
   uint16_t                     len;
 
+  /* Total number of buffers, fixed at init time */
+
+  uint16_t                     num;
+
   /* Free list of buffers which have not been occupied data yet */
 
   struct rpmsg_port_list_s     free;
diff --git a/drivers/rpmsg/rpmsg_port_spi.c b/drivers/rpmsg/rpmsg_port_spi.c
index 43beeea73a5..0aa74dcb3b8 100644
--- a/drivers/rpmsg/rpmsg_port_spi.c
+++ b/drivers/rpmsg/rpmsg_port_spi.c
@@ -428,7 +428,9 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg)
     }
 
   if (rpspi->rxhdr->len < sizeof(struct rpmsg_port_header_s) ||
-      rpspi->rxhdr->len > rpspi->port.rxq.len)
+      rpspi->rxhdr->len > rpspi->port.rxq.len ||
+      rpspi->rxhdr->cmd < RPMSG_PORT_SPI_CMD_CONNECT ||
+      rpspi->rxhdr->cmd > RPMSG_PORT_SPI_CMD_SHUTDOWN)
     {
       rpmsgerr("drop malformed rx: len=%u crc=%u cmd=%u avail=%u\n",
                rpspi->rxhdr->len, rpspi->rxhdr->crc, rpspi->rxhdr->cmd,
diff --git a/drivers/rpmsg/rpmsg_port_spi_slave.c 
b/drivers/rpmsg/rpmsg_port_spi_slave.c
index 897ba5ac742..4b66bef7a3c 100644
--- a/drivers/rpmsg/rpmsg_port_spi_slave.c
+++ b/drivers/rpmsg/rpmsg_port_spi_slave.c
@@ -506,7 +506,9 @@ static void rpmsg_port_spi_slave_notify(FAR struct 
spi_slave_dev_s *dev,
     }
 
   if (rpspi->rxhdr->len < sizeof(struct rpmsg_port_header_s) ||
-      rpspi->rxhdr->len > rpspi->port.rxq.len)
+      rpspi->rxhdr->len > rpspi->port.rxq.len ||
+      rpspi->rxhdr->cmd < RPMSG_PORT_SPI_CMD_CONNECT ||
+      rpspi->rxhdr->cmd > RPMSG_PORT_SPI_CMD_SHUTDOWN)
     {
       rpmsgerr("drop malformed rx: len=%u crc=%u cmd=%u avail=%u\n",
                rpspi->rxhdr->len, rpspi->rxhdr->crc, rpspi->rxhdr->cmd,

Reply via email to