Skip to content

gh-133009: fix UAF in xml.etree.ElementTree.Element.__deepcopy__ #133010

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

Merged
merged 8 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2960,6 +2960,24 @@ def element_factory(x, y):
del b
gc_collect()

def test_deepcopy(self):
# Prevent crashes when __deepcopy__() mutates the element being copied.
# See https://github.com/python/cpython/issues/133009.
class X(ET.Element):
def __deepcopy__(self, memo):
root.clear()
return self

root = ET.Element('a')
evil = X('x')
root.extend([evil, ET.Element('y')])
if is_python_implementation():
Copy link
Member

Choose a reason for hiding this comment

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

We could also make the C implementation raising RuntimeError. It is fine either way.

Copy link
Member Author

@picnixz picnixz May 10, 2025

Choose a reason for hiding this comment

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

I'll postpone this for a future PR as I want to backport this one to 3.13 and 3.14 first.

self.assertRaises(RuntimeError, copy.deepcopy, root)
else:
c = copy.deepcopy(root)
# In the C implementation, we can still copy the evil element.
self.assertListEqual(list(c), [evil])


class MutationDeleteElementPath(str):
def __new__(cls, elem, *args):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`xml.etree.ElementTree`: Fix a crash in :meth:`Element.__deepcopy__
<object.__deepcopy__>` when the element is concurrently mutated.
Patch by Bénédikt Tran.
33 changes: 25 additions & 8 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,8 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo)

PyTypeObject *tp = Py_TYPE(self);
elementtreestate *st = get_elementtree_state_by_type(tp);
// The deepcopy() helper takes care of incrementing the refcount
// of the object to copy so to avoid use-after-frees.
tag = deepcopy(st, self->tag, memo);
if (!tag)
return NULL;
Expand Down Expand Up @@ -850,8 +852,7 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo)
if (element_resize(element, self->extra->length) < 0)
goto error;

// TODO(picnixz): check for an evil child's __deepcopy__ on 'self'
for (i = 0; i < self->extra->length; i++) {
for (i = 0; self->extra && i < self->extra->length; i++) {
PyObject* child = deepcopy(st, self->extra->children[i], memo);
if (!child || !Element_Check(st, child)) {
if (child) {
Expand All @@ -865,7 +866,12 @@ _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo)
}

assert(!element->extra->length);
element->extra->length = self->extra->length;
/*
* The original 'self->extra' may be gone at this point if deepcopy()
* has side-effects. However, 'i' is the number of copied items that
* we were able to successfully copy.
*/
element->extra->length = i;
}

/* add object to memo dictionary (so deepcopy won't visit it again) */
Expand Down Expand Up @@ -899,6 +905,8 @@ deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)

if (Py_REFCNT(object) == 1) {
if (PyDict_CheckExact(object)) {
// Exact dictionaries do not execute arbitrary code as it's
Copy link
Member Author

Choose a reason for hiding this comment

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

@serhiy-storchaka is this assumption correct? namely, here I don't need to incref object temporarily right?

Copy link
Member

Choose a reason for hiding this comment

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

PyDict_Next() does not use __iter__, so this comment is redundant. PyDict_Next() does not call any user code.

// impossible to override their __iter__() method.
PyObject *key, *value;
Py_ssize_t pos = 0;
int simple = 1;
Expand All @@ -908,13 +916,20 @@ deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
break;
}
}
if (simple)
if (simple) {
// This does not call object.__copy__(), so it's still safe.
return PyDict_Copy(object);
}
/* Fall through to general case */
}
else if (Element_CheckExact(st, object)) {
return _elementtree_Element___deepcopy___impl(
(ElementObject *)object, memo);
// The __deepcopy__() call may call arbitrary code even if the
// object to copy is a built-in XML element (one of its children
// may mutate 'object' in its own __deepcopy__() implementation).
ElementObject *tmp = (ElementObject *)Py_NewRef(object);
PyObject *res = _elementtree_Element___deepcopy___impl(tmp, memo);
Py_DECREF(tmp);
return res;
}
}

Expand All @@ -925,8 +940,10 @@ deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
return NULL;
}

PyObject *args[2] = {object, memo};
return PyObject_Vectorcall(st->deepcopy_obj, args, 2, NULL);
PyObject *args[2] = {Py_NewRef(object), memo};
PyObject *res = PyObject_Vectorcall(st->deepcopy_obj, args, 2, NULL);
Py_DECREF(args[0]);
return res;
}


Expand Down
Loading