-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Changes from 6 commits
2b5be5f
721481d
cef5146
8297c8a
7f8ed28
c5badac
f56a368
d585b09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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++) { | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyObject* child = deepcopy(st, self->extra->children[i], memo); | ||
if (!child || !Element_Check(st, child)) { | ||
if (child) { | ||
|
@@ -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; | ||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/* add object to memo dictionary (so deepcopy won't visit it again) */ | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// impossible to override their __iter__() method. | ||
PyObject *key, *value; | ||
Py_ssize_t pos = 0; | ||
int simple = 1; | ||
|
@@ -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. | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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). | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ElementObject *tmp = (ElementObject *)Py_NewRef(object); | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyObject *res = _elementtree_Element___deepcopy___impl(tmp, memo); | ||
Py_DECREF(tmp); | ||
return res; | ||
} | ||
} | ||
|
||
|
@@ -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}; | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyObject *res = PyObject_Vectorcall(st->deepcopy_obj, args, 2, NULL); | ||
Py_DECREF(args[0]); | ||
return res; | ||
} | ||
|
||
|
||
|
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.
We could also make the C implementation raising RuntimeError. It is fine either way.
Uh oh!
There was an error while loading. Please reload this page.
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.
I'll postpone this for a future PR as I want to backport this one to 3.13 and 3.14 first.