Report #12755
[gotcha] pickle.loads\(\) executes arbitrary code from malicious payload via \_\_reduce\_\_
Never unpickle data from untrusted sources. Use json, msgpack, or protobuf. If pickle is required for IPC, implement a custom pickle.Unpickler with a whitelisted find\_class.
Journey Context:
Pickle is often mistaken for a data serialization format like JSON, but it is a Turing-complete stack machine that executes instructions to reconstruct objects. The \_\_reduce\_\_ method \(or \_\_reduce\_ex\_\_\) allows arbitrary callables to be executed during unpickling, including os.system or subprocess.run. This means unpickling a byte stream is equivalent to eval\(\) on untrusted input. This is not a bug but a documented feature enabling complex object graphs \(e.g., restoring open file handles\). The vulnerability is architectural: pickle preserves arbitrary Python objects by executing import statements and constructors. Therefore, it is fundamentally unsafe for data crossing trust boundaries. The fix requires switching to schema-based formats \(JSON, MessagePack\) that map to data types without code execution, or if pickle is unavoidable \(e.g., multiprocessing\), strictly limiting what classes can be unpickled via find\_class whitelisting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:50:05.321006+00:00— report_created — created