From patchwork Fri Mar 8 20:46:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 31791 Received: (qmail 22562 invoked by alias); 8 Mar 2019 20:46:31 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 22449 invoked by uid 89); 8 Mar 2019 20:46:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx1.redhat.com Date: Fri, 08 Mar 2019 21:46:25 +0100 To: libc-alpha@sourceware.org Subject: [PATCH] nss_dns: Adjust ns_name_ntop calls for gethostby*, getaddrinfo User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20190308204625.7864F80DD6B5@oldenburg2.str.redhat.com> From: Florian Weimer ns_name_ntop failure always means that the output buffer is not large enough. 2019-03-08 Florian Weimer * resolv/nss_dns/dns-host.c (getanswer_r, gaih_getanswer_slice): On ns_name_ntop failure, the buffer is always too small. diff --git a/resolv/nss_dns/dns-host.c b/resolv/nss_dns/dns-host.c index a18b8a6bf4..f04e08016c 100644 --- a/resolv/nss_dns/dns-host.c +++ b/resolv/nss_dns/dns-host.c @@ -698,23 +698,18 @@ getanswer_r (struct resolv_context *ctx, n = __ns_name_unpack (answer->buf, end_of_message, cp, packtmp, sizeof packtmp); - if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) + if (__glibc_unlikely (n < 0)) { - if (__glibc_unlikely (errno == EMSGSIZE)) - goto too_small; - - n = -1; + *errnop = errno; + *h_errnop = NO_RECOVERY; + return NSS_STATUS_UNAVAIL; } + if (__ns_name_ntop (packtmp, bp, linebuflen) < 0) + goto too_small; - if (n > 0 && bp[0] == '.') + if (bp[0] == '.') bp[0] = '\0'; - if (__glibc_unlikely (n < 0)) - { - *errnop = errno; - *h_errnop = NO_RECOVERY; - return NSS_STATUS_UNAVAIL; - } if (__glibc_unlikely (name_ok (bp) == 0)) { errno = EBADMSG; @@ -761,15 +756,16 @@ getanswer_r (struct resolv_context *ctx, n = __ns_name_unpack (answer->buf, end_of_message, cp, packtmp, sizeof packtmp); - if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) + if (n < 0) { - if (__glibc_unlikely (errno == EMSGSIZE)) - goto too_small; - - n = -1; + ++had_error; + continue; } - if (__glibc_unlikely (n < 0 || (*name_ok) (bp) == 0)) + if (__ns_name_ntop (packtmp, bp, linebuflen) < 0) + goto too_small; + + if (__glibc_unlikely ((*name_ok) (bp) == 0)) { ++had_error; continue; @@ -892,15 +888,14 @@ getanswer_r (struct resolv_context *ctx, n = __ns_name_unpack (answer->buf, end_of_message, cp, packtmp, sizeof packtmp); - if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) + if (n < 0) { - if (__glibc_unlikely (errno == EMSGSIZE)) - goto too_small; - - n = -1; + ++had_error; + break; } - - if (__glibc_unlikely (n < 0 || res_hnok (bp) == 0)) + if (__ns_name_ntop (packtmp, bp, linebuflen) < 0) + goto too_small; + if (__glibc_unlikely (res_hnok (bp) == 0)) { ++had_error; break; @@ -1032,27 +1027,22 @@ gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname, u_char packtmp[NS_MAXCDNAME]; int n = __ns_name_unpack (answer->buf, end_of_message, cp, packtmp, sizeof packtmp); + if (__glibc_unlikely (n < 0)) + { + *errnop = errno; + *h_errnop = NO_RECOVERY; + return NSS_STATUS_UNAVAIL; + } /* We unpack the name to check it for validity. But we do not need it later. */ - if (n != -1 && __ns_name_ntop (packtmp, buffer, buflen) == -1) + if (__ns_name_ntop (packtmp, buffer, buflen) < 0) { - if (__glibc_unlikely (errno == EMSGSIZE)) - { - too_small: - *errnop = ERANGE; - *h_errnop = NETDB_INTERNAL; - return NSS_STATUS_TRYAGAIN; - } - - n = -1; + too_small: + *errnop = ERANGE; + *h_errnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; } - if (__glibc_unlikely (n < 0)) - { - *errnop = errno; - *h_errnop = NO_RECOVERY; - return NSS_STATUS_UNAVAIL; - } if (__glibc_unlikely (res_hnok (buffer) == 0)) { errno = EBADMSG; @@ -1078,15 +1068,14 @@ gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname, { n = __ns_name_unpack (answer->buf, end_of_message, cp, packtmp, sizeof packtmp); - if (n != -1 && - (h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) == -1) + if (n < 0) { - if (__glibc_unlikely (errno == EMSGSIZE)) - goto too_small; - - n = -1; + ++had_error; + continue; } - if (__glibc_unlikely (n < 0 || res_hnok (buffer) == 0)) + if ((h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) < 0) + goto too_small; + if (__glibc_unlikely (res_hnok (buffer) == 0)) { ++had_error; continue;