a03026
From d4f2e0b8d8f0b5daa0d468f62a0d5f1df58ac325 Mon Sep 17 00:00:00 2001
a03026
From: Doran Moppert <dmoppert@redhat.com>
a03026
Date: Tue, 26 Sep 2017 14:48:20 +0930
a03026
Subject: [PATCH 9/9]     google patch hand-applied
a03026
a03026
---
a03026
 src/edns0.c   | 10 +++++-----
a03026
 src/forward.c |  4 ++++
a03026
 src/rfc1035.c |  6 ++++--
a03026
 3 files changed, 13 insertions(+), 7 deletions(-)
a03026
a03026
diff --git a/src/edns0.c b/src/edns0.c
a03026
index 0552d38..bec4a36 100644
a03026
--- a/src/edns0.c
a03026
+++ b/src/edns0.c
a03026
@@ -212,11 +212,11 @@ size_t add_pseudoheader(struct dns_header *header, size_t plen, unsigned char *l
a03026
       /* Copy back any options */
a03026
       if (buff)
a03026
 	{
a03026
-          if (p + rdlen > limit)
a03026
-          {
a03026
-            free(buff);
a03026
-            return plen; /* Too big */
a03026
-          }
a03026
+	  if (p + rdlen > limit)
a03026
+	  {
a03026
+	    free(buff);
a03026
+	    return plen; /* Too big */
a03026
+	  }
a03026
 	  memcpy(p, buff, rdlen);
a03026
 	  free(buff);
a03026
 	  p += rdlen;
a03026
diff --git a/src/forward.c b/src/forward.c
a03026
index 0f8f462..a729c06 100644
a03026
--- a/src/forward.c
a03026
+++ b/src/forward.c
a03026
@@ -1412,6 +1412,10 @@ void receive_query(struct listener *listen, time_t now)
a03026
 	udp_size = PACKETSZ; /* Sanity check - can't reduce below default. RFC 6891 6.2.3 */
a03026
     }
a03026
 
a03026
+  // Make sure the udp size is not smaller than the incoming message so that we
a03026
+  // do not underflow
a03026
+  if (udp_size < n) udp_size = n;
a03026
+
a03026
 #ifdef HAVE_AUTH
a03026
   if (auth_dns)
a03026
     {
a03026
diff --git a/src/rfc1035.c b/src/rfc1035.c
a03026
index 917bac2..ae65702 100644
a03026
--- a/src/rfc1035.c
a03026
+++ b/src/rfc1035.c
a03026
@@ -1182,8 +1182,8 @@ int add_resource_record(struct dns_header *header, char *limit, int *truncp, int
a03026
   va_end(ap);	/* clean up variable argument pointer */
a03026
   
a03026
   j = p - sav - 2;
a03026
- /* this has already been checked against limit before */
a03026
- PUTSHORT(j, sav);     /* Now, store real RDLength */
a03026
+  /* this has already been checked against limit before */
a03026
+  PUTSHORT(j, sav);     /* Now, store real RDLength */
a03026
   
a03026
   /* check for overflow of buffer */
a03026
   if (limit && ((unsigned char *)limit - p) < 0)
a03026
@@ -1243,6 +1243,8 @@ size_t answer_request(struct dns_header *header, char *limit, size_t qlen,
a03026
   int nxdomain = 0, auth = 1, trunc = 0, sec_data = 1;
a03026
   struct mx_srv_record *rec;
a03026
   size_t len;
a03026
+  // Make sure we do not underflow here too.
a03026
+  if (qlen > (limit - ((char *)header))) return 0;
a03026
   
a03026
   if (ntohs(header->ancount) != 0 ||
a03026
       ntohs(header->nscount) != 0 ||
a03026
-- 
a03026
2.9.5
a03026