Skip to content

gh-146266: Add bounds validation for _Py_DebugOffsets offset fields#146267

Closed
jackhax wants to merge 1 commit intopython:mainfrom
jackhax:fix/remote-debugging-bounds-check
Closed

gh-146266: Add bounds validation for _Py_DebugOffsets offset fields#146267
jackhax wants to merge 1 commit intopython:mainfrom
jackhax:fix/remote-debugging-bounds-check

Conversation

@jackhax
Copy link

@jackhax jackhax commented Mar 22, 2026

Summary

Problem

The GET_MEMBER macro in _remote_debugging.h performs unchecked pointer arithmetic using offsets read from the target process. If those offsets are corrupted (memory corruption, partially-initialized interpreter, misbehaving extension), RemoteUnwinder dereferences a wild pointer and crashes with SIGSEGV.

The existing validate_debug_offsets() only checked the cookie and version — not the actual offset values.

Fix

Added a _CHECK_OFFSET(section, field) macro that verifies offset < size for each sub-struct's fields. This catches invalid offsets at attach time, before any GET_MEMBER calls.

Test plan

  • Verified the fix compiles cleanly
  • Existing test_remote_debugging tests should pass unchanged (valid offsets always satisfy bounds checks)
  • A target with corrupted offsets now gets RuntimeError instead of crashing the debugger

…elds

validate_debug_offsets() only checked the cookie and version but did
not validate that individual offset fields are within their declared
struct sizes. If a target process has corrupted _Py_DebugOffsets (e.g.
from memory corruption or a misbehaving extension), the GET_MEMBER
macro performs unchecked pointer arithmetic with the invalid offsets,
causing RemoteUnwinder to crash with SIGSEGV.

Add per-field bounds checking: each offset must be less than the .size
of its containing sub-struct. Invalid offsets now raise RuntimeError
with a descriptive message instead of crashing the debugger process.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jackhax jackhax requested a review from pablogsal as a code owner March 22, 2026 02:41
@bedevere-app
Copy link

bedevere-app bot commented Mar 22, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot
Copy link

python-cla-bot bot commented Mar 22, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@pablogsal
Copy link
Member

Thanks for the PR @jackhax. Unfortunately I don't think this PR is the right way (see the comment in the issue). Feel free to continue the discussion there if we are missing anything.

Thanks for you work!

@pablogsal pablogsal closed this Mar 22, 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.

_remote_debugging: RemoteUnwinder crashes with SIGSEGV when _Py_DebugOffsets contains out-of-range values

2 participants