Python Pickle Deserialization
pickle.loads()(and everything built on it —joblib,torch.load,numpy.load(allow_pickle=True),pandas.read_pickle,cloudpickle) executing attacker-controlled bytes, because the pickle format is a stack-based VM that can name any importable callable and ask for it to be called.
How it works
- Pickle is code, not data. Unlike JSON, a pickle stream can encode a call. The
__reduce__protocol lets an object declare a callable and its arguments that run during unpickling;find_classresolves a(module, name)pair and imports the module if needed, so any importable callable in the process is reachable. The standard library alone is enough — no third-party gadget required. - Magic bytes. Protocol is announced by the opcode framing: proto 2
80 02(gAJ), proto 380 03(gAN), proto 4 (3.12 default)80 04 95(gASV), proto 580 05 95(gAWV). Protocols 0–1 have no fixed header, so the absence of80 0xdoes not rule out a pickle. The module and function names sit in the stream as readable length-prefixed strings;os.systemis recorded under its real homeposix, which is the name an allowlist sees. - Reversing.
pickletools.disand Doyensec’sr2pickledec(Radare2, all opcodes to proto 5) decompile a stream statically — “don’t run it” is the correct triage posture.
The ML model supply chain
PyTorch adopted pickle as its model format in 2018, making it the de-facto standard; torch.load, joblib.load, HuggingFace model hubs and many pipelines unpickle by default. Loading an untrusted model is remote code execution with the privileges of the inference process (often root in containers, with cloud instance-metadata credentials in reach). See Model Deserialization Attacks for the scanner-bypass and supply-chain-poisoning dimension and Python AI and ML Framework Vulnerabilities.
Detection and scanning limits
- Decode the value and check the opcode bytes, not just a base64 prefix — a two-character prefix collides by chance;
gASV/rO0AB/H4sIdo not. - Response-identical handlers run code and still return a uniform error, so an out-of-band callback (HTTP or DNS) is the reliable proof — see Insecure Deserialization and Out-of-Band Detection.
picklescanandmodelscanparse bytecode against a denylist of unsafe globals. This is fragile: JFrog (CVE-2025-10155/10156/10157) and Sonatype (CVE-2025-1716/1889/1944/1945) each found multiple bypasses turning on file-extension trust, ZIP CRC/flag tampering, and subclass-import gaps; academic work found 22 model loading paths (19 missed by scanners) and 133 exploitable gadgets with ~100% bypass. Denylisting is losing ground to allowlist unpicklers.
Notable cases
| CVE / advisory | Target | Note |
|---|---|---|
| — | Google DevSite (Simon Köck, $90k) | Flask session pickle carried the OAuth client secret, reused as SECRET_KEY; a forged cookie reached pickle.loads → unauth RCE |
| CVE-2026-49950 | Xorbits Xoscar | Unauth cloudpickle.loads() on socket data |
| CVE-2026-105192 | LMCache | Unauth pickle over ZeroMQ IPC; fixed with validated MessagePack |
| CVE-2026-119706 | LMDeploy | Unauth pickle on /update_weights |
| GHSA-m448-vp23-j393 | DocsGPT | FAISS index.pkl upload, allow_dangerous_deserialization=True |
| GHSA-g8c6-8fjj-2r4m | python-socketio | Pickle over the message queue; fixed by moving to JSON |
| — | RELATE LMS | Celery worker configured to accept pickle |
Many ML-library cases (Stanza, MLflow, NVIDIA Megatron/Model Optimizer, Meta FAIRChem/Detectron2/fairseq, Ultralytics, audio-separator, numcodecs, Qiskit QPY) are catalogued in Model Deserialization Attacks.
Prevention
- Do not unpickle untrusted data. For model weights use
safetensors; for interchange use JSON/MessagePack. torch.load(weights_only=True)helps but is not a full boundary — Stanza (CVE-2026-115878) showed attacker bytes still reachingpickle.loadsinside a “weights-only” load.- A restricted
Unpickleroverridingfind_classwith an exact-name allowlist blocks code paths; a module allowlist is weaker (it letsbuiltins.evalthrough). Allowed classes still run their__setstate__/__reduce__, so audit them. NLTK’sAllowlistUnpicklergap (numpy file-I/O sinks left un-denied) shows the subtlety. - Treat a leaked signing key for a signed pickle as a deserialization finding; verify the signature before unpickling, not after.
Related
- Insecure Deserialization
- Model Deserialization Attacks
- Python AI and ML Framework Vulnerabilities
- Gadget Chains
- Remote Code Execution
- Unsafe Python Functions
Sources
- Pickle Deserialization in ML Pipelines · original
- PayloadsAllTheThings: Insecure Deserialization Python · original
- Reversing Pickles with r2pickledec · original
- PyTorch Users at Risk: 3 Zero-Day PickleScan Vulnerabilities · original
- Exposing 4 Critical Vulnerabilities in Python PickleScan · original
- $90k RCE Vulnerability in Google · original
- Xoscar unauthenticated cloudpickle RCE · original
- LMCache unauthenticated pickle RCE · original
- LMDeploy CVE-2026-119706 · original
- DocsGPT unauthenticated RCE via FAISS pickle · original
- python-socketio pickle RCE advisory · original
- RELATE LMS RCE via Celery pickle · original
- NLTK AllowlistUnpickler numpy sinks · original
- Stanza RCE via pickle deserialization · original
- Breaking Pickle: RCE Through Python Deserialization · original
- SOUR PICKLE: Insecure Deserialization with Python Pickle · original
- Python-Pickle-RCE-Exploit + vulnerable Flask App · original
- Leaking internal headers in Flask Ninja with deserialization · original
- Pickle-Me-Not: CTF pickle writeup · original