b77848
From 2fefae47716d501aec41c1102f3fd4531f070b05 Mon Sep 17 00:00:00 2001
b77848
From: Remi Collet <remi@php.net>
b77848
Date: Tue, 19 Aug 2014 08:33:49 +0200
b77848
Subject: [PATCH] Fixed Sec Bug #67717 segfault in dns_get_record CVE-2014-3597
b77848
b77848
Incomplete fix for CVE-2014-4049
b77848
b77848
Check possible buffer overflow
b77848
- pass real buffer end to dn_expand calls
b77848
- check buffer len before each read
b77848
---
b77848
 ext/standard/dns.c | 84 ++++++++++++++++++++++++++++++++++++++----------------
b77848
 1 file changed, 60 insertions(+), 24 deletions(-)
b77848
b77848
diff --git a/ext/standard/dns.c b/ext/standard/dns.c
b77848
index 214a7dc..0b5e69c 100644
b77848
--- a/ext/standard/dns.c
b77848
+++ b/ext/standard/dns.c
b77848
@@ -412,8 +412,14 @@ PHP_FUNCTION(dns_check_record)
b77848
 
b77848
 #if HAVE_FULL_DNS_FUNCS
b77848
 
b77848
+#define CHECKCP(n) do { \
b77848
+	if (cp + n > end) { \
b77848
+		return NULL; \
b77848
+	} \
b77848
+} while (0)
b77848
+
b77848
 /* {{{ php_parserr */
b77848
-static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int store, int raw, zval **subarray)
b77848
+static u_char *php_parserr(u_char *cp, u_char *end, querybuf *answer, int type_to_fetch, int store, int raw, zval **subarray)
b77848
 {
b77848
 	u_short type, class, dlen;
b77848
 	u_long ttl;
b77848
@@ -425,16 +431,18 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 
b77848
 	*subarray = NULL;
b77848
 
b77848
-	n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, sizeof(name) - 2);
b77848
+	n = dn_expand(answer->qb2, end, cp, name, sizeof(name) - 2);
b77848
 	if (n < 0) {
b77848
 		return NULL;
b77848
 	}
b77848
 	cp += n;
b77848
 
b77848
+	CHECKCP(10);
b77848
 	GETSHORT(type, cp);
b77848
 	GETSHORT(class, cp);
b77848
 	GETLONG(ttl, cp);
b77848
 	GETSHORT(dlen, cp);
b77848
+	CHECKCP(dlen);
b77848
 	if (type_to_fetch != T_ANY && type != type_to_fetch) {
b77848
 		cp += dlen;
b77848
 		return cp;
b77848
@@ -461,12 +469,14 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 
b77848
 	switch (type) {
b77848
 		case DNS_T_A:
b77848
+			CHECKCP(4);
b77848
 			add_assoc_string(*subarray, "type", "A", 1);
b77848
 			snprintf(name, sizeof(name), "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
b77848
 			add_assoc_string(*subarray, "ip", name, 1);
b77848
 			cp += dlen;
b77848
 			break;
b77848
 		case DNS_T_MX:
b77848
+			CHECKCP(2);
b77848
 			add_assoc_string(*subarray, "type", "MX", 1);
b77848
 			GETSHORT(n, cp);
b77848
 			add_assoc_long(*subarray, "pri", n);
b77848
@@ -485,7 +495,7 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 			if (type == DNS_T_PTR) {
b77848
 				add_assoc_string(*subarray, "type", "PTR", 1);
b77848
 			}
b77848
-			n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
b77848
+			n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
b77848
 			if (n < 0) {
b77848
 				return NULL;
b77848
 			}
b77848
@@ -495,18 +505,22 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 		case DNS_T_HINFO:
b77848
 			/* See RFC 1010 for values */
b77848
 			add_assoc_string(*subarray, "type", "HINFO", 1);
b77848
+			CHECKCP(1);
b77848
 			n = *cp & 0xFF;
b77848
 			cp++;
b77848
+			CHECKCP(n);
b77848
 			add_assoc_stringl(*subarray, "cpu", (char*)cp, n, 1);
b77848
 			cp += n;
b77848
+			CHECKCP(1);
b77848
 			n = *cp & 0xFF;
b77848
 			cp++;
b77848
+			CHECKCP(n);
b77848
 			add_assoc_stringl(*subarray, "os", (char*)cp, n, 1);
b77848
 			cp += n;
b77848
 			break;
b77848
 		case DNS_T_TXT:
b77848
 			{
b77848
-				int ll = 0;
b77848
+				int l1 = 0, l2 = 0;
b77848
 				zval *entries = NULL;
b77848
 
b77848
 				add_assoc_string(*subarray, "type", "TXT", 1);
b77848
@@ -515,37 +529,41 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 				MAKE_STD_ZVAL(entries);
b77848
 				array_init(entries);
b77848
 				
b77848
-				while (ll < dlen) {
b77848
-					n = cp[ll];
b77848
-					if ((ll + n) >= dlen) {
b77848
+				while (l1 < dlen) {
b77848
+					n = cp[l1];
b77848
+					if ((l1 + n) >= dlen) {
b77848
 						// Invalid chunk length, truncate
b77848
-						n = dlen - (ll + 1);
b77848
+						n = dlen - (l1 + 1);
b77848
+					}
b77848
+					if (n) {
b77848
+						memcpy(tp + l2 , cp + l1 + 1, n);
b77848
+						add_next_index_stringl(entries, cp + l1 + 1, n, 1);
b77848
 					}
b77848
-					memcpy(tp + ll , cp + ll + 1, n);
b77848
-					add_next_index_stringl(entries, cp + ll + 1, n, 1);
b77848
-					ll = ll + n + 1;
b77848
+					l1 = l1 + n + 1;
b77848
+					l2 = l2 + n;
b77848
 				}
b77848
-				tp[dlen] = '\0';
b77848
+				tp[l2] = '\0';
b77848
 				cp += dlen;
b77848
 
b77848
-				add_assoc_stringl(*subarray, "txt", tp, (dlen>0)?dlen - 1:0, 0);
b77848
+				add_assoc_stringl(*subarray, "txt", tp, l2, 0);
b77848
 				add_assoc_zval(*subarray, "entries", entries);
b77848
 			}
b77848
 			break;
b77848
 		case DNS_T_SOA:
b77848
 			add_assoc_string(*subarray, "type", "SOA", 1);
b77848
-			n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
b77848
+			n = dn_expand(answer->qb2, end, cp, name, (sizeof name) -2);
b77848
 			if (n < 0) {
b77848
 				return NULL;
b77848
 			}
b77848
 			cp += n;
b77848
 			add_assoc_string(*subarray, "mname", name, 1);
b77848
-			n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) -2);
b77848
+			n = dn_expand(answer->qb2, end, cp, name, (sizeof name) -2);
b77848
 			if (n < 0) {
b77848
 				return NULL;
b77848
 			}
b77848
 			cp += n;
b77848
 			add_assoc_string(*subarray, "rname", name, 1);
b77848
+			CHECKCP(5*4);
b77848
 			GETLONG(n, cp);
b77848
 			add_assoc_long(*subarray, "serial", n);
b77848
 			GETLONG(n, cp);
b77848
@@ -559,6 +577,7 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 			break;
b77848
 		case DNS_T_AAAA:
b77848
 			tp = (u_char*)name;
b77848
+			CHECKCP(8*2);
b77848
 			for(i=0; i < 8; i++) {
b77848
 				GETSHORT(s, cp);
b77848
 				if (s != 0) {
b77848
@@ -593,6 +612,7 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 		case DNS_T_A6:
b77848
 			p = cp;
b77848
 			add_assoc_string(*subarray, "type", "A6", 1);
b77848
+			CHECKCP(1);
b77848
 			n = ((int)cp[0]) & 0xFF;
b77848
 			cp++;
b77848
 			add_assoc_long(*subarray, "masklen", n);
b77848
@@ -628,6 +648,7 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 				cp++;
b77848
 			}
b77848
 			for (i = (n + 8) / 16; i < 8; i++) {
b77848
+				CHECKCP(2);
b77848
 				GETSHORT(s, cp);
b77848
 				if (s != 0) {
b77848
 					if (tp > (u_char *)name) {
b77848
@@ -657,7 +678,7 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 			tp[0] = '\0';
b77848
 			add_assoc_string(*subarray, "ipv6", name, 1);
b77848
 			if (cp < p + dlen) {
b77848
-				n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
b77848
+				n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
b77848
 				if (n < 0) {
b77848
 					return NULL;
b77848
 				}
b77848
@@ -666,6 +687,7 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 			}
b77848
 			break;
b77848
 		case DNS_T_SRV:
b77848
+			CHECKCP(3*2);
b77848
 			add_assoc_string(*subarray, "type", "SRV", 1);
b77848
 			GETSHORT(n, cp);
b77848
 			add_assoc_long(*subarray, "pri", n);
b77848
@@ -673,7 +695,7 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 			add_assoc_long(*subarray, "weight", n);
b77848
 			GETSHORT(n, cp);
b77848
 			add_assoc_long(*subarray, "port", n);
b77848
-			n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
b77848
+			n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
b77848
 			if (n < 0) {
b77848
 				return NULL;
b77848
 			}
b77848
@@ -681,21 +703,35 @@ static u_char *php_parserr(u_char *cp, querybuf *answer, int type_to_fetch, int
b77848
 			add_assoc_string(*subarray, "target", name, 1);
b77848
 			break;
b77848
 		case DNS_T_NAPTR:
b77848
+			CHECKCP(2*2);
b77848
 			add_assoc_string(*subarray, "type", "NAPTR", 1);
b77848
 			GETSHORT(n, cp);
b77848
 			add_assoc_long(*subarray, "order", n);
b77848
 			GETSHORT(n, cp);
b77848
 			add_assoc_long(*subarray, "pref", n);
b77848
+
b77848
+			CHECKCP(1);
b77848
 			n = (cp[0] & 0xFF);
b77848
-			add_assoc_stringl(*subarray, "flags", (char*)++cp, n, 1);
b77848
+			cp++;
b77848
+			CHECKCP(n);
b77848
+			add_assoc_stringl(*subarray, "flags", (char*)cp, n, 1);
b77848
 			cp += n;
b77848
+
b77848
+			CHECKCP(1);
b77848
 			n = (cp[0] & 0xFF);
b77848
-			add_assoc_stringl(*subarray, "services", (char*)++cp, n, 1);
b77848
+			cp++;
b77848
+			CHECKCP(n);
b77848
+			add_assoc_stringl(*subarray, "services", (char*)cp, n, 1);
b77848
 			cp += n;
b77848
+
b77848
+			CHECKCP(1);
b77848
 			n = (cp[0] & 0xFF);
b77848
-			add_assoc_stringl(*subarray, "regex", (char*)++cp, n, 1);
b77848
+			cp++;
b77848
+			CHECKCP(n);
b77848
+			add_assoc_stringl(*subarray, "regex", (char*)cp, n, 1);
b77848
 			cp += n;
b77848
-			n = dn_expand(answer->qb2, answer->qb2+65536, cp, name, (sizeof name) - 2);
b77848
+
b77848
+			n = dn_expand(answer->qb2, end, cp, name, (sizeof name) - 2);
b77848
 			if (n < 0) {
b77848
 				return NULL;
b77848
 			}
b77848
@@ -888,7 +924,7 @@ PHP_FUNCTION(dns_get_record)
b77848
 			while (an-- && cp && cp < end) {
b77848
 				zval *retval;
b77848
 
b77848
-				cp = php_parserr(cp, &answer, type_to_fetch, store_results, raw, &retval);
b77848
+				cp = php_parserr(cp, end, &answer, type_to_fetch, store_results, raw, &retval);
b77848
 				if (retval != NULL && store_results) {
b77848
 					add_next_index_zval(return_value, retval);
b77848
 				}
b77848
@@ -901,7 +937,7 @@ PHP_FUNCTION(dns_get_record)
b77848
 				while (ns-- > 0 && cp && cp < end) {
b77848
 					zval *retval = NULL;
b77848
 
b77848
-					cp = php_parserr(cp, &answer, DNS_T_ANY, authns != NULL, raw, &retval);
b77848
+					cp = php_parserr(cp, end, &answer, DNS_T_ANY, authns != NULL, raw, &retval);
b77848
 					if (retval != NULL) {
b77848
 						add_next_index_zval(authns, retval);
b77848
 					}
b77848
@@ -913,7 +949,7 @@ PHP_FUNCTION(dns_get_record)
b77848
 				while (ar-- > 0 && cp && cp < end) {
b77848
 					zval *retval = NULL;
b77848
 
b77848
-					cp = php_parserr(cp, &answer, DNS_T_ANY, 1, raw, &retval);
b77848
+					cp = php_parserr(cp, end, &answer, DNS_T_ANY, 1, raw, &retval);
b77848
 					if (retval != NULL) {
b77848
 						add_next_index_zval(addtl, retval);
b77848
 					}
b77848
-- 
b77848
1.9.2
b77848