Skip to content

Commit 332baba

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix use-after-free in XPath php:function argument nodes
2 parents da1f323 + 860c33b commit 332baba

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
DOMXPath: a php:function callback that removes its argument node must not free it mid-evaluation
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument();
8+
$doc->loadXML('<root><a>1</a><a>2</a><a>3</a><a>4</a></root>');
9+
$xp = new DOMXPath($doc);
10+
$xp->registerNamespace('php', 'http://php.net/xpath');
11+
$xp->registerPhpFunctions();
12+
13+
function cb($nodes) {
14+
foreach ($nodes as $n) {
15+
if ($n->parentNode) {
16+
$n->parentNode->removeChild($n);
17+
}
18+
}
19+
return true;
20+
}
21+
22+
$res = $xp->query('//a[php:function("cb", .)]');
23+
foreach ($res as $r) {
24+
var_dump($r->nodeName);
25+
}
26+
echo "done\n";
27+
?>
28+
--EXPECT--
29+
string(1) "a"
30+
string(1) "a"
31+
string(1) "a"
32+
string(1) "a"
33+
done

‎ext/dom/xpath_callbacks.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,19 @@ static zval *php_dom_xpath_callback_fetch_args(xmlXPathParserContextPtr ctxt, ui
379379
return params;
380380
}
381381

382-
static void php_dom_xpath_callback_cleanup_args(zval *params, uint32_t param_count)
382+
static void php_dom_xpath_callback_cleanup_args(php_dom_xpath_callbacks *xpath_callbacks, zval *params, uint32_t param_count)
383383
{
384384
if (params) {
385385
for (uint32_t i = 0; i < param_count; i++) {
386-
zval_ptr_dtor(&params[i]);
386+
zval *param = &params[i];
387+
if (Z_TYPE_P(param) == IS_OBJECT || Z_TYPE_P(param) == IS_ARRAY) {
388+
if (xpath_callbacks->node_list == NULL) {
389+
xpath_callbacks->node_list = zend_new_array(0);
390+
}
391+
zend_hash_next_index_insert_new(xpath_callbacks->node_list, param);
392+
} else {
393+
zval_ptr_dtor(param);
394+
}
387395
}
388396
efree(params);
389397
}
@@ -478,7 +486,7 @@ PHP_DOM_EXPORT zend_result php_dom_xpath_callbacks_call_php_ns(php_dom_xpath_cal
478486

479487
cleanup:
480488
xmlXPathFreeObject(obj);
481-
php_dom_xpath_callback_cleanup_args(params, param_count);
489+
php_dom_xpath_callback_cleanup_args(xpath_callbacks, params, param_count);
482490
cleanup_no_obj:
483491
if (UNEXPECTED(result != SUCCESS)) {
484492
/* Push sentinel value */
@@ -506,7 +514,7 @@ PHP_DOM_EXPORT zend_result php_dom_xpath_callbacks_call_custom_ns(php_dom_xpath_
506514

507515
zend_result result = php_dom_xpath_callback_dispatch(xpath_callbacks, ns, ctxt, params, param_count, function_name, function_name_length);
508516

509-
php_dom_xpath_callback_cleanup_args(params, param_count);
517+
php_dom_xpath_callback_cleanup_args(xpath_callbacks, params, param_count);
510518
if (UNEXPECTED(result != SUCCESS)) {
511519
/* Push sentinel value */
512520
valuePush(ctxt, xmlXPathNewString((const xmlChar *) ""));

0 commit comments

Comments
 (0)