1 From 257ac0c5f7732cbc6aa96fdd3b06602234593aca Mon Sep 17 00:00:00 2001
2 From: Simon Kelley <simon@thekelleys.org.uk>
3 Date: Thu, 12 Nov 2020 18:49:23 +0000
4 Subject: Check destination of DNS UDP query replies.
6 At any time, dnsmasq will have a set of sockets open, bound to
7 random ports, on which it sends queries to upstream nameservers.
8 This patch fixes the existing problem that a reply for ANY in-flight
9 query would be accepted via ANY open port, which increases the
10 chances of an attacker flooding answers "in the blind" in an
11 attempt to poison the DNS cache. CERT VU#434904 refers.
14 src/forward.c | 37 ++++++++++++++++++++++++++++---------
15 2 files changed, 33 insertions(+), 10 deletions(-)
20 dnsmasq with DNSSEC compiled in and enabled is vulnerable to this,
21 referenced by CERT VU#434904.
23 + Be sure to only accept UDP DNS query replies at the address
24 + from which the query was originated. This keeps as much entropy
25 + in the {query-ID, random-port} tuple as possible, help defeat
26 + cache poisoning attacks. Refer: CERT VU#434904.
29 ->>>>>>> Fix remote buffer overflow CERT VU#434904
31 Impove cache behaviour for TCP connections. For ease of
32 implementaion, dnsmasq has always forked a new process to handle
39 -static struct frec *lookup_frec(unsigned short id, void *hash);
40 +static struct frec *lookup_frec(unsigned short id, int fd, int family, void *hash);
41 static struct frec *lookup_frec_by_sender(unsigned short id,
42 union mysockaddr *addr,
44 @@ -797,7 +797,7 @@ void reply_query(int fd, int family, tim
45 crc = questions_crc(header, n, daemon->namebuff);
48 - if (!(forward = lookup_frec(ntohs(header->id), hash)))
49 + if (!(forward = lookup_frec(ntohs(header->id), fd, family, hash)))
53 @@ -2289,14 +2289,25 @@ struct frec *get_new_frec(time_t now, in
56 /* crc is all-ones if not known. */
57 -static struct frec *lookup_frec(unsigned short id, void *hash)
58 +static struct frec *lookup_frec(unsigned short id, int fd, int family, void *hash)
62 for(f = daemon->frec_list; f; f = f->next)
63 if (f->sentto && f->new_id == id &&
64 (!hash || memcmp(hash, f->hash, HASH_SIZE) == 0))
67 + /* sent from random port */
68 + if (family == AF_INET && f->rfd4 && f->rfd4->fd == fd)
71 + if (family == AF_INET6 && f->rfd6 && f->rfd6->fd == fd)
74 + /* sent to upstream from bound socket. */
75 + if (f->sentto->sfd && f->sentto->sfd->fd == fd)
81 @@ -2357,12 +2368,20 @@ void server_gone(struct server *server)
82 static unsigned short get_id(void)
84 unsigned short ret = 0;
89 - while (lookup_frec(ret, NULL));
96 + /* ensure id is unique. */
97 + for (f = daemon->frec_list; f; f = f->next)
98 + if (f->sentto && f->new_id == ret)