Blame SOURCES/bind99-CVE-2016-2776.patch

97004c
diff --git a/lib/dns/message.c b/lib/dns/message.c
97004c
index 73def73..3d2de4f 100644
97004c
--- a/lib/dns/message.c
97004c
+++ b/lib/dns/message.c
97004c
@@ -1736,7 +1736,7 @@ dns_message_renderbegin(dns_message_t *msg, dns_compress_t *cctx,
97004c
 	if (r.length < DNS_MESSAGE_HEADERLEN)
97004c
 		return (ISC_R_NOSPACE);
97004c
 
97004c
-	if (r.length < msg->reserved)
97004c
+	if (r.length - DNS_MESSAGE_HEADERLEN < msg->reserved)
97004c
 		return (ISC_R_NOSPACE);
97004c
 
97004c
 	/*
97004c
@@ -1863,8 +1863,29 @@ norender_rdataset(const dns_rdataset_t *rdataset, unsigned int options)
97004c
 
97004c
 	return (ISC_TRUE);
97004c
 }
97004c
-
97004c
 #endif
97004c
+
97004c
+static isc_result_t
97004c
+renderset(dns_rdataset_t *rdataset, dns_name_t *owner_name,
97004c
+	  dns_compress_t *cctx, isc_buffer_t *target,
97004c
+	  unsigned int reserved, unsigned int options, unsigned int *countp)
97004c
+{
97004c
+	isc_result_t result;
97004c
+
97004c
+	/*
97004c
+	 * Shrink the space in the buffer by the reserved amount.
97004c
+	 */
97004c
+	if (target->length - target->used < reserved)
97004c
+		return (ISC_R_NOSPACE);
97004c
+
97004c
+	target->length -= reserved;
97004c
+	result = dns_rdataset_towire(rdataset, owner_name,
97004c
+				     cctx, target, options, countp);
97004c
+	target->length += reserved;
97004c
+
97004c
+	return (result);
97004c
+}
97004c
+
97004c
 isc_result_t
97004c
 dns_message_rendersection(dns_message_t *msg, dns_section_t sectionid,
97004c
 			  unsigned int options)
97004c
@@ -1907,6 +1928,8 @@ dns_message_rendersection(dns_message_t *msg, dns_section_t sectionid,
97004c
 	/*
97004c
 	 * Shrink the space in the buffer by the reserved amount.
97004c
 	 */
97004c
+	if (msg->buffer->length - msg->buffer->used < msg->reserved)
97004c
+		return (ISC_R_NOSPACE);
97004c
 	msg->buffer->length -= msg->reserved;
97004c
 
97004c
 	total = 0;
97004c
@@ -2183,9 +2206,8 @@ dns_message_renderend(dns_message_t *msg) {
97004c
 		 * Render.
97004c
 		 */
97004c
 		count = 0;
97004c
-		result = dns_rdataset_towire(msg->opt, dns_rootname,
97004c
-					     msg->cctx, msg->buffer, 0,
97004c
-					     &count);
97004c
+		result = renderset(msg->opt, dns_rootname, msg->cctx,
97004c
+				   msg->buffer, msg->reserved, 0, &count);
97004c
 		msg->counts[DNS_SECTION_ADDITIONAL] += count;
97004c
 		if (result != ISC_R_SUCCESS)
97004c
 			return (result);
97004c
@@ -2201,9 +2223,8 @@ dns_message_renderend(dns_message_t *msg) {
97004c
 		if (result != ISC_R_SUCCESS)
97004c
 			return (result);
97004c
 		count = 0;
97004c
-		result = dns_rdataset_towire(msg->tsig, msg->tsigname,
97004c
-					     msg->cctx, msg->buffer, 0,
97004c
-					     &count);
97004c
+		result = renderset(msg->tsig, msg->tsigname, msg->cctx,
97004c
+				   msg->buffer, msg->reserved, 0, &count);
97004c
 		msg->counts[DNS_SECTION_ADDITIONAL] += count;
97004c
 		if (result != ISC_R_SUCCESS)
97004c
 			return (result);
97004c
@@ -2224,9 +2245,8 @@ dns_message_renderend(dns_message_t *msg) {
97004c
 		 * the owner name of a SIG(0) is irrelevant, and will not
97004c
 		 * be set in a message being rendered.
97004c
 		 */
97004c
-		result = dns_rdataset_towire(msg->sig0, dns_rootname,
97004c
-					     msg->cctx, msg->buffer, 0,
97004c
-					     &count);
97004c
+		result = renderset(msg->sig0, dns_rootname, msg->cctx,
97004c
+				   msg->buffer, msg->reserved, 0, &count);
97004c
 		msg->counts[DNS_SECTION_ADDITIONAL] += count;
97004c
 		if (result != ISC_R_SUCCESS)
97004c
 			return (result);