From 2ed6eb16b50df9dd228abf978337a687a68159c9 Mon Sep 17 00:00:00 2001
From: maurycy <5383+maurycy@users.noreply.github.com>
Date: Fri, 24 Apr 2026 22:17:06 +0200
Subject: [PATCH 1/4] cannot be simpler?
---
Modules/_remote_debugging/binary_io_reader.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/Modules/_remote_debugging/binary_io_reader.c b/Modules/_remote_debugging/binary_io_reader.c
index aca93e9cb1a30e..b64e94145937ef 100644
--- a/Modules/_remote_debugging/binary_io_reader.c
+++ b/Modules/_remote_debugging/binary_io_reader.c
@@ -24,6 +24,7 @@
/* File structure sizes */
#define FILE_FOOTER_SIZE 32
+#define SAMPLE_RECORD_HEADER_SIZE (sizeof(uint64_t) + sizeof(uint32_t) + 1)
#define MIN_DECOMPRESS_BUFFER_SIZE (64 * 1024) /* Minimum decompression buffer */
/* Progress callback frequency */
@@ -258,7 +259,7 @@ reader_parse_string_table(BinaryReader *reader, const uint8_t *data, size_t file
PyErr_SetString(PyExc_ValueError, "Malformed varint in string table");
return -1;
}
- if (offset + str_len > file_size) {
+ if (offset > file_size || str_len > file_size - offset) {
PyErr_SetString(PyExc_ValueError, "String table overflow");
return -1;
}
@@ -976,8 +977,8 @@ binary_reader_replay(BinaryReader *reader, PyObject *collector, PyObject *progre
}
while (offset < reader->sample_data_size) {
- /* Read thread_id (8 bytes) + interpreter_id (4 bytes) */
- if (offset + 13 > reader->sample_data_size) {
+ /* Read thread_id (8 bytes) + interpreter_id (4 bytes) + encoding byte */
+ if (reader->sample_data_size - offset < SAMPLE_RECORD_HEADER_SIZE) {
break; /* End of data */
}
From 3538dc32cd2bc52d88f5551c4e3645d208d866bd Mon Sep 17 00:00:00 2001
From: maurycy <5383+maurycy@users.noreply.github.com>
Date: Fri, 24 Apr 2026 23:15:45 +0200
Subject: [PATCH 2/4] blurb
---
.../Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst
diff --git a/Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst b/Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst
new file mode 100644
index 00000000000000..f6aa08170ed555
--- /dev/null
+++ b/Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst
@@ -0,0 +1,3 @@
+Fixed string table and sample record bounds checks in
+:mod:`!_remote_debugging` when decoding certain ``.pyb`` inputs on 32
+builds. Patch by Maurycy Pawłowski-Wieroński.
From 70e11ce32571a1770a46237ad96d68b8b8e3a2cc Mon Sep 17 00:00:00 2001
From: maurycy <5383+maurycy@users.noreply.github.com>
Date: Fri, 24 Apr 2026 23:16:10 +0200
Subject: [PATCH 3/4] gqgq
---
.../Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst b/Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst
index f6aa08170ed555..531ea2348ffdef 100644
--- a/Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst
+++ b/Misc/NEWS.d/next/Security/2026-04-24-23-15-42.gh-issue-148252.8BLmzd.rst
@@ -1,3 +1,3 @@
-Fixed string table and sample record bounds checks in
-:mod:`!_remote_debugging` when decoding certain ``.pyb`` inputs on 32
-builds. Patch by Maurycy Pawłowski-Wieroński.
+Fixed string table and sample record bounds checks in :mod:`!_remote_debugging`
+when decoding certain ``.pyb`` inputs on 32-bit builds. Patch by Maurycy
+Pawłowski-Wieroński.
From f14741f422d0983f9a3c6a7bdd9c47b931e3fb1e Mon Sep 17 00:00:00 2001
From: maurycy <5383+maurycy@users.noreply.github.com>
Date: Sun, 26 Apr 2026 00:22:04 +0200
Subject: [PATCH 4/4] SAMPLE_HEADER_FIXED_SIZE less magic
---
Modules/_remote_debugging/binary_io.h | 1 +
Modules/_remote_debugging/binary_io_reader.c | 3 +--
Modules/_remote_debugging/binary_io_writer.c | 10 ++++++----
3 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/Modules/_remote_debugging/binary_io.h b/Modules/_remote_debugging/binary_io.h
index d90546078bf68c..18f989f672e103 100644
--- a/Modules/_remote_debugging/binary_io.h
+++ b/Modules/_remote_debugging/binary_io.h
@@ -61,6 +61,7 @@ extern "C" {
#define HDR_SIZE_COMPRESSION 4
#define FILE_HEADER_SIZE (HDR_OFF_COMPRESSION + HDR_SIZE_COMPRESSION)
#define FILE_HEADER_PLACEHOLDER_SIZE 64
+#define SAMPLE_HEADER_FIXED_SIZE (sizeof(uint64_t) + sizeof(uint32_t) + 1)
static_assert(FILE_HEADER_SIZE <= FILE_HEADER_PLACEHOLDER_SIZE,
"FILE_HEADER_SIZE exceeds FILE_HEADER_PLACEHOLDER_SIZE");
diff --git a/Modules/_remote_debugging/binary_io_reader.c b/Modules/_remote_debugging/binary_io_reader.c
index b64e94145937ef..6c32ef70ac3f65 100644
--- a/Modules/_remote_debugging/binary_io_reader.c
+++ b/Modules/_remote_debugging/binary_io_reader.c
@@ -24,7 +24,6 @@
/* File structure sizes */
#define FILE_FOOTER_SIZE 32
-#define SAMPLE_RECORD_HEADER_SIZE (sizeof(uint64_t) + sizeof(uint32_t) + 1)
#define MIN_DECOMPRESS_BUFFER_SIZE (64 * 1024) /* Minimum decompression buffer */
/* Progress callback frequency */
@@ -978,7 +977,7 @@ binary_reader_replay(BinaryReader *reader, PyObject *collector, PyObject *progre
while (offset < reader->sample_data_size) {
/* Read thread_id (8 bytes) + interpreter_id (4 bytes) + encoding byte */
- if (reader->sample_data_size - offset < SAMPLE_RECORD_HEADER_SIZE) {
+ if (reader->sample_data_size - offset < SAMPLE_HEADER_FIXED_SIZE) {
break; /* End of data */
}
diff --git a/Modules/_remote_debugging/binary_io_writer.c b/Modules/_remote_debugging/binary_io_writer.c
index c129c93efe23c5..0ac6c88d0373a7 100644
--- a/Modules/_remote_debugging/binary_io_writer.c
+++ b/Modules/_remote_debugging/binary_io_writer.c
@@ -23,7 +23,6 @@
* ============================================================================ */
/* Sample header sizes */
-#define SAMPLE_HEADER_FIXED_SIZE 13 /* thread_id(8) + interpreter_id(4) + encoding(1) */
#define SAMPLE_HEADER_MAX_SIZE 26 /* fixed + max_varint(10) + status(1) + margin */
#define MAX_VARINT_SIZE 10 /* Maximum bytes for a varint64 */
#define MAX_VARINT_SIZE_U32 5 /* Maximum bytes for a varint32 */
@@ -653,10 +652,13 @@ write_sample_with_encoding(BinaryWriter *writer, ThreadEntry *entry,
memcpy(header_buf, &entry->thread_id, 8);
memcpy(header_buf + 8, &entry->interpreter_id, 4);
header_buf[12] = (uint8_t)encoding_type;
- size_t varint_len = encode_varint_u64(header_buf + 13, timestamp_delta);
- header_buf[13 + varint_len] = status;
+ size_t varint_len = encode_varint_u64(
+ header_buf + SAMPLE_HEADER_FIXED_SIZE,
+ timestamp_delta);
+ header_buf[SAMPLE_HEADER_FIXED_SIZE + varint_len] = status;
- if (writer_write_bytes(writer, header_buf, 14 + varint_len) < 0) {
+ if (writer_write_bytes(writer, header_buf,
+ SAMPLE_HEADER_FIXED_SIZE + varint_len + 1) < 0) {
return -1;
}