DEV Community

Arsenii
Arsenii

Posted on

Stop trusting torch.load(): A complete guide to AI Supply Chain Security (Malware, Licenses, and Signing)

We all know the drill: find a cool model on Hugging Face, download the weights, and run model.load_state_dict(torch.load('weights.bin')).

But here is the scary part: Pickle is not a data format. It is a Virtual Machine.

When you load a pickle file (and PyTorch uses pickle under the hood), you are essentially executing a program. A malicious actor can inject a payload that executes os.system("rm -rf /") or steals your AWS credentials the moment you load the model.

The Problem: Regex is not enough

Many security scripts just grep for import os. But hackers are smarter. They use obfuscation like getattr(__import__('o'+'s'), 'sys'+'tem').

But malware isn't the only risk. What if the file was corrupted or tampered with in transit? What if you accidentally deploy a model with a "Non-Commercial" license into your paid product?

To solve all three problems, I built open-source tool Veritensor. Here is how to secure your pipeline in 5 minutes.

1. Install
It's a lightweight CLI tool written in Python. It doesn't require heavy ML libraries like PyTorch or TensorFlow to run.

pip install veritensor
Enter fullscreen mode Exit fullscreen mode

2. Detect Malware (Static Analysis)
Standard antiviruses don't understand Pickle bytecode. Many simple security scripts just grep for import os, which is easily bypassed by obfuscation.

Veritensor implements a Stack Emulator that traces the opcodes to reconstruct the execution flow without actually running the code.

Scan a local file:

veritensor scan ./models/bert-base.pt
Enter fullscreen mode Exit fullscreen mode

Output example:

╭─────────────────────────────────────╮
│🛡️Veritensor Security Scanner v1.2.2 │
╰─────────────────────────────────────╯
                                  Scan Results
┏━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ File         ┃ Status ┃ Threats / Details                      ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ model.pt     │  FAIL  │ CRITICAL: os.system (RCE Detected)     │
└──────────────┴────────┴────────────────────────────────────────┘
❌ BLOCKING DEPLOYMENT
Enter fullscreen mode Exit fullscreen mode

(It catches obfuscated payloads like STACK_GLOBAL assembly).

3. Verify Integrity (The "Identity Check")
Even if the file has no virus, how do you know it's the exact file released by Meta or Google?

Veritensor calculates the SHA256 of your local file and queries the Hugging Face Hub API to ensure it matches the official upstream version bit-for-bit.

# Tell Veritensor where this file supposedly comes from
veritensor scan ./pytorch_model.bin --repo meta-llama/Llama-2-7b
Enter fullscreen mode Exit fullscreen mode

If the hash doesn't match, Veritensor blocks the deployment. This protects you from Man-in-the-Middle attacks, corrupted downloads, or "typosquatting" models.

4. The License Firewall
Legal risks can be just as damaging as security risks. You don't want to accidentally use a CC-BY-NC (Non-Commercial) model in a proprietary product.
Veritensor parses metadata headers from safetensors and GGUF files. If it detects a restrictive license, it flags it.

veritensor scan ./model.safetensors
Enter fullscreen mode Exit fullscreen mode

Output:
HIGH: Restricted license detected: 'cc-by-nc-4.0'
❌ BLOCKING DEPLOYMENT

Note: You can whitelist specific models in veritensor.yaml if you have permission to use them.

5. Sign your Container (Supply Chain Trust)
Once a model passes all checks (Malware, Identity, License), you want to ensure it isn't tampered with after the scan.

Veritensor integrates with Sigstore Cosign to cryptographically sign your Docker image.

Generate keys:

veritensor keygen
# Output: veritensor.key (Private) and veritensor.pub (Public)
Enter fullscreen mode Exit fullscreen mode

Scan & Sign:

export VERITENSOR_PRIVATE_KEY_PATH=veritensor.key

veritensor scan ./models/my_model.pkl --image my-org/my-app:v1.0.0
Enter fullscreen mode Exit fullscreen mode

If the scan passes, Veritensor signs the image and pushes the signature to your OCI registry. Your Kubernetes cluster can then verify this signature before starting the pod.

Automate in GitHub Actions

You shouldn't do this manually. Add this to your CI pipeline to block unsafe models automatically:

- name: Scan AI Models
  uses: ArseniiBrazhnyk/Veritensor@v1.2.2
  with:
    path: './models'
    repo: 'meta-llama/Llama-2-7b'
    fail_on_severity: 'CRITICAL'
Enter fullscreen mode Exit fullscreen mode

Conclusion
Security shouldn't be an afterthought in AI. The supply chain is the new attack vector.

Veritensor is fully Open Source (Apache 2.0).

Let me know what you think!

Top comments (0)