-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-142560: bytearray: prevent UAF in search-like methods by exporting self buffer #142564
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
base: main
Are you sure you want to change the base?
Conversation
8ad1901 to
887afac
Compare
|
Hi, please add a news entry for this change via blurb or blurb-it: https://devguide.python.org/contrib/core-team/committing/#how-to-add-a-news-entry |
e14f7cf to
2dd958b
Compare
Misc/NEWS.d/next/Library/2025-12-11-22-59-33.gh-issue-142560.GkJrkk.rst
Outdated
Show resolved
Hide resolved
|
Hi , according to the devguide, force push should be avoided. |
|
A lot of the methods changed in this PR share the same pattern, such as Can we add a wrapper function to reduce this boilerplate? |
|
vstinner
left a comment
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.
cc @cmaloney
bytearray: prevent UAF in search-like methods by exporting self buffer
Fix a heap use-after-free when bytearray search helpers captured the raw
buffer pointer before normalizing the “sub” argument. A crafted index
or buffer provider could clear/resize the same bytearray during argument
conversion, invalidating the saved pointer and leading to UAF.
Change:
• For bytearray methods find/rfind/index/rindex/count/startswith/endswith/
contains/split/rsplit, export a temporary Py_buffer on self and pass
view.buf/view.len to the Py_bytes* helpers, then release it. While the
export is live, resizing/clearing raises BufferError, preventing stale
pointer dereferences.
Tests:
• Add re-entrancy tests to Lib/test/test_bytes.py that verify BufferError is
raised when index clears the target during find/count/index/rfind/rindex.
This mirrors existing protection used in bytearray.join and removes the
re-entrancy hazard without changing public APIs.
bytearray.countvia re-entrant__index__#142558bytearraysearch methods via re-entrant__index__#142560