Skip to content

bpo-31758: Prevent reference leaks in __setstate__() and __init__() of _elementtree.Element #3956

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
wants to merge 6 commits into from

Conversation

orenmn
Copy link
Contributor

@orenmn orenmn commented Oct 11, 2017

In addition, add a test to test_xml_etree_c, to make sure that the refleaks in __setstate__() are no more.

https://bugs.python.org/issue31758

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Note that element_resize() allocates extra nchildren items. If self->extra->length != 0, self->extra->allocated > nchildren.

Also note that if children and attrib are empty, the element keeps old children and attributes instead of clearing them.

The code at line 941 looks suspicious to me. It looks like a double decref.


self->handle_start = PyObject_GetAttrString(target, "start");
Py_XSETREF(self->handle_start, PyObject_GetAttrString(target, "start"));
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps PyObject_GetAttrString(), Py_XSETREF() and ignore_attribute_error() can be combined in one function.

@@ -994,6 +994,10 @@ element_setstate_from_attributes(ElementObject *self,
}
assert(self->extra && self->extra->allocated >= nchildren);

/* Decref each of the old children. */
for (i = 0; i < self->extra->length; i++) {
Py_DECREF(self->extra->children[i]);
Copy link
Member

Choose a reason for hiding this comment

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

Py_DECREF() can cause executing arbitrary Python code. This can change the size of the children list. PyList_GET_ITEM() in the following loop can crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, indeed.
Then ISTM that we should try to write a helper function that would be used both here and by element_ass_subscr().
what do you think?

@orenmn
Copy link
Contributor Author

orenmn commented Oct 13, 2017

Note that element_resize() allocates extra nchildren items. If self->extra->length != 0, self->extra->allocated > nchildren.

Also note that if children and attrib are empty, the element keeps old children and attributes instead of clearing them.

What is the context of these comments? I guess i missed something that should be obvious after reading them..

The code at line 941 looks suspicious to me. It looks like a double decref.

I agree. ISTM that these if and else blocks can be replaced with simply:

Py_DECREF(children);
return instancedict;

On my pc, this patch seems to work fine.
To test it, i patched _Py_BuildValue_SizeT() to return NULL when it is called from _elementtree_Element___getstate___impl(), but i don't know how to write a proper test for the test suite that would simulate a failure of Py_BuildValue().

@@ -117,6 +118,18 @@ def __del__(self):
elem.tail = X()
elem.__setstate__({'tag': 42}) # shouldn't cause an assertion failure

def test_refleaks_in_Element___setstate__(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe add @refcount_test?

@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error needs backport to 3.6 labels Feb 26, 2018
@serhiy-storchaka serhiy-storchaka self-assigned this Feb 26, 2018
@vstinner
Copy link
Member

I removed the " needs backport to 3.6" label, the 3.6 branch no longer accept bugfixes (only security fixes are accepted): https://devguide.python.org/#status-of-python-branches

@scoder
Copy link
Contributor

scoder commented Sep 10, 2019

I resolved the conflicts, but Serhiy's comments still apply.
@orenmn, is there any chance that you could update your PR?

@orenmn
Copy link
Contributor Author

orenmn commented Sep 10, 2019

I resolved the conflicts, but Serhiy's comments still apply.
@orenmn, is there any chance that you could update your PR?

Sorry, but no chance for that currently.. (though I am sure you could do a better job than me)

@csabella csabella added the stale Stale PR or inactive for long period of time. label Feb 4, 2020
@csabella
Copy link
Contributor

It looks like this issue was resolved with #3997.

@csabella csabella closed this May 27, 2020
@serhiy-storchaka serhiy-storchaka removed their assignment Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review stale Stale PR or inactive for long period of time. type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants