Skip to content

gh-91153: Fix bytearray holding a reference to its internal buffer when calling into potentially mutating __index__ methods #132379

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

bast0006
Copy link

@bast0006 bast0006 commented Apr 10, 2025

bytearray's __setitem__ implementation currently grabs a reference to its internal buffer before calling _getbyvalue to determine the index that needs assignment. _getbyvalue can call into arbitrary python code via __index__ dunders, which could alter the internal buffer and leave said reference dangling.

A prior fix for this issue ensures that bounds checking occurs after _getbyvalue is called. However, python code is capable of resizing the bytearray, resulting in limited but still broken behavior.

This patch ensures that the reference to the internal buffer is fetched only after _getbyvalue is called to prevent it from being held while any python code is run.

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 10, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Apr 10, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bast0006
Copy link
Author

bast0006 commented Apr 10, 2025

I've signed the CLA. Let me know if a NEWS entry is required. I'm not sure it is, especially since there was already a previous change that this is a fix to.

@picnixz picnixz self-requested a review May 17, 2025 11:00
Copy link
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.

Can you add a NEWS entry please? It's still worth to mention that we fixed more crashes.

@bast0006
Copy link
Author

Done

@bast0006 bast0006 requested a review from picnixz May 17, 2025 23:25
@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 18, 2025
@bast0006 bast0006 force-pushed the bast0006/gh-91153 branch from 3c1de75 to d3d1974 Compare May 18, 2025 03:53
@@ -805,6 +806,7 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
/* Delete slice */
size_t cur;
Py_ssize_t i;
char* buf = PyByteArray_AS_STRING(self);
Copy link
Member

Choose a reason for hiding this comment

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

We can also hold PyByteArray_GET_SIZE(self) temporarily here.

Copy link
Author

Choose a reason for hiding this comment

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

I agree, but I'm not as familiar with this system or C as I would want to be to feel comfortable making that change. Especially since the relevant code is mostly >10 years old and that change is not immediately required to solve the issue.

@@ -1889,6 +1889,39 @@ def __index__(self):
with self.assertRaises(IndexError):
self._testlimitedcapi.sequence_setitem(b, 0, Boom())

def test_mutating_index_inbounds(self):
# See gh-91153
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a brief description of the issue?

Copy link
Author

Choose a reason for hiding this comment

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

Done

char *buf = PyByteArray_AS_STRING(self);
// Do not store a reference to the internal buffer since
// index.__index__() or _getbytevalue() may alter 'self'.
// See https://github.com/python/cpython/issues/91153.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to fully link to it, just the gh- reference should be fine.

Suggested change
// See https://github.com/python/cpython/issues/91153.
// See gh-91153

Copy link
Author

@bast0006 bast0006 Jun 3, 2025

Choose a reason for hiding this comment

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

picnixz suggested this above, and it's common for the c source to have complete links, so I think it's fine as-is.

Copy link
Member

Choose a reason for hiding this comment

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

With complete links, I can open them from my IDE. It's much easier that way IMO

@bast0006 bast0006 force-pushed the bast0006/gh-91153 branch from 50b7998 to 4b7ec9c Compare June 3, 2025 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants