Login Get a Demo
01 Glossary · AI supply chain

What Is a Model Serialization Attack?

How 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.

The short answer

What is a model serialization attack?

Last updated
Read time
4 min read
From
Cranium AI
Also called
Unsafe deserialization, Malicious model file, Pickle exploit
Share on LinkedIn

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.

Key takeaways
  • Python’s documentation says the pickle module is not secure and warns never to unpickle data from an untrusted source.
  • The attack needs nothing from the model’s weights: loading the file is enough to run the attacker’s code.
  • Safetensors is a format for storing tensors safely, as opposed to pickle.
  • CVE‑2025‑32434 showed that PyTorch’s weights_only loading mode could be bypassed in versions before 2.6.0.
02 How it works

How does a model serialization attack work?

  1. A model is saved as a pickle

    Pickle is Python’s protocol for serializing objects, and many model checkpoints are pickled files.

  2. The attacker adds a payload

    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.

  3. The file is shared

    It is published to a model hub, attached to a ticket or swapped into a pipeline, and it looks like any other checkpoint.

  4. Loading runs the code

    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.

03 Reduce the risk

How do you reduce the risk?

01

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 documentation
02

Scan, without relying on it

Scanners 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 scanning
03

Patch the loader

Safe-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 Database
04

Verify the source

Load 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
04 Compare

Serialization attack vs. model backdoor

Model serialization attackModel backdoor
What is maliciousCode embedded in the model fileBehavior learned into the weights
When it actsAt load time, before any predictionAt inference, when the trigger appears
What it reachesThe machine that loaded the fileThe model’s outputs and actions
Found byFile scanning and safer formatsTrigger testing and provenance checks
05 Keep going

Related reading.

06 Questions

Frequently asked questions.

Straight answers, kept current. Last updated .

Ask us anything →
Why is pickle unsafe for model files?

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.

Does switching to safetensors eliminate the risk?

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.

Who is exposed to model serialization attacks?

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.

07 See it for yourself

From the definition
to the evidence.

See how Cranium AI discovers, observes, governs, secures and proves every AI model, agent and vendor in your enterprise.