Prefer tensor-only formats
Use a format built to hold tensors, not code. Safetensors was designed for storing tensors safely, as opposed to pickle.
Safetensors documentationHow a saved model file can run code the moment it is loaded, why pickle is the usual culprit, and what safer formats and scanning do about it.
A model serialization attack is the hiding of executable code inside a saved model file so that the code runs when someone loads the model.
It exploits file formats, most notably Python’s pickle, that can execute instructions during deserialization, and it gives the attacker code execution on the machine that loaded the file.
Pickle is Python’s protocol for serializing objects, and many model checkpoints are pickled files.
Pickle data can be built to import a module and call a function during loading. Hugging Face names the instructions that pose a threat: STACK_GLOBAL, GLOBAL and REDUCE.
It is published to a model hub, attached to a ticket or swapped into a pipeline, and it looks like any other checkpoint.
When a data scientist or a build job loads the model, the payload executes with that process’s permissions: its credentials, its network access and its data.
Use a format built to hold tensors, not code. Safetensors was designed for storing tensors safely, as opposed to pickle.
Safetensors documentationScanners read a pickle’s imports without executing it. OWASP’s 2026 guidance is to treat scanners and safe-loader flags as defense-in-depth layers, not guarantees.
Model scanningSafe-loading modes have had bypasses. CVE‑2025‑32434 allowed remote code execution through torch.load with weights_only=True in PyTorch versions before 2.6.0.
GitHub Advisory DatabaseLoad models from publishers you trust and pin exact versions. Hugging Face notes that a signed commit guarantees the origin of a file, not that the file is safe.
Hugging Face Hub docs| Model serialization attack | Model backdoor | |
|---|---|---|
| What is malicious | Code embedded in the model file | Behavior learned into the weights |
| When it acts | At load time, before any prediction | At inference, when the trigger appears |
| What it reaches | The machine that loaded the file | The model’s outputs and actions |
| Found by | File scanning and safer formats | Trigger testing and provenance checks |
Straight answers, kept current. Last updated .
Ask us anything →Because unpickling can execute code. Python’s own documentation states that the pickle module is not secure, that malicious pickle data can execute arbitrary code during unpickling, and that you should never unpickle data that could have come from an untrusted source or been tampered with. A model checkpoint downloaded from the internet is that kind of data.
It removes the pickle code-execution path. It does not make a model trustworthy. OWASP’s 2026 Supply Chain entry notes that moving to other formats reduces the risk without removing it: crafted files can still exploit memory-corruption bugs in parsers, and a backdoor in a model’s weights or computational graph persists in formats regarded as safe.
Anyone who loads model files they did not create: data scientists pulling checkpoints from public hubs, build jobs that fetch models automatically, and platforms that convert or evaluate uploaded models. The payload runs with the permissions of the process that loaded it, so a notebook holding cloud credentials or a build runner holding deployment keys is a valuable target.
Primary sources for the figures and frameworks cited on this page. Last verified .
See how Cranium AI discovers, observes, governs, secures and proves every AI model, agent and vendor in your enterprise.
Cookie Policy · Privacy Policy
Global Privacy Control detected. Opt-out request honored: marketing tags stay off in this browser.