Blame SOURCES/dnsmasq-2.79-CVE-2020-25681.patch

ed3ca9
From 6689e336042d2ca0b075f8db1fe30ed6b47c7d4c Mon Sep 17 00:00:00 2001
ed3ca9
From: Simon Kelley <simon@thekelleys.org.uk>
ed3ca9
Date: Wed, 11 Nov 2020 23:25:04 +0000
ed3ca9
Subject: [PATCH 1/4] Fix remote buffer overflow CERT VU#434904
ed3ca9
ed3ca9
The problem is in the sort_rrset() function and allows a remote
ed3ca9
attacker to overwrite memory. Any dnsmasq instance with DNSSEC
ed3ca9
enabled is vulnerable.
ed3ca9
---
ed3ca9
 src/dnssec.c | 273 ++++++++++++++++++++++++++++-----------------------
ed3ca9
 1 file changed, 152 insertions(+), 121 deletions(-)
ed3ca9
ed3ca9
diff --git a/src/dnssec.c b/src/dnssec.c
ed3ca9
index 8143185..0c703ac 100644
ed3ca9
--- a/src/dnssec.c
ed3ca9
+++ b/src/dnssec.c
ed3ca9
@@ -222,138 +222,147 @@ static int check_date_range(u32 date_start, u32 date_end)
ed3ca9
     && serial_compare_32(curtime, date_end) == SERIAL_LT;
ed3ca9
 }
ed3ca9
 
ed3ca9
-/* Return bytes of canonicalised rdata, when the return value is zero, the remaining 
ed3ca9
-   data, pointed to by *p, should be used raw. */
ed3ca9
-static int get_rdata(struct dns_header *header, size_t plen, unsigned char *end, char *buff, int bufflen,
ed3ca9
-		     unsigned char **p, u16 **desc)
ed3ca9
+/* Return bytes of canonicalised rrdata one by one.
ed3ca9
+   Init state->ip with the RR, and state->end with the end of same.
ed3ca9
+   Init state->op to NULL.
ed3ca9
+   Init state->desc to RR descriptor.
ed3ca9
+   Init state->buff with a MAXDNAME * 2 buffer.
ed3ca9
+   
ed3ca9
+   After each call which returns 1, state->op points to the next byte of data.
ed3ca9
+   On returning 0, the end has been reached.
ed3ca9
+*/
ed3ca9
+struct rdata_state {
ed3ca9
+  u16 *desc;
ed3ca9
+  size_t c;
ed3ca9
+  unsigned char *end, *ip, *op;
ed3ca9
+  char *buff;
ed3ca9
+};
ed3ca9
+
ed3ca9
+static int get_rdata(struct dns_header *header, size_t plen, struct rdata_state *state)
ed3ca9
 {
ed3ca9
-  int d = **desc;
ed3ca9
+  int d;
ed3ca9
   
ed3ca9
-  /* No more data needs mangling */
ed3ca9
-  if (d == (u16)-1)
ed3ca9
+  if (state->op && state->c != 1)
ed3ca9
     {
ed3ca9
-      /* If there's more data than we have space for, just return what fits,
ed3ca9
-	 we'll get called again for more chunks */
ed3ca9
-      if (end - *p > bufflen)
ed3ca9
-	{
ed3ca9
-	  memcpy(buff, *p, bufflen);
ed3ca9
-	  *p += bufflen;
ed3ca9
-	  return bufflen;
ed3ca9
-	}
ed3ca9
-      
ed3ca9
-      return 0;
ed3ca9
+      state->op++;
ed3ca9
+      state->c--;
ed3ca9
+      return 1;
ed3ca9
     }
ed3ca9
- 
ed3ca9
-  (*desc)++;
ed3ca9
-  
ed3ca9
-  if (d == 0 && extract_name(header, plen, p, buff, 1, 0))
ed3ca9
-    /* domain-name, canonicalise */
ed3ca9
-    return to_wire(buff);
ed3ca9
-  else
ed3ca9
-    { 
ed3ca9
-      /* plain data preceding a domain-name, don't run off the end of the data */
ed3ca9
-      if ((end - *p) < d)
ed3ca9
-	d = end - *p;
ed3ca9
+
ed3ca9
+  while (1)
ed3ca9
+    {
ed3ca9
+      d = *(state->desc);
ed3ca9
       
ed3ca9
-      if (d != 0)
ed3ca9
+      if (d == (u16)-1)
ed3ca9
 	{
ed3ca9
-	  memcpy(buff, *p, d);
ed3ca9
-	  *p += d;
ed3ca9
+	  /* all the bytes to the end. */
ed3ca9
+	  if ((state->c = state->end - state->ip) != 0)
ed3ca9
+	    {
ed3ca9
+	      state->op = state->ip;
ed3ca9
+	      state->ip = state->end;;
ed3ca9
+	    }
ed3ca9
+	  else
ed3ca9
+	    return 0;
ed3ca9
+	}
ed3ca9
+      else
ed3ca9
+	{
ed3ca9
+	  state->desc++;
ed3ca9
+	  
ed3ca9
+	  if (d == (u16)0)
ed3ca9
+	    {
ed3ca9
+	      /* domain-name, canonicalise */
ed3ca9
+	      int len;
ed3ca9
+	      
ed3ca9
+	      if (!extract_name(header, plen, &state->ip, state->buff, 1, 0) ||
ed3ca9
+		  (len = to_wire(state->buff)) == 0)
ed3ca9
+		continue;
ed3ca9
+	      
ed3ca9
+	      state->c = len;
ed3ca9
+	      state->op = (unsigned char *)state->buff;
ed3ca9
+	    }
ed3ca9
+	  else
ed3ca9
+	    {
ed3ca9
+	      /* plain data preceding a domain-name, don't run off the end of the data */
ed3ca9
+	      if ((state->end - state->ip) < d)
ed3ca9
+		d = state->end - state->ip;
ed3ca9
+	      
ed3ca9
+	      if (d == 0)
ed3ca9
+		continue;
ed3ca9
+		  
ed3ca9
+	      state->op = state->ip;
ed3ca9
+	      state->c = d;
ed3ca9
+	      state->ip += d;
ed3ca9
+	    }
ed3ca9
 	}
ed3ca9
       
ed3ca9
-      return d;
ed3ca9
+      return 1;
ed3ca9
     }
ed3ca9
 }
ed3ca9
 
ed3ca9
-/* Bubble sort the RRset into the canonical order. 
ed3ca9
-   Note that the byte-streams from two RRs may get unsynced: consider 
ed3ca9
-   RRs which have two domain-names at the start and then other data.
ed3ca9
-   The domain-names may have different lengths in each RR, but sort equal
ed3ca9
-
ed3ca9
-   ------------
ed3ca9
-   |abcde|fghi|
ed3ca9
-   ------------
ed3ca9
-   |abcd|efghi|
ed3ca9
-   ------------
ed3ca9
-
ed3ca9
-   leaving the following bytes as deciding the order. Hence the nasty left1 and left2 variables.
ed3ca9
-*/
ed3ca9
+/* Bubble sort the RRset into the canonical order. */
ed3ca9
 
ed3ca9
 static int sort_rrset(struct dns_header *header, size_t plen, u16 *rr_desc, int rrsetidx, 
ed3ca9
 		      unsigned char **rrset, char *buff1, char *buff2)
ed3ca9
 {
ed3ca9
-  int swap, quit, i, j;
ed3ca9
+  int swap, i, j;
ed3ca9
   
ed3ca9
   do
ed3ca9
     {
ed3ca9
       for (swap = 0, i = 0; i < rrsetidx-1; i++)
ed3ca9
 	{
ed3ca9
-	  int rdlen1, rdlen2, left1, left2, len1, len2, len, rc;
ed3ca9
-	  u16 *dp1, *dp2;
ed3ca9
-	  unsigned char *end1, *end2;
ed3ca9
+	  int rdlen1, rdlen2;
ed3ca9
+	  struct rdata_state state1, state2;
ed3ca9
+	  
ed3ca9
 	  /* Note that these have been determined to be OK previously,
ed3ca9
 	     so we don't need to check for NULL return here. */
ed3ca9
-	  unsigned char *p1 = skip_name(rrset[i], header, plen, 10);
ed3ca9
-	  unsigned char *p2 = skip_name(rrset[i+1], header, plen, 10);
ed3ca9
-	  
ed3ca9
-	  p1 += 8; /* skip class, type, ttl */
ed3ca9
-	  GETSHORT(rdlen1, p1);
ed3ca9
-	  end1 = p1 + rdlen1;
ed3ca9
-	  
ed3ca9
-	  p2 += 8; /* skip class, type, ttl */
ed3ca9
-	  GETSHORT(rdlen2, p2);
ed3ca9
-	  end2 = p2 + rdlen2; 
ed3ca9
+	  state1.ip = skip_name(rrset[i], header, plen, 10);
ed3ca9
+	  state2.ip = skip_name(rrset[i+1], header, plen, 10);
ed3ca9
+	  state1.op = state2.op = NULL;
ed3ca9
+	  state1.buff = buff1;
ed3ca9
+	  state2.buff = buff2;
ed3ca9
+	  state1.desc = state2.desc = rr_desc;
ed3ca9
 	  
ed3ca9
-	  dp1 = dp2 = rr_desc;
ed3ca9
+	  state1.ip += 8; /* skip class, type, ttl */
ed3ca9
+	  GETSHORT(rdlen1, state1.ip);
ed3ca9
+	  if (!CHECK_LEN(header, state1.ip, plen, rdlen1))
ed3ca9
+	    return rrsetidx; /* short packet */
ed3ca9
+	  state1.end = state1.ip + rdlen1;
ed3ca9
 	  
ed3ca9
-	  for (quit = 0, left1 = 0, left2 = 0, len1 = 0, len2 = 0; !quit;)
ed3ca9
+	  state2.ip += 8; /* skip class, type, ttl */
ed3ca9
+	  GETSHORT(rdlen2, state2.ip);
ed3ca9
+	  if (!CHECK_LEN(header, state2.ip, plen, rdlen2))
ed3ca9
+	    return rrsetidx; /* short packet */
ed3ca9
+	  state2.end = state2.ip + rdlen2; 
ed3ca9
+	  	  
ed3ca9
+	  while (1)
ed3ca9
 	    {
ed3ca9
-	      if (left1 != 0)
ed3ca9
-		memmove(buff1, buff1 + len1 - left1, left1);
ed3ca9
+	      int ok1, ok2;
ed3ca9
 	      
ed3ca9
-	      if ((len1 = get_rdata(header, plen, end1, buff1 + left1, (MAXDNAME * 2) - left1, &p1, &dp1)) == 0)
ed3ca9
-		{
ed3ca9
-		  quit = 1;
ed3ca9
-		  len1 = end1 - p1;
ed3ca9
-		  memcpy(buff1 + left1, p1, len1);
ed3ca9
-		}
ed3ca9
-	      len1 += left1;
ed3ca9
-	      
ed3ca9
-	      if (left2 != 0)
ed3ca9
-		memmove(buff2, buff2 + len2 - left2, left2);
ed3ca9
-	      
ed3ca9
-	      if ((len2 = get_rdata(header, plen, end2, buff2 + left2, (MAXDNAME *2) - left2, &p2, &dp2)) == 0)
ed3ca9
-		{
ed3ca9
-		  quit = 1;
ed3ca9
-		  len2 = end2 - p2;
ed3ca9
-		  memcpy(buff2 + left2, p2, len2);
ed3ca9
-		}
ed3ca9
-	      len2 += left2;
ed3ca9
-	       
ed3ca9
-	      if (len1 > len2)
ed3ca9
-		left1 = len1 - len2, left2 = 0, len = len2;
ed3ca9
-	      else
ed3ca9
-		left2 = len2 - len1, left1 = 0, len = len1;
ed3ca9
-	      
ed3ca9
-	      rc = (len == 0) ? 0 : memcmp(buff1, buff2, len);
ed3ca9
-	      
ed3ca9
-	      if (rc > 0 || (rc == 0 && quit && len1 > len2))
ed3ca9
-		{
ed3ca9
-		  unsigned char *tmp = rrset[i+1];
ed3ca9
-		  rrset[i+1] = rrset[i];
ed3ca9
-		  rrset[i] = tmp;
ed3ca9
-		  swap = quit = 1;
ed3ca9
-		}
ed3ca9
-	      else if (rc == 0 && quit && len1 == len2)
ed3ca9
+	      ok1 = get_rdata(header, plen, &state1);
ed3ca9
+	      ok2 = get_rdata(header, plen, &state2);
ed3ca9
+
ed3ca9
+	      if (!ok1 && !ok2)
ed3ca9
 		{
ed3ca9
 		  /* Two RRs are equal, remove one copy. RFC 4034, para 6.3 */
ed3ca9
 		  for (j = i+1; j < rrsetidx-1; j++)
ed3ca9
 		    rrset[j] = rrset[j+1];
ed3ca9
 		  rrsetidx--;
ed3ca9
 		  i--;
ed3ca9
+		  break;
ed3ca9
+		}
ed3ca9
+	      else if (ok1 && (!ok2 || *state1.op > *state2.op)) 
ed3ca9
+		{
ed3ca9
+		  unsigned char *tmp = rrset[i+1];
ed3ca9
+		  rrset[i+1] = rrset[i];
ed3ca9
+		  rrset[i] = tmp;
ed3ca9
+		  swap = 1;
ed3ca9
+		  break;
ed3ca9
 		}
ed3ca9
-	      else if (rc < 0)
ed3ca9
-		quit = 1;
ed3ca9
+	      else if (ok2 && (!ok1 || *state2.op > *state1.op))
ed3ca9
+		break;
ed3ca9
+	      
ed3ca9
+	      /* arrive here when bytes are equal, go round the loop again
ed3ca9
+		 and compare the next ones. */
ed3ca9
 	    }
ed3ca9
 	}
ed3ca9
     } while (swap);
ed3ca9
@@ -549,15 +558,18 @@ static int validate_rrset(time_t now, struct dns_header *header, size_t plen, in
ed3ca9
       wire_len = to_wire(keyname);
ed3ca9
       hash->update(ctx, (unsigned int)wire_len, (unsigned char*)keyname);
ed3ca9
       from_wire(keyname);
ed3ca9
+
ed3ca9
+#define RRBUFLEN 300 /* Most RRs are smaller than this. */
ed3ca9
       
ed3ca9
       for (i = 0; i < rrsetidx; ++i)
ed3ca9
 	{
ed3ca9
-	  int seg;
ed3ca9
-	  unsigned char *end, *cp;
ed3ca9
-	  u16 len, *dp;
ed3ca9
+	  int j;
ed3ca9
+	  struct rdata_state state;
ed3ca9
+	  u16 len;
ed3ca9
+	  unsigned char rrbuf[RRBUFLEN];
ed3ca9
 	  
ed3ca9
 	  p = rrset[i];
ed3ca9
-	 	  
ed3ca9
+	  
ed3ca9
 	  if (!extract_name(header, plen, &p, name, 1, 10)) 
ed3ca9
 	    return STAT_BOGUS;
ed3ca9
 
ed3ca9
@@ -566,12 +578,11 @@ static int validate_rrset(time_t now, struct dns_header *header, size_t plen, in
ed3ca9
 	  /* if more labels than in RRsig name, hash *.<no labels in rrsig labels field>  4035 5.3.2 */
ed3ca9
 	  if (labels < name_labels)
ed3ca9
 	    {
ed3ca9
-	      int k;
ed3ca9
-	      for (k = name_labels - labels; k != 0; k--)
ed3ca9
+	      for (j = name_labels - labels; j != 0; j--)
ed3ca9
 		{
ed3ca9
 		  while (*name_start != '.' && *name_start != 0)
ed3ca9
 		    name_start++;
ed3ca9
-		  if (k != 1 && *name_start == '.')
ed3ca9
+		  if (j != 1 && *name_start == '.')
ed3ca9
 		    name_start++;
ed3ca9
 		}
ed3ca9
 	      
ed3ca9
@@ -592,24 +603,44 @@ static int validate_rrset(time_t now, struct dns_header *header, size_t plen, in
ed3ca9
 	  if (!CHECK_LEN(header, p, plen, rdlen))
ed3ca9
 	    return STAT_BOGUS; 
ed3ca9
 	  
ed3ca9
-	  end = p + rdlen;
ed3ca9
+	  /* canonicalise rdata and calculate length of same, use 
ed3ca9
+	     name buffer as workspace for get_rdata. */
ed3ca9
+	  state.ip = p;
ed3ca9
+	  state.op = NULL;
ed3ca9
+	  state.desc = rr_desc;
ed3ca9
+	  state.buff = name;
ed3ca9
+	  state.end = p + rdlen;
ed3ca9
 	  
ed3ca9
-	  /* canonicalise rdata and calculate length of same, use name buffer as workspace.
ed3ca9
-	     Note that name buffer is twice MAXDNAME long in DNSSEC mode. */
ed3ca9
-	  cp = p;
ed3ca9
-	  dp = rr_desc;
ed3ca9
-	  for (len = 0; (seg = get_rdata(header, plen, end, name, MAXDNAME * 2, &cp, &dp)) != 0; len += seg);
ed3ca9
-	  len += end - cp;
ed3ca9
-	  len = htons(len);
ed3ca9
+	  for (j = 0; get_rdata(header, plen, &state); j++)
ed3ca9
+	    if (j < RRBUFLEN)
ed3ca9
+	      rrbuf[j] = *state.op;
ed3ca9
+
ed3ca9
+	  len = htons((u16)j);
ed3ca9
 	  hash->update(ctx, 2, (unsigned char *)&len;; 
ed3ca9
+
ed3ca9
+	  /* If the RR is shorter than RRBUFLEN (most of them, in practice)
ed3ca9
+	     then we can just digest it now. If it exceeds RRBUFLEN we have to
ed3ca9
+	     go back to the start and do it in chunks. */
ed3ca9
+	  if (j >= RRBUFLEN)
ed3ca9
+	    {
ed3ca9
+	      state.ip = p;
ed3ca9
+	      state.op = NULL;
ed3ca9
+	      state.desc = rr_desc;
ed3ca9
+
ed3ca9
+	      for (j = 0; get_rdata(header, plen, &state); j++)
ed3ca9
+		{
ed3ca9
+		   rrbuf[j] = *state.op;
ed3ca9
+
ed3ca9
+		   if (j == RRBUFLEN - 1)
ed3ca9
+		     {
ed3ca9
+		       hash->update(ctx, RRBUFLEN, rrbuf);
ed3ca9
+		       j = -1;
ed3ca9
+		     }
ed3ca9
+		}
ed3ca9
+	    }
ed3ca9
 	  
ed3ca9
-	  /* Now canonicalise again and digest. */
ed3ca9
-	  cp = p;
ed3ca9
-	  dp = rr_desc;
ed3ca9
-	  while ((seg = get_rdata(header, plen, end, name, MAXDNAME * 2, &cp, &dp)))
ed3ca9
-	    hash->update(ctx, seg, (unsigned char *)name);
ed3ca9
-	  if (cp != end)
ed3ca9
-	    hash->update(ctx, end - cp, cp);
ed3ca9
+	  if (j != 0)
ed3ca9
+	    hash->update(ctx, j, rrbuf);
ed3ca9
 	}
ed3ca9
      
ed3ca9
       hash->digest(ctx, hash->digest_size, digest);
ed3ca9
-- 
ed3ca9
2.26.2
ed3ca9