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