Pickle model file and safetensors checks
Brief
A PyTorch checkpoint or any pickle-based model file can name functions that run the moment a service loads it, so whoever can replace a model file can run code inside the service. The review reads each file's calls without loading it, checks how the service loads models, and compares each file with the hash recorded when it was published.
On this page
- Check first
- Which model files are pickles?
- Which functions would each pickle call on load?
- What does the extra call do?
- Does the service load files with full unpickling?
- Do the files match the hashes recorded when they were published?
- What does a safetensors file hold instead?
- The defender sees
- Done when
- Not covered
- Tools
- Related research
- Reviewed
- Situation
- Read access to the model store or a copy of its files (a registry, a bucket, the hub repositories the team pulls from), and the code of the service that loads them, granted for the review. No model file is loaded or run at any step; each is read as bytes.
- Goal
- A list of model files with their format, the functions each pickle would call on load, whether each matches its recorded hash, and how the service loads them.
- Outputs
- Captured on 6 Nov 2026 in the Atarus Bench review lab, edition
review-lab-0.2: Python 3.13 on Debian 13, against model files a seed script writes byte for byte. PyTorch is not installed: stand-in modules let the script write the same pickle opcodes a PyTorch state dict carries, the injected call is toprintwith a lab string, and no file was ever loaded.
Check first
- Every model file the service can load, and where each comes from: a hub repository, an internal registry, a bucket, a build artifact.
- Who can write to each of those places, since writing a model file is the access this page is about.
- The service's code and its dependency pins, so each loader call can be matched to a library version.
- Whether the team recorded a hash or a hub commit for each file when it was published.
- The client's agreement that files are read as bytes only and nothing is loaded.
Steps
Which model files are pickles?
for f in models/*; do
printf '%-32s %s\n' "$f" "$(head -c 4 "$f" | od -An -tx1 | tr -d ' ')"
done
python3 -m zipfile -l models/sentiment-v3-ft.pt
Expected output
models/sentiment-v3-ft.pt 504b0304 # <- PK: a zip archive
models/sentiment-v3.pt 504b0304
models/sentiment-v3.safetensors b0000000 # <- 0xb0 = 176, little-endian: the JSON header's length
File Name Modified Size
sentiment-v3-ft/data.pkl 2026-10-20 09:00:00 352 # <- the pickle that runs on load
sentiment-v3-ft/byteorder 2026-10-20 09:00:00 6
sentiment-v3-ft/data/0 2026-10-20 09:00:00 32
sentiment-v3-ft/data/1 2026-10-20 09:00:00 8
sentiment-v3-ft/version 2026-10-20 09:00:00 2
The first four bytes name the format. A PyTorch checkpoint (.pt, .pth, .bin) is a zip archive, and the zip holds data.pkl, a Python pickle that rebuilds the model's weights, beside the raw weight data. A plain pickle (.pkl, .joblib) starts with 80 and the protocol number. The safetensors file starts with the length of its header, here 176 bytes of JSON, and holds no code at all. Every zip with a data.pkl and every plain pickle goes on to the next step. On a real store, loop over find models -type f so no extension is missed.
Negative result. Every file is safetensors or GGUF, with no pickle inside. Keras files and NumPy arrays saved with object data can also run code on load, so check those with their own loaders' safe modes.
Fix. Convert each checkpoint to safetensors where the framework supports it (below), and refuse pickle-based files at the model store.
Which functions would each pickle call on load?
for f in models/sentiment-v3.pt models/sentiment-v3-ft.pt; do
echo "$f:"
unzip -p "$f" '*/data.pkl' > /tmp/data.pkl
python3 -m pickletools /tmp/data.pkl | grep -oE "GLOBAL +'[^']+'" | sed -E 's/GLOBAL +/ /' | sort -u
done
Expected output
models/sentiment-v3.pt:
'collections OrderedDict'
'torch FloatStorage'
'torch._utils _rebuild_tensor_v2' # <- the three names a plain state dict needs
models/sentiment-v3-ft.pt:
'__builtin__ print' # <- a fourth name, outside what weights need
'collections OrderedDict'
'torch FloatStorage'
'torch._utils _rebuild_tensor_v2'
A pickle is a small program: its GLOBAL opcodes name the functions and classes the loader imports and calls. pickletools disassembles it without running anything. A PyTorch state dict needs a short, known list of names: an ordered dict, a storage type and the function that rebuilds a tensor. The fine-tuned file names one more, print from the builtins, which Python writes as __builtin__ at protocol 2. In a real tampered file the extra name is usually os.system (which a pickle names posix system on Linux and nt system on Windows), builtins.exec, eval, subprocess.Popen or runpy. Any name outside the list that weights_only=True allows is a finding, whatever it is. A whole saved model, rather than a state dict, names its own classes too; report that it needs full unpickling to load.
Negative result. Every pickle names only the functions its framework uses to rebuild weights. That holds for protocol 2 and 3 pickles, which use GLOBAL. In protocol 4 and 5 pickles (most .pkl and .joblib files) the names sit in strings before a STACK_GLOBAL opcode, which this grep misses, so run fickling or a model scanner on those.
Why this works
Loading a pickle runs it. REDUCE calls whatever the pickle named, with arguments the pickle supplies, before the loader returns anything to the code that asked for it.
Fix. Treat any extra name as a compromise of the file: replace it from a trusted copy and find who wrote it.
What does the extra call do?
unzip -p models/sentiment-v3-ft.pt '*/data.pkl' > /tmp/data.pkl
python3 -m pickletools /tmp/data.pkl | sed -n '8,16p'
Expected output
34: X BINUNICODE '_lab_onload'
50: q BINPUT 2
52: c GLOBAL '__builtin__ print'
71: q BINPUT 3
73: X BINUNICODE 'lab: this line runs when the file is loaded' # <- the argument
121: q BINPUT 4
123: \x85 TUPLE1
124: q BINPUT 5
126: R REDUCE # <- the call
The disassembly reads in order: a key named _lab_onload, the function print, its one argument, a tuple, and REDUCE, which makes the call. The call sits in the state dict before the first weight, so it runs before the service sees a single weight. Here it prints a lab line; the same four opcodes with os.system and a command string give the file's author a shell inside the service. The report quotes these lines, never runs them. On another file, find the extra name with grep -n in the full disassembly and read the lines that follow it.
Negative result. None: this step explains the extra name the previous step found.
Does the service load files with full unpickling?
grep -rnE 'torch\.(hub\.)?load|pickle\.loads?\(|joblib\.load|dill\.load|cloudpickle|read_pickle|allow_pickle=True|from_pretrained' ml-service
grep -E '^torch==' ml-service/requirements.txt
Expected output
ml-service/serve.py:15: state = torch.load(MODEL_PATH, map_location="cpu") # <- no weights_only
torch==2.3.1 # <- before 2.6, torch.load runs the whole pickle
The search covers the usual loaders: torch.load and torch.hub.load, pickle, joblib, dill and cloudpickle, pandas read_pickle, NumPy with allow_pickle=True, and from_pretrained, which falls back to a pickle checkpoint when a repository has no safetensors file. The service calls torch.load without weights_only, on PyTorch 2.3.1. Before 2.6, torch.load unpickles everything by default, so the fine-tuned file's call would run. From 2.6 the default is weights_only=True, which allows only the names weights need and refuses the rest with an error. pickle.load and joblib.load have no such mode. The finding is the pair: a file that names an extra function, and a loader that would call it.
Negative result. Every torch.load runs on PyTorch 2.6 or later and none sets weights_only=False, and no code calls pickle.load or joblib.load on a model file.
Fix. Upgrade PyTorch to 2.6 or later. Before 2.6, weights_only=True could be bypassed (CVE-2025-32434), so passing it on 2.3.1 is not enough. Replace pickle and joblib model files with safetensors, or with the framework's own format that holds no code.
Do the files match the hashes recorded when they were published?
cd models && sha256sum -c ../release-record/models.sha256 2>&1
Expected output
sentiment-v3-ft.pt: FAILED # <- the file changed after its hash was recorded
sentiment-v3.pt: OK
sentiment-v3.safetensors: OK
sha256sum: WARNING: 1 computed checksum did NOT match
The team recorded a hash for each file at release, in a record kept outside the model store, so whoever can write a model file cannot rewrite its hash. The fine-tuned checkpoint changed after its hash was recorded; with the extra call, treat it as replaced. Where the team pulls from a hub, the record to compare is the commit or revision the code pins; a model pulled by name alone takes whatever the repository holds that day. The report names the file, its recorded and current hashes, and who can write to the store, from the access list in Check first.
Negative result. Every file matches its recorded hash, and the code pins each hub model to a revision.
Fix. Record a hash or hub revision for every model at release, where the store's writers cannot edit it, check it in the loader before loading, and pin hub downloads with revision= set to a commit, not a branch.
What does a safetensors file hold instead?
python3 -c 'import json, struct, sys
f = open(sys.argv[1], "rb")
n = struct.unpack("<Q", f.read(8))[0]
for name, entry in json.loads(f.read(n)).items():
print(name, entry)' models/sentiment-v3.safetensors
Expected output
__metadata__ {'format': 'pt'}
classifier.weight {'dtype': 'F32', 'shape': [2, 4], 'data_offsets': [0, 32]} # <- a name, a type, a shape and where its bytes sit
classifier.bias {'dtype': 'F32', 'shape': [2], 'data_offsets': [32, 40]}
A safetensors file is a JSON header and raw numbers: each tensor has a name, a type, a shape and the byte range of its data, and nothing in the format can name a function. Loading it reads bytes into arrays. That is why the fix for every pickle finding on this page ends here.
Negative result. None: this step shows the fixed form.
Fix. Save and publish models with safetensors (save_file in the safetensors library, or safe_serialization=True in Hugging Face Transformers) and load them with load_file.
The defender sees
Nothing from the review: every step reads files the team handed over. The finding is what the defender should have seen: a write to the model store that replaced a published file. On a bucket, that write is an object write, logged only where data event logging is on (S3 data events, Azure storage logs, Google Cloud Data Access logs). On a hub, it is a commit to the model repository. A hash check in the loader turns the same change into a refused load and an alert. If neither the store's writes nor the loads are logged, that goes in the report beside the finding.
Done when
Every model file the service can load is listed with its format and its pickle calls, every hash mismatch is reported, and each loader call is recorded with its library version.
Not covered
Loading or running any model, adversarial inputs and model theft, Keras Lambda layers and NumPy object arrays (which also run code on load), ONNX and GGUF, training data poisoning, and the hub account's own security.