Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20220412163950.GD8499@voyager>
Date: Tue, 12 Apr 2022 18:39:50 +0200
From: Markus Wichmann <nullplan@....net>
To: musl@...ts.openwall.com
Subject: res_msend_rc problem with IPv6?

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.