5136d3
From 684bede049a006a0a47ce88f017ada9f73bf4430 Mon Sep 17 00:00:00 2001
5136d3
From: Doran Moppert <dmoppert@redhat.com>
5136d3
Date: Tue, 26 Sep 2017 14:48:20 +0930
5136d3
Subject: [PATCH] google patch hand-applied
5136d3
5136d3
---
5136d3
 src/edns0.c   | 10 +++++-----
5136d3
 src/forward.c |  4 ++++
5136d3
 src/rfc1035.c |  3 +++
5136d3
 3 files changed, 12 insertions(+), 5 deletions(-)
5136d3
5136d3
diff --git a/src/edns0.c b/src/edns0.c
5136d3
index d75d3cc..7d8cf7f 100644
5136d3
--- a/src/edns0.c
5136d3
+++ b/src/edns0.c
5136d3
@@ -212,11 +212,11 @@ size_t add_pseudoheader(struct dns_header *header, size_t plen, unsigned char *l
5136d3
       /* Copy back any options */
5136d3
       if (buff)
5136d3
 	{
5136d3
-          if (p + rdlen > limit)
5136d3
-          {
5136d3
-            free(buff);
5136d3
-            return plen; /* Too big */
5136d3
-          }
5136d3
+	  if (p + rdlen > limit)
5136d3
+	  {
5136d3
+	    free(buff);
5136d3
+	    return plen; /* Too big */
5136d3
+	  }
5136d3
 	  memcpy(p, buff, rdlen);
5136d3
 	  free(buff);
5136d3
 	  p += rdlen;
5136d3
diff --git a/src/forward.c b/src/forward.c
5136d3
index ed9c8f6..77059ed 100644
5136d3
--- a/src/forward.c
5136d3
+++ b/src/forward.c
5136d3
@@ -1542,6 +1542,10 @@ void receive_query(struct listener *listen, time_t now)
5136d3
 	udp_size = PACKETSZ; /* Sanity check - can't reduce below default. RFC 6891 6.2.3 */
5136d3
     }
5136d3
 
5136d3
+  // Make sure the udp size is not smaller than the incoming message so that we
5136d3
+  // do not underflow
5136d3
+  if (udp_size < n) udp_size = n;
5136d3
+
5136d3
 #ifdef HAVE_AUTH
5136d3
   if (auth_dns)
5136d3
     {
5136d3
diff --git a/src/rfc1035.c b/src/rfc1035.c
5136d3
index f1edc45..15041cc 100644
5136d3
--- a/src/rfc1035.c
5136d3
+++ b/src/rfc1035.c
5136d3
@@ -1326,6 +1326,9 @@ size_t answer_request(struct dns_header *header, char *limit, size_t qlen,
5136d3
   size_t len;
5136d3
   int rd_bit = (header->hb3 & HB3_RD);
5136d3
 
5136d3
+  // Make sure we do not underflow here too.
5136d3
+  if (qlen > (limit - ((char *)header))) return 0;
5136d3
+
5136d3
   /* never answer queries with RD unset, to avoid cache snooping. */
5136d3
   if (ntohs(header->ancount) != 0 ||
5136d3
       ntohs(header->nscount) != 0 ||
5136d3
-- 
5136d3
2.21.1
5136d3