gh-142560: bytearray: prevent UAF in search-like methods by exporting self buffer#142564
gh-142560: bytearray: prevent UAF in search-like methods by exporting self buffer#142564fatelei wants to merge 47 commits intopython:mainfrom
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? |
|
cmaloney
left a comment
There was a problem hiding this comment.
At a high level looks reasonable to me and I like the helper function to simplify. Planning to do a more thorough review this weekend.
Are there other methods which we should audit/check that they don't stash PyByteArray_AS_STRING / PyByteArray_GET_SIZE results before calling arbitrary user code then use after?
(Doesn't need to be in this PR, but would be nice to systemically check these)
|
Hi, please create a new PR. The rebase triggered a review from every codeowner, and they won't be unsubscribed by removing the request for review. |
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