-
-
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 #142938
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
Conversation
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.
LGTM. Tests are now covered all modified methods, and methods now only use a cheap ob_exports++ and ob_exports--.
|
The removed asserts were redundant as the functions are marked with |
… by exporting buffer in bytearray (pythonGH-142938) (cherry picked from commit 220f0b1) Co-authored-by: wangxiaolei <fatelei@gmail.com>
|
|
Buildbot failure unrelated to this change |
|
Should we backport this change to 3.13 and 3.14? |
|
Thanks @fatelei for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
|
Thanks @fatelei for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14. |
…orting buffer in bytearray (pythonGH-142938) (cherry picked from commit 220f0b1) Co-authored-by: wangxiaolei <fatelei@gmail.com>
|
Sorry, @fatelei and @kumaraditya303, I could not cleanly backport this to |
|
GH-142983 is a backport of this pull request to the 3.14 branch. |
|
I think so / the previous iteration of this PR that was closed was; working on the 3.13 backport |
… by exporting buffer in bytearray (pythonGH-142938) (cherry picked from commit 220f0b1) Co-authored-by: wangxiaolei <fatelei@gmail.com>
|
GH-142986 is a backport of this pull request to the 3.13 branch. |
|
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.
bytearraysearch methods via re-entrant__index__#142560