python / python/cpython

decimal: crash when a re-entrant __bool__ deallocates the Context during Context.flags assignment or comparison

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

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

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

説明

Bug report

Context.flags (and traps) is a SignalDict that borrows a pointer to
flags owned by its Context. signaldict_setitem and
signaldict_richcompare read that pointer after calling back into Python
(PyObject_IsTrue, i.e. the value's __bool__), which can deallocate the
Context. gh-146011 cleared the borrowed pointer on Context teardown and
guarded signaldict_repr, but these two methods were left unguarded, so the
same teardown leaves them dereferencing a NULL pointer and crashing.

Assignment:

import decimal, gc
ctx = decimal.Context()
flags = ctx.flags
class Evil:
    def __bool__(self):
        global ctx; del ctx; gc.collect()
        return True
flags[decimal.InvalidOperation] = Evil()   # segfault

Comparison:

import decimal, gc
ctx = decimal.Context()
other = ctx.flags.copy()
class Evil:
    def __bool__(self):
        global ctx; del ctx; gc.collect()
        return True
other[decimal.InvalidOperation] = Evil()
ctx.flags == other                          # segfault

Both crash with SIGSEGV on current main; the affected code exists on 3.13+.

Linked PRs
  • gh-155494

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

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

はじめの一歩

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

調査の方向性

decimal モジュールのエントリポイント signaldict_setitem と signaldict_richcompare から始め、その後 Python 3.13+ で issue の代入および比較の例を再現します。Context の teardown 後に、両方の再入可能な bool ケースでクラッシュしなくなれば完了です。リンクされた PR gh-155494 がこの作業をすでにカバーしている点に注意してください。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

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

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