gh-152718: Reject oversized table counts in the profiling binary reader#152719
Conversation
| } | ||
| #endif | ||
|
|
||
| size_t max_frames = |
There was a problem hiding this comment.
Wondering about a helper. With these two and this one:
cpython/Modules/_remote_debugging/binary_io_reader.c
Lines 1056 to 1066 in ecdef17
it'd be three. Something along the lines of reader_validate_count("RLE", count, reader->sample_data_size - offset, 2) etc.
Generally, wondering about centralizing all these validations in a single place.
There was a problem hiding this comment.
…count Addresses review feedback: centralize the string/frame/RLE oversized-count validations behind one helper.
|
@tonghuaroot seems tests are failing can you take a look? |
On 32-bit builds a footer frame count of 0xFFFFFFFF overflows the allocation size, so the size_t overflow guard raises OverflowError before the count-vs-file-size guard raises ValueError. Accept either rejection.
|
Thanks @pablogsal, pushed a fix in 7c23d37. The failure was 32-bit only (Win32 and Emscripten): |
|
Thanks @tonghuaroot for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
Thanks for te PR @tonghuaroot |
|
GH-153050 is a backport of this pull request to the 3.15 branch. |
This bounds the eager string-table and frame-table allocations in the
_remote_debuggingbinary profile reader against the file size beforeallocating, so a
.pybfile whose footer declares an oversized count isrejected with a
ValueErrorinstead of triggering a multi-gigabyte allocation.Each table entry occupies at least one byte on disk (a string is a >=1-byte
length varint, a frame is six >=1-byte varints plus the opcode byte), so a count
larger than
(file_size - table_offset) / MIN_ENTRY_SIZEcannot be backed byreal data. This mirrors the existing RLE-count bound in
binary_reader_replay.Legitimate files are unaffected.
Regression tests in
test_binary_formatpatch the footer counts and assert thereader rejects both an oversized and a one-past-capacity count, while a count
exactly at the cap still opens.