Skip to content

Commit 860c33b

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix use-after-free in XPath php:function argument nodes
2 parents ec5a4f4 + f9821dd commit 860c33b

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
@@ -381,11 +381,19 @@ static zval *php_dom_xpath_callback_fetch_args(xmlXPathParserContextPtr ctxt, ui
381381
return params;
382382
}
383383

384-
static void php_dom_xpath_callback_cleanup_args(zval *params, uint32_t param_count)
384+
static void php_dom_xpath_callback_cleanup_args(php_dom_xpath_callbacks *xpath_callbacks, zval *params, uint32_t param_count)
385385
{
386386
if (params) {
387387
for (uint32_t i = 0; i < param_count; i++) {
388-
zval_ptr_dtor(&params[i]);
388+
zval *param = &params[i];
389+
if (Z_TYPE_P(param) == IS_OBJECT || Z_TYPE_P(param) == IS_ARRAY) {
390+
if (xpath_callbacks->node_list == NULL) {
391+
xpath_callbacks->node_list = zend_new_array(0);
392+
}
393+
zend_hash_next_index_insert_new(xpath_callbacks->node_list, param);
394+
} else {
395+
zval_ptr_dtor(param);
396+
}
389397
}
390398
efree(params);
391399
}
@@ -484,7 +492,7 @@ PHP_DOM_EXPORT zend_result php_dom_xpath_callbacks_call_php_ns(php_dom_xpath_cal
484492

485493
cleanup:
486494
xmlXPathFreeObject(obj);
487-
php_dom_xpath_callback_cleanup_args(params, param_count);
495+
php_dom_xpath_callback_cleanup_args(xpath_callbacks, params, param_count);
488496
cleanup_no_obj:
489497
if (UNEXPECTED(result != SUCCESS)) {
490498
/* Push sentinel value */
@@ -512,7 +520,7 @@ PHP_DOM_EXPORT zend_result php_dom_xpath_callbacks_call_custom_ns(php_dom_xpath_
512520

513521
zend_result result = php_dom_xpath_callback_dispatch(xpath_callbacks, ns, ctxt, params, param_count, function_name, function_name_length);
514522

515-
php_dom_xpath_callback_cleanup_args(params, param_count);
523+
php_dom_xpath_callback_cleanup_args(xpath_callbacks, params, param_count);
516524
if (UNEXPECTED(result != SUCCESS)) {
517525
/* Push sentinel value */
518526
valuePush(ctxt, xmlXPathNewString((const xmlChar *) ""));

0 commit comments

Comments
 (0)