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 6093cc100b3cef67ac9d6b9b099198178498449f
Author: wenquan1 <[email protected]>
AuthorDate: Mon Nov 9 11:11:04 2026 +0800

    libc/tls: Keep the cleanup stack balanced on overflow.
    
    tls_cleanup_push() silently drops the entry when the stack
    is full but does not increment tl_tos.  Subsequent pop calls
    then remove the wrong entries, executing outer cleanup
    handlers prematurely (use-after-free).
    
    Always increment tl_tos in push and decrement in pop, but
    only access the stack array when the index is within
    CONFIG_TLS_NCLEANUP.  Overflow now only loses the innermost
    entries while keeping push/pop balanced.
    
    The push no longer asserts that the stack has room.  libc itself
    pushes cleanup handlers around blocking calls (for example the DNS
    lookup), so a configuration with a small CONFIG_TLS_NCLEANUP would
    assert in debug builds even when no thread is ever cancelled.  An
    overflow is now well defined: the handler is not registered, and the
    stack stays balanced.
    
    Signed-off-by: wenquan1 <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/tls/tls_cleanup.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)

diff --git a/libs/libc/tls/tls_cleanup.c b/libs/libc/tls/tls_cleanup.c
index 0465dc7318f..4a76d0e8e46 100644
--- a/libs/libc/tls/tls_cleanup.c
+++ b/libs/libc/tls/tls_cleanup.c
@@ -41,16 +41,14 @@ void tls_cleanup_push(FAR struct tls_info_s *tls,
                       tls_cleanup_t routine, FAR void *arg)
 {
   DEBUGASSERT(tls != NULL);
-  DEBUGASSERT(tls->tl_tos < CONFIG_TLS_NCLEANUP);
 
   if (tls->tl_tos < CONFIG_TLS_NCLEANUP)
     {
-      unsigned int ndx = tls->tl_tos;
-
-      tls->tl_tos++;
-      tls->tl_stack[ndx].tc_cleaner = routine;
-      tls->tl_stack[ndx].tc_arg = arg;
+      tls->tl_stack[tls->tl_tos].tc_cleaner = routine;
+      tls->tl_stack[tls->tl_tos].tc_arg = arg;
     }
+
+  tls->tl_tos++;
 }
 
 void tls_cleanup_pop(FAR struct tls_info_s *tls, int execute)
@@ -59,26 +57,20 @@ void tls_cleanup_pop(FAR struct tls_info_s *tls, int 
execute)
 
   if (tls->tl_tos > 0)
     {
-      unsigned int ndx;
-
-      /* Get the index to the last cleaner function pushed onto the stack */
+      tls->tl_tos--;
 
-      ndx = tls->tl_tos - 1;
-      DEBUGASSERT(ndx < CONFIG_TLS_NCLEANUP);
+      /* Only access the stack array when the index is within bounds.
+       * Entries that overflowed during push were silently dropped,
+       * so there is nothing to execute for those slots.
+       */
 
-      /* Should we execute the cleanup routine at the top of the stack? */
-
-      if (execute != 0)
+      if (tls->tl_tos < CONFIG_TLS_NCLEANUP && execute != 0)
         {
           FAR struct tls_cleanup_s *cb;
 
-          /* Yes..  Execute the clean-up routine. */
-
-          cb  = &tls->tl_stack[ndx];
+          cb = &tls->tl_stack[tls->tl_tos];
           cb->tc_cleaner(cb->tc_arg);
         }
-
-      tls->tl_tos = ndx;
     }
 }
 

Reply via email to