Skip to content

Commit 33a49bb

Browse files
afflerbachdevnexen
authored andcommitted
ext/dom: Fix UAF in custom XPath function
Co-authored-by: David CARLIER <devnexen@gmail.com> close GH-22078
1 parent ed2c6ba commit 33a49bb

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ PHP NEWS
55
- CLI:
66
. Fixed bug GH-21901 (Stale getopt() optional value). (onthebed)
77

8+
- DOM:
9+
. Fixed bug GH-22077 (UAF in custom XPath function).
10+
(afflerbach/David Carlier)
11+
812
- Opcache:
913
. Fixed tracing JIT crash when a VM interrupt is handled during an observed
1014
user function call. (Levi Morrison)

ext/dom/tests/gh22077.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-22077 (UAF in custom XPath function)
3+
--FILE--
4+
<?php
5+
$document = new DOMDocument;
6+
$xpath = new DOMXPath($document);
7+
$xpath->registerNamespace("my", "my.ns");
8+
$xpath->registerPHPFunctionNS('my.ns', 'include', function(): DOMElement {
9+
$includedDocument = new DOMDocument;
10+
$includedDocument->loadXML('<root><uaf/><node/><uaf/></root>');
11+
return $includedDocument->documentElement;
12+
});
13+
$nodeset = $xpath->query('my:include()/uaf');
14+
$node = $nodeset->item(0);
15+
var_dump($nodeset->length);
16+
var_dump($node->ownerDocument->saveXML($node));
17+
?>
18+
--EXPECT--
19+
int(2)
20+
string(6) "<uaf/>"

ext/dom/xpath.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@
3535

3636
#ifdef LIBXML_XPATH_ENABLED
3737

38+
static dom_object *dom_xpath_intern_for_doc(dom_xpath_object *xpath_obj, xmlDocPtr doc)
39+
{
40+
if (xpath_obj->dom.document && xpath_obj->dom.document->ptr == doc) {
41+
return &xpath_obj->dom;
42+
}
43+
HashTable *node_list = xpath_obj->xpath_callbacks.node_list;
44+
if (node_list) {
45+
zval *entry;
46+
ZEND_HASH_PACKED_FOREACH_VAL(node_list, entry) {
47+
dom_object *obj = Z_DOMOBJ_P(entry);
48+
if (obj->document && obj->document->ptr == doc) {
49+
return obj;
50+
}
51+
} ZEND_HASH_FOREACH_END();
52+
}
53+
return &xpath_obj->dom;
54+
}
55+
3856
void dom_xpath_objects_free_storage(zend_object *object)
3957
{
4058
dom_xpath_object *intern = php_xpath_obj_from_obj(object);
@@ -357,7 +375,8 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type, bool modern)
357375

358376
node = php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern);
359377
} else {
360-
php_dom_create_object(node, &child, &intern->dom);
378+
dom_object *parent = dom_xpath_intern_for_doc(intern, node->doc);
379+
php_dom_create_object(node, &child, parent);
361380
}
362381
add_next_index_zval(&retval, &child);
363382
}

0 commit comments

Comments
 (0)