Skip to content

gh-158648: Reject code objects whose stacksize overflows co_framesize - #159036

Closed
Joekrry wants to merge 1 commit into
python:mainfrom
Joekrry:fix-marshal-stacksize-overflow
Closed

Joekrry wants to merge 1 commit into
python:mainfrom
Joekrry:fix-marshal-stacksize-overflow

Conversation

@Joekrry

@Joekrry Joekrry commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Fixes gh-158648.

Python/marshal.c reads stacksize from untrusted marshal data with only a >= 0 check. When the code object is constructed, co_framesize = nlocalsplus + stacksize + FRAME_SPECIALS_SIZE is computed as a C int; a stacksize near INT_MAX overflows this, producing a negative co_framesize. That value defeats the stack-space check in _PyThreadState_HasStackSpace _PyThreadState_PushFrame and can move the interpreter's data-stack pointer backwards, corrupting memory rather than failing cleanly.

This adds a bounds check to _PyCode_Validate() — the single validation path shared by marshal, types.CodeType(), and code.replace() — that rejects a stacksize large enough to overflow co_framesize, raising OverflowError instead.

Verified against the PoC in the issue (now raises OverflowError instead of corrupting frame state), plus full test_marshal, test_code, test_sys_settrace, and test_frame runs (all passing).

…mesize

  marshal.c reads stacksize from untrusted data with only a >= 0 check.
  A stacksize near INT_MAX overflows the co_framesize computation in
  codeobject.c, producing a negative frame size that corrupts the
  interpreter's data stack instead of failing cleanly. Reject it in
  _PyCode_Validate(), the common validation path used by marshal,
  types.CodeType(), and code.replace().
@picnixz

picnixz commented Oct 8, 2026

Copy link
Copy Markdown
Member

We did not decide whethet to consider the issue. Please do not open PRs for those cases.

@Joekrry
Joekrry deleted the fix-marshal-stacksize-overflow branch October 8, 2026 18:18
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.

Marshal Stacksize Integer Overflow leading to Frame Crash

2 participants