Skip to content

UAF: xml.etree.ElementTree.Element.remove when concurrent mutations happen #126033

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
picnixz opened this issue Oct 27, 2024 · 6 comments
Closed
Assignees
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes extension-modules C modules in the Modules dir topic-XML type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@picnixz
Copy link
Member

picnixz commented Oct 27, 2024

Crash report

What happened?

A UAF in Element.remove was fixed in #68279 but one can mutate the child's list during .remove and cause an OOB crash:

import xml.etree.ElementTree as ET

class EvilElement(ET.Element):
    def __eq__(self, other):
        base.clear()
        return False

base = ET.Element('a')
base.append(EvilElement('a'))
base.append(EvilElement('a'))
base.remove(ET.Element('b'))

Attacked code:

cpython/Modules/_elementtree.c

Lines 1648 to 1656 in dc76a4a

for (i = 0; i < self->extra->length; i++) {
if (self->extra->children[i] == subelement)
break;
rc = PyObject_RichCompareBool(self->extra->children[i], subelement, Py_EQ);
if (rc > 0)
break;
if (rc < 0)
return NULL;
}

I think we need to introduce some state integer to check that there is no evil mutation (similar to what's being done for OrderedDict).

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Output from running 'python -VV' on the command line:

No response

Linked PRs

@picnixz picnixz added extension-modules C modules in the Modules dir topic-XML type-crash A hard crash of the interpreter, possibly with a core dump labels Oct 27, 2024
@ZeroIntensity
Copy link
Member

Hmm, there seem to be a lot of issues with PyObject_RichCompareBool and custom __eq__'s, right? It might be worth trying to fix things more globally there rather than on a case-by-case basis like this.

@picnixz
Copy link
Member Author

picnixz commented Oct 27, 2024

I've roughly skimmed through the other occurrences and couldn't find UAFs or bad OOB. And I'd prefer fixing them one by one because the patch is not always the same.

@picnixz picnixz self-assigned this Oct 27, 2024
@picnixz

This comment was marked as resolved.

@picnixz picnixz added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes labels Oct 27, 2024
@ZeroIntensity
Copy link
Member

My main concern here is that while we can fix this as much as we want for the core, this looks like it's common enough that downstream is having this problem too.

@sobolevn
Copy link
Member

sobolevn commented Oct 28, 2024

Basically, we need to protect base from mutation during the iteration over it. An extra field maybe?

@picnixz
Copy link
Member Author

picnixz commented Oct 28, 2024

See #126041 for that (the issue and PR may have passed under your radar :))

@picnixz picnixz changed the title Crash: xml.etree.ElementTree.Element.remove with an evil Element.__eq__ Crash: xml.etree.ElementTree.Element.remove when concurrent mutations happen Dec 17, 2024
@picnixz picnixz changed the title Crash: xml.etree.ElementTree.Element.remove when concurrent mutations happen UAF: xml.etree.ElementTree.Element.remove when concurrent mutations happen Mar 31, 2025
picnixz added a commit that referenced this issue Mar 31, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 31, 2025
…en concurrent mutations happen (pythonGH-126124)

(cherry picked from commit bab1398)

Co-authored-by: Bénédikt Tran <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 31, 2025
…en concurrent mutations happen (pythonGH-126124)

(cherry picked from commit bab1398)

Co-authored-by: Bénédikt Tran <[email protected]>
picnixz added a commit that referenced this issue Mar 31, 2025
…hen concurrent mutations happen (GH-126124) (#131929)

gh-126033: fix UAF in `xml.etree.ElementTree.Element.remove` when concurrent mutations happen (GH-126124)
(cherry picked from commit bab1398)

Co-authored-by: Bénédikt Tran <[email protected]>
picnixz added a commit that referenced this issue Mar 31, 2025
…hen concurrent mutations happen (GH-126124) (#131930)

gh-126033: fix UAF in `xml.etree.ElementTree.Element.remove` when concurrent mutations happen (GH-126124)
(cherry picked from commit bab1398)

Co-authored-by: Bénédikt Tran <[email protected]>
@picnixz picnixz closed this as completed Mar 31, 2025
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes extension-modules C modules in the Modules dir topic-XML type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

3 participants