mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: [musl] res_msend_rc problem with IPv6?
Date: Tue, 12 Apr 2022 18:39:50 +0200	[thread overview]
Message-ID: <20220412163950.GD8499@voyager> (raw)

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

                 reply	other threads:[~2022-04-12 16:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220412163950.GD8499@voyager \
    --to=nullplan@gmx.net \
    --cc=musl@lists.openwall.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).