Skip to content

gh-148252: Avoid overflow in _remote_debugging binary reader bounds checks#148972

Open
maurycy wants to merge 4 commits intopython:mainfrom
maurycy:remote-debugging-int-bounds
Open

gh-148252: Avoid overflow in _remote_debugging binary reader bounds checks#148972
maurycy wants to merge 4 commits intopython:mainfrom
maurycy:remote-debugging-int-bounds

Conversation

@maurycy
Copy link
Copy Markdown
Contributor

@maurycy maurycy commented Apr 24, 2026


/* File structure sizes */
#define FILE_FOOTER_SIZE 32
#define SAMPLE_RECORD_HEADER_SIZE (sizeof(uint64_t) + sizeof(uint32_t) + 1)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense indeed 👍 Can you do the change in this PR?

Copy link
Copy Markdown
Contributor Author

@maurycy maurycy Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pablogsal

Voilà: f14741f

Truth to be told, I think there's a massive room for improvement with consts here:

https://github.com/maurycy/cpython/blob/remote-debugging-int-bounds/Modules/_remote_debugging/binary_io_reader.c#L26
https://github.com/maurycy/cpython/blob/remote-debugging-int-bounds/Modules/_remote_debugging/binary_io_reader.c#L26

#define MAX_DECOMPRESS_SIZE (1ULL << 30)

#define MAX_DECOMPRESS_SIZE (1ULL << 30)

or magic offsets:

uint64_t file_size = (uint64_t)footer_offset + 32;
uint8_t footer[32] = {0};
/* Cast size_t to uint32_t before memcpy to ensure correct bytes are copied
* on both little-endian and big-endian systems (size_t is 8 bytes on 64-bit) */
uint32_t string_count_u32 = (uint32_t)writer->string_count;
uint32_t frame_count_u32 = (uint32_t)writer->frame_count;
memcpy(footer + 0, &string_count_u32, 4);
memcpy(footer + 4, &frame_count_u32, 4);
memcpy(footer + 8, &file_size, 8);

(perhaps just FILE_FOOTER_SIZE here etc.)

https://github.com/maurycy/cpython/blob/f14741f422d0983f9a3c6a7bdd9c47b931e3fb1e/Modules/_remote_debugging/binary_io_reader.c#L985-L1003
https://github.com/maurycy/cpython/blob/f14741f422d0983f9a3c6a7bdd9c47b931e3fb1e/Modules/_remote_debugging/binary_io_writer.c#L651-L662
https://github.com/maurycy/cpython/blob/f14741f422d0983f9a3c6a7bdd9c47b931e3fb1e/Modules/_remote_debugging/binary_io_writer.c#L590-L595

(perhaps just SAMPLE_OFF warranted here as well)

etc., etc.

If interest in cleaning up these – I can create a PR.

@maurycy maurycy changed the title gh-148252: Avoid overflow in _remote_debugging binary reader bound checks gh-148252: Avoid overflow in _remote_debugging binary reader bounds checks Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants