Dash:
[t2-trunk.git] / architecture / sparc / package / glibc / CVE-2015-7547.patch
blob27bc6a15b3ffb2d2f0a2bc24d104a48d4fdbd18d
1 From b995d95a5943785be3ab862b2d3276f3b4a22481 Mon Sep 17 00:00:00 2001
2 From: Carlos O'Donell <carlos@systemhalted.org>
3 Date: Tue, 16 Feb 2016 21:26:37 -0500
4 Subject: [PATCH] CVE-2015-7547: getaddrinfo() stack-based buffer overflow (Bug
5 18665).
7 * A stack-based buffer overflow was found in libresolv when invoked from
8 libnss_dns, allowing specially crafted DNS responses to seize control
9 of execution flow in the DNS client. The buffer overflow occurs in
10 the functions send_dg (send datagram) and send_vc (send TCP) for the
11 NSS module libnss_dns.so.2 when calling getaddrinfo with AF_UNSPEC
12 family. The use of AF_UNSPEC triggers the low-level resolver code to
13 send out two parallel queries for A and AAAA. A mismanagement of the
14 buffers used for those queries could result in the response of a query
15 writing beyond the alloca allocated buffer created by
16 _nss_dns_gethostbyname4_r. Buffer management is simplified to remove
17 the overflow. Thanks to the Google Security Team and Red Hat for
18 reporting the security impact of this issue, and Robert Holiday of
19 Ciena for reporting the related bug 18665. (CVE-2015-7547)
21 See also:
22 https://sourceware.org/ml/libc-alpha/2016-02/msg00416.html
23 https://sourceware.org/ml/libc-alpha/2016-02/msg00418.html
25 (cherry picked from commit e9db92d3acfe1822d56d11abcea5bfc4c41cf6ca)
26 ---
27 ChangeLog | 15 +++
28 NEWS | 14 +++
29 resolv/nss_dns/dns-host.c | 111 ++++++++++++++++++-
30 resolv/res_query.c | 3 +
31 resolv/res_send.c | 264 ++++++++++++++++++++++++++++++++++------------
32 5 files changed, 338 insertions(+), 69 deletions(-)
34 diff --git a/resolv/nss_dns/dns-host.c b/resolv/nss_dns/dns-host.c
35 index 357ac04..a0fe9a8 100644
36 --- a/resolv/nss_dns/dns-host.c
37 +++ b/resolv/nss_dns/dns-host.c
38 @@ -1031,7 +1031,10 @@ gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname,
39 int h_namelen = 0;
41 if (ancount == 0)
42 - return NSS_STATUS_NOTFOUND;
43 + {
44 + *h_errnop = HOST_NOT_FOUND;
45 + return NSS_STATUS_NOTFOUND;
46 + }
48 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
50 @@ -1208,7 +1211,14 @@ gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname,
51 /* Special case here: if the resolver sent a result but it only
52 contains a CNAME while we are looking for a T_A or T_AAAA record,
53 we fail with NOTFOUND instead of TRYAGAIN. */
54 - return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
55 + if (canon != NULL)
56 + {
57 + *h_errnop = HOST_NOT_FOUND;
58 + return NSS_STATUS_NOTFOUND;
59 + }
61 + *h_errnop = NETDB_INTERNAL;
62 + return NSS_STATUS_TRYAGAIN;
66 @@ -1222,11 +1232,101 @@ gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2,
68 enum nss_status status = NSS_STATUS_NOTFOUND;
70 + /* Combining the NSS status of two distinct queries requires some
71 + compromise and attention to symmetry (A or AAAA queries can be
72 + returned in any order). What follows is a breakdown of how this
73 + code is expected to work and why. We discuss only SUCCESS,
74 + TRYAGAIN, NOTFOUND and UNAVAIL, since they are the only returns
75 + that apply (though RETURN and MERGE exist). We make a distinction
76 + between TRYAGAIN (recoverable) and TRYAGAIN' (not-recoverable).
77 + A recoverable TRYAGAIN is almost always due to buffer size issues
78 + and returns ERANGE in errno and the caller is expected to retry
79 + with a larger buffer.
81 + Lastly, you may be tempted to make significant changes to the
82 + conditions in this code to bring about symmetry between responses.
83 + Please don't change anything without due consideration for
84 + expected application behaviour. Some of the synthesized responses
85 + aren't very well thought out and sometimes appear to imply that
86 + IPv4 responses are always answer 1, and IPv6 responses are always
87 + answer 2, but that's not true (see the implementation of send_dg
88 + and send_vc to see response can arrive in any order, particularly
89 + for UDP). However, we expect it holds roughly enough of the time
90 + that this code works, but certainly needs to be fixed to make this
91 + a more robust implementation.
93 + ----------------------------------------------
94 + | Answer 1 Status / | Synthesized | Reason |
95 + | Answer 2 Status | Status | |
96 + |--------------------------------------------|
97 + | SUCCESS/SUCCESS | SUCCESS | [1] |
98 + | SUCCESS/TRYAGAIN | TRYAGAIN | [5] |
99 + | SUCCESS/TRYAGAIN' | SUCCESS | [1] |
100 + | SUCCESS/NOTFOUND | SUCCESS | [1] |
101 + | SUCCESS/UNAVAIL | SUCCESS | [1] |
102 + | TRYAGAIN/SUCCESS | TRYAGAIN | [2] |
103 + | TRYAGAIN/TRYAGAIN | TRYAGAIN | [2] |
104 + | TRYAGAIN/TRYAGAIN' | TRYAGAIN | [2] |
105 + | TRYAGAIN/NOTFOUND | TRYAGAIN | [2] |
106 + | TRYAGAIN/UNAVAIL | TRYAGAIN | [2] |
107 + | TRYAGAIN'/SUCCESS | SUCCESS | [3] |
108 + | TRYAGAIN'/TRYAGAIN | TRYAGAIN | [3] |
109 + | TRYAGAIN'/TRYAGAIN' | TRYAGAIN' | [3] |
110 + | TRYAGAIN'/NOTFOUND | TRYAGAIN' | [3] |
111 + | TRYAGAIN'/UNAVAIL | UNAVAIL | [3] |
112 + | NOTFOUND/SUCCESS | SUCCESS | [3] |
113 + | NOTFOUND/TRYAGAIN | TRYAGAIN | [3] |
114 + | NOTFOUND/TRYAGAIN' | TRYAGAIN' | [3] |
115 + | NOTFOUND/NOTFOUND | NOTFOUND | [3] |
116 + | NOTFOUND/UNAVAIL | UNAVAIL | [3] |
117 + | UNAVAIL/SUCCESS | UNAVAIL | [4] |
118 + | UNAVAIL/TRYAGAIN | UNAVAIL | [4] |
119 + | UNAVAIL/TRYAGAIN' | UNAVAIL | [4] |
120 + | UNAVAIL/NOTFOUND | UNAVAIL | [4] |
121 + | UNAVAIL/UNAVAIL | UNAVAIL | [4] |
122 + ----------------------------------------------
124 + [1] If the first response is a success we return success.
125 + This ignores the state of the second answer and in fact
126 + incorrectly sets errno and h_errno to that of the second
127 + answer. However because the response is a success we ignore
128 + *errnop and *h_errnop (though that means you touched errno on
129 + success). We are being conservative here and returning the
130 + likely IPv4 response in the first answer as a success.
132 + [2] If the first response is a recoverable TRYAGAIN we return
133 + that instead of looking at the second response. The
134 + expectation here is that we have failed to get an IPv4 response
135 + and should retry both queries.
137 + [3] If the first response was not a SUCCESS and the second
138 + response is not NOTFOUND (had a SUCCESS, need to TRYAGAIN,
139 + or failed entirely e.g. TRYAGAIN' and UNAVAIL) then use the
140 + result from the second response, otherwise the first responses
141 + status is used. Again we have some odd side-effects when the
142 + second response is NOTFOUND because we overwrite *errnop and
143 + *h_errnop that means that a first answer of NOTFOUND might see
144 + its *errnop and *h_errnop values altered. Whether it matters
145 + in practice that a first response NOTFOUND has the wrong
146 + *errnop and *h_errnop is undecided.
148 + [4] If the first response is UNAVAIL we return that instead of
149 + looking at the second response. The expectation here is that
150 + it will have failed similarly e.g. configuration failure.
152 + [5] Testing this code is complicated by the fact that truncated
153 + second response buffers might be returned as SUCCESS if the
154 + first answer is a SUCCESS. To fix this we add symmetry to
155 + TRYAGAIN with the second response. If the second response
156 + is a recoverable error we now return TRYAGIN even if the first
157 + response was SUCCESS. */
159 if (anslen1 > 0)
160 status = gaih_getanswer_slice(answer1, anslen1, qname,
161 &pat, &buffer, &buflen,
162 errnop, h_errnop, ttlp,
163 &first);
165 if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND
166 || (status == NSS_STATUS_TRYAGAIN
167 /* We want to look at the second answer in case of an
168 @@ -1242,8 +1342,15 @@ gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2,
169 &pat, &buffer, &buflen,
170 errnop, h_errnop, ttlp,
171 &first);
172 + /* Use the second response status in some cases. */
173 if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND)
174 status = status2;
175 + /* Do not return a truncated second response (unless it was
176 + unavoidable e.g. unrecoverable TRYAGAIN). */
177 + if (status == NSS_STATUS_SUCCESS
178 + && (status2 == NSS_STATUS_TRYAGAIN
179 + && *errnop == ERANGE && *h_errnop != NO_RECOVERY))
180 + status = NSS_STATUS_TRYAGAIN;
183 return status;
184 diff --git a/resolv/res_query.c b/resolv/res_query.c
185 index 4a9b3b3..95470a9 100644
186 --- a/resolv/res_query.c
187 +++ b/resolv/res_query.c
188 @@ -396,6 +396,7 @@ __libc_res_nsearch(res_state statp,
190 free (*answerp2);
191 *answerp2 = NULL;
192 + *nanswerp2 = 0;
193 *answerp2_malloced = 0;
196 @@ -447,6 +448,7 @@ __libc_res_nsearch(res_state statp,
198 free (*answerp2);
199 *answerp2 = NULL;
200 + *nanswerp2 = 0;
201 *answerp2_malloced = 0;
204 @@ -521,6 +523,7 @@ __libc_res_nsearch(res_state statp,
206 free (*answerp2);
207 *answerp2 = NULL;
208 + *nanswerp2 = 0;
209 *answerp2_malloced = 0;
211 if (saved_herrno != -1)
212 diff --git a/resolv/res_send.c b/resolv/res_send.c
213 index 5e53cc2..6511bb1 100644
214 --- a/resolv/res_send.c
215 +++ b/resolv/res_send.c
216 @@ -1,3 +1,20 @@
217 +/* Copyright (C) 2016 Free Software Foundation, Inc.
218 + This file is part of the GNU C Library.
220 + The GNU C Library is free software; you can redistribute it and/or
221 + modify it under the terms of the GNU Lesser General Public
222 + License as published by the Free Software Foundation; either
223 + version 2.1 of the License, or (at your option) any later version.
225 + The GNU C Library is distributed in the hope that it will be useful,
226 + but WITHOUT ANY WARRANTY; without even the implied warranty of
227 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
228 + Lesser General Public License for more details.
230 + You should have received a copy of the GNU Lesser General Public
231 + License along with the GNU C Library; if not, see
232 + <http://www.gnu.org/licenses/>. */
235 * Copyright (c) 1985, 1989, 1993
236 * The Regents of the University of California. All rights reserved.
237 @@ -363,6 +380,8 @@ __libc_res_nsend(res_state statp, const u_char *buf, int buflen,
238 #ifdef USE_HOOKS
239 if (__glibc_unlikely (statp->qhook || statp->rhook)) {
240 if (anssiz < MAXPACKET && ansp) {
241 + /* Always allocate MAXPACKET, callers expect
242 + this specific size. */
243 u_char *buf = malloc (MAXPACKET);
244 if (buf == NULL)
245 return (-1);
246 @@ -638,6 +657,77 @@ get_nsaddr (res_state statp, int n)
247 return (struct sockaddr *) (void *) &statp->nsaddr_list[n];
250 +/* The send_vc function is responsible for sending a DNS query over TCP
251 + to the nameserver numbered NS from the res_state STATP i.e.
252 + EXT(statp).nssocks[ns]. The function supports sending both IPv4 and
253 + IPv6 queries at the same serially on the same socket.
255 + Please note that for TCP there is no way to disable sending both
256 + queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP
257 + and sends the queries serially and waits for the result after each
258 + sent query. This implemetnation should be corrected to honour these
259 + options.
261 + Please also note that for TCP we send both queries over the same
262 + socket one after another. This technically violates best practice
263 + since the server is allowed to read the first query, respond, and
264 + then close the socket (to service another client). If the server
265 + does this, then the remaining second query in the socket data buffer
266 + will cause the server to send the client an RST which will arrive
267 + asynchronously and the client's OS will likely tear down the socket
268 + receive buffer resulting in a potentially short read and lost
269 + response data. This will force the client to retry the query again,
270 + and this process may repeat until all servers and connection resets
271 + are exhausted and then the query will fail. It's not known if this
272 + happens with any frequency in real DNS server implementations. This
273 + implementation should be corrected to use two sockets by default for
274 + parallel queries.
276 + The query stored in BUF of BUFLEN length is sent first followed by
277 + the query stored in BUF2 of BUFLEN2 length. Queries are sent
278 + serially on the same socket.
280 + Answers to the query are stored firstly in *ANSP up to a max of
281 + *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
282 + is non-NULL (to indicate that modifying the answer buffer is allowed)
283 + then malloc is used to allocate a new response buffer and ANSCP and
284 + ANSP will both point to the new buffer. If more than *ANSSIZP bytes
285 + are needed but ANSCP is NULL, then as much of the response as
286 + possible is read into the buffer, but the results will be truncated.
287 + When truncation happens because of a small answer buffer the DNS
288 + packets header field TC will bet set to 1, indicating a truncated
289 + message and the rest of the socket data will be read and discarded.
291 + Answers to the query are stored secondly in *ANSP2 up to a max of
292 + *ANSSIZP2 bytes, with the actual response length stored in
293 + *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
294 + is non-NULL (required for a second query) then malloc is used to
295 + allocate a new response buffer, *ANSSIZP2 is set to the new buffer
296 + size and *ANSP2_MALLOCED is set to 1.
298 + The ANSP2_MALLOCED argument will eventually be removed as the
299 + change in buffer pointer can be used to detect the buffer has
300 + changed and that the caller should use free on the new buffer.
302 + Note that the answers may arrive in any order from the server and
303 + therefore the first and second answer buffers may not correspond to
304 + the first and second queries.
306 + It is not supported to call this function with a non-NULL ANSP2
307 + but a NULL ANSCP. Put another way, you can call send_vc with a
308 + single unmodifiable buffer or two modifiable buffers, but no other
309 + combination is supported.
311 + It is the caller's responsibility to free the malloc allocated
312 + buffers by detecting that the pointers have changed from their
313 + original values i.e. *ANSCP or *ANSP2 has changed.
315 + If errors are encountered then *TERRNO is set to an appropriate
316 + errno value and a zero result is returned for a recoverable error,
317 + and a less-than zero result is returned for a non-recoverable error.
319 + If no errors are encountered then *TERRNO is left unmodified and
320 + a the length of the first response in bytes is returned. */
321 static int
322 send_vc(res_state statp,
323 const u_char *buf, int buflen, const u_char *buf2, int buflen2,
324 @@ -647,11 +737,7 @@ send_vc(res_state statp,
326 const HEADER *hp = (HEADER *) buf;
327 const HEADER *hp2 = (HEADER *) buf2;
328 - u_char *ans = *ansp;
329 - int orig_anssizp = *anssizp;
330 - // XXX REMOVE
331 - // int anssiz = *anssizp;
332 - HEADER *anhp = (HEADER *) ans;
333 + HEADER *anhp = (HEADER *) *ansp;
334 struct sockaddr *nsap = get_nsaddr (statp, ns);
335 int truncating, connreset, n;
336 /* On some architectures compiler might emit a warning indicating
337 @@ -743,6 +829,8 @@ send_vc(res_state statp,
338 * Receive length & response
340 int recvresp1 = 0;
341 + /* Skip the second response if there is no second query.
342 + To do that we mark the second response as received. */
343 int recvresp2 = buf2 == NULL;
344 uint16_t rlen16;
345 read_len:
346 @@ -779,40 +867,14 @@ send_vc(res_state statp,
347 u_char **thisansp;
348 int *thisresplenp;
349 if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
350 + /* We have not received any responses
351 + yet or we only have one response to
352 + receive. */
353 thisanssizp = anssizp;
354 thisansp = anscp ?: ansp;
355 assert (anscp != NULL || ansp2 == NULL);
356 thisresplenp = &resplen;
357 } else {
358 - if (*anssizp != MAXPACKET) {
359 - /* No buffer allocated for the first
360 - reply. We can try to use the rest
361 - of the user-provided buffer. */
362 -#if __GNUC_PREREQ (4, 7)
363 - DIAG_PUSH_NEEDS_COMMENT;
364 - DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
365 -#endif
366 -#if _STRING_ARCH_unaligned
367 - *anssizp2 = orig_anssizp - resplen;
368 - *ansp2 = *ansp + resplen;
369 -#else
370 - int aligned_resplen
371 - = ((resplen + __alignof__ (HEADER) - 1)
372 - & ~(__alignof__ (HEADER) - 1));
373 - *anssizp2 = orig_anssizp - aligned_resplen;
374 - *ansp2 = *ansp + aligned_resplen;
375 -#endif
376 -#if __GNUC_PREREQ (4, 7)
377 - DIAG_POP_NEEDS_COMMENT;
378 -#endif
379 - } else {
380 - /* The first reply did not fit into the
381 - user-provided buffer. Maybe the second
382 - answer will. */
383 - *anssizp2 = orig_anssizp;
384 - *ansp2 = *ansp;
387 thisanssizp = anssizp2;
388 thisansp = ansp2;
389 thisresplenp = resplen2;
390 @@ -820,10 +882,14 @@ send_vc(res_state statp,
391 anhp = (HEADER *) *thisansp;
393 *thisresplenp = rlen;
394 - if (rlen > *thisanssizp) {
395 - /* Yes, we test ANSCP here. If we have two buffers
396 - both will be allocatable. */
397 - if (__glibc_likely (anscp != NULL)) {
398 + /* Is the answer buffer too small? */
399 + if (*thisanssizp < rlen) {
400 + /* If the current buffer is not the the static
401 + user-supplied buffer then we can reallocate
402 + it. */
403 + if (thisansp != NULL && thisansp != ansp) {
404 + /* Always allocate MAXPACKET, callers expect
405 + this specific size. */
406 u_char *newp = malloc (MAXPACKET);
407 if (newp == NULL) {
408 *terrno = ENOMEM;
409 @@ -835,6 +901,9 @@ send_vc(res_state statp,
410 if (thisansp == ansp2)
411 *ansp2_malloced = 1;
412 anhp = (HEADER *) newp;
413 + /* A uint16_t can't be larger than MAXPACKET
414 + thus it's safe to allocate MAXPACKET but
415 + read RLEN bytes instead. */
416 len = rlen;
417 } else {
418 Dprint(statp->options & RES_DEBUG,
419 @@ -997,6 +1066,66 @@ reopen (res_state statp, int *terrno, int ns)
420 return 1;
423 +/* The send_dg function is responsible for sending a DNS query over UDP
424 + to the nameserver numbered NS from the res_state STATP i.e.
425 + EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries
426 + along with the ability to send the query in parallel for both stacks
427 + (default) or serially (RES_SINGLKUP). It also supports serial lookup
428 + with a close and reopen of the socket used to talk to the server
429 + (RES_SNGLKUPREOP) to work around broken name servers.
431 + The query stored in BUF of BUFLEN length is sent first followed by
432 + the query stored in BUF2 of BUFLEN2 length. Queries are sent
433 + in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP).
435 + Answers to the query are stored firstly in *ANSP up to a max of
436 + *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
437 + is non-NULL (to indicate that modifying the answer buffer is allowed)
438 + then malloc is used to allocate a new response buffer and ANSCP and
439 + ANSP will both point to the new buffer. If more than *ANSSIZP bytes
440 + are needed but ANSCP is NULL, then as much of the response as
441 + possible is read into the buffer, but the results will be truncated.
442 + When truncation happens because of a small answer buffer the DNS
443 + packets header field TC will bet set to 1, indicating a truncated
444 + message, while the rest of the UDP packet is discarded.
446 + Answers to the query are stored secondly in *ANSP2 up to a max of
447 + *ANSSIZP2 bytes, with the actual response length stored in
448 + *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
449 + is non-NULL (required for a second query) then malloc is used to
450 + allocate a new response buffer, *ANSSIZP2 is set to the new buffer
451 + size and *ANSP2_MALLOCED is set to 1.
453 + The ANSP2_MALLOCED argument will eventually be removed as the
454 + change in buffer pointer can be used to detect the buffer has
455 + changed and that the caller should use free on the new buffer.
457 + Note that the answers may arrive in any order from the server and
458 + therefore the first and second answer buffers may not correspond to
459 + the first and second queries.
461 + It is not supported to call this function with a non-NULL ANSP2
462 + but a NULL ANSCP. Put another way, you can call send_vc with a
463 + single unmodifiable buffer or two modifiable buffers, but no other
464 + combination is supported.
466 + It is the caller's responsibility to free the malloc allocated
467 + buffers by detecting that the pointers have changed from their
468 + original values i.e. *ANSCP or *ANSP2 has changed.
470 + If an answer is truncated because of UDP datagram DNS limits then
471 + *V_CIRCUIT is set to 1 and the return value non-zero to indicate to
472 + the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1
473 + if any progress was made reading a response from the nameserver and
474 + is used by the caller to distinguish between ECONNREFUSED and
475 + ETIMEDOUT (the latter if *GOTSOMEWHERE is 1).
477 + If errors are encountered then *TERRNO is set to an appropriate
478 + errno value and a zero result is returned for a recoverable error,
479 + and a less-than zero result is returned for a non-recoverable error.
481 + If no errors are encountered then *TERRNO is left unmodified and
482 + a the length of the first response in bytes is returned. */
483 static int
484 send_dg(res_state statp,
485 const u_char *buf, int buflen, const u_char *buf2, int buflen2,
486 @@ -1006,8 +1135,6 @@ send_dg(res_state statp,
488 const HEADER *hp = (HEADER *) buf;
489 const HEADER *hp2 = (HEADER *) buf2;
490 - u_char *ans = *ansp;
491 - int orig_anssizp = *anssizp;
492 struct timespec now, timeout, finish;
493 struct pollfd pfd[1];
494 int ptimeout;
495 @@ -1040,6 +1167,8 @@ send_dg(res_state statp,
496 int need_recompute = 0;
497 int nwritten = 0;
498 int recvresp1 = 0;
499 + /* Skip the second response if there is no second query.
500 + To do that we mark the second response as received. */
501 int recvresp2 = buf2 == NULL;
502 pfd[0].fd = EXT(statp).nssocks[ns];
503 pfd[0].events = POLLOUT;
504 @@ -1203,55 +1332,56 @@ send_dg(res_state statp,
505 int *thisresplenp;
507 if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
508 + /* We have not received any responses
509 + yet or we only have one response to
510 + receive. */
511 thisanssizp = anssizp;
512 thisansp = anscp ?: ansp;
513 assert (anscp != NULL || ansp2 == NULL);
514 thisresplenp = &resplen;
515 } else {
516 - if (*anssizp != MAXPACKET) {
517 - /* No buffer allocated for the first
518 - reply. We can try to use the rest
519 - of the user-provided buffer. */
520 -#if _STRING_ARCH_unaligned
521 - *anssizp2 = orig_anssizp - resplen;
522 - *ansp2 = *ansp + resplen;
523 -#else
524 - int aligned_resplen
525 - = ((resplen + __alignof__ (HEADER) - 1)
526 - & ~(__alignof__ (HEADER) - 1));
527 - *anssizp2 = orig_anssizp - aligned_resplen;
528 - *ansp2 = *ansp + aligned_resplen;
529 -#endif
530 - } else {
531 - /* The first reply did not fit into the
532 - user-provided buffer. Maybe the second
533 - answer will. */
534 - *anssizp2 = orig_anssizp;
535 - *ansp2 = *ansp;
538 thisanssizp = anssizp2;
539 thisansp = ansp2;
540 thisresplenp = resplen2;
543 if (*thisanssizp < MAXPACKET
544 - /* Yes, we test ANSCP here. If we have two buffers
545 - both will be allocatable. */
546 - && anscp
547 + /* If the current buffer is not the the static
548 + user-supplied buffer then we can reallocate
549 + it. */
550 + && (thisansp != NULL && thisansp != ansp)
551 #ifdef FIONREAD
552 + /* Is the size too small? */
553 && (ioctl (pfd[0].fd, FIONREAD, thisresplenp) < 0
554 || *thisanssizp < *thisresplenp)
555 #endif
557 + /* Always allocate MAXPACKET, callers expect
558 + this specific size. */
559 u_char *newp = malloc (MAXPACKET);
560 if (newp != NULL) {
561 - *anssizp = MAXPACKET;
562 - *thisansp = ans = newp;
563 + *thisanssizp = MAXPACKET;
564 + *thisansp = newp;
565 if (thisansp == ansp2)
566 *ansp2_malloced = 1;
569 + /* We could end up with truncation if anscp was NULL
570 + (not allowed to change caller's buffer) and the
571 + response buffer size is too small. This isn't a
572 + reliable way to detect truncation because the ioctl
573 + may be an inaccurate report of the UDP message size.
574 + Therefore we use this only to issue debug output.
575 + To do truncation accurately with UDP we need
576 + MSG_TRUNC which is only available on Linux. We
577 + can abstract out the Linux-specific feature in the
578 + future to detect truncation. */
579 + if (__glibc_unlikely (*thisanssizp < *thisresplenp)) {
580 + Dprint(statp->options & RES_DEBUG,
581 + (stdout, ";; response may be truncated (UDP)\n")
582 + );
585 HEADER *anhp = (HEADER *) *thisansp;
586 socklen_t fromlen = sizeof(struct sockaddr_in6);
587 assert (sizeof(from) <= fromlen);
589 1.9.4