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 68163a6b3d83e6fa094b6549bbaed53facbe6bff
Author: ganjing <[email protected]>
AuthorDate: Fri Nov 20 17:19:27 2026 +0800

    libc/netdb: Release resolv.conf and the DNS lock of a cancelled thread.
    
    With CONFIG_NETDB_RESOLVCONF, dns_foreach_nameserver() opens resolv.conf
    and holds dns_lock() while it reads the file.  fgets() and the name
    server callback are cancellation points, so a thread cancelled there
    left the FILE open and the DNS lock taken, and every later lookup
    deadlocked on the lock.
    
    Register a cleanup handler that closes the stream and, when the lock is
    held at that point, releases it.  The lock state is tracked around the
    callback, which already runs with the lock broken.
    
    Signed-off-by: wenquan1 <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/netdb/lib_dnsforeach.c | 79 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 72 insertions(+), 7 deletions(-)

diff --git a/libs/libc/netdb/lib_dnsforeach.c b/libs/libc/netdb/lib_dnsforeach.c
index c7d18b6e8a2..42c31b3cfef 100644
--- a/libs/libc/netdb/lib_dnsforeach.c
+++ b/libs/libc/netdb/lib_dnsforeach.c
@@ -37,25 +37,76 @@
 #include <arpa/inet.h>
 
 #include <nuttx/net/dns.h>
+#if CONFIG_TLS_NCLEANUP > 0
+#  include <nuttx/tls.h>
+#endif
 
 #include "netdb/lib_dns.h"
 
 #ifdef CONFIG_NETDB_DNSCLIENT
 
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_NETDB_RESOLVCONF
+struct dns_foreach_resolvconf_s
+{
+  FAR FILE *stream;
+  bool locked;
+};
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
 #ifdef CONFIG_NETDB_RESOLVCONF
+
+/****************************************************************************
+ * Name: dns_foreach_cleanup
+ *
+ * Description:
+ *   Cleanup handler for dns_foreach_nameserver() resolvconf path.
+ *   Releases the DNS lock (if held) and closes the resolv.conf stream.
+ *
+ ****************************************************************************/
+
+#if CONFIG_TLS_NCLEANUP > 0
+static void dns_foreach_cleanup(FAR void *arg)
+{
+  FAR struct dns_foreach_resolvconf_s *state = arg;
+
+  if (state->locked)
+    {
+      unsigned int count;
+
+      /* Release all nesting levels of the recursive DNS lock */
+
+      dns_breaklock(&count);
+    }
+
+  fclose(state->stream);
+}
+#endif
+
 static FAR char *skip_spaces(FAR char *ptr)
 {
-  while (isspace(*ptr)) ptr++;
+  while (isspace(*ptr))
+    {
+      ptr++;
+    }
+
   return ptr;
 }
 
 static FAR char *find_spaces(FAR char *ptr)
 {
-  while (*ptr && !isspace(*ptr)) ptr++;
+  while (*ptr && !isspace(*ptr))
+    {
+      ptr++;
+    }
+
   return ptr;
 }
 #endif
@@ -77,8 +128,8 @@ static FAR char *find_spaces(FAR char *ptr)
 
 int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg)
 {
+  struct dns_foreach_resolvconf_s state;
   union dns_addr_u u;
-  FAR FILE *stream;
   char line[DNS_MAX_LINE];
   FAR char *addrstr;
   FAR char *ptr;
@@ -89,8 +140,8 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR void 
*arg)
 
   /* Open the resolver configuration file */
 
-  stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "r");
-  if (stream == NULL)
+  state.stream = fopen(CONFIG_NETDB_RESOLVCONF_PATH, "r");
+  if (state.stream == NULL)
     {
       ret = -get_errno();
       nerr("ERROR: Failed to open %s: %d\n",
@@ -99,10 +150,16 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR 
void *arg)
       return ret;
     }
 
+  state.locked = false;
+#if CONFIG_TLS_NCLEANUP > 0
+  tls_cleanup_push(tls_get_info(), dns_foreach_cleanup, &state);
+#endif
+
   dns_lock();
+  state.locked = true;
 
   keylen = strlen(NETDB_DNS_KEYWORD);
-  while (fgets(line, DNS_MAX_LINE, stream) != NULL)
+  while (fgets(line, DNS_MAX_LINE, state.stream) != NULL)
     {
       ptr = skip_spaces(line);
       if (strncmp(ptr, NETDB_DNS_KEYWORD, keylen) == 0)
@@ -190,10 +247,12 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR 
void *arg)
             {
               u.ipv4.sin_family = AF_INET;
               u.ipv4.sin_port   = port;
+              state.locked = false;
               dns_breaklock(&count);
               ret = callback(arg, (FAR struct sockaddr *)&u.ipv4,
                              sizeof(struct sockaddr_in));
               dns_restorelock(count);
+              state.locked = true;
             }
           else
 #endif
@@ -211,10 +270,12 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR 
void *arg)
                 {
                   u.ipv6.sin6_family = AF_INET6;
                   u.ipv6.sin6_port   = port;
+                  state.locked = false;
                   dns_breaklock(&count);
                   ret = callback(arg, (FAR struct sockaddr *)&u.ipv6,
                                  sizeof(struct sockaddr_in6));
                   dns_restorelock(count);
+                  state.locked = true;
                 }
               else
 #endif
@@ -233,8 +294,12 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR 
void *arg)
         }
     }
 
+  state.locked = false;
   dns_unlock();
-  fclose(stream);
+#if CONFIG_TLS_NCLEANUP > 0
+  tls_cleanup_pop(tls_get_info(), 0);
+#endif
+  fclose(state.stream);
   return ret;
 }
 

Reply via email to