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 32a9cc9b050af927fde2490b4c0c3deb5807c3ca
Author: pangzhen1 <[email protected]>
AuthorDate: Tue Jul 28 22:27:18 2026 +0800

    sched/signal: fix SMP TOCTOU race in nxsig_timedwait
    
    nxsig_pendingset() releases tg_lock before nxsig_remove_pendingsignal()
    re-acquires it. On SMP another CPU can consume the signal in that
    window, making remove() return NULL and trip DEBUGASSERT(sigpend).
    Replace the assert with a retry that re-reads the pending set.
    
    Signed-off-by: pangzhen1 <[email protected]>
    Signed-off-by: zhangyuan29 <[email protected]>
---
 sched/signal/sig_timedwait.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/sched/signal/sig_timedwait.c b/sched/signal/sig_timedwait.c
index bd8daa0c6c1..30a53268133 100644
--- a/sched/signal/sig_timedwait.c
+++ b/sched/signal/sig_timedwait.c
@@ -107,7 +107,7 @@ int nxsig_timedwait(FAR const sigset_t *set, FAR struct 
siginfo *info,
   FAR sigpendq_t *sigpend;
   irqstate_t flags;
   siginfo_t unbinfo;
-  int ret;
+  int ret = ERROR;
 
   DEBUGASSERT(set != NULL && up_interrupt_context() == false);
 
@@ -126,7 +126,15 @@ int nxsig_timedwait(FAR const sigset_t *set, FAR struct 
siginfo *info,
 
   intersection = nxsig_pendingset(rtcb);
   sigandset(&intersection, &intersection, set);
-  if (!sigisemptyset(&intersection))
+
+  /* Loop because the pending-set snapshot may be stale: nxsig_pendingset()
+   * releases tg_lock before nxsig_remove_pendingsignal() re-acquires it.
+   * On SMP, another CPU may consume the signal in that window, causing
+   * remove() to return NULL even though the snapshot was non-empty.
+   * Re-read and retry.  Bounded because pending signals are finite.
+   */
+
+  while (!sigisemptyset(&intersection))
     {
       /* One or more of the signals in intersections is sufficient to cause
        * us to not wait.  Pick the lowest numbered signal and mark it not
@@ -135,7 +143,16 @@ int nxsig_timedwait(FAR const sigset_t *set, FAR struct 
siginfo *info,
 
       sigpend = nxsig_remove_pendingsignal(rtcb,
                                            nxsig_lowest(&intersection));
-      DEBUGASSERT(sigpend);
+      if (sigpend == NULL)
+        {
+          /* Snapshot was stale on SMP: another CPU consumed the signal
+           * between pendingset() and remove().  Re-read and retry.
+           */
+
+          intersection = nxsig_pendingset(rtcb);
+          sigandset(&intersection, &intersection, set);
+          continue;
+        }
 
       /* Return the signal info to the caller if so requested */
 
@@ -151,11 +168,12 @@ int nxsig_timedwait(FAR const sigset_t *set, FAR struct 
siginfo *info,
       /* Then dispose of the pending signal structure properly */
 
       nxsig_release_pendingsignal(sigpend);
+      break;
     }
 
   /* We will have to wait for a signal to be posted to this task. */
 
-  else
+  if (ret < 0)
     {
       /* Always unblock into the stack local, never into the caller's
        * buffer.  The poster writes it from its own context (another

Reply via email to