Skip to content

Fix UAF when setAttributeNS() frees a wrapped attribute child - #237

Closed
iliaal wants to merge 1 commit into
PHP-8.4from
fix/dom-modern-setattributens-uaf
Closed

Fix UAF when setAttributeNS() frees a wrapped attribute child#237
iliaal wants to merge 1 commit into
PHP-8.4from
fix/dom-modern-setattributens-uaf

Conversation

@iliaal

@iliaal iliaal commented Aug 17, 2026

Copy link
Copy Markdown
Owner

Dom\Element::setAttributeNS() replaces an existing attribute's value by calling xmlSetNsProp() directly, and libxml2 frees the attribute's child list there. Nothing unlinks the children that still carry a PHP wrapper first, so a live Dom\Text goes on pointing at freed memory: it reports the new attribute text and claims to still be attached. dom_set_attribute_ns_legacy() has always unlinked them, and the modern setAttribute() replaces the value through dom_remove_all_children(), so only the modern namespace-aware setter is missing the step.

No DTD and no entity reference are needed. A single-text-child attribute is enough, which is why the test builds one rather than reusing the phpGH-23331 shape.

Independent of php#23337, which repairs node_list_unlink() itself. Neither fix needs the other, but an attribute holding TEXT/ENTITY_REF/TEXT needs both: with the two applied that case is valgrind-clean, and with either one alone valgrind still reports an invalid read through the surviving wrapper.

dom_set_attribute_ns_modern() handed the element straight to
xmlSetNsProp(), which frees the existing attribute's child list, so a
live Dom\Text wrapper for one of those children was left pointing at
freed memory. Unlink the wrapped children first, as
dom_set_attribute_ns_legacy() already does.
@iliaal
iliaal force-pushed the fix/dom-modern-setattributens-uaf branch from 8303625 to 1932a9e Compare August 18, 2026 11:51
@iliaal

iliaal commented Aug 18, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream: php#23348

@iliaal iliaal closed this Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant