Skip to content

Commit 234b3cb

Browse files
authored
Fix GH-14124: Segmentation fault on unknown address 0x0001ffff8041 with XML extension under certain memory limit (#14126)
The ltags were not initialized, so when an OOM happens before the new value is written, uninitialized data is used.
1 parent d9a9696 commit 234b3cb

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

ext/xml/tests/gh14124.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
GH-14124 (Segmentation fault on unknown address 0x0001ffff8041 with XML extension under certain memory limit)
3+
--EXTENSIONS--
4+
xml
5+
--INI--
6+
memory_limit=33M
7+
--SKIPIF--
8+
<?php
9+
if (!defined("LIBXML_VERSION")) die('skip this is a libxml2 test');
10+
if (getenv('SKIP_ASAN')) die('xleak libxml does not use the request allocator');
11+
?>
12+
--FILE--
13+
<?php
14+
function createParser(bool $huge) {
15+
$parser = xml_parser_create();
16+
xml_parser_set_option($parser, XML_OPTION_PARSE_HUGE, $huge);
17+
return $parser;
18+
}
19+
20+
$long_text = str_repeat("A", 1000 * 1000 * 5 /* 5 MB */);
21+
$long_xml_head = "<?xml version=\"1.0\"?><container><$long_text/><$long_text/><second>foo</second>";
22+
$long_xml_tail = "</container>";
23+
$parser = createParser(true);
24+
xml_parse_into_struct($parser, $long_xml_head . $long_xml_tail, $values, $index);
25+
?>
26+
--EXPECTF--
27+
Fatal error: Allowed memory size of %d bytes exhausted %s in %s on line %d

ext/xml/xml.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,7 @@ PHP_FUNCTION(xml_parse_into_struct)
13631363
parser->level = 0;
13641364
xml_parser_free_ltags(parser);
13651365
parser->ltags = safe_emalloc(XML_MAXLEVEL, sizeof(char *), 0);
1366+
memset(parser->ltags, 0, XML_MAXLEVEL * sizeof(char *));
13661367

13671368
XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
13681369
XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);

0 commit comments

Comments
 (0)