# 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_class` resolves 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 3 `80 03` (`gAN`), proto 4 (3.12 default) `80 04 95` (`gASV`), proto 5 `80 05 95` (`gAWV`). Protocols 0–1 have no fixed header, so the absence of `80 0x` does not rule out a pickle. The module and function names sit in the stream as readable length-prefixed strings; `os.system` is recorded under its real home `posix`, which is the name an allowlist sees.
- **Reversing.** `pickletools.dis` and Doyensec's `r2pickledec` (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`/`H4sI` do 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]].
- `picklescan` and `modelscan` parse 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 reaching `pickle.loads` inside a "weights-only" load.
- A restricted `Unpickler` overriding `find_class` with an **exact-name allowlist** blocks code paths; a **module allowlist** is weaker (it lets `builtins.eval` through). Allowed classes still run their `__setstate__`/`__reduce__`, so audit them. NLTK's `AllowlistUnpickler` gap (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
- [[raw/afine.com/pickle-deserialization-in-ml-pipelines-the-rce-that-won-t-go-away-3815|Pickle Deserialization in ML Pipelines]]
- [[raw/github.com/payloadsallthethings-insecure-deserialization-python-4516|PayloadsAllTheThings: Insecure Deserialization Python]]
- [[raw/blog.doyensec.com/reversing-pickles-with-r2pickledec-5429|Reversing Pickles with r2pickledec]]
- [[raw/jfrog.com/pytorch-users-at-risk-3-zero-day-picklescan-vulnerabilities-jfrog-2855|PyTorch Users at Risk: 3 Zero-Day PickleScan Vulnerabilities]]
- [[raw/sonatype.com/exposing-4-critical-vulnerabilities-in-python-picklescan-sonatype-2851|Exposing 4 Critical Vulnerabilities in Python PickleScan]]
- [[raw/simonkoeck.com/i-found-a-90k-remote-code-execution-vulnerability-in-google-12253|$90k RCE Vulnerability in Google]]
- [[raw/github.com/xoscar-unauthenticated-cloudpickle-rce-cve-2026-49950-13232|Xoscar unauthenticated cloudpickle RCE]]
- [[raw/github.com/lmcache-unauthenticated-pickle-rce-on-multiprocess-transport-13237|LMCache unauthenticated pickle RCE]]
- [[raw/zeropath.com/lmdeploy-cve-2026-119706-unauthenticated-rce-via-pickle-deserializatio-13757|LMDeploy CVE-2026-119706]]
- [[raw/github.com/docsgpt-unauthenticated-rce-via-faiss-pickle-deserialization-13228|DocsGPT unauthenticated RCE via FAISS pickle]]
- [[raw/github.com/python-socketio-pickle-deserialization-rce-advisory-4518|python-socketio pickle RCE advisory]]
- [[raw/github.com/relate-lms-rce-via-insecure-celery-pickle-deserialization-13227|RELATE LMS RCE via Celery pickle]]
- [[raw/github.com/nltk-allowlistunpickler-doesn-t-deny-numpy-file-i-o-sinks-15351|NLTK AllowlistUnpickler numpy sinks]]
- [[raw/github.com/stanza-rce-via-pickle-deserialization-in-the-lemmatizer-13585|Stanza RCE via pickle deserialization]]
- [[raw/medium.com/breaking-pickle-rce-through-python-deserialization-3814|Breaking Pickle: RCE Through Python Deserialization]]
- [[raw/medium.com/sour-pickle-insecure-deserialization-with-python-pickle-4515|SOUR PICKLE: Insecure Deserialization with Python Pickle]]
- [[raw/github.com/python-pickle-rce-exploit-vulnerable-flask-app-github-4514|Python-Pickle-RCE-Exploit + vulnerable Flask App]]
- [[raw/eval.blog/leaking-internal-headers-in-flask-ninja-with-deserialization-10658|Leaking internal headers in Flask Ninja with deserialization]]
- [[raw/c0dn.dev/pickle-me-not-ctf-pickle-deserialization-writeup-14229|Pickle-Me-Not: CTF pickle writeup]]