Skip to content

Commit e5e1f21

Browse files
committed
Fix integer overflow in binary reader stack decoding on 32-bit platforms
In `decode_stack_pop_push()` and `decode_stack_suffix()`, the final stack depth was computed as a `size_t` addition (e.g. `keep + push`). On 32-bit platforms where `size_t` is 32-bit, a crafted .pyb file could provide values that cause this addition to wrap around to a small value, bypassing the subsequent capacity check. This could lead to a heap buffer overflow via `memmove()` writing to an attacker-controlled offset. Fix by computing `final_depth` as `uint64_t`, which cannot wrap on either 32-bit or 64-bit platforms since the operands are at most 32-bit each. The capacity check then correctly rejects the overflowed value.
1 parent f6f2faf commit e5e1f21

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

Modules/_remote_debugging/binary_io_reader.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,20 @@ reader_get_or_create_thread_state(BinaryReader *reader, uint64_t thread_id,
601601
* STACK DECODING HELPERS
602602
* ============================================================================ */
603603

604+
/* Validate that final_depth fits in the stack buffer.
605+
* Uses uint64_t to prevent overflow on 32-bit platforms. */
606+
static inline int
607+
validate_stack_depth(ReaderThreadState *ts, uint64_t final_depth)
608+
{
609+
if (final_depth > ts->current_stack_capacity) {
610+
PyErr_Format(PyExc_ValueError,
611+
"Final stack depth %llu exceeds capacity %zu",
612+
(unsigned long long)final_depth, ts->current_stack_capacity);
613+
return -1;
614+
}
615+
return 0;
616+
}
617+
604618
/* Decode a full stack from sample data.
605619
* Updates ts->current_stack and ts->current_stack_depth.
606620
* Returns 0 on success, -1 on error (bounds violation). */
@@ -658,12 +672,9 @@ decode_stack_suffix(ReaderThreadState *ts, const uint8_t *data,
658672
return -1;
659673
}
660674

661-
/* Validate final depth doesn't exceed capacity */
662-
size_t final_depth = (size_t)shared + new_count;
663-
if (final_depth > ts->current_stack_capacity) {
664-
PyErr_Format(PyExc_ValueError,
665-
"Final stack depth %zu exceeds capacity %zu",
666-
final_depth, ts->current_stack_capacity);
675+
/* Use uint64_t to prevent overflow on 32-bit platforms */
676+
uint64_t final_depth = (uint64_t)shared + new_count;
677+
if (validate_stack_depth(ts, final_depth) < 0) {
667678
return -1;
668679
}
669680

@@ -713,12 +724,9 @@ decode_stack_pop_push(ReaderThreadState *ts, const uint8_t *data,
713724
}
714725
size_t keep = (ts->current_stack_depth > pop) ? ts->current_stack_depth - pop : 0;
715726

716-
/* Validate final depth doesn't exceed capacity */
717-
size_t final_depth = keep + push;
718-
if (final_depth > ts->current_stack_capacity) {
719-
PyErr_Format(PyExc_ValueError,
720-
"Final stack depth %zu exceeds capacity %zu",
721-
final_depth, ts->current_stack_capacity);
727+
/* Use uint64_t to prevent overflow on 32-bit platforms */
728+
uint64_t final_depth = (uint64_t)keep + push;
729+
if (validate_stack_depth(ts, final_depth) < 0) {
722730
return -1;
723731
}
724732

0 commit comments

Comments
 (0)