7bfc213
From a3303e196e5d304ec955c4d63afb923ade66c6e8 Mon Sep 17 00:00:00 2001
7bfc213
From: Simon Kelley <simon@thekelleys.org.uk>
7bfc213
Date: Thu, 7 Sep 2017 20:45:00 +0100
7bfc213
Subject: [PATCH] Don't return arcount=1 if EDNS0 RR won't fit in the packet.
7bfc213
7bfc213
Omitting the EDNS0 RR but setting arcount gives a malformed packet.
7bfc213
Also, don't accept UDP packet size less than 512 in recieved EDNS0.
7bfc213
---
7bfc213
 src/edns0.c   | 5 ++++-
7bfc213
 src/forward.c | 2 ++
7bfc213
 2 files changed, 6 insertions(+), 1 deletion(-)
7bfc213
7bfc213
diff --git a/src/edns0.c b/src/edns0.c
7bfc213
index 3fde17f..f5b798c 100644
7bfc213
--- a/src/edns0.c
7bfc213
+++ b/src/edns0.c
7bfc213
@@ -208,7 +208,10 @@ size_t add_pseudoheader(struct dns_header *header, size_t plen, unsigned char *l
7bfc213
 	  free(buff);
7bfc213
 	  p += rdlen;
7bfc213
 	}
7bfc213
-      header->arcount = htons(ntohs(header->arcount) + 1);
7bfc213
+      
7bfc213
+      /* Only bump arcount if RR is going to fit */ 
7bfc213
+      if (((ssize_t)optlen) <= (limit - (p + 4)))
7bfc213
+	header->arcount = htons(ntohs(header->arcount) + 1);
7bfc213
     }
7bfc213
   
7bfc213
   if (((ssize_t)optlen) > (limit - (p + 4)))
7bfc213
diff --git a/src/forward.c b/src/forward.c
7bfc213
index e3fa94b..942b02d 100644
7bfc213
--- a/src/forward.c
7bfc213
+++ b/src/forward.c
7bfc213
@@ -1412,6 +1412,8 @@ void receive_query(struct listener *listen, time_t now)
7bfc213
 	 defaults to 512 */
7bfc213
       if (udp_size > daemon->edns_pktsz)
7bfc213
 	udp_size = daemon->edns_pktsz;
7bfc213
+      else if (udp_size < PACKETSZ)
7bfc213
+	udp_size = PACKETSZ; /* Sanity check - can't reduce below default. RFC 6891 6.2.3 */
7bfc213
     }
7bfc213
 
7bfc213
 #ifdef HAVE_AUTH
7bfc213
-- 
7bfc213
2.9.5
7bfc213