mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Tom Shen <sjiagc.dev@gmail.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] gethostbyname2_r returns invalid IPv6 address if DNS server replies IPv4 address
Date: Thu, 20 Oct 2022 19:55:22 -0400	[thread overview]
Message-ID: <20221020235521.GN29905@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAFBikZ6Ktt5UDkoyywLJ2wkO1NVx_+WYqc04eBnW2jUjVqsd1g@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1246 bytes --]

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

[-- Attachment #2: 0001-fix-return-value-of-gethostby-name-2-addr-with-no-re.patch --]
[-- Type: text/plain, Size: 1713 bytes --]

From 8f9259450aa43a6fd539e428e61e2961b725fbae Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
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


      reply	other threads:[~2022-10-20 23:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-18 16:02 Tom Shen
2022-10-18 17:27 ` Rich Felker
2022-10-19  3:44   ` Tom Shen
2022-10-19 14:24     ` Rich Felker
2022-10-19 16:00       ` Tom Shen
2022-10-20  0:52         ` Rich Felker
2022-10-20 17:25           ` Tom Shen
2022-10-20 23:55             ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221020235521.GN29905@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=sjiagc.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).