Skip to content

Commit 9d39ff7

Browse files
committed
Fix GH-16906: Reloading document can cause UAF in iterator
Closes GH-16909.
1 parent 58ed759 commit 9d39ff7

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ PHP NEWS
2121
- DOM:
2222
. Fixed bug GH-16777 (Calling the constructor again on a DOM object after it
2323
is in a document causes UAF). (nielsdos)
24+
. Fixed bug GH-16906 (Reloading document can cause UAF in iterator).
25+
(nielsdos)
2426

2527
- FPM:
2628
. Fixed GH-16432 (PHP-FPM 8.2 SIGSEGV in fpm_get_status). (Jakub Zelenka)

ext/dom/php_dom.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,10 @@ void dom_namednode_iter(dom_object *basenode, int ntype, dom_object *intern, xml
10181018
mapptr->baseobj = basenode;
10191019
mapptr->nodetype = ntype;
10201020
mapptr->ht = ht;
1021+
if (EXPECTED(doc != NULL)) {
1022+
mapptr->dict = doc->dict;
1023+
xmlDictReference(doc->dict);
1024+
}
10211025

10221026
const xmlChar* tmp;
10231027

@@ -1128,6 +1132,7 @@ void dom_nnodemap_objects_free_storage(zend_object *object) /* {{{ */
11281132
if (!Z_ISUNDEF(objmap->baseobj_zv)) {
11291133
zval_ptr_dtor(&objmap->baseobj_zv);
11301134
}
1135+
xmlDictFree(objmap->dict);
11311136
efree(objmap);
11321137
intern->ptr = NULL;
11331138
}
@@ -1158,6 +1163,7 @@ zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type)
11581163
objmap->cached_length = -1;
11591164
objmap->cached_obj = NULL;
11601165
objmap->cached_obj_index = 0;
1166+
objmap->dict = NULL;
11611167

11621168
return &intern->std;
11631169
}

ext/dom/php_dom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ typedef struct _dom_nnodemap_object {
8989
php_libxml_cache_tag cache_tag;
9090
dom_object *cached_obj;
9191
zend_long cached_obj_index;
92+
xmlDictPtr dict;
9293
bool free_local : 1;
9394
bool free_ns : 1;
9495
} dom_nnodemap_object;

ext/dom/tests/gh16906.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-16906 (Reloading document can cause UAF in iterator)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument;
8+
$doc->loadXML('<?xml version="1.0"?><span><strong id="1"/><strong id="2"/></span>');
9+
$list = $doc->getElementsByTagName('strong');
10+
$doc->load(__DIR__."/book.xml");
11+
var_dump($list);
12+
?>
13+
--EXPECT--
14+
object(DOMNodeList)#2 (1) {
15+
["length"]=>
16+
int(0)
17+
}

0 commit comments

Comments
 (0)