python / python/cpython

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

オープン
#153,664 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

extension-modules topic-subinterpreters type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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 https://github.com/python/cpython/pull/147999

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-153665

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

crossinterp_exceptions.h:85、crossinterp_data_lookup.h:102、crossinterp.c:2968 から始め、次に提供された _interpchannels の参照カウント再現プログラムを CPython main 上で実行します。shadowed-variable の問題はすでに修正済みと報告されているため、既存の PR を確認してください。残る例外と参照管理の 2 つのバグが、リークや use-after-free の動作なしに修正されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。