gh-142560: bytearray: prevent UAF in search-like methods by exporting self buffer#142938
gh-142560: bytearray: prevent UAF in search-like methods by exporting self buffer#142938kumaraditya303 merged 9 commits intopython:mainfrom
Conversation
vstinner
left a comment
There was a problem hiding this comment.
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. |
|
|
I just spent a bit of time trying to remove the critical section but found a complicating case: After a call to Currently that is done by the critical section. Only one operation which depends on ob_exports being 0 or increments it off 0 at a time can run. To remove the critical section would need a new way to keep that invariant. |
…orting buffer in bytearray (python#142938)
I suppose that your comment is related to the 3.13 backport, #142986. |
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