Skip to content

gh-148735: Fix a UAF in Element.findtext()#148738

Open
StanFromIreland wants to merge 2 commits intopython:mainfrom
StanFromIreland:findall
Open

gh-148735: Fix a UAF in Element.findtext()#148738
StanFromIreland wants to merge 2 commits intopython:mainfrom
StanFromIreland:findall

Conversation

@StanFromIreland
Copy link
Copy Markdown
Member

@StanFromIreland StanFromIreland commented Apr 18, 2026

This was a regression introduced by c57623c.

/* return borrowed reference to text attribute */

`Element.findtext()`
Comment thread Modules/_elementtree.c Outdated
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the occurrences you change are of the form (get borrowed) followed by (incref) right? if so, it is ok to change it to (get strong ref) but otherwise we may have un-necessary incref/decref. Currently XML is not FT-safe but if we (I) want to make it so, we should avoid incref/decref in hot paths

@@ -0,0 +1,4 @@
:mod:`xml.etree.ElementTree`: Fix a use-after-free in
:meth:`Element.findtext <xml.etree.ElementTree.Element.findtext>` when the
tag to find implements an :meth:`~object.__eq__` method that drops every
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just mention that this happens when there are mutations. Using eq is one possibility (probably the only one really easy to trigger though)

@@ -0,0 +1,4 @@
:mod:`xml.etree.ElementTree`: Fix a use-after-free in
:meth:`Element.findtext <xml.etree.ElementTree.Element.findtext>` when the
tag to find implements an :meth:`~object.__eq__` method that drops every
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This describes how to reproduce the issue. But this is not what happens in most user code. Users rarely implement __eq__ which intentionally mutates the element. More realistic scenario -- it is mutated concurrently with running findtext(). This usually happens with threads, but can also happen with garbage collector or indirectly in __eq__. Anyway, the cause is a concurrent mutation.

@serhiy-storchaka
Copy link
Copy Markdown
Member

but otherwise we may have un-necessary incref/decref

The number of incerfs/decrefs remains the same. They are not unnecessary.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 22, 2026

Yeah I was not sure about this as I am on mobile. Thanks for checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants