From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 15313 invoked from network); 20 Oct 2022 23:55:42 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 20 Oct 2022 23:55:42 -0000 Received: (qmail 12275 invoked by uid 550); 20 Oct 2022 23:55:38 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 12243 invoked from network); 20 Oct 2022 23:55:37 -0000 Date: Thu, 20 Oct 2022 19:55:22 -0400 From: Rich Felker To: Tom Shen Cc: musl@lists.openwall.com Message-ID: <20221020235521.GN29905@brightrain.aerifal.cx> References: <20221018172727.GK29905@brightrain.aerifal.cx> <20221019142452.GL29905@brightrain.aerifal.cx> <20221020005214.GM29905@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="GLp9dJVi+aaipsRk" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] gethostbyname2_r returns invalid IPv6 address if DNS server replies IPv4 address --GLp9dJVi+aaipsRk Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Oct 21, 2022 at 01:25:50AM +0800, Tom Shen wrote: > I tested the fix in your earlier email locally with my own test code and > getent in Alpine Linux 3.16.2. They both crashed. After debugging, I found > in gethostbyname2_r we should return non-zero if no address is returned. > Then gethostbyname2 will return NULL. I also check the Linux doc, it says: > > > Return Value > > The gethostbyname() and gethostbyaddr() functions return the hostent > structure or *a NULL pointer if an error occurs*. On error, the h_errno > variable holds an error number. When non-NULL, the return value may point > at static data, see the notes below. > > Based on your patch (except the "for (i=nq-1; i>=0; i--)"), I made a minor > change to address it. Tested with command getent hosts, it works well with > my CoreDNS. Although the h_errno is better to be NO_DATA rather than > HOST_NOT_FOUND, I think it's not a big issue. > > The diff file attached. This is a different bug you've found in gethostbyname2. It seems it was not updated when commit f081d5336a80b68d3e1bed789cc373c5c3d6699b fixed the return behavior of gethostbyname2_r to treat NODATA and NxDomain as success conditions rather than errors. Attached patch should fix it right. Rich --GLp9dJVi+aaipsRk Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-fix-return-value-of-gethostby-name-2-addr-with-no-re.patch" >From 8f9259450aa43a6fd539e428e61e2961b725fbae Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Thu, 20 Oct 2022 19:48:32 -0400 Subject: [PATCH] fix return value of gethostby{name[2],addr} with no result but no error commit f081d5336a80b68d3e1bed789cc373c5c3d6699b fixed gethostbyname[2]_r to treat negative results as a non-error, leaving gethostbyname[2] wrongly returning a pointer to the unfilled result buffer rather than a null pointer. since, as documented with commit fe82bb9b921be34370e6b71a1c6f062c20999ae0, the caller of gethostby{name[2],addr}_r can always rely on the result pointer being set, use that consistently rather than trying to duplicate logic about whether we have a result or not in gethostby{name[2],addr}. --- src/network/gethostbyaddr.c | 2 +- src/network/gethostbyname2.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/network/gethostbyaddr.c b/src/network/gethostbyaddr.c index 598e2241..c3cacaac 100644 --- a/src/network/gethostbyaddr.c +++ b/src/network/gethostbyaddr.c @@ -20,5 +20,5 @@ struct hostent *gethostbyaddr(const void *a, socklen_t l, int af) err = gethostbyaddr_r(a, l, af, h, (void *)(h+1), size-sizeof *h, &res, &h_errno); } while (err == ERANGE); - return err ? 0 : h; + return res; } diff --git a/src/network/gethostbyname2.c b/src/network/gethostbyname2.c index dc9d6621..bd0da7f8 100644 --- a/src/network/gethostbyname2.c +++ b/src/network/gethostbyname2.c @@ -21,5 +21,5 @@ struct hostent *gethostbyname2(const char *name, int af) err = gethostbyname2_r(name, af, h, (void *)(h+1), size-sizeof *h, &res, &h_errno); } while (err == ERANGE); - return err ? 0 : h; + return res; } -- 2.21.0 --GLp9dJVi+aaipsRk--