What is a model serialisation attack?
A model serialisation attack hides malicious code in a model file saved in an unsafe format that allows code to run on load, so it runs on the loading system the moment the model opens; even with normal weights, the file behaves like software.
Unlike model weight tampering, which corrupts weights to change behaviour, a serialisation attack targets the system loading the model, running code directly on the server. Python's official documentation for its object serialisation format states plainly that it is not secure and untrusted data should not be loaded.
- Topic
- AI security
- Also known as
- Model serialization attack, unsafe deserialisation
How a model serialisation attack works
- Some common model formats can store, alongside objects, instructions to run during loading.
- The attacker adds a shell command or malicious script to the model file as such an instruction.
- When a user or automated pipeline loads the model with a common library, the command runs silently.
- It can steal credentials, open a backdoor or spread to other systems on the server, while the model keeps working as expected.
In more detail
The mechanism: __reduce__ and .pt and .bin ZIP containers
In Python's `pickle` format, the `__reduce__` method lets serialised objects declare a callable and arguments to invoke during reconstruction, commonly `os.system` or `subprocess.Popen`. Frameworks such as PyTorch historically stored checkpoints in ZIP archives containing pickled data, meaning a benign-looking `.pt` or `.bin` file triggers arbitrary code execution on `torch.load()`.
The weights_only=True default in PyTorch 2.6 and other risky libraries (joblib, numpy)
Starting with PyTorch 2.6 (2025), `torch.load()` changed its default from `weights_only=False` to `weights_only=True` to block arbitrary pickle execution by default. However, legacy pipelines still explicitly pass `weights_only=False`; `numpy.load(allow_pickle=True)`, `joblib`, `dill` and Keras `Lambda` layers remain active risks if untrusted files are loaded.
The limit of Safetensors: trust_remote_code and whole-repository security
Safe tensor formats like Safetensors eliminate deserialisation code execution for weights, yet loading a full model repository can still run code if `trust_remote_code=True` is enabled or unsafe helper scripts are imported. Safe format enforcement must therefore pair with disabling remote code execution and isolating any format conversion in an air-gapped sandbox.
Commonly confused terms
| Term | How it differs |
|---|---|
| Model weight tampering | Model weight tampering alters the numerical parameters inside a model to degrade accuracy or implant a hidden backdoor trigger. A model serialisation attack exploits the file loading mechanism to execute arbitrary OS code on the host machine. |
| AI data poisoning | Data poisoning corrupts training or fine-tuning datasets so the resulting model learns biased or backdoored behaviour. A serialisation attack needs no training phase; it injects executable payloads directly into an already-trained checkpoint file. |
| Prompt injection | Prompt injection manipulates model inputs at inference time to hijack the LLM's instructions. A model serialisation attack compromises the underlying server infrastructure before inference even starts, granting shell-level access to attackers. |
Why a model serialisation attack matters for on-premises AI
Model files are often seen as data and checked less strictly than software; this attack turns them into executable code. Controls: requiring safe tensor-only formats, scanning unsafe files or converting them in isolation, taking models only from trusted sources with verified signatures and running loading services with least privilege and network isolation.
A telecom operator scans every external model in a quarantine environment without internet access and converts it to a safe weight-only format; the production pipeline accepts no unsafe-format files at all.
Common questions
How can a model file run code?
Some serialisation formats can hold instructions run on load. An attacker puts a malicious command there, which runs as soon as the model opens.
What is the most effective control against serialisation attacks?
Using safe formats that hold only tensor data and run no code on load. Files are also scanned, signatures verified and loading done in isolation.
Related terms
- AI supply chain security
- Model scanning
- Model signing
- Model weight tampering
- Model hub typosquatting
- Code execution tool
Sources
In the glossary: Glossary, M to Q