python / python/cpython

Python pickle `load_reduce` function doesn't check if `args` is a tuple

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

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

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

説明

Bug report

Bug description:

When load_reduce() is called in Python pickle, the args variable is pulled off the stack and passed into the functions using the * operator. This syntax is type-permissive, meaning the type can be any iterable that will return the needed arguments.

https://github.com/python/cpython/blob/29acc08c8dad664cd5713cb392e5beba65724c10/Lib/pickle.py#L1723-L1727

However, in C _pickle, argtup is pulled off the stack and passed into PyObject_CallObject(), which checks if the args parameter is specifically a PyTuple().

https://github.com/python/cpython/blob/29acc08c8dad664cd5713cb392e5beba65724c10/Modules/_pickle.c#L6964

https://github.com/python/cpython/blob/29acc08c8dad664cd5713cb392e5beba65724c10/Objects/call.c#L460-L473

This means that any non-tuple iterator being used as the arguments passed into the function will cause C _pickle to throw an error, but Python pickle deserialization to unfold just fine.

payload:      b'cbuiltins\nprint\n}R.'

pickle:
None
_pickle.c:    FAILURE argument list must be a tuple
pickletools:
    0: c    GLOBAL     'builtins print'
   16: }    EMPTY_DICT
   17: R    REDUCE
   18: .    STOP
highest protocol among opcodes = 1

I think the easiest way to remedy the discrepancy is to explicitly type check the args parameter in pickle.py's load_reduce() function.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-144419

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

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

はじめの一歩

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

調査の方向性

Lib/pickle.py の load_reduce() から始め、その引数処理を Modules/_pickle.c および Objects/call.c で参照されている PyObject_CallObject() の動作と比較してください。リンクされている PR gh-144419 と既存の pickle テストを確認してください。完了の条件は、Python pickle と _pickle が報告された非タプル引数のケースを一貫して処理することです。

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

評価

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

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

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