Skip to content

gh-153570: Fix use-after-free in bytearray.take_bytes() with a reentrant __index__#153572

Merged
vstinner merged 1 commit into
python:mainfrom
tonghuaroot:take-bytes-reentrant-uaf
Jul 24, 2026
Merged

gh-153570: Fix use-after-free in bytearray.take_bytes() with a reentrant __index__#153572
vstinner merged 1 commit into
python:mainfrom
tonghuaroot:take-bytes-reentrant-uaf

Conversation

@tonghuaroot

Copy link
Copy Markdown
Contributor

bytearray.take_bytes() used the size cached before the argument's __index__ call for its bounds check and
buffer reads, so an __index__ that resizes the bytearray left it reading freed memory. Re-read the size after
__index__, like bytearray.__setitem__ (GH-91153).

…reentrant __index__

bytearray.take_bytes() cached the size before the argument's __index__ call and
used it for the bounds check and the buffer reads, so an __index__ that resizes
the bytearray left it reading freed memory. Re-read the size after __index__,
matching bytearray.__setitem__ (pythonGH-91153).

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't like that during take_bytes() the byterray is resizable... but critical section doesn't prevent same thread mutation of the object.

This looks like a good fix for the current state

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@vstinner
vstinner enabled auto-merge (squash) July 24, 2026 09:53
@vstinner
vstinner merged commit ee21d99 into python:main Jul 24, 2026
108 of 110 checks passed
@cmaloney cmaloney added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 24, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app

bedevere-app Bot commented Jul 24, 2026

Copy link
Copy Markdown

GH-154648 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 24, 2026
vstinner pushed a commit that referenced this pull request Jul 24, 2026
… reentrant __index__ (GH-153572) (#154648)

gh-153570: Fix use-after-free in bytearray.take_bytes() with a reentrant __index__ (GH-153572)

bytearray.take_bytes() cached the size before the argument's __index__ call and
used it for the bounds check and the buffer reads, so an __index__ that resizes
the bytearray left it reading freed memory. Re-read the size after __index__,
matching bytearray.__setitem__ (GH-91153).
(cherry picked from commit ee21d99)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
@vstinner

Copy link
Copy Markdown
Member

Merged and backported to 3.15. Thanks for your fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants