e87dd3
From 8868a04895b27d42d42e364f1a0c0196c1505b04 Mon Sep 17 00:00:00 2001
e87dd3
From: Simon Kelley <simon@thekelleys.org.uk>
e87dd3
Date: Mon, 25 Sep 2017 18:17:11 +0100
e87dd3
Subject: [PATCH 1/9]     Security fix, CVE-2017-14491 DNS heap buffer
e87dd3
 overflow.
e87dd3
e87dd3
    Fix heap overflow in DNS code. This is a potentially serious
e87dd3
    security hole. It allows an attacker who can make DNS
e87dd3
    requests to dnsmasq, and who controls the contents of
e87dd3
    a domain, which is thereby queried, to overflow
e87dd3
    (by 2 bytes) a heap buffer and either crash, or
e87dd3
    even take control of, dnsmasq.
e87dd3
---
e87dd3
 src/dnsmasq.h |  2 +-
e87dd3
 src/dnssec.c  |  2 +-
e87dd3
 src/option.c  |  2 +-
e87dd3
 src/rfc1035.c | 50 +++++++++++++++++++++++++++++++++++++++++---------
e87dd3
 src/rfc2131.c |  4 ++--
e87dd3
 src/rfc3315.c |  4 ++--
e87dd3
 src/util.c    |  7 ++++++-
e87dd3
 7 files changed, 54 insertions(+), 17 deletions(-)
e87dd3
e87dd3
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
e87dd3
index 1179492..06e5579 100644
e87dd3
--- a/src/dnsmasq.h
e87dd3
+++ b/src/dnsmasq.h
e87dd3
@@ -1162,7 +1162,7 @@ u32 rand32(void);
e87dd3
 u64 rand64(void);
e87dd3
 int legal_hostname(char *c);
e87dd3
 char *canonicalise(char *s, int *nomem);
e87dd3
-unsigned char *do_rfc1035_name(unsigned char *p, char *sval);
e87dd3
+unsigned char *do_rfc1035_name(unsigned char *p, char *sval, char *limit);
e87dd3
 void *safe_malloc(size_t size);
e87dd3
 void safe_pipe(int *fd, int read_noblock);
e87dd3
 void *whine_malloc(size_t size);
e87dd3
diff --git a/src/dnssec.c b/src/dnssec.c
e87dd3
index 3c77c7d..f45c804 100644
e87dd3
--- a/src/dnssec.c
e87dd3
+++ b/src/dnssec.c
e87dd3
@@ -2227,7 +2227,7 @@ size_t dnssec_generate_query(struct dns_header *header, unsigned char *end, char
e87dd3
 
e87dd3
   p = (unsigned char *)(header+1);
e87dd3
 	
e87dd3
-  p = do_rfc1035_name(p, name);
e87dd3
+  p = do_rfc1035_name(p, name, NULL);
e87dd3
   *p++ = 0;
e87dd3
   PUTSHORT(type, p);
e87dd3
   PUTSHORT(class, p);
e87dd3
diff --git a/src/option.c b/src/option.c
e87dd3
index eb78b1a..3469f53 100644
e87dd3
--- a/src/option.c
e87dd3
+++ b/src/option.c
e87dd3
@@ -1378,7 +1378,7 @@ static int parse_dhcp_opt(char *errstr, char *arg, int flags)
e87dd3
 		    }
e87dd3
 		  
e87dd3
 		  p = newp;
e87dd3
-		  end = do_rfc1035_name(p + len, dom);
e87dd3
+		  end = do_rfc1035_name(p + len, dom, NULL);
e87dd3
 		  *end++ = 0;
e87dd3
 		  len = end - p;
e87dd3
 		  free(dom);
e87dd3
diff --git a/src/rfc1035.c b/src/rfc1035.c
e87dd3
index 24d08c1..78410d6 100644
e87dd3
--- a/src/rfc1035.c
e87dd3
+++ b/src/rfc1035.c
e87dd3
@@ -1049,6 +1049,7 @@ int check_for_ignored_address(struct dns_header *header, size_t qlen, struct bog
e87dd3
   return 0;
e87dd3
 }
e87dd3
 
e87dd3
+
e87dd3
 int add_resource_record(struct dns_header *header, char *limit, int *truncp, int nameoffset, unsigned char **pp, 
e87dd3
 			unsigned long ttl, int *offset, unsigned short type, unsigned short class, char *format, ...)
e87dd3
 {
e87dd3
@@ -1058,12 +1059,21 @@ int add_resource_record(struct dns_header *header, char *limit, int *truncp, int
e87dd3
   unsigned short usval;
e87dd3
   long lval;
e87dd3
   char *sval;
e87dd3
+#define CHECK_LIMIT(size) \
e87dd3
+  if (limit && p + (size) > (unsigned char*)limit) \
e87dd3
+    { \
e87dd3
+      va_end(ap); \
e87dd3
+      goto truncated; \
e87dd3
+    }
e87dd3
 
e87dd3
   if (truncp && *truncp)
e87dd3
     return 0;
e87dd3
- 
e87dd3
+
e87dd3
   va_start(ap, format);   /* make ap point to 1st unamed argument */
e87dd3
-  
e87dd3
+
e87dd3
+  /* nameoffset (1 or 2) + type (2) + class (2) + ttl (4) + 0 (2) */
e87dd3
+  CHECK_LIMIT(12);
e87dd3
+
e87dd3
   if (nameoffset > 0)
e87dd3
     {
e87dd3
       PUTSHORT(nameoffset | 0xc000, p);
e87dd3
@@ -1072,7 +1082,13 @@ int add_resource_record(struct dns_header *header, char *limit, int *truncp, int
e87dd3
     {
e87dd3
       char *name = va_arg(ap, char *);
e87dd3
       if (name)
e87dd3
-	p = do_rfc1035_name(p, name);
e87dd3
+	p = do_rfc1035_name(p, name, limit);
e87dd3
+        if (!p)
e87dd3
+          {
e87dd3
+            va_end(ap);
e87dd3
+            goto truncated;
e87dd3
+          }
e87dd3
+
e87dd3
       if (nameoffset < 0)
e87dd3
 	{
e87dd3
 	  PUTSHORT(-nameoffset | 0xc000, p);
e87dd3
@@ -1093,6 +1109,7 @@ int add_resource_record(struct dns_header *header, char *limit, int *truncp, int
e87dd3
       {
e87dd3
 #ifdef HAVE_IPV6
e87dd3
       case '6':
e87dd3
+        CHECK_LIMIT(IN6ADDRSZ);
e87dd3
 	sval = va_arg(ap, char *); 
e87dd3
 	memcpy(p, sval, IN6ADDRSZ);
e87dd3
 	p += IN6ADDRSZ;
e87dd3
@@ -1100,36 +1117,47 @@ int add_resource_record(struct dns_header *header, char *limit, int *truncp, int
e87dd3
 #endif
e87dd3
 	
e87dd3
       case '4':
e87dd3
+        CHECK_LIMIT(INADDRSZ);
e87dd3
 	sval = va_arg(ap, char *); 
e87dd3
 	memcpy(p, sval, INADDRSZ);
e87dd3
 	p += INADDRSZ;
e87dd3
 	break;
e87dd3
 	
e87dd3
       case 'b':
e87dd3
+        CHECK_LIMIT(1);
e87dd3
 	usval = va_arg(ap, int);
e87dd3
 	*p++ = usval;
e87dd3
 	break;
e87dd3
 	
e87dd3
       case 's':
e87dd3
+        CHECK_LIMIT(2);
e87dd3
 	usval = va_arg(ap, int);
e87dd3
 	PUTSHORT(usval, p);
e87dd3
 	break;
e87dd3
 	
e87dd3
       case 'l':
e87dd3
+        CHECK_LIMIT(4);
e87dd3
 	lval = va_arg(ap, long);
e87dd3
 	PUTLONG(lval, p);
e87dd3
 	break;
e87dd3
 	
e87dd3
       case 'd':
e87dd3
-	/* get domain-name answer arg and store it in RDATA field */
e87dd3
-	if (offset)
e87dd3
-	  *offset = p - (unsigned char *)header;
e87dd3
-	p = do_rfc1035_name(p, va_arg(ap, char *));
e87dd3
-	*p++ = 0;
e87dd3
+        /* get domain-name answer arg and store it in RDATA field */
e87dd3
+        if (offset)
e87dd3
+          *offset = p - (unsigned char *)header;
e87dd3
+        p = do_rfc1035_name(p, va_arg(ap, char *), limit);
e87dd3
+        if (!p)
e87dd3
+          {
e87dd3
+            va_end(ap);
e87dd3
+            goto truncated;
e87dd3
+          }
e87dd3
+        CHECK_LIMIT(1);
e87dd3
+        *p++ = 0;
e87dd3
 	break;
e87dd3
 	
e87dd3
       case 't':
e87dd3
 	usval = va_arg(ap, int);
e87dd3
+        CHECK_LIMIT(usval);
e87dd3
 	sval = va_arg(ap, char *);
e87dd3
 	if (usval != 0)
e87dd3
 	  memcpy(p, sval, usval);
e87dd3
@@ -1141,20 +1169,24 @@ int add_resource_record(struct dns_header *header, char *limit, int *truncp, int
e87dd3
 	usval = sval ? strlen(sval) : 0;
e87dd3
 	if (usval > 255)
e87dd3
 	  usval = 255;
e87dd3
+        CHECK_LIMIT(usval + 1);
e87dd3
 	*p++ = (unsigned char)usval;
e87dd3
 	memcpy(p, sval, usval);
e87dd3
 	p += usval;
e87dd3
 	break;
e87dd3
       }
e87dd3
 
e87dd3
+#undef CHECK_LIMIT
e87dd3
   va_end(ap);	/* clean up variable argument pointer */
e87dd3
   
e87dd3
   j = p - sav - 2;
e87dd3
-  PUTSHORT(j, sav);     /* Now, store real RDLength */
e87dd3
+ /* this has already been checked against limit before */
e87dd3
+ PUTSHORT(j, sav);     /* Now, store real RDLength */
e87dd3
   
e87dd3
   /* check for overflow of buffer */
e87dd3
   if (limit && ((unsigned char *)limit - p) < 0)
e87dd3
     {
e87dd3
+truncated:
e87dd3
       if (truncp)
e87dd3
 	*truncp = 1;
e87dd3
       return 0;
e87dd3
diff --git a/src/rfc2131.c b/src/rfc2131.c
e87dd3
index 8b99d4b..75893a6 100644
e87dd3
--- a/src/rfc2131.c
e87dd3
+++ b/src/rfc2131.c
e87dd3
@@ -2420,10 +2420,10 @@ static void do_options(struct dhcp_context *context,
e87dd3
 
e87dd3
 	      if (fqdn_flags & 0x04)
e87dd3
 		{
e87dd3
-		  p = do_rfc1035_name(p, hostname);
e87dd3
+		  p = do_rfc1035_name(p, hostname, NULL);
e87dd3
 		  if (domain)
e87dd3
 		    {
e87dd3
-		      p = do_rfc1035_name(p, domain);
e87dd3
+		      p = do_rfc1035_name(p, domain, NULL);
e87dd3
 		      *p++ = 0;
e87dd3
 		    }
e87dd3
 		}
e87dd3
diff --git a/src/rfc3315.c b/src/rfc3315.c
e87dd3
index 3f4d69c..73bdee4 100644
e87dd3
--- a/src/rfc3315.c
e87dd3
+++ b/src/rfc3315.c
e87dd3
@@ -1472,10 +1472,10 @@ static struct dhcp_netid *add_options(struct state *state, int do_refresh)
e87dd3
       if ((p = expand(len + 2)))
e87dd3
 	{
e87dd3
 	  *(p++) = state->fqdn_flags;
e87dd3
-	  p = do_rfc1035_name(p, state->hostname);
e87dd3
+	  p = do_rfc1035_name(p, state->hostname, NULL);
e87dd3
 	  if (state->send_domain)
e87dd3
 	    {
e87dd3
-	      p = do_rfc1035_name(p, state->send_domain);
e87dd3
+	      p = do_rfc1035_name(p, state->send_domain, NULL);
e87dd3
 	      *p = 0;
e87dd3
 	    }
e87dd3
 	}
e87dd3
diff --git a/src/util.c b/src/util.c
e87dd3
index 1a9f228..be9f8a6 100644
e87dd3
--- a/src/util.c
e87dd3
+++ b/src/util.c
e87dd3
@@ -218,15 +218,20 @@ char *canonicalise(char *in, int *nomem)
e87dd3
   return ret;
e87dd3
 }
e87dd3
 
e87dd3
-unsigned char *do_rfc1035_name(unsigned char *p, char *sval)
e87dd3
+unsigned char *do_rfc1035_name(unsigned char *p, char *sval, char *limit)
e87dd3
 {
e87dd3
   int j;
e87dd3
   
e87dd3
   while (sval && *sval)
e87dd3
     {
e87dd3
+      if (limit && p + 1 > (unsigned char*)limit)
e87dd3
+        return p;
e87dd3
+
e87dd3
       unsigned char *cp = p++;
e87dd3
       for (j = 0; *sval && (*sval != '.'); sval++, j++)
e87dd3
 	{
e87dd3
+          if (limit && p + 1 > (unsigned char*)limit)
e87dd3
+            return p;
e87dd3
 #ifdef HAVE_DNSSEC
e87dd3
 	  if (option_bool(OPT_DNSSEC_VALID) && *sval == NAME_ESCAPE)
e87dd3
 	    *p++ = (*(++sval))-1;
e87dd3
-- 
e87dd3
2.9.5
e87dd3