arrfab / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1296031.patch

147e83
Upstream commit:
147e83
147e83
commit e9db92d3acfe1822d56d11abcea5bfc4c41cf6ca
147e83
Author: Carlos O'Donell <carlos@systemhalted.org>
147e83
Date:   Tue Feb 16 21:26:37 2016 -0500
147e83
147e83
    CVE-2015-7547: getaddrinfo() stack-based buffer overflow (Bug 18665).
147e83
147e83
Index: b/resolv/nss_dns/dns-host.c
147e83
===================================================================
147e83
--- a/resolv/nss_dns/dns-host.c
147e83
+++ b/resolv/nss_dns/dns-host.c
147e83
@@ -1051,7 +1051,10 @@ gaih_getanswer_slice (const querybuf *an
147e83
   int h_namelen = 0;
147e83
 
147e83
   if (ancount == 0)
147e83
-    return NSS_STATUS_NOTFOUND;
147e83
+    {
147e83
+      *h_errnop = HOST_NOT_FOUND;
147e83
+      return NSS_STATUS_NOTFOUND;
147e83
+    }
147e83
 
147e83
   while (ancount-- > 0 && cp < end_of_message && had_error == 0)
147e83
     {
147e83
@@ -1228,7 +1231,14 @@ gaih_getanswer_slice (const querybuf *an
147e83
   /* Special case here: if the resolver sent a result but it only
147e83
      contains a CNAME while we are looking for a T_A or T_AAAA record,
147e83
      we fail with NOTFOUND instead of TRYAGAIN.  */
147e83
-  return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
147e83
+  if (canon != NULL)
147e83
+    {
147e83
+      *h_errnop = HOST_NOT_FOUND;
147e83
+      return NSS_STATUS_NOTFOUND;
147e83
+    }
147e83
+
147e83
+  *h_errnop = NETDB_INTERNAL;
147e83
+  return NSS_STATUS_TRYAGAIN;
147e83
 }
147e83
 
147e83
 
147e83
@@ -1242,11 +1252,101 @@ gaih_getanswer (const querybuf *answer1,
147e83
 
147e83
   enum nss_status status = NSS_STATUS_NOTFOUND;
147e83
 
147e83
+  /* Combining the NSS status of two distinct queries requires some
147e83
+     compromise and attention to symmetry (A or AAAA queries can be
147e83
+     returned in any order).  What follows is a breakdown of how this
147e83
+     code is expected to work and why. We discuss only SUCCESS,
147e83
+     TRYAGAIN, NOTFOUND and UNAVAIL, since they are the only returns
147e83
+     that apply (though RETURN and MERGE exist).  We make a distinction
147e83
+     between TRYAGAIN (recoverable) and TRYAGAIN' (not-recoverable).
147e83
+     A recoverable TRYAGAIN is almost always due to buffer size issues
147e83
+     and returns ERANGE in errno and the caller is expected to retry
147e83
+     with a larger buffer.
147e83
+
147e83
+     Lastly, you may be tempted to make significant changes to the
147e83
+     conditions in this code to bring about symmetry between responses.
147e83
+     Please don't change anything without due consideration for
147e83
+     expected application behaviour.  Some of the synthesized responses
147e83
+     aren't very well thought out and sometimes appear to imply that
147e83
+     IPv4 responses are always answer 1, and IPv6 responses are always
147e83
+     answer 2, but that's not true (see the implemetnation of send_dg
147e83
+     and send_vc to see response can arrive in any order, particlarly
147e83
+     for UDP). However, we expect it holds roughly enough of the time
147e83
+     that this code works, but certainly needs to be fixed to make this
147e83
+     a more robust implementation.
147e83
+
147e83
+     ----------------------------------------------
147e83
+     | Answer 1 Status /   | Synthesized | Reason |
147e83
+     | Answer 2 Status     | Status      |        |
147e83
+     |--------------------------------------------|
147e83
+     | SUCCESS/SUCCESS     | SUCCESS     | [1]    |
147e83
+     | SUCCESS/TRYAGAIN    | TRYAGAIN    | [5]    |
147e83
+     | SUCCESS/TRYAGAIN'   | SUCCESS     | [1]    |
147e83
+     | SUCCESS/NOTFOUND    | SUCCESS     | [1]    |
147e83
+     | SUCCESS/UNAVAIL     | SUCCESS     | [1]    |
147e83
+     | TRYAGAIN/SUCCESS    | TRYAGAIN    | [2]    |
147e83
+     | TRYAGAIN/TRYAGAIN   | TRYAGAIN    | [2]    |
147e83
+     | TRYAGAIN/TRYAGAIN'  | TRYAGAIN    | [2]    |
147e83
+     | TRYAGAIN/NOTFOUND   | TRYAGAIN    | [2]    |
147e83
+     | TRYAGAIN/UNAVAIL    | TRYAGAIN    | [2]    |
147e83
+     | TRYAGAIN'/SUCCESS   | SUCCESS     | [3]    |
147e83
+     | TRYAGAIN'/TRYAGAIN  | TRYAGAIN    | [3]    |
147e83
+     | TRYAGAIN'/TRYAGAIN' | TRYAGAIN'   | [3]    |
147e83
+     | TRYAGAIN'/NOTFOUND  | TRYAGAIN'   | [3]    |
147e83
+     | TRYAGAIN'/UNAVAIL   | UNAVAIL     | [3]    |
147e83
+     | NOTFOUND/SUCCESS    | SUCCESS     | [3]    |
147e83
+     | NOTFOUND/TRYAGAIN   | TRYAGAIN    | [3]    |
147e83
+     | NOTFOUND/TRYAGAIN'  | TRYAGAIN'   | [3]    |
147e83
+     | NOTFOUND/NOTFOUND   | NOTFOUND    | [3]    |
147e83
+     | NOTFOUND/UNAVAIL    | UNAVAIL     | [3]    |
147e83
+     | UNAVAIL/SUCCESS     | UNAVAIL     | [4]    |
147e83
+     | UNAVAIL/TRYAGAIN    | UNAVAIL     | [4]    |
147e83
+     | UNAVAIL/TRYAGAIN'   | UNAVAIL     | [4]    |
147e83
+     | UNAVAIL/NOTFOUND    | UNAVAIL     | [4]    |
147e83
+     | UNAVAIL/UNAVAIL     | UNAVAIL     | [4]    |
147e83
+     ----------------------------------------------
147e83
+
147e83
+     [1] If the first response is a success we return success.
147e83
+         This ignores the state of the second answer and in fact
147e83
+         incorrectly sets errno and h_errno to that of the second
147e83
+	 answer.  However because the response is a success we ignore
147e83
+	 *errnop and *h_errnop (though that means you touched errno on
147e83
+         success).  We are being conservative here and returning the
147e83
+         likely IPv4 response in the first answer as a success.
147e83
+
147e83
+     [2] If the first response is a recoverable TRYAGAIN we return
147e83
+	 that instead of looking at the second response.  The
147e83
+	 expectation here is that we have failed to get an IPv4 response
147e83
+	 and should retry both queries.
147e83
+
147e83
+     [3] If the first response was not a SUCCESS and the second
147e83
+	 response is not NOTFOUND (had a SUCCESS, need to TRYAGAIN,
147e83
+	 or failed entirely e.g. TRYAGAIN' and UNAVAIL) then use the
147e83
+	 result from the second response, otherwise the first responses
147e83
+	 status is used.  Again we have some odd side-effects when the
147e83
+	 second response is NOTFOUND because we overwrite *errnop and
147e83
+	 *h_errnop that means that a first answer of NOTFOUND might see
147e83
+	 its *errnop and *h_errnop values altered.  Whether it matters
147e83
+	 in practice that a first response NOTFOUND has the wrong
147e83
+	 *errnop and *h_errnop is undecided.
147e83
+
147e83
+     [4] If the first response is UNAVAIL we return that instead of
147e83
+	 looking at the second response.  The expectation here is that
147e83
+	 it will have failed similarly e.g. configuration failure.
147e83
+
147e83
+     [5] Testing this code is complicated by the fact that truncated
147e83
+	 second response buffers might be returned as SUCCESS if the
147e83
+	 first answer is a SUCCESS.  To fix this we add symmetry to
147e83
+	 TRYAGAIN with the second response.  If the second response
147e83
+	 is a recoverable error we now return TRYAGIN even if the first
147e83
+	 response was SUCCESS.  */
147e83
+
147e83
   if (anslen1 > 0)
147e83
     status = gaih_getanswer_slice(answer1, anslen1, qname,
147e83
 				  &pat, &buffer, &buflen,
147e83
 				  errnop, h_errnop, ttlp,
147e83
 				  &first);
147e83
+
147e83
   if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND
147e83
        || (status == NSS_STATUS_TRYAGAIN
147e83
 	   /* We want to look at the second answer in case of an
147e83
@@ -1262,8 +1362,15 @@ gaih_getanswer (const querybuf *answer1,
147e83
 						     &pat, &buffer, &buflen,
147e83
 						     errnop, h_errnop, ttlp,
147e83
 						     &first);
147e83
+      /* Use the second response status in some cases.  */
147e83
       if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND)
147e83
 	status = status2;
147e83
+      /* Do not return a truncated second response (unless it was
147e83
+         unavoidable e.g. unrecoverable TRYAGAIN).  */
147e83
+      if (status == NSS_STATUS_SUCCESS
147e83
+	  && (status2 == NSS_STATUS_TRYAGAIN
147e83
+	      && *errnop == ERANGE && *h_errnop != NO_RECOVERY))
147e83
+	status = NSS_STATUS_TRYAGAIN;
147e83
     }
147e83
 
147e83
   return status;
147e83
Index: b/resolv/res_query.c
147e83
===================================================================
147e83
--- a/resolv/res_query.c
147e83
+++ b/resolv/res_query.c
147e83
@@ -396,6 +396,7 @@ __libc_res_nsearch(res_state statp,
147e83
 		  {
147e83
 		    free (*answerp2);
147e83
 		    *answerp2 = NULL;
147e83
+		    *nanswerp2 = 0;
147e83
 		    *answerp2_malloced = 0;
147e83
 		  }
147e83
 	}
147e83
@@ -436,6 +437,7 @@ __libc_res_nsearch(res_state statp,
147e83
 			  {
147e83
 			    free (*answerp2);
147e83
 			    *answerp2 = NULL;
147e83
+			    *nanswerp2 = 0;
147e83
 			    *answerp2_malloced = 0;
147e83
 			  }
147e83
 
147e83
@@ -510,6 +512,7 @@ __libc_res_nsearch(res_state statp,
147e83
 	  {
147e83
 	    free (*answerp2);
147e83
 	    *answerp2 = NULL;
147e83
+	    *nanswerp2 = 0;
147e83
 	    *answerp2_malloced = 0;
147e83
 	  }
147e83
 	if (saved_herrno != -1)
147e83
Index: b/resolv/res_send.c
147e83
===================================================================
147e83
--- a/resolv/res_send.c
147e83
+++ b/resolv/res_send.c
147e83
@@ -1,3 +1,20 @@
147e83
+/* Copyright (C) 2016 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
 /*
147e83
  * Copyright (c) 1985, 1989, 1993
147e83
  *    The Regents of the University of California.  All rights reserved.
147e83
@@ -360,6 +377,8 @@ __libc_res_nsend(res_state statp, const
147e83
 #ifdef USE_HOOKS
147e83
 	if (__builtin_expect (statp->qhook || statp->rhook, 0)) {
147e83
 		if (anssiz < MAXPACKET && ansp) {
147e83
+			/* Always allocate MAXPACKET, callers expect
147e83
+			   this specific size.  */
147e83
 			u_char *buf = malloc (MAXPACKET);
147e83
 			if (buf == NULL)
147e83
 				return (-1);
147e83
@@ -653,6 +672,77 @@ libresolv_hidden_def (res_nsend)
147e83
 
147e83
 /* Private */
147e83
 
147e83
+/* The send_vc function is responsible for sending a DNS query over TCP
147e83
+   to the nameserver numbered NS from the res_state STATP i.e.
147e83
+   EXT(statp).nssocks[ns].  The function supports sending both IPv4 and
147e83
+   IPv6 queries at the same serially on the same socket.
147e83
+
147e83
+   Please note that for TCP there is no way to disable sending both
147e83
+   queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP
147e83
+   and sends the queries serially and waits for the result after each
147e83
+   sent query.  This implemetnation should be corrected to honour these
147e83
+   options.
147e83
+
147e83
+   Please also note that for TCP we send both queries over the same
147e83
+   socket one after another.  This technically violates best practice
147e83
+   since the server is allowed to read the first query, respond, and
147e83
+   then close the socket (to service another client).  If the server
147e83
+   does this, then the remaining second query in the socket data buffer
147e83
+   will cause the server to send the client an RST which will arrive
147e83
+   asynchronously and the client's OS will likely tear down the socket
147e83
+   receive buffer resulting in a potentially short read and lost
147e83
+   response data.  This will force the client to retry the query again,
147e83
+   and this process may repeat until all servers and connection resets
147e83
+   are exhausted and then the query will fail.  It's not known if this
147e83
+   happens with any frequency in real DNS server implementations.  This
147e83
+   implementation should be corrected to use two sockets by default for
147e83
+   parallel queries.
147e83
+
147e83
+   The query stored in BUF of BUFLEN length is sent first followed by
147e83
+   the query stored in BUF2 of BUFLEN2 length.  Queries are sent
147e83
+   serially on the same socket.
147e83
+
147e83
+   Answers to the query are stored firstly in *ANSP up to a max of
147e83
+   *ANSSIZP bytes.  If more than *ANSSIZP bytes are needed and ANSCP
147e83
+   is non-NULL (to indicate that modifying the answer buffer is allowed)
147e83
+   then malloc is used to allocate a new response buffer and ANSCP and
147e83
+   ANSP will both point to the new buffer.  If more than *ANSSIZP bytes
147e83
+   are needed but ANSCP is NULL, then as much of the response as
147e83
+   possible is read into the buffer, but the results will be truncated.
147e83
+   When truncation happens because of a small answer buffer the DNS
147e83
+   packets header feild TC will bet set to 1, indicating a truncated
147e83
+   message and the rest of the socket data will be read and discarded.
147e83
+
147e83
+   Answers to the query are stored secondly in *ANSP2 up to a max of
147e83
+   *ANSSIZP2 bytes, with the actual response length stored in
147e83
+   *RESPLEN2.  If more than *ANSSIZP bytes are needed and ANSP2
147e83
+   is non-NULL (required for a second query) then malloc is used to
147e83
+   allocate a new response buffer, *ANSSIZP2 is set to the new buffer
147e83
+   size and *ANSP2_MALLOCED is set to 1.
147e83
+
147e83
+   The ANSP2_MALLOCED argument will eventually be removed as the
147e83
+   change in buffer pointer can be used to detect the buffer has
147e83
+   changed and that the caller should use free on the new buffer.
147e83
+
147e83
+   Note that the answers may arrive in any order from the server and
147e83
+   therefore the first and second answer buffers may not correspond to
147e83
+   the first and second queries.
147e83
+
147e83
+   It is not supported to call this function with a non-NULL ANSP2
147e83
+   but a NULL ANSCP.  Put another way, you can call send_vc with a
147e83
+   single unmodifiable buffer or two modifiable buffers, but no other
147e83
+   combination is supported.
147e83
+
147e83
+   It is the caller's responsibility to free the malloc allocated
147e83
+   buffers by detecting that the pointers have changed from their
147e83
+   original values i.e. *ANSCP or *ANSP2 has changed.
147e83
+
147e83
+   If errors are encountered then *TERRNO is set to an appropriate
147e83
+   errno value and a zero result is returned for a recoverable error,
147e83
+   and a less-than zero result is returned for a non-recoverable error.
147e83
+
147e83
+   If no errors are encountered then *TERRNO is left unmodified and
147e83
+   a the length of the first response in bytes is returned.  */
147e83
 static int
147e83
 send_vc(res_state statp,
147e83
 	const u_char *buf, int buflen, const u_char *buf2, int buflen2,
147e83
@@ -662,11 +752,7 @@ send_vc(res_state statp,
147e83
 {
147e83
 	const HEADER *hp = (HEADER *) buf;
147e83
 	const HEADER *hp2 = (HEADER *) buf2;
147e83
-	u_char *ans = *ansp;
147e83
-	int orig_anssizp = *anssizp;
147e83
-	// XXX REMOVE
147e83
-	// int anssiz = *anssizp;
147e83
-	HEADER *anhp = (HEADER *) ans;
147e83
+	HEADER *anhp = (HEADER *) *ansp;
147e83
 	struct sockaddr_in6 *nsap = EXT(statp).nsaddrs[ns];
147e83
 	int truncating, connreset, resplen, n;
147e83
 	struct iovec iov[4];
147e83
@@ -742,6 +828,8 @@ send_vc(res_state statp,
147e83
 	 * Receive length & response
147e83
 	 */
147e83
 	int recvresp1 = 0;
147e83
+	/* Skip the second response if there is no second query.
147e83
+           To do that we mark the second response as received.  */
147e83
 	int recvresp2 = buf2 == NULL;
147e83
 	uint16_t rlen16;
147e83
  read_len:
147e83
@@ -778,33 +866,14 @@ send_vc(res_state statp,
147e83
 	u_char **thisansp;
147e83
 	int *thisresplenp;
147e83
 	if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
147e83
+		/* We have not received any responses
147e83
+		   yet or we only have one response to
147e83
+		   receive.  */
147e83
 		thisanssizp = anssizp;
147e83
 		thisansp = anscp ?: ansp;
147e83
 		assert (anscp != NULL || ansp2 == NULL);
147e83
 		thisresplenp = &resplen;
147e83
 	} else {
147e83
-		if (*anssizp != MAXPACKET) {
147e83
-			/* No buffer allocated for the first
147e83
-			   reply.  We can try to use the rest
147e83
-			   of the user-provided buffer.  */
147e83
-#ifdef _STRING_ARCH_unaligned
147e83
-			*anssizp2 = orig_anssizp - resplen;
147e83
-			*ansp2 = *ansp + resplen;
147e83
-#else
147e83
-			int aligned_resplen
147e83
-			  = ((resplen + __alignof__ (HEADER) - 1)
147e83
-			     & ~(__alignof__ (HEADER) - 1));
147e83
-			*anssizp2 = orig_anssizp - aligned_resplen;
147e83
-			*ansp2 = *ansp + aligned_resplen;
147e83
-#endif
147e83
-		} else {
147e83
-			/* The first reply did not fit into the
147e83
-			   user-provided buffer.  Maybe the second
147e83
-			   answer will.  */
147e83
-			*anssizp2 = orig_anssizp;
147e83
-			*ansp2 = *ansp;
147e83
-		}
147e83
-
147e83
 		thisanssizp = anssizp2;
147e83
 		thisansp = ansp2;
147e83
 		thisresplenp = resplen2;
147e83
@@ -812,10 +881,14 @@ send_vc(res_state statp,
147e83
 	anhp = (HEADER *) *thisansp;
147e83
 
147e83
 	*thisresplenp = rlen;
147e83
-	if (rlen > *thisanssizp) {
147e83
-		/* Yes, we test ANSCP here.  If we have two buffers
147e83
-		   both will be allocatable.  */
147e83
-		if (__builtin_expect (anscp != NULL, 1)) {
147e83
+	/* Is the answer buffer too small?  */
147e83
+	if (*thisanssizp < rlen) {
147e83
+		/* If the current buffer is non-NULL and it's not
147e83
+		   pointing at the static user-supplied buffer then
147e83
+		   we can reallocate it.  */
147e83
+		if (thisansp != NULL && thisansp != ansp) {
147e83
+			/* Always allocate MAXPACKET, callers expect
147e83
+			   this specific size.  */
147e83
 			u_char *newp = malloc (MAXPACKET);
147e83
 			if (newp == NULL) {
147e83
 				*terrno = ENOMEM;
147e83
@@ -827,6 +900,9 @@ send_vc(res_state statp,
147e83
 			if (thisansp == ansp2)
147e83
 			  *ansp2_malloced = 1;
147e83
 			anhp = (HEADER *) newp;
147e83
+			/* A uint16_t can't be larger than MAXPACKET
147e83
+			   thus it's safe to allocate MAXPACKET but
147e83
+			   read RLEN bytes instead.  */
147e83
 			len = rlen;
147e83
 		} else {
147e83
 			Dprint(statp->options & RES_DEBUG,
147e83
@@ -990,6 +1066,66 @@ reopen (res_state statp, int *terrno, in
147e83
 	return 1;
147e83
 }
147e83
 
147e83
+/* The send_dg function is responsible for sending a DNS query over UDP
147e83
+   to the nameserver numbered NS from the res_state STATP i.e.
147e83
+   EXT(statp).nssocks[ns].  The function supports IPv4 and IPv6 queries
147e83
+   along with the ability to send the query in parallel for both stacks
147e83
+   (default) or serially (RES_SINGLKUP).  It also supports serial lookup
147e83
+   with a close and reopen of the socket used to talk to the server
147e83
+   (RES_SNGLKUPREOP) to work around broken name servers.
147e83
+
147e83
+   The query stored in BUF of BUFLEN length is sent first followed by
147e83
+   the query stored in BUF2 of BUFLEN2 length.  Queries are sent
147e83
+   in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP).
147e83
+
147e83
+   Answers to the query are stored firstly in *ANSP up to a max of
147e83
+   *ANSSIZP bytes.  If more than *ANSSIZP bytes are needed and ANSCP
147e83
+   is non-NULL (to indicate that modifying the answer buffer is allowed)
147e83
+   then malloc is used to allocate a new response buffer and ANSCP and
147e83
+   ANSP will both point to the new buffer.  If more than *ANSSIZP bytes
147e83
+   are needed but ANSCP is NULL, then as much of the response as
147e83
+   possible is read into the buffer, but the results will be truncated.
147e83
+   When truncation happens because of a small answer buffer the DNS
147e83
+   packets header feild TC will bet set to 1, indicating a truncated
147e83
+   message, while the rest of the UDP packet is discarded.
147e83
+
147e83
+   Answers to the query are stored secondly in *ANSP2 up to a max of
147e83
+   *ANSSIZP2 bytes, with the actual response length stored in
147e83
+   *RESPLEN2.  If more than *ANSSIZP bytes are needed and ANSP2
147e83
+   is non-NULL (required for a second query) then malloc is used to
147e83
+   allocate a new response buffer, *ANSSIZP2 is set to the new buffer
147e83
+   size and *ANSP2_MALLOCED is set to 1.
147e83
+
147e83
+   The ANSP2_MALLOCED argument will eventually be removed as the
147e83
+   change in buffer pointer can be used to detect the buffer has
147e83
+   changed and that the caller should use free on the new buffer.
147e83
+
147e83
+   Note that the answers may arrive in any order from the server and
147e83
+   therefore the first and second answer buffers may not correspond to
147e83
+   the first and second queries.
147e83
+
147e83
+   It is not supported to call this function with a non-NULL ANSP2
147e83
+   but a NULL ANSCP.  Put another way, you can call send_vc with a
147e83
+   single unmodifiable buffer or two modifiable buffers, but no other
147e83
+   combination is supported.
147e83
+
147e83
+   It is the caller's responsibility to free the malloc allocated
147e83
+   buffers by detecting that the pointers have changed from their
147e83
+   original values i.e. *ANSCP or *ANSP2 has changed.
147e83
+
147e83
+   If an answer is truncated because of UDP datagram DNS limits then
147e83
+   *V_CIRCUIT is set to 1 and the return value non-zero to indicate to
147e83
+   the caller to retry with TCP.  The value *GOTSOMEWHERE is set to 1
147e83
+   if any progress was made reading a response from the nameserver and
147e83
+   is used by the caller to distinguish between ECONNREFUSED and
147e83
+   ETIMEDOUT (the latter if *GOTSOMEWHERE is 1).
147e83
+
147e83
+   If errors are encountered then *TERRNO is set to an appropriate
147e83
+   errno value and a zero result is returned for a recoverable error,
147e83
+   and a less-than zero result is returned for a non-recoverable error.
147e83
+
147e83
+   If no errors are encountered then *TERRNO is left unmodified and
147e83
+   a the length of the first response in bytes is returned.  */
147e83
 static int
147e83
 send_dg(res_state statp,
147e83
 	const u_char *buf, int buflen, const u_char *buf2, int buflen2,
147e83
@@ -999,8 +1135,6 @@ send_dg(res_state statp,
147e83
 {
147e83
 	const HEADER *hp = (HEADER *) buf;
147e83
 	const HEADER *hp2 = (HEADER *) buf2;
147e83
-	u_char *ans = *ansp;
147e83
-	int orig_anssizp = *anssizp;
147e83
 	struct timespec now, timeout, finish;
147e83
 	struct pollfd pfd[1];
147e83
 	int ptimeout;
147e83
@@ -1033,6 +1167,8 @@ send_dg(res_state statp,
147e83
 	int need_recompute = 0;
147e83
 	int nwritten = 0;
147e83
 	int recvresp1 = 0;
147e83
+	/* Skip the second response if there is no second query.
147e83
+           To do that we mark the second response as received.  */
147e83
 	int recvresp2 = buf2 == NULL;
147e83
 	pfd[0].fd = EXT(statp).nssocks[ns];
147e83
 	pfd[0].events = POLLOUT;
147e83
@@ -1196,52 +1332,54 @@ send_dg(res_state statp,
147e83
 		int *thisresplenp;
147e83
 
147e83
 		if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
147e83
+			/* We have not received any responses
147e83
+			   yet or we only have one response to
147e83
+			   receive.  */
147e83
 			thisanssizp = anssizp;
147e83
 			thisansp = anscp ?: ansp;
147e83
 			assert (anscp != NULL || ansp2 == NULL);
147e83
 			thisresplenp = &resplen;
147e83
 		} else {
147e83
-			if (*anssizp != MAXPACKET) {
147e83
-				/* No buffer allocated for the first
147e83
-				   reply.  We can try to use the rest
147e83
-				   of the user-provided buffer.  */
147e83
-#ifdef _STRING_ARCH_unaligned
147e83
-				*anssizp2 = orig_anssizp - resplen;
147e83
-				*ansp2 = *ansp + resplen;
147e83
-#else
147e83
-				int aligned_resplen
147e83
-				  = ((resplen + __alignof__ (HEADER) - 1)
147e83
-				     & ~(__alignof__ (HEADER) - 1));
147e83
-				*anssizp2 = orig_anssizp - aligned_resplen;
147e83
-				*ansp2 = *ansp + aligned_resplen;
147e83
-#endif
147e83
-			} else {
147e83
-				/* The first reply did not fit into the
147e83
-				   user-provided buffer.  Maybe the second
147e83
-				   answer will.  */
147e83
-				*anssizp2 = orig_anssizp;
147e83
-				*ansp2 = *ansp;
147e83
-			}
147e83
-
147e83
 			thisanssizp = anssizp2;
147e83
 			thisansp = ansp2;
147e83
 			thisresplenp = resplen2;
147e83
 		}
147e83
 
147e83
 		if (*thisanssizp < MAXPACKET
147e83
-		    /* Yes, we test ANSCP here.  If we have two buffers
147e83
-		       both will be allocatable.  */
147e83
-		    && anscp
147e83
+		    /* If the current buffer is non-NULL and it's not
147e83
+		       pointing at the static user-supplied buffer then
147e83
+		       we can reallocate it.  */
147e83
+		    && (thisansp != NULL && thisansp != ansp)
147e83
+		    /* Is the size too small?  */
147e83
 		    && (ioctl (pfd[0].fd, FIONREAD, thisresplenp) < 0
147e83
-			|| *thisanssizp < *thisresplenp)) {
147e83
+			|| *thisanssizp < *thisresplenp)
147e83
+		    ) {
147e83
+			/* Always allocate MAXPACKET, callers expect
147e83
+			   this specific size.  */
147e83
 			u_char *newp = malloc (MAXPACKET);
147e83
 			if (newp != NULL) {
147e83
-				*anssizp = MAXPACKET;
147e83
-				*thisansp = ans = newp;
147e83
+				*thisanssizp = MAXPACKET;
147e83
+				*thisansp = newp;
147e83
 				if (thisansp == ansp2)
147e83
 				  *ansp2_malloced = 1;
147e83
 			}
147e83
 		}
147e83
+		/* We could end up with truncation if anscp was NULL
147e83
+		   (not allowed to change caller's buffer) and the
147e83
+		   response buffer size is too small.  This isn't a
147e83
+		   reliable way to detect truncation because the ioctl
147e83
+		   may be an inaccurate report of the UDP message size.
147e83
+		   Therefore we use this only to issue debug output.
147e83
+		   To do truncation accurately with UDP we need
147e83
+		   MSG_TRUNC which is only available on Linux.  We
147e83
+		   can abstract out the Linux-specific feature in the
147e83
+		   future to detect truncation.  */
147e83
+		if (__glibc_unlikely (*thisanssizp < *thisresplenp)) {
147e83
+			Dprint(statp->options & RES_DEBUG,
147e83
+			       (stdout, ";; response may be truncated (UDP)\n")
147e83
+			);
147e83
+		}
147e83
+
147e83
 		HEADER *anhp = (HEADER *) *thisansp;
147e83
 		socklen_t fromlen = sizeof(struct sockaddr_in6);
147e83
 		assert (sizeof(from) <= fromlen);