Skip to content

crossinterp.c: Use-after-free + leaked exception + shadowed variable #153664

Description

@aisk

Bug report

Bug description:

Original gist: https://gist.github.com/devdanzin/a56b8cb579b1950b4206e8a42d6be650

Summary

Three bugs in the cross-interpreter subsystem:

  1. _ensure_notshareableerror (crossinterp_exceptions.h:85): Missing return after _PyErr_SetRaisedException steals ctx ref. Falls through to code using freed pointer.
  2. _PyXI_UnwrapNotShareableError (crossinterp_data_lookup.h:102): Exception removed from thread state via _PyErr_GetRaisedException, then _PyXI_InitFailure fails → exception leaked, thread error indicator cleared.
  3. _pop_preserved (crossinterp.c:2968): Inner _PyXI_namespace *xidata shadows outer. goto error cleanup checks outer (NULL) variable → inner allocation leaked.

Reproducer (ref leak)

import _interpchannels as ch, sys

cid = ch.create()
before = sys.gettotalrefcount()
for i in range(10000):
    try:
        ch.send(cid, lambda: None, blocking=False)
    except Exception:
        pass
after = sys.gettotalrefcount()
ch.destroy(cid)
print(f"Leaked {after - before} refs (~{(after-before)//10000}/send)")

The third issue is already fixed by #147999

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions