Skip to content

Commit 61615d5

Browse files
committed
Fix phpGH-17224: UAF in importNode
Wrong document pointer is used for the namespace copy. Closes phpGH-17230.
1 parent 2c3b56d commit 61615d5

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ PHP NEWS
1919
- DBA:
2020
. Skip test if inifile is disabled. (orlitzky)
2121

22+
- DOM:
23+
. Fixed bug GH-17224 (UAF in importNode). (nielsdos)
24+
2225
- FFI:
2326
. Fixed bug #79075 (FFI header parser chokes on comments). (nielsdos)
2427

ext/dom/document.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,14 +809,14 @@ PHP_METHOD(DOMDocument, importNode)
809809
xmlNsPtr nsptr = NULL;
810810
xmlNodePtr root = xmlDocGetRootElement(docp);
811811

812-
nsptr = xmlSearchNsByHref (nodep->doc, root, nodep->ns->href);
812+
nsptr = xmlSearchNsByHref (docp, root, nodep->ns->href);
813813
if (nsptr == NULL || nsptr->prefix == NULL) {
814814
int errorcode;
815815
nsptr = dom_get_ns(root, (char *) nodep->ns->href, &errorcode, (char *) nodep->ns->prefix);
816816

817817
/* If there is no root, the namespace cannot be attached to it, so we have to attach it to the old list. */
818818
if (nsptr != NULL && root == NULL) {
819-
php_libxml_set_old_ns(nodep->doc, nsptr);
819+
php_libxml_set_old_ns(docp, nsptr);
820820
}
821821
}
822822
retnodep->ns = nsptr;

ext/dom/tests/gh17224.phpt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
GH-17224 (UAF in importNode)
3+
--EXTENSIONS--
4+
dom
5+
--CREDITS--
6+
YuanchengJiang
7+
--FILE--
8+
<?php
9+
$aDOM = new DOMDocument();
10+
$fromdom = new DOMDocument();
11+
$fromdom->loadXML('<data xmlns:ai="http://test.org" ai:attr="namespaced" />');
12+
$attr = $fromdom->firstChild->attributes->item(0);
13+
$att = $aDOM->importNode($attr);
14+
$doc = new DOMDocument;
15+
$fromdom->load(__DIR__."/book.xml");
16+
unset($attr);
17+
var_dump($att);
18+
?>
19+
--EXPECTF--
20+
object(DOMAttr)#%d (%d) {
21+
["specified"]=>
22+
bool(true)
23+
["schemaTypeInfo"]=>
24+
NULL
25+
["name"]=>
26+
string(4) "attr"
27+
["value"]=>
28+
string(10) "namespaced"
29+
["ownerElement"]=>
30+
NULL
31+
["nodeName"]=>
32+
string(7) "ai:attr"
33+
["nodeValue"]=>
34+
string(10) "namespaced"
35+
["nodeType"]=>
36+
int(2)
37+
["parentNode"]=>
38+
NULL
39+
["parentElement"]=>
40+
NULL
41+
["childNodes"]=>
42+
string(22) "(object value omitted)"
43+
["firstChild"]=>
44+
string(22) "(object value omitted)"
45+
["lastChild"]=>
46+
string(22) "(object value omitted)"
47+
["previousSibling"]=>
48+
NULL
49+
["nextSibling"]=>
50+
NULL
51+
["attributes"]=>
52+
NULL
53+
["isConnected"]=>
54+
bool(false)
55+
["ownerDocument"]=>
56+
string(22) "(object value omitted)"
57+
["namespaceURI"]=>
58+
string(15) "http://test.org"
59+
["prefix"]=>
60+
string(2) "ai"
61+
["localName"]=>
62+
string(4) "attr"
63+
["baseURI"]=>
64+
NULL
65+
["textContent"]=>
66+
string(10) "namespaced"
67+
}

0 commit comments

Comments
 (0)