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;
|fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
|/* Handle case where system lacks IPv6 support */
|if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
|	fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|	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
if-statement?

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,
either.

Ciao,
Markus

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