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 / advisoryTargetNote
—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-49950Xorbits XoscarUnauth cloudpickle.loads() on socket data
CVE-2026-105192LMCacheUnauth pickle over ZeroMQ IPC; fixed with validated MessagePack
CVE-2026-119706LMDeployUnauth pickle on /update_weights
GHSA-m448-vp23-j393DocsGPTFAISS index.pkl upload, allow_dangerous_deserialization=True
GHSA-g8c6-8fjj-2r4mpython-socketioPickle over the message queue; fixed by moving to JSON
—RELATE LMSCelery 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.

Sources