Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f3c42075 authored by Ken Chen's avatar Ken Chen
Browse files

Don't use 5353 source port for mDNS queries

RFC 6762 section 5.1 says that one-shot Multicast DNS queries MUST NOT
be sent using UDP source port 5353, since using UDP source port 5353
signals the presence of a fully compliant Multicast DNS querier.

DNS resolver works as a one-shot Multicast DNS querier, which send
queries from random port and does not fully implement Multicast DNS
features. The 5353 source port should be excluded from random port
selection.

Bug: 227147672
Test: atest
Change-Id: I05d426c179182d3f08b037cb1bab3a43ea501063
parent 637d7718
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -253,7 +253,7 @@ static struct timespec evNowTime(void) {
// END: Code copied from ISC eventlib

/* BIONIC-BEGIN: implement source port randomization */
static int random_bind(int s, int family) {
static int random_bind(int s, int family, bool isMdns) {
    sockaddr_union u;
    int j;
    socklen_t slen;
@@ -279,6 +279,10 @@ static int random_bind(int s, int family) {
    for (j = 0; j < 10; j++) {
        /* find a random port between 1025 .. 65534 */
        int port = 1025 + (arc4random_uniform(65535 - 1025));
        // RFC 6762 section 5.1: Don't use 5353 source port on one-shot Multicast DNS queries. DNS
        // resolver does not fully compliant mDNS.
        if (isMdns && port == 5353) continue;

        if (family == AF_INET)
            u.sin.sin_port = htons(port);
        else
@@ -775,7 +779,7 @@ same_ns:
            }
        }
        errno = 0;
        if (random_bind(statp->tcp_nssock, nsap->sa_family) < 0) {
        if (random_bind(statp->tcp_nssock, nsap->sa_family, false /* isMdns */) < 0) {
            *terrno = errno;
            dump_error("bind/vc", nsap);
            statp->closeSockets();
@@ -1078,7 +1082,9 @@ static int setupUdpSocket(ResState* statp, const sockaddr* sockap, unique_fd* fd
        }
    }

    if (random_bind(*fd_out, sockap->sa_family) < 0) {
    const auto addr = IPSockAddr::toIPSockAddr(*sockap);
    const bool isMdns = (addr == mdns_addrs[0] || addr == mdns_addrs[1]);
    if (random_bind(*fd_out, sockap->sa_family, isMdns) < 0) {
        *terrno = errno;
        dump_error("bind", sockap);
        return 0;