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