5b055a
From b9b8413cfdb70a3f99e1573333b23052d57ec1ae Mon Sep 17 00:00:00 2001
5b055a
From: Brad House <brad@brad-house.com>
5b055a
Date: Mon, 22 May 2023 06:51:49 -0400
5b055a
Subject: [PATCH] Merge pull request from GHSA-9g78-jv2r-p7vc
5b055a
5b055a
---
5b055a
 src/lib/ares_process.c | 41 +++++++++++++++++++++++++----------------
5b055a
 1 file changed, 25 insertions(+), 16 deletions(-)
5b055a
5b055a
diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c
5b055a
index bf0cde4..6cac0a9 100644
5b055a
--- a/src/lib/ares_process.c
5b055a
+++ b/src/lib/ares_process.c
5b055a
@@ -470,7 +470,7 @@ static void read_udp_packets(ares_channel channel, fd_set *read_fds,
5b055a
 {
5b055a
   struct server_state *server;
5b055a
   int i;
5b055a
-  ares_ssize_t count;
5b055a
+  ares_ssize_t read_len;
5b055a
   unsigned char buf[MAXENDSSZ + 1];
5b055a
 #ifdef HAVE_RECVFROM
5b055a
   ares_socklen_t fromlen;
5b055a
@@ -513,32 +513,41 @@ static void read_udp_packets(ares_channel channel, fd_set *read_fds,
5b055a
       /* To reduce event loop overhead, read and process as many
5b055a
        * packets as we can. */
5b055a
       do {
5b055a
-        if (server->udp_socket == ARES_SOCKET_BAD)
5b055a
-          count = 0;
5b055a
-
5b055a
-        else {
5b055a
-          if (server->addr.family == AF_INET)
5b055a
+        if (server->udp_socket == ARES_SOCKET_BAD) {
5b055a
+          read_len = -1;
5b055a
+        } else {
5b055a
+          if (server->addr.family == AF_INET) {
5b055a
             fromlen = sizeof(from.sa4);
5b055a
-          else
5b055a
+          } else {
5b055a
             fromlen = sizeof(from.sa6);
5b055a
-          count = socket_recvfrom(channel, server->udp_socket, (void *)buf,
5b055a
-                                  sizeof(buf), 0, &from.sa, &fromlen);
5b055a
+          }
5b055a
+          read_len = socket_recvfrom(channel, server->udp_socket, (void *)buf,
5b055a
+                                     sizeof(buf), 0, &from.sa, &fromlen);
5b055a
         }
5b055a
 
5b055a
-        if (count == -1 && try_again(SOCKERRNO))
5b055a
+        if (read_len == 0) {
5b055a
+          /* UDP is connectionless, so result code of 0 is a 0-length UDP
5b055a
+           * packet, and not an indication the connection is closed like on
5b055a
+           * tcp */
5b055a
           continue;
5b055a
-        else if (count <= 0)
5b055a
+        } else if (read_len < 0) {
5b055a
+          if (try_again(SOCKERRNO))
5b055a
+            continue;
5b055a
+
5b055a
           handle_error(channel, i, now);
5b055a
+
5b055a
 #ifdef HAVE_RECVFROM
5b055a
-        else if (!same_address(&from.sa, &server->addr))
5b055a
+        } else if (!same_address(&from.sa, &server->addr)) {
5b055a
           /* The address the response comes from does not match the address we
5b055a
            * sent the request to. Someone may be attempting to perform a cache
5b055a
            * poisoning attack. */
5b055a
-          break;
5b055a
+          continue;
5b055a
 #endif
5b055a
-        else
5b055a
-          process_answer(channel, buf, (int)count, i, 0, now);
5b055a
-       } while (count > 0);
5b055a
+
5b055a
+        } else {
5b055a
+          process_answer(channel, buf, (int)read_len, i, 0, now);
5b055a
+        }
5b055a
+      } while (read_len >= 0);
5b055a
     }
5b055a
 }
5b055a
 
5b055a
-- 
5b055a
2.38.1
5b055a