b74969
From 7d163e8a0880ae8af2dd869071393e5dc07ef271 Mon Sep 17 00:00:00 2001
b74969
From: Rob Richards <rrichards@php.net>
b74969
Date: Sat, 6 Jul 2013 07:53:07 -0400
b74969
Subject: [PATCH] truncate results at depth of 255 to prevent corruption
b74969
b74969
---
b74969
 ext/xml/xml.c | 90 +++++++++++++++++++++++++++++++++--------------------------
b74969
 1 file changed, 50 insertions(+), 40 deletions(-)
b74969
b74969
diff --git a/ext/xml/xml.c b/ext/xml/xml.c
b74969
index 1f0480b..9f0bc30 100644
b74969
--- a/ext/xml/xml.c
b74969
+++ b/ext/xml/xml.c
b74969
@@ -428,7 +428,7 @@ static void xml_parser_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
b74969
 	}
b74969
 	if (parser->ltags) {
b74969
 		int inx;
b74969
-		for (inx = 0; inx < parser->level; inx++)
b74969
+		for (inx = 0; ((inx < parser->level) && (inx < XML_MAXLEVEL)); inx++)
b74969
 			efree(parser->ltags[ inx ]);
b74969
 		efree(parser->ltags);
b74969
 	}
b74969
@@ -805,45 +805,50 @@ void _xml_startElementHandler(void *userData, const XML_Char *name, const XML_Ch
b74969
 		} 
b74969
 
b74969
 		if (parser->data) {
b74969
-			zval *tag, *atr;
b74969
-			int atcnt = 0;
b74969
+			if (parser->level <= XML_MAXLEVEL)  {
b74969
+				zval *tag, *atr;
b74969
+				int atcnt = 0;
b74969
 
b74969
-			MAKE_STD_ZVAL(tag);
b74969
-			MAKE_STD_ZVAL(atr);
b74969
+				MAKE_STD_ZVAL(tag);
b74969
+				MAKE_STD_ZVAL(atr);
b74969
 
b74969
-			array_init(tag);
b74969
-			array_init(atr);
b74969
+				array_init(tag);
b74969
+				array_init(atr);
b74969
 
b74969
-			_xml_add_to_info(parser,((char *) tag_name) + parser->toffset);
b74969
+				_xml_add_to_info(parser,((char *) tag_name) + parser->toffset);
b74969
 
b74969
-			add_assoc_string(tag,"tag",((char *) tag_name) + parser->toffset,1); /* cast to avoid gcc-warning */
b74969
-			add_assoc_string(tag,"type","open",1);
b74969
-			add_assoc_long(tag,"level",parser->level);
b74969
+				add_assoc_string(tag,"tag",((char *) tag_name) + parser->toffset,1); /* cast to avoid gcc-warning */
b74969
+				add_assoc_string(tag,"type","open",1);
b74969
+				add_assoc_long(tag,"level",parser->level);
b74969
 
b74969
-			parser->ltags[parser->level-1] = estrdup(tag_name);
b74969
-			parser->lastwasopen = 1;
b74969
+				parser->ltags[parser->level-1] = estrdup(tag_name);
b74969
+				parser->lastwasopen = 1;
b74969
 
b74969
-			attributes = (const XML_Char **) attrs;
b74969
+				attributes = (const XML_Char **) attrs;
b74969
 
b74969
-			while (attributes && *attributes) {
b74969
-				att = _xml_decode_tag(parser, attributes[0]);
b74969
-				val = xml_utf8_decode(attributes[1], strlen(attributes[1]), &val_len, parser->target_encoding);
b74969
-				
b74969
-				add_assoc_stringl(atr,att,val,val_len,0);
b74969
+				while (attributes && *attributes) {
b74969
+					att = _xml_decode_tag(parser, attributes[0]);
b74969
+					val = xml_utf8_decode(attributes[1], strlen(attributes[1]), &val_len, parser->target_encoding);
b74969
 
b74969
-				atcnt++;
b74969
-				attributes += 2;
b74969
+					add_assoc_stringl(atr,att,val,val_len,0);
b74969
 
b74969
-				efree(att);
b74969
-			}
b74969
+					atcnt++;
b74969
+					attributes += 2;
b74969
 
b74969
-			if (atcnt) {
b74969
-				zend_hash_add(Z_ARRVAL_P(tag),"attributes",sizeof("attributes"),&atr,sizeof(zval*),NULL);
b74969
-			} else {
b74969
-				zval_ptr_dtor(&atr;;
b74969
-			}
b74969
+					efree(att);
b74969
+				}
b74969
+
b74969
+				if (atcnt) {
b74969
+					zend_hash_add(Z_ARRVAL_P(tag),"attributes",sizeof("attributes"),&atr,sizeof(zval*),NULL);
b74969
+				} else {
b74969
+					zval_ptr_dtor(&atr;;
b74969
+				}
b74969
 
b74969
-			zend_hash_next_index_insert(Z_ARRVAL_P(parser->data),&tag,sizeof(zval*),(void *) &parser->ctag);
b74969
+				zend_hash_next_index_insert(Z_ARRVAL_P(parser->data),&tag,sizeof(zval*),(void *) &parser->ctag);
b74969
+			} else if (parser->level == (XML_MAXLEVEL + 1)) {
b74969
+				TSRMLS_FETCH();
b74969
+				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Maximum depth exceeded - Results truncated");
b74969
+			}
b74969
 		}
b74969
 
b74969
 		efree(tag_name);
b74969
@@ -895,7 +900,7 @@ void _xml_endElementHandler(void *userData, const XML_Char *name)
b74969
 
b74969
 		efree(tag_name);
b74969
 
b74969
-		if (parser->ltags) {
b74969
+		if ((parser->ltags) && (parser->level <= XML_MAXLEVEL)) {
b74969
 			efree(parser->ltags[parser->level-1]);
b74969
 		}
b74969
 
b74969
@@ -979,18 +984,23 @@ void _xml_characterDataHandler(void *userData, const XML_Char *s, int len)
b74969
 						}
b74969
 					}
b74969
 
b74969
-					MAKE_STD_ZVAL(tag);
b74969
-					
b74969
-					array_init(tag);
b74969
-					
b74969
-					_xml_add_to_info(parser,parser->ltags[parser->level-1] + parser->toffset);
b74969
+					if (parser->level <= XML_MAXLEVEL) {
b74969
+						MAKE_STD_ZVAL(tag);
b74969
 
b74969
-					add_assoc_string(tag,"tag",parser->ltags[parser->level-1] + parser->toffset,1);
b74969
-					add_assoc_string(tag,"value",decoded_value,0);
b74969
-					add_assoc_string(tag,"type","cdata",1);
b74969
-					add_assoc_long(tag,"level",parser->level);
b74969
+						array_init(tag);
b74969
 
b74969
-					zend_hash_next_index_insert(Z_ARRVAL_P(parser->data),&tag,sizeof(zval*),NULL);
b74969
+						_xml_add_to_info(parser,parser->ltags[parser->level-1] + parser->toffset);
b74969
+
b74969
+						add_assoc_string(tag,"tag",parser->ltags[parser->level-1] + parser->toffset,1);
b74969
+						add_assoc_string(tag,"value",decoded_value,0);
b74969
+						add_assoc_string(tag,"type","cdata",1);
b74969
+						add_assoc_long(tag,"level",parser->level);
b74969
+
b74969
+						zend_hash_next_index_insert(Z_ARRVAL_P(parser->data),&tag,sizeof(zval*),NULL);
b74969
+					} else if (parser->level == (XML_MAXLEVEL + 1)) {
b74969
+						TSRMLS_FETCH();
b74969
+						php_error_docref(NULL TSRMLS_CC, E_WARNING, "Maximum depth exceeded - Results truncated");
b74969
+					}
b74969
 				}
b74969
 			} else {
b74969
 				efree(decoded_value);
b74969
-- 
b74969
1.7.11.5
b74969
b74969
From 710eee5555bc5c95692bd3c84f5d2b5d687349b6 Mon Sep 17 00:00:00 2001
b74969
From: =?utf8?q?Johannes=20Schl=C3=BCter?= <johannes@php.net>
b74969
Date: Wed, 10 Jul 2013 19:35:18 +0200
b74969
Subject: [PATCH] add test for bug #65236
b74969
b74969
---
b74969
 ext/xml/tests/bug65236.phpt | 15 +++++++++++++++
b74969
 1 file changed, 15 insertions(+)
b74969
 create mode 100644 ext/xml/tests/bug65236.phpt
b74969
b74969
diff --git a/ext/xml/tests/bug65236.phpt b/ext/xml/tests/bug65236.phpt
b74969
new file mode 100644
b74969
index 0000000..67b26d6
b74969
--- /dev/null
b74969
+++ b/ext/xml/tests/bug65236.phpt
b74969
@@ -0,0 +1,15 @@
b74969
+--TEST--
b74969
+Bug #65236 (heap corruption in xml parser)
b74969
+--SKIPIF--
b74969
+
b74969
+require_once("skipif.inc");
b74969
+?>
b74969
+--FILE--
b74969
+
b74969
+xml_parse_into_struct(xml_parser_create_ns(), str_repeat("<blah>", 1000), $a);
b74969
+
b74969
+echo "Done\n";
b74969
+?>
b74969
+--EXPECTF--
b74969
+Warning: xml_parse_into_struct(): Maximum depth exceeded - Results truncated in %s on line %d
b74969
+Done
b74969
-- 
b74969
1.7.11.5
b74969