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

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

commit e62d6fa60ae8ea7de6bd5452e2fc77f833e7b39d
Author: yangsong8 <[email protected]>
AuthorDate: Tue Apr 7 14:42:51 2026 +0800

    usbdev/cdcacm: fix race condition in cdcacm_sndpacket
    
    Fix two issues in cdcacm_sndpacket when CONFIG_CDCACM_DISABLE_TXBUF
    is enabled:
    
    1. wrcontainer overwrite on interrupt reentry: When wrcontainer is
       NULL and an interrupt fires between EP_SUBMIT branch and
       spin_lock, wrcomplete calls inner sndpacket which assigns a new
       container. The outer sndpacket then unconditionally overwrites
       wrcontainer to NULL, leaking the container and losing data.
       Fix by using a local snapshot and conditional check:
       if (priv->wrcontainer == wrcontainer) before modifying.
    
    2. Dangling xmit.buffer after submit: When txfree is empty after
       submit, xmit.buffer still points to the submitted req->buf.
       The serial layer continues writing to this DMA buffer, causing
       data corruption or loss. Fix by setting xmit.buffer to NULL
       when txfree is empty, so the serial layer treats it as full.
    
    Signed-off-by: yangsong8 <[email protected]>
---
 drivers/usbdev/cdcacm.c | 49 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/drivers/usbdev/cdcacm.c b/drivers/usbdev/cdcacm.c
index f068f48777f..a2d4e742f0c 100644
--- a/drivers/usbdev/cdcacm.c
+++ b/drivers/usbdev/cdcacm.c
@@ -431,7 +431,7 @@ static int cdcacm_sndpacket(FAR struct cdcacm_dev_s *priv)
 #ifdef CONFIG_CDCACM_DISABLE_TXBUF
   FAR struct usbdev_ep_s *ep = priv->epbulkin;
   FAR struct uart_dev_s *dev = &priv->serdev;
-  FAR struct cdcacm_wrreq_s *wrcontainer;
+  FAR struct cdcacm_wrreq_s *wrcontainer = NULL;
   FAR struct usbdev_req_s *req;
   int ret;
 #endif
@@ -480,16 +480,45 @@ static int cdcacm_sndpacket(FAR struct cdcacm_dev_s *priv)
     }
 
   flags = spin_lock_irqsave_nopreempt(&priv->lock);
-  priv->wrcontainer = NULL;
-  if (!sq_empty(&priv->txfree))
+
+  /* Check if wrcontainer has been modified by an interrupt-context
+   * call to cdcacm_wrcomplete -> cdcacm_sndpacket while we were
+   * outside the lock.  The local 'wrcontainer' holds a snapshot:
+   *   - If we entered the submit branch above, it equals the old
+   *     priv->wrcontainer that was just submitted.
+   *   - If we skipped submit (priv->wrcontainer was NULL), it
+   *     remains NULL (its initialized value).
+   *
+   * If priv->wrcontainer no longer matches our snapshot, an
+   * interrupt has already taken over (allocated a new container),
+   * so we must skip to avoid overwriting that assignment.
+   */
+
+  if (priv->wrcontainer == wrcontainer)
     {
-      priv->wrcontainer = (FAR struct cdcacm_wrreq_s *)
-                          sq_remfirst(&priv->txfree);
-      dev->xmit.buffer  = (FAR char *)priv->wrcontainer->req->buf;
-      priv->nwrq--;
-      dev->xmit.head = 0;
-      dev->xmit.tail = 0;
-      uart_datasent(dev);
+      priv->wrcontainer = NULL;
+      if (!sq_empty(&priv->txfree))
+        {
+          priv->wrcontainer = (FAR struct cdcacm_wrreq_s *)
+                              sq_remfirst(&priv->txfree);
+          dev->xmit.buffer  = (FAR char *)priv->wrcontainer->req->buf;
+          priv->nwrq--;
+          dev->xmit.head = 0;
+          dev->xmit.tail = 0;
+          uart_datasent(dev);
+        }
+      else
+        {
+          /* No free write requests available.  Set xmit.buffer to
+           * NULL so that the serial layer (uart_putxmitchar) treats
+           * the buffer as full and blocks or returns -EAGAIN,
+           * instead of writing into the already-submitted req->buf.
+           */
+
+          dev->xmit.buffer = NULL;
+          dev->xmit.head   = 0;
+          dev->xmit.tail   = 0;
+        }
     }
 
   spin_unlock_irqrestore_nopreempt(&priv->lock, flags);

Reply via email to