Loading a model checkpoint can run code, because PyTorch's default checkpoint format is a Python pickle and a pickle can tell the loader to call any function. That turns every model download into a software install. We show what a checkpoint holds, run a harmless demonstration in a throwaway container, and list the checks we use before a model file touches a real machine.
What a model file holds
A model file carries two kinds of content: the tensors that hold the weights, and metadata that says how to rebuild the Python objects around them.
PyTorch's torch.save writes an uncompressed ZIP archive.
Inside it, data/ holds the tensor storages, one file each, and data.pkl holds everything else, pickled.
That pickle is the problem.
Python's documentation says unpickling imports any class or function it finds in the pickle data by default, which "permits the unpickler to import and invoke arbitrary code."
An object controls how it is pickled through __reduce__, which returns a callable and a tuple of arguments; on load, the unpickler calls that callable with those arguments.
The module's own warning is blunt: pickle "is not secure", and malicious pickle data can "execute arbitrary code during unpickling."
So a checkpoint is a program that rebuilds tensors, and the program can do more than that.
Sources: PyTorch documentation, Serialization semantics; Python documentation, pickle.
What torch.load does by default
PyTorch changed the default to make the common case safer.
Starting in version 2.6, torch.load uses weights_only=True unless a pickle_module argument is passed.
That setting restricts the unpickler to what a state_dict of plain tensors and some primitive types needs, and unlike the standard unpickler it cannot import anything while it loads.
The documentation is precise about what this does not cover:
- it "does not guard against denial of service attacks";
- memory corruption during loading "might still be possible";
- loaded objects can still surprise downstream code, such as a sparse tensor built from crafted indices.
The same page recommends weights_only=False for an old checkpoint that contains a whole nn.Module, and the error a blocked load prints explains how to pass that flag.
That is the step to watch for: a tutorial or a model card that tells readers to set weights_only=False is asking them to run whatever the file contains.
To see which functions a checkpoint would call before loading it, torch.serialization.get_unsafe_globals_in_checkpoint() lists the non-allowlisted globals it can see; the docs warn that types built dynamically during unpickling are not reported.
Sources: PyTorch documentation, Serialization semantics and torch.load, version 2.15.
A pickle that runs a function on load
We wrote two short scripts to watch this happen, and we ran them.
They are our illustrative code: the hidden call is Python's print, so the only effect of loading the file is one line of output.
The first script saves the same small tensor three ways, and adds a Note object whose __reduce__ asks the loader to call print:
"""Our illustrative demo: save one tensor as a pickle, a torch checkpoint and safetensors.
The hidden call is harmless: it only prints a line.
"""
import pickle
import torch
from safetensors.torch import save_file
class Note:
"""Pickles as an instruction to call print() when the file is loaded."""
def __reduce__(self):
return (print, ("print() ran while the file was loading",))
weight = torch.arange(6, dtype=torch.float32).reshape(2, 3)
with open("demo.pkl", "wb") as f:
pickle.dump({"weight": weight.tolist(), "note": Note()}, f)
torch.save({"weight": weight, "note": Note()}, "demo.pt")
save_file({"weight": weight}, "demo.safetensors")
print("wrote demo.pkl, demo.pt, demo.safetensors")
try:
save_file({"weight": weight, "note": Note()}, "rejected.safetensors")
except Exception as err: # safetensors accepts tensors only
print("safetensors refused the note:", type(err).__name__)
The second script plays the victim and loads each file in a fresh process, where the Note class does not exist:
"""Our illustrative demo: load the three files in a fresh process, as a victim would."""
import pickle
import torch
from safetensors.torch import load_file
print("1. pickle.load")
with open("demo.pkl", "rb") as f:
pickle.load(f)
print("2. torch.load with the default")
try:
torch.load("demo.pt")
except pickle.UnpicklingError as err:
reason = [s for s in str(err).splitlines() if "WeightsUnpickler" in s]
print("refused:", reason[0].strip())
print("3. torch.load with weights_only=False")
torch.load("demo.pt", weights_only=False)
print("4. safetensors load_file")
print(load_file("demo.safetensors"))
with open("demo.safetensors", "rb") as f:
size = int.from_bytes(f.read(8), "little")
print("header:", f.read(size).decode())
We ran both in a container with no network, a read-only root filesystem and a scratch working directory that disappears with the container:
cat > Dockerfile <<'EOF'
FROM python:3.12-slim
RUN pip install --no-cache-dir --index-url https://download.pytorch.org/whl/cpu torch==2.15.0
RUN pip install --no-cache-dir safetensors==0.8.0 picklescan==1.0.5 numpy==2.5.4
EOF
docker build -q -t model-files-demo .
docker run --rm --network none --read-only --tmpfs /work --tmpfs /tmp \
-v "$PWD":/demo:ro model-files-demo sh /demo/run.sh
The run.sh script copies the two Python files into the scratch directory and runs them, then scans the checkpoint with picklescan.
This is the captured output, without the version banner, the exit-code lines and the final ls:
$ python make_files.py
wrote demo.pkl, demo.pt, demo.safetensors
safetensors refused the note: ValueError
$ python load_files.py
1. pickle.load
print() ran while the file was loading
2. torch.load with the default
refused: WeightsUnpickler error: Unsupported global: GLOBAL print was not an allowed global by default. Please use `torch.serialization.add_safe_globals([print])` or the `torch.serialization.safe_globals([print])` context manager to allowlist this global if you trust this class/function.
3. torch.load with weights_only=False
print() ran while the file was loading
4. safetensors load_file
{'weight': tensor([[0., 1., 2.],
[3., 4., 5.]])}
header: {"weight":{"dtype":"F32","shape":[2,3],"data_offsets":[0,24]}}
$ picklescan --globals --path demo.pt
----------- SCAN SUMMARY -----------
Scanned files: 1
Infected files: 0
Suspicious globals: 1
Dangerous globals: 0
All globals found:
* torch.FloatStorage - innocuous
* torch._utils._rebuild_tensor_v2 - innocuous
* collections.OrderedDict - innocuous
* __builtin__.print - suspicious
$ picklescan --strict --path demo.pt
/work/demo.pt:demo/data.pkl: dangerous import '__builtin__ print' FOUND (promoted by --strict)
----------- SCAN SUMMARY -----------
Scanned files: 1
Infected files: 1
Suspicious globals: 0
Dangerous globals: 1
Four results matter:
- the plain pickle called
printin a process that had never seen ourNoteclass, because the file names a built-in function and the loader needs nothing else; - torch 2.15 refused the same payload under its default and named the global it would have called;
- one flag,
weights_only=False, brought the call back; - picklescan rated
printas suspicious, reported zero infected files and exited 0, and only--strictturned that into a finding with exit code 1.
The safetensors file was 96 bytes: an 8-byte size field, a 64-byte JSON header padded with spaces, and the 24 bytes of six 32-bit floats.
The library refused to store the Note at all.

- One tensor, saved three ways: the first script saves the same small tensor as a plain pickle, a PyTorch checkpoint and a safetensors file, and adds a
Noteobject whose__reduce__asks the loader to callprint. - The plain pickle called print: the plain pickle called
printin a process that had never seen theNoteclass (load 1 in the output above). - The default refused it: torch 2.15 refused the same payload under its default (load 2).
- One flag brought it back: one flag,
weights_only=False, brought the call back (load 3). - Safetensors did not store it: the library refused to store the
Noteat all, and the safetensors file was 96 bytes.
Sources: our run (torch 2.15.0, safetensors 0.8.0, picklescan 1.0.5, Python 3.12); picklescan, README.
What safetensors protects and what it does not
Safetensors was built to remove pickle from this path. Its README gives the layout: eight bytes holding the header size, a JSON header with each tensor's dtype, shape and byte offsets, then a flat byte buffer. The format adds limits that close specific tricks:
- the optional
__metadata__entry may hold only string-to-string pairs; - the byte buffer must be fully indexed with no holes, which prevents polyglot files;
- the header is capped at 100 MB and tensor addresses may not overlap.
Its design goal is that a randomly downloaded file can be used without running arbitrary code.
That goal covers the loader, not the model. The README states that tensor values are not checked. OWASP's supply chain entry describes tampered and poisoned models that carry backdoors, and cites PoisonGPT, which changed model parameters directly and was published to Hugging Face. MITRE ATLAS lists other parts of a model package that can carry unsafe behavior: chat templates, tokenizer metadata, configuration, and pre- or post-processing logic. A safetensors file next to a malicious tokenizer config or custom loading code is still a malicious model repository.
Sources: safetensors README; Hugging Face, Safetensors documentation; OWASP, LLM03:2025 Supply Chain; MITRE ATLAS, AML.T0011.000 Unsafe AI Artifacts.
Payloads hidden in tensor values
Because nothing checks tensor values, the weights themselves can hide data. The 2021 EvilModel paper by Wang, Liu and Cui embedded 36.9 MB of malware in a 178 MB AlexNet model with less than 1% accuracy loss, and antivirus engines on VirusTotal raised no suspicion.
The paper's design also shows the limit of the technique. Its payload is inert data: a separate receiver program on the target downloads the model, extracts the bytes from the parameters, checks them and then runs them. Loading the model, in any format, does not start the payload; the receiver has to be on the machine already. So tensor steganography is a way to move a payload past scanners, and the defenses are the ones that catch any modified file: knowing which file you expected and checking that you got it.
Source: Wang, Liu and Cui, EvilModel: Hiding Malware Inside of Neural Network Models, arXiv 2107.08590.
What we check before loading a model
OWASP notes that published models carry no strong provenance guarantees, and that model cards give no guarantee of a model's origin. These are the checks we apply, in order:
- Prefer the safetensors files in a repository, and skip
.bin,.ptand.pklfiles when a safetensors copy exists. - Load PyTorch checkpoints with
weights_only=True, which is the default from 2.6, and treat any instruction to turn it off as a request to run the file's code. - Pin the exact revision: the Hugging Face Hub accepts a
revisionargument, requires the full commit hash rather than a 7-character one, and otherwise downloads the latest revision. - Record the hash of every file we load, as OWASP recommends, and keep the model, its revision and its source in the same inventory as our other dependencies, which ATLAS calls an AI bill of materials.
- Inspect a pickle without running it:
python -m pickletoolsdoes not execute pickle bytecode, andget_unsafe_globals_in_checkpoint()lists the globals a checkpoint would call. - Scan with picklescan in
--strictmode, and read the result as a second opinion: its default mode passed ourprintpayload, and its maintainer published three critical blocklist bypasses in March 2026. - Load anything untrusted in a container like the one above, with no network and no mounted secrets, so a payload that runs finds nothing to steal and nowhere to send it.
Sources: OWASP, LLM03:2025 Supply Chain; Hugging Face, Download files from the Hub; MITRE ATLAS, AML.T0010 AI Supply Chain Compromise; Python documentation, pickle; picklescan, security advisories.
Where to go next
A model file reaches a machine through a server that downloads and serves it, and Model servers are web apps covers the bugs in that layer. For where model downloads sit among the traffic of a local inference box, read Planning memory for inference on one DGX Spark.