mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] res_msend_rc problem with IPv6?
@ 2022-04-12 16:39 Markus Wichmann
  0 siblings, 0 replies; only message in thread
From: Markus Wichmann @ 2022-04-12 16:39 UTC (permalink / raw)
  To: musl

Hi all,

I don't know if this is intentional, but while reading the code for the
DNS resolver (I was trying to figure out if the musl resolver uses a
randomized source port. It doesn't), I noticed a problem in the code.
__res_msend_rc() contains these lines at the start:

|/* Get local address and open/bind a socket */
|sa.sin.sin_family = family;
|/* Handle case where system lacks IPv6 support */
|if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
|	family = AF_INET;
|if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) {
|	if (fd >= 0) close(fd);
|	pthread_setcancelstate(cs, 0);
|	return -1;

If "family" is AF_INET6, and the kernel returns EAFNOSUPPORT, the bind()
call will always fail. This is because in that case, "fd" will refer to
an IPv4 socket, but both "sa" and "sl" will reflect an IPv6 address.
Binding an IPv4 socket to an IPv6 address does not work. I tested it!

It is, of course, entirely possible that you do not want res_msend() to
succeed if resolv.conf contains an IPv6 address, but the kernel does not
support IPv6. In that case, might I suggest making this explicit in the

In the event this was not intentional, a possible solution would be to
set "sl" back to the length of an IPv4 address inside of the
if-statement and moving the assignment to "sa.sin.sin_family" behind the
if-statement. Then later on in the send loop, you could just skip the
sendto() for all addresses with the wrong address family, since in the
case where we are sending to IPv6, all IPv4 addresses have been
converted already, so the only possible mismatches would be IPv6
addresses when sending to IPv4. Or you could just not bother and let
these sendto() calls fail silently. In either case, these numbers cannot
possibly come up as addresses received from, and so cannot be resent to,


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-12 16:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-12 16:39 [musl] res_msend_rc problem with IPv6? Markus Wichmann

Code repositories for project(s) associated with this public inbox

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).