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