dnsmasq: Backport some security updates
[librecmc/librecmc.git] / package / network / services / dnsmasq / patches / 0103-Check-destination-of-DNS-UDP-query-replies.patch
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.
5
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.
12 ---
13  CHANGELOG     |  6 +++++-
14  src/forward.c | 37 ++++++++++++++++++++++++++++---------
15  2 files changed, 33 insertions(+), 10 deletions(-)
16
17 --- a/CHANGELOG
18 +++ b/CHANGELOG
19 @@ -2,8 +2,12 @@
20         dnsmasq with DNSSEC compiled in and enabled is vulnerable to this,
21         referenced by CERT VU#434904.
22  
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.
27 +
28  
29 ->>>>>>> Fix remote buffer overflow CERT VU#434904
30  version 2.81
31         Impove cache behaviour for TCP connections. For ease of
32         implementaion, dnsmasq has always forked a new process to handle
33 --- a/src/forward.c
34 +++ b/src/forward.c
35 @@ -16,7 +16,7 @@
36  
37  #include "dnsmasq.h"
38  
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,
43                                           void *hash);
44 @@ -797,7 +797,7 @@ void reply_query(int fd, int family, tim
45    crc = questions_crc(header, n, daemon->namebuff);
46  #endif
47    
48 -  if (!(forward = lookup_frec(ntohs(header->id), hash)))
49 +  if (!(forward = lookup_frec(ntohs(header->id), fd, family, hash)))
50      return;
51    
52  #ifdef HAVE_DUMPFILE
53 @@ -2289,14 +2289,25 @@ struct frec *get_new_frec(time_t now, in
54  }
55  
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)
59  {
60    struct frec *f;
61  
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))
65 -      return f;
66 +      {
67 +       /* sent from random port */
68 +       if (family == AF_INET && f->rfd4 && f->rfd4->fd == fd)
69 +         return f;
70 +
71 +       if (family == AF_INET6 && f->rfd6 && f->rfd6->fd == fd)
72 +         return f;
73 +
74 +       /* sent to upstream from bound socket. */
75 +       if (f->sentto->sfd && f->sentto->sfd->fd == fd)
76 +         return f;
77 +      }
78        
79    return NULL;
80  }
81 @@ -2357,12 +2368,20 @@ void server_gone(struct server *server)
82  static unsigned short get_id(void)
83  {
84    unsigned short ret = 0;
85 +  struct frec *f;
86    
87 -  do 
88 -    ret = rand16();
89 -  while (lookup_frec(ret, NULL));
90 -  
91 -  return ret;
92 +  while (1)
93 +    {
94 +      ret = rand16();
95 +
96 +      /* ensure id is unique. */
97 +      for (f = daemon->frec_list; f; f = f->next)
98 +       if (f->sentto && f->new_id == ret)
99 +         break;
100 +
101 +      if (!f)
102 +       return ret;
103 +    }
104  }
105  
106