-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix GH-23331: UAF when an attribute child keeps a live wrapper #23337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+117
−4
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| --TEST-- | ||
| GH-23331 (Use-after-free when an attribute child past an entity reference keeps a live wrapper) | ||
| --EXTENSIONS-- | ||
| dom | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $doc = new DOMDocument(); | ||
| $doc->loadXML('<!DOCTYPE root [<!ENTITY e "X">]><root attr="a&e;b"/>'); | ||
| $attr = $doc->documentElement->getAttributeNode('attr'); | ||
| $first = $attr->firstChild; | ||
| $entity = $attr->childNodes[1]; | ||
| $last = $attr->lastChild; | ||
|
|
||
| $doc->documentElement->setAttribute('attr', 'updated'); | ||
|
|
||
| echo "text before the entity reference: "; | ||
| var_dump($first->textContent); | ||
| echo "entity reference name: "; | ||
| var_dump($entity->nodeName); | ||
| echo "entity reference detached: "; | ||
| var_dump($entity->parentNode === null); | ||
| echo "text after the entity reference: "; | ||
| var_dump($last->textContent); | ||
| echo "detached from the attribute: "; | ||
| var_dump($last->parentNode === null); | ||
| echo "new attribute value: "; | ||
| var_dump($doc->documentElement->getAttribute('attr')); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| text before the entity reference: string(1) "a" | ||
| entity reference name: string(1) "e" | ||
| entity reference detached: bool(true) | ||
| text after the entity reference: string(1) "b" | ||
| detached from the attribute: bool(true) | ||
| new attribute value: string(7) "updated" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| GH-23331 (Use-after-free when an attribute child past an entity reference keeps a live wrapper) - setAttributeNS() and removeAttribute() | ||
| --EXTENSIONS-- | ||
| dom | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $doc = new DOMDocument(); | ||
| $doc->loadXML('<!DOCTYPE root [<!ENTITY e "X">]><root xmlns:p="urn:x" p:attr="a&e;b"/>'); | ||
| $attr = $doc->documentElement->getAttributeNodeNS('urn:x', 'attr'); | ||
| $last = $attr->lastChild; | ||
| $doc->documentElement->setAttributeNS('urn:x', 'p:attr', 'updated'); | ||
| echo "setAttributeNS, detached: "; | ||
| var_dump($last->parentNode === null); | ||
| echo "setAttributeNS, text: "; | ||
| var_dump($last->textContent); | ||
|
|
||
| $doc = new DOMDocument(); | ||
| $doc->loadXML('<!DOCTYPE root [<!ENTITY e "X">]><root attr="a&e;b"/>'); | ||
| $attr = $doc->documentElement->getAttributeNode('attr'); | ||
| $last = $attr->lastChild; | ||
| unset($attr); | ||
| $doc->documentElement->removeAttribute('attr'); | ||
| echo "removeAttribute, no wrapper on the attribute, detached: "; | ||
| var_dump($last->parentNode === null); | ||
| echo "removeAttribute, no wrapper on the attribute, text: "; | ||
| var_dump($last->textContent); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| setAttributeNS, detached: bool(true) | ||
| setAttributeNS, text: string(1) "b" | ||
| removeAttribute, no wrapper on the attribute, detached: bool(true) | ||
| removeAttribute, no wrapper on the attribute, text: string(1) "b" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| --TEST-- | ||
| GH-23331 (Use-after-free when an attribute child past an entity reference keeps a live wrapper) - Dom\XMLDocument | ||
| --EXTENSIONS-- | ||
| dom | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $xml = '<!DOCTYPE root [<!ENTITY e "X">]><root xmlns:p="urn:x" attr="a&e;b" p:nsattr="c&e;d"/>'; | ||
|
|
||
| $doc = Dom\XMLDocument::createFromString($xml); | ||
| $el = $doc->documentElement; | ||
| $attr = $el->getAttributeNode('attr'); | ||
| $first = $attr->firstChild; | ||
| $last = $attr->lastChild; | ||
| unset($attr); | ||
| $el->removeAttribute('attr'); | ||
| echo "removeAttribute, first: "; | ||
| var_dump($first->textContent); | ||
| echo "removeAttribute, detached: "; | ||
| var_dump($last->parentNode === null); | ||
| echo "removeAttribute, text: "; | ||
| var_dump($last->textContent); | ||
|
|
||
| $doc = Dom\XMLDocument::createFromString($xml); | ||
| $el = $doc->documentElement; | ||
| $attr = $el->getAttributeNodeNS('urn:x', 'nsattr'); | ||
| $last = $attr->lastChild; | ||
| unset($attr); | ||
| $el->removeAttributeNS('urn:x', 'nsattr'); | ||
| echo "removeAttributeNS, detached: "; | ||
| var_dump($last->parentNode === null); | ||
| echo "removeAttributeNS, text: "; | ||
| var_dump($last->textContent); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| removeAttribute, first: string(1) "a" | ||
| removeAttribute, detached: bool(true) | ||
| removeAttribute, text: string(1) "b" | ||
| removeAttributeNS, detached: bool(true) | ||
| removeAttributeNS, text: string(1) "d" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a case for Dom\XMLDocument would be nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
gh23331_3.phpt: removeAttribute() and removeAttributeNS() onDom\XMLDocument, both segfault pre-patch. Modern setAttribute() goes viadom_remove_all_children()so it was never affected. Modern setAttributeNS() never reachesnode_list_unlink()at all, separate defect, own PR.