/

CVE-2026-31219

Severity:

HIGH RISK

(8.8)

Unsafe Deserialization in Optimate neural_magic_training.py

CVE-2026-31219

Deferred

Vulnerability type

Category:

Unsafe Deserialization

CWE:

CWE-502

Impact:

Remote Code Execution via Malicious Model Files

Severity:

HIGH RISK

(8.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

The _load_model() function in Optimate's neural_magic_training.py script performs insecure deserialization of user-supplied model files using torch.load() without enabling the weights_only=True safety parameter.

An attacker who can supply a crafted .pt/.pth model file via the --model argument may trigger arbitrary Python object deserialization and execute code on the victim's system during model loading.

Affected context

This vulnerability appears in environments where Optimate's neural_magic_training.py script is used to load single model files specified through the --model command-line option.

It is most relevant in scenarios where:

  • Model artifacts (.pt, .pth) are obtained from untrusted, semi-trusted, or user-contributed sources
  • The training or evaluation pipeline automatically consumes externally provided model files
  • The script is executed on developer workstations, CI pipelines, or shared compute infrastructure without additional sandboxing

Under these conditions, maliciously crafted model files can traverse the trust boundary between external content and internal execution.

Exploit preconditions

  • Attack surface: Model loading interface exposed via the --model command-line argument in neural_magic_training.py.
  • Required access: Ability to provide or influence the model file selected by the victim (e.g., uploading a file, controlling a download location, or convincing a user to use a provided artifact).
  • Trigger point: Execution of the script with a single model file path passed to --model, leading to torch.load() being invoked on that file.
  • Why exploitation is possible: torch.load() defaults to full Pickle-based deserialization, which reconstructs arbitrary Python objects.

Without weights_only=True or an equivalent restriction, the loader trusts the serialized content and executes object construction logic embedded in the Pickle stream.

Why this happens

The core issue aligns with CWE-502: Deserialization of Untrusted Data.

The _load_model() function uses torch.load() directly on externally supplied model files without applying available safety controls.

By not enabling weights_only=True, the loader invokes Python's Pickle mechanisms to reconstruct arbitrary objects rather than limiting itself to raw tensor weights.

This pattern treats untrusted model artifacts as trusted executable state, allowing attacker-controlled Pickle opcodes and object constructors to run during deserialization.

In abstract terms, the design conflates data (model weights) with code (Python objects), crossing a critical trust boundary without validation or confinement.

Potential impact

  • Execution of arbitrary Python code on the machine running neural_magic_training.py
  • Compromise of developer workstations, CI agents, or shared training servers
  • Installation of persistent backdoors, trojans, or credential stealers on the victim system
  • Exfiltration or tampering of training data, model weights, and other sensitive assets
  • Use of compromised infrastructure for lateral movement within a broader network

General mitigation guidance

Effective mitigation focuses on treating model files as untrusted inputs and constraining what deserialization is permitted to do.

Common mitigation patterns include:

  • Restrict deserialization behavior: Configure torch.load() to operate in a restricted mode, such as enabling weights_only=True or an equivalent option that interprets the file as data-only tensors rather than arbitrary Python objects.
  • Enforce strict artifact provenance: Limit model loading to artifacts produced and signed by trusted pipelines, and avoid loading ad-hoc or user-supplied models without additional verification.
  • Isolate untrusted model evaluation: When untrusted models must be processed, run deserialization and subsequent processing inside a sandboxed environment (e.g., containers, VMs, or restricted users) with minimal privileges and filesystem access.
  • Reduce runtime privileges: Execute training and evaluation scripts under least-privilege accounts, avoiding unnecessary access to secrets, system utilities, or sensitive directories.
  • Add explicit validation and checks: Introduce validation layers that verify expected model structure, metadata, and format before deserialization, rejecting unexpected or malformed artifacts.

How teams detect issues like this

Detection typically combines code-centric and process-centric approaches:

  • Static analysis and code review:
  • Search for uses of torch.load(), pickle.load(), or similar APIs where inputs originate from files, network streams, or user-controlled paths.
  • Flag invocations of torch.load() that omit safety parameters (such as weights_only=True) when loading external artifacts.
  • Review scripts that take file paths via command-line arguments or configuration and directly pass them into deserialization routines.
  • Policy and pipeline enforcement:
  • Add CI checks or pre-commit hooks that prevent merging new usages of unsafe deserialization APIs without justification.
  • Require security reviews for any component that ingests external model artifacts or serialized states.
  • Runtime monitoring:
  • Monitor execution environments running training scripts for unusual subprocess creation, network connections, or filesystem activity during model loading.
  • Use endpoint security or EDR tools to detect suspicious behavior patterns triggered shortly after deserialization operations.

Sources

SourceLink
github.comgithub.com/nebuly-ai/optimate
notion.sonotion.so/CVE-2026-31219-35d1e139318881c8a629ec19671e74c3

Frequently asked

questions

Exploitability depends on who controls the model files.

If attackers can influence or provide the .pt/.pth files used by the script (e.g., via shared repositories, uploads, or compromised artifact stores), they can potentially achieve arbitrary code execution when the file is loaded.

The attacker must be able to supply or modify the model file path consumed by the --model argument.

This could be through direct file access, control over a download source, or social engineering that convinces a user to run the script with a malicious model artifact.

The safest pattern is to treat serialized models as untrusted data and constrain deserialization:

use restricted loading modes like weights_only=True (or equivalents) and ensure that only expected data structures (e.g., tensors, state dictionaries) are reconstructed, not arbitrary Python objects.

Establish guidelines that ban or tightly control generic deserialization of untrusted inputs, especially in ML tooling.

Prefer data-only serialization formats, enforce provenance checks on artifacts, and integrate static analysis rules that flag unsafe deserialization patterns during development and code review.

Find out what's already
exploitable in your codebase.