Skip to content

Commit 9d8983c

Browse files
committed
Fix GH-16595: Another UAF in DOM -> cloneNode
We need to perform all sanity checks before doing any modification. I don't have a reliable and easy test for this on 8.2, but I have one for 8.4. Closes GH-16598.
1 parent d89dd28 commit 9d8983c

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ PHP NEWS
4040
an element). (nielsdos)
4141
. Fixed bug GH-16535 (UAF when using document as a child). (nielsdos)
4242
. Fixed bug GH-16593 (Assertion failure in DOM->replaceChild). (nielsdos)
43+
. Fixed bug GH-16595 (Another UAF in DOM -> cloneNode). (nielsdos)
4344

4445
- EXIF:
4546
. Fixed bug GH-16409 (Segfault in exif_thumbnail when not dealing with a

ext/dom/node.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNode
893893
PHP_METHOD(DOMNode, insertBefore)
894894
{
895895
zval *id, *node, *ref = NULL;
896-
xmlNodePtr child, new_child, parentp, refp;
896+
xmlNodePtr child, new_child, parentp, refp = NULL;
897897
dom_object *intern, *childobj, *refpobj;
898898
int ret, stricterror;
899899

@@ -918,18 +918,21 @@ PHP_METHOD(DOMNode, insertBefore)
918918
RETURN_FALSE;
919919
}
920920

921-
if (child->doc == NULL && parentp->doc != NULL) {
922-
childobj->document = intern->document;
923-
php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
924-
}
925-
921+
/* Fetch and perform sanity checks before modifying reference pointers. */
926922
if (ref != NULL) {
927923
DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj);
928924
if (refp->parent != parentp) {
929925
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
930926
RETURN_FALSE;
931927
}
928+
}
932929

930+
if (child->doc == NULL && parentp->doc != NULL) {
931+
childobj->document = intern->document;
932+
php_libxml_increment_doc_ref((php_libxml_node_object *)childobj, NULL);
933+
}
934+
935+
if (ref != NULL) {
933936
if (child->parent != NULL) {
934937
xmlUnlinkNode(child);
935938
}

0 commit comments

Comments
 (0)