dnsmasq: Backport some security updates
[librecmc/librecmc.git] / package / network / services / dnsmasq / patches / 0109-Handle-caching-with-EDNS-options-better.patch
1 From 25e63f1e56f5acdcf91893a1b92ad1e0f2f552d8 Mon Sep 17 00:00:00 2001
2 From: Simon Kelley <simon@thekelleys.org.uk>
3 Date: Wed, 25 Nov 2020 21:17:52 +0000
4 Subject: Handle caching with EDNS options better.
5
6 If we add the EDNS client subnet option, or the client's
7 MAC address, then the reply we get back may very depending on
8 that. Since the cache is ignorant of such things, it's not safe to
9 cache such replies. This patch determines when a dangerous EDNS
10 option is being added and disables caching.
11
12 Note that for much the same reason, we can't combine multiple
13 queries for the same question when dangerous EDNS options are
14 being added, and the code now handles that in the same way. This
15 query combining is required for security against cache poisoning,
16 so disabling the cache has a security function as well as a
17 correctness one.
18 ---
19  man/dnsmasq.8 |  4 +--
20  src/dnsmasq.h |  3 ++-
21  src/edns0.c   | 75 ++++++++++++++++++++++++++++++++-------------------
22  src/forward.c | 41 ++++++++++++++++++----------
23  4 files changed, 78 insertions(+), 45 deletions(-)
24
25 --- a/man/dnsmasq.8
26 +++ b/man/dnsmasq.8
27 @@ -690,8 +690,8 @@ still marks the request so that no upstr
28  address information either. The default is zero for both IPv4 and
29  IPv6. Note that upstream nameservers may be configured to return
30  different results based on this information, but the dnsmasq cache
31 -does not take account. If a dnsmasq instance is configured such that
32 -different results may be encountered, caching should be disabled.
33 +does not take account. Caching is therefore disabled for such replies,
34 +unless the subnet address being added is constant.
35  
36  For example,
37  .B --add-subnet=24,96
38 --- a/src/dnsmasq.h
39 +++ b/src/dnsmasq.h
40 @@ -644,6 +644,7 @@ struct hostsfile {
41  #define FREC_TEST_PKTSZ       256
42  #define FREC_HAS_EXTRADATA    512
43  #define FREC_HAS_PHEADER     1024
44 +#define FREC_NO_CACHE        2048
45  
46  #define HASH_SIZE 32 /* SHA-256 digest size */
47  
48 @@ -1628,7 +1629,7 @@ size_t add_pseudoheader(struct dns_heade
49                         unsigned short udp_sz, int optno, unsigned char *opt, size_t optlen, int set_do, int replace);
50  size_t add_do_bit(struct dns_header *header, size_t plen, unsigned char *limit);
51  size_t add_edns0_config(struct dns_header *header, size_t plen, unsigned char *limit, 
52 -                       union mysockaddr *source, time_t now, int *check_subnet);
53 +                       union mysockaddr *source, time_t now, int *check_subnet, int *cacheable);
54  int check_source(struct dns_header *header, size_t plen, unsigned char *pseudoheader, union mysockaddr *peer);
55  
56  /* arp.c */
57 --- a/src/edns0.c
58 +++ b/src/edns0.c
59 @@ -264,7 +264,8 @@ static void encoder(unsigned char *in, c
60    out[3] = char64(in[2]);
61  }
62  
63 -static size_t add_dns_client(struct dns_header *header, size_t plen, unsigned char *limit, union mysockaddr *l3, time_t now)
64 +static size_t add_dns_client(struct dns_header *header, size_t plen, unsigned char *limit,
65 +                            union mysockaddr *l3, time_t now, int *cacheablep)
66  {
67    int maclen, replace = 2; /* can't get mac address, just delete any incoming. */
68    unsigned char mac[DHCP_CHADDR_MAX];
69 @@ -273,6 +274,7 @@ static size_t add_dns_client(struct dns_
70    if ((maclen = find_mac(l3, mac, 1, now)) == 6)
71      {
72        replace = 1;
73 +      *cacheablep = 0;
74  
75        if (option_bool(OPT_MAC_HEX))
76         print_mac(encode, mac, maclen);
77 @@ -288,14 +290,18 @@ static size_t add_dns_client(struct dns_
78  }
79  
80  
81 -static size_t add_mac(struct dns_header *header, size_t plen, unsigned char *limit, union mysockaddr *l3, time_t now)
82 +static size_t add_mac(struct dns_header *header, size_t plen, unsigned char *limit,
83 +                     union mysockaddr *l3, time_t now, int *cacheablep)
84  {
85    int maclen;
86    unsigned char mac[DHCP_CHADDR_MAX];
87  
88    if ((maclen = find_mac(l3, mac, 1, now)) != 0)
89 -    plen = add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_MAC, mac, maclen, 0, 0); 
90 -    
91 +    {
92 +      *cacheablep = 0;
93 +      plen = add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_MAC, mac, maclen, 0, 0); 
94 +    }
95 +  
96    return plen; 
97  }
98  
99 @@ -313,17 +319,18 @@ static void *get_addrp(union mysockaddr
100    return &addr->in.sin_addr;
101  }
102  
103 -static size_t calc_subnet_opt(struct subnet_opt *opt, union mysockaddr *source)
104 +static size_t calc_subnet_opt(struct subnet_opt *opt, union mysockaddr *source, int *cacheablep)
105  {
106    /* http://tools.ietf.org/html/draft-vandergaast-edns-client-subnet-02 */
107    
108    int len;
109    void *addrp = NULL;
110    int sa_family = source->sa.sa_family;
111 -
112 +  int cacheable = 0;
113 +  
114    opt->source_netmask = 0;
115    opt->scope_netmask = 0;
116 -
117 +    
118    if (source->sa.sa_family == AF_INET6 && daemon->add_subnet6)
119      {
120        opt->source_netmask = daemon->add_subnet6->mask;
121 @@ -331,6 +338,7 @@ static size_t calc_subnet_opt(struct sub
122         {
123           sa_family = daemon->add_subnet6->addr.sa.sa_family;
124           addrp = get_addrp(&daemon->add_subnet6->addr, sa_family);
125 +         cacheable = 1;
126         } 
127        else 
128         addrp = &source->in6.sin6_addr;
129 @@ -343,6 +351,7 @@ static size_t calc_subnet_opt(struct sub
130         {
131           sa_family = daemon->add_subnet4->addr.sa.sa_family;
132           addrp = get_addrp(&daemon->add_subnet4->addr, sa_family);
133 +         cacheable = 1; /* Address is constant */
134         } 
135         else 
136           addrp = &source->in.sin_addr;
137 @@ -350,8 +359,6 @@ static size_t calc_subnet_opt(struct sub
138    
139    opt->family = htons(sa_family == AF_INET6 ? 2 : 1);
140    
141 -  len = 0;
142 -  
143    if (addrp && opt->source_netmask != 0)
144      {
145        len = ((opt->source_netmask - 1) >> 3) + 1;
146 @@ -359,18 +366,26 @@ static size_t calc_subnet_opt(struct sub
147        if (opt->source_netmask & 7)
148         opt->addr[len-1] &= 0xff << (8 - (opt->source_netmask & 7));
149      }
150 +  else
151 +    {
152 +      cacheable = 1; /* No address ever supplied. */
153 +      len = 0;
154 +    }
155 +
156 +  if (cacheablep)
157 +    *cacheablep = cacheable;
158    
159    return len + 4;
160  }
161   
162 -static size_t add_source_addr(struct dns_header *header, size_t plen, unsigned char *limit, union mysockaddr *source)
163 +static size_t add_source_addr(struct dns_header *header, size_t plen, unsigned char *limit, union mysockaddr *source, int *cacheable)
164  {
165    /* http://tools.ietf.org/html/draft-vandergaast-edns-client-subnet-02 */
166    
167    int len;
168    struct subnet_opt opt;
169    
170 -  len = calc_subnet_opt(&opt, source);
171 +  len = calc_subnet_opt(&opt, source, cacheable);
172    return add_pseudoheader(header, plen, (unsigned char *)limit, PACKETSZ, EDNS0_OPTION_CLIENT_SUBNET, (unsigned char *)&opt, len, 0, 0);
173  }
174  
175 @@ -383,18 +398,18 @@ int check_source(struct dns_header *head
176    unsigned char *p;
177    int code, i, rdlen;
178    
179 -   calc_len = calc_subnet_opt(&opt, peer);
180 -   
181 -   if (!(p = skip_name(pseudoheader, header, plen, 10)))
182 -     return 1;
183 -   
184 -   p += 8; /* skip UDP length and RCODE */
185 +  calc_len = calc_subnet_opt(&opt, peer, NULL);
186     
187 -   GETSHORT(rdlen, p);
188 -   if (!CHECK_LEN(header, p, plen, rdlen))
189 -     return 1; /* bad packet */
190 -   
191 -   /* check if option there */
192 +  if (!(p = skip_name(pseudoheader, header, plen, 10)))
193 +    return 1;
194 +  
195 +  p += 8; /* skip UDP length and RCODE */
196 +  
197 +  GETSHORT(rdlen, p);
198 +  if (!CHECK_LEN(header, p, plen, rdlen))
199 +    return 1; /* bad packet */
200 +  
201 +  /* check if option there */
202     for (i = 0; i + 4 < rdlen; i += len + 4)
203       {
204         GETSHORT(code, p);
205 @@ -412,24 +427,28 @@ int check_source(struct dns_header *head
206     return 1;
207  }
208  
209 +/* Set *check_subnet if we add a client subnet option, which needs to checked 
210 +   in the reply. Set *cacheable to zero if we add an option which the answer
211 +   may depend on. */
212  size_t add_edns0_config(struct dns_header *header, size_t plen, unsigned char *limit, 
213 -                       union mysockaddr *source, time_t now, int *check_subnet)    
214 +                       union mysockaddr *source, time_t now, int *check_subnet, int *cacheable)    
215  {
216    *check_subnet = 0;
217 -
218 +  *cacheable = 1;
219 +  
220    if (option_bool(OPT_ADD_MAC))
221 -    plen  = add_mac(header, plen, limit, source, now);
222 +    plen  = add_mac(header, plen, limit, source, now, cacheable);
223    
224    if (option_bool(OPT_MAC_B64) || option_bool(OPT_MAC_HEX))
225 -    plen = add_dns_client(header, plen, limit, source, now);
226 -
227 +    plen = add_dns_client(header, plen, limit, source, now, cacheable);
228 +  
229    if (daemon->dns_client_id)
230      plen = add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_NOMCPEID, 
231                             (unsigned char *)daemon->dns_client_id, strlen(daemon->dns_client_id), 0, 1);
232    
233    if (option_bool(OPT_CLIENT_SUBNET))
234      {
235 -      plen = add_source_addr(header, plen, limit, source); 
236 +      plen = add_source_addr(header, plen, limit, source, cacheable); 
237        *check_subnet = 1;
238      }
239           
240 --- a/src/forward.c
241 +++ b/src/forward.c
242 @@ -344,13 +344,10 @@ static int forward_query(int udpfd, unio
243      {
244        /* Query from new source, but the same query may be in progress
245          from another source. If so, just add this client to the
246 -        list that will get the reply.
247 +        list that will get the reply.*/
248          
249 -        Note that is the EDNS client subnet option is in use, we can't do this,
250 -        as the clients (and therefore query EDNS options) will be different
251 -        for each query. The EDNS subnet code has checks to avoid
252 -        attacks in this case. */
253 -      if (!option_bool(OPT_CLIENT_SUBNET) && (forward = lookup_frec_by_query(hash, fwd_flags)))
254 +      if (!option_bool(OPT_ADD_MAC) && !option_bool(OPT_MAC_B64) &&
255 +         (forward = lookup_frec_by_query(hash, fwd_flags)))
256         {
257           /* Note whine_malloc() zeros memory. */
258           if (!daemon->free_frec_src &&
259 @@ -447,18 +444,21 @@ static int forward_query(int udpfd, unio
260    if (!flags && forward)
261      {
262        struct server *firstsentto = start;
263 -      int subnet, forwarded = 0;
264 +      int subnet, cacheable, forwarded = 0;
265        size_t edns0_len;
266        unsigned char *pheader;
267        
268        /* If a query is retried, use the log_id for the retry when logging the answer. */
269        forward->frec_src.log_id = daemon->log_id;
270        
271 -      plen = add_edns0_config(header, plen, ((unsigned char *)header) + PACKETSZ, &forward->frec_src.source, now, &subnet);
272 +      plen = add_edns0_config(header, plen, ((unsigned char *)header) + PACKETSZ, &forward->frec_src.source, now, &subnet, &cacheable);
273        
274        if (subnet)
275         forward->flags |= FREC_HAS_SUBNET;
276 -      
277 +
278 +      if (!cacheable)
279 +       forward->flags |= FREC_NO_CACHE;
280 +
281  #ifdef HAVE_DNSSEC
282        if (option_bool(OPT_DNSSEC_VALID) && do_dnssec)
283         {
284 @@ -642,7 +642,7 @@ static size_t process_reply(struct dns_h
285         }
286      }
287  #endif
288 -  
289 +
290    if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign, NULL)))
291      {
292        /* Get extended RCODE. */
293 @@ -1244,6 +1244,11 @@ void reply_query(int fd, int family, tim
294         header->hb4 |= HB4_CD;
295        else
296         header->hb4 &= ~HB4_CD;
297 +
298 +      /* Never cache answers which are contingent on the source or MAC address EDSN0 option,
299 +        since the cache is ignorant of such things. */
300 +      if (forward->flags & FREC_NO_CACHE)
301 +       no_cache_dnssec = 1;
302        
303        if ((nn = process_reply(header, now, forward->sentto, (size_t)n, check_rebind, no_cache_dnssec, cache_secure, bogusanswer, 
304                               forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION, 
305 @@ -1788,7 +1793,7 @@ unsigned char *tcp_request(int confd, ti
306    int local_auth = 0;
307  #endif
308    int checking_disabled, do_bit, added_pheader = 0, have_pseudoheader = 0;
309 -  int check_subnet, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0;
310 +  int check_subnet, cacheable, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0;
311    size_t m;
312    unsigned short qtype;
313    unsigned int gotname;
314 @@ -1959,7 +1964,7 @@ unsigned char *tcp_request(int confd, ti
315               char *domain = NULL;
316               unsigned char *oph = find_pseudoheader(header, size, NULL, NULL, NULL, NULL);
317  
318 -             size = add_edns0_config(header, size, ((unsigned char *) header) + 65536, &peer_addr, now, &check_subnet);
319 +             size = add_edns0_config(header, size, ((unsigned char *) header) + 65536, &peer_addr, now, &check_subnet, &cacheable);
320  
321               if (gotname)
322                 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
323 @@ -2122,6 +2127,11 @@ unsigned char *tcp_request(int confd, ti
324                           break;
325                         }
326  
327 +                     /* Never cache answers which are contingent on the source or MAC address EDSN0 option,
328 +                        since the cache is ignorant of such things. */
329 +                     if (!cacheable)
330 +                       no_cache_dnssec = 1;
331 +                     
332                       m = process_reply(header, now, last_server, (unsigned int)m, 
333                                         option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec, cache_secure, bogusanswer,
334                                         ad_reqd, do_bit, added_pheader, check_subnet, &peer_addr); 
335 @@ -2385,10 +2395,13 @@ static struct frec *lookup_frec_by_query
336    struct frec *f;
337  
338    /* FREC_DNSKEY and FREC_DS_QUERY are never set in flags, so the test below 
339 -     ensures that no frec created for internal DNSSEC query can be returned here. */
340 +     ensures that no frec created for internal DNSSEC query can be returned here.
341 +     
342 +     Similarly FREC_NO_CACHE is never set in flags, so a query which is
343 +     contigent on a particular source address EDNS0 option will never be matched. */
344  
345  #define FLAGMASK (FREC_CHECKING_DISABLED | FREC_AD_QUESTION | FREC_DO_QUESTION \
346 -                 | FREC_HAS_PHEADER | FREC_DNSKEY_QUERY | FREC_DS_QUERY)
347 +                 | FREC_HAS_PHEADER | FREC_DNSKEY_QUERY | FREC_DS_QUERY | FREC_NO_CACHE)
348    
349    for(f = daemon->frec_list; f; f = f->next)
350      if (f->sentto &&