Fix UDP upstream socket bind before connect

Connecting an unbound UDP socket implicitly assigns a local address and
port. A subsequent bind() fails with EINVAL when min-port, max-port or
an explicit source address requires local_bind() to bind the socket.

Bind the local socket and set the outgoing interface before connecting
to the upstream server. Preserve the existing error reporting.

Fixes: cc32bce779 ("connect() UDP sockets to upstream DNS servers to mitigate SAD DNS attack.")
This commit is contained in:
pedro authored and Simon Kelley committed 2026-12-04 21:10:15 +00:00
1 parent c936b17bc7
commit 6aee2a45f8
1 file changed
+7 -4
+7 -4
View File
@@ -2847,10 +2847,13 @@ static int random_sock(struct server *s)
}
}
if (connect(fd, &s->addr.sa, sa_len(&s->addr)) == -1)
connecterr = 1;
else if (local_bind(fd, &s->source_addr, s->interface, s->ifindex, 0))
return fd;
if (local_bind(fd, &s->source_addr, s->interface, s->ifindex, 0))
{
if (connect(fd, &s->addr.sa, sa_len(&s->addr)) == -1)
connecterr = 1;
else
return fd;
}
/* don't log errors due to running out of available ports, we handle those. */
if (connecterr || !sockaddr_isnull(&s->source_addr) || errno != EADDRINUSE)