/

CVE-2026-31214

Severity:

CRITICAL RISK

(9.8)

Unsafe Deserialization in ml-engineering

CVE-2026-31214

Deferred

Vulnerability type

Category:

Unsafe Deserialization

CWE:

CWE-502

Impact:

Remote Code Execution

Severity:

CRITICAL RISK

(9.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 torch-checkpoint-shrink.py utility in the ml-engineering project unsafely deserializes PyTorch checkpoint (.pt) files using torch.load() without the security-focused weights_only=True option.

When a user processes an untrusted or tampered checkpoint, a malicious file can trigger arbitrary Python object loading via pickle, potentially leading to code execution under the user's account.

Affected context

This vulnerability affects environments where the ml-engineering torch-checkpoint-shrink.py script is used to manipulate or shrink PyTorch checkpoint files.

It becomes relevant when:

  • Checkpoints originate from external, shared, or otherwise untrusted sources (e.g., downloads, artifact exchanges, CI artifacts).
  • Users execute the script on their local machine, development workstation, or server without isolating the process.
  • The runtime Python environment has access to sensitive files, credentials, or network resources that could be abused by arbitrary code running during deserialization.

Exploit preconditions

  • Attack surface: The script's checkpoint-loading interface (torch.load on .pt files).
  • Required access: Ability to supply or influence the checkpoint file processed by torch-checkpoint-shrink.py (e.g., via shared storage, download links, or repository contributions).
  • Trigger point: Invocation of torch-checkpoint-shrink.py that calls torch.load() on the attacker-controlled checkpoint.
  • Why exploitation is possible: torch.load() relies on Python's pickle mechanism, and without weights_only=True or equivalent restrictions, it will reconstruct arbitrary Python objects. Pickle allows constructors and special methods that can execute code at load time, enabling execution of attacker-provided logic instead of purely loading tensor weights.

Why this happens

The root cause lies in treating serialized model checkpoints as trusted data and using a generic, feature-rich deserialization mechanism (pickle via torch.load()) without restrictions.

This aligns with CWE-502: Untrusted data is deserialized into live Python objects, allowing attacker-controlled state and behavior to be instantiated. The torch-checkpoint-shrink.py script does not enforce a "weights-only" or schema-constrained loading mode and does not isolate or sandbox the deserialization process, making it possible for malicious pickle payloads to execute arbitrary code during loading.

Potential impact

  • Execution of arbitrary Python code with the privileges of the user running torch-checkpoint-shrink.py.
  • Access to and exfiltration of local files, including API keys, configuration files, or training data.
  • Installation of backdoors or persistence mechanisms in the environment where the script runs.
  • Lateral movement within the network if the process has access to internal systems or sensitive services.
  • Manipulation or corruption of ML artifacts, leading to poisoned or backdoored models.

General mitigation guidance

Mitigation generally focuses on restricting deserialization behavior and limiting trust in model artifacts:

  • Prefer safe or restricted loading modes (e.g., weights_only-style behavior) that deserialize only raw tensors or well-defined structures instead of arbitrary Python objects.
  • Treat all externally sourced checkpoint files as untrusted input; apply verification steps, such as integrity checks, digital signatures, or trusted distribution channels, before processing.
  • Refactor scripts to separate file parsing logic from execution logic so that untrusted data never directly controls object construction or code paths.
  • Run model-processing utilities with least privilege, in isolated or containerized environments that limit filesystem, network, and credential access.
  • Establish internal guidelines that disallow generic pickle-based loading paths for untrusted artifacts and recommend safer interchange formats or serialization patterns.

How teams detect issues like this

Organizations can detect and monitor similar unsafe deserialization patterns through multiple approaches:

  • Static analysis: Configure rules to flag use of torch.load(), pickle.load(), or related APIs when invoked on potentially untrusted inputs or when safer alternatives (such as weights-only loading) are not specified.
  • Code review: During peer review, look for scripts and utilities that handle external model files or checkpoints and ensure they do not use generic pickle-based deserialization without constraints.
  • Policy enforcement: Introduce coding standards that require explicit justification when using general-purpose deserialization mechanisms and mandate safer defaults for ML tooling.
  • Runtime monitoring: Optionally run these utilities in monitored sandboxes where unexpected child processes, file modifications, or outbound connections can be detected and investigated.

Sources

SourceLink
github.comgithub.com/stas00/ml-engineering/blob/master/training/checkpoints/torch-checkpoint-shrink.py#L57
notion.sonotion.so/CVE-2026-31214-35d1e1393188813fa40eef73c174cee5

Frequently asked

questions

It can be, if an attacker can deliver a malicious checkpoint file that a user then processes with the vulnerable script—for example, via a download link, shared storage, or a contributed artifact in a collaborative project. The actual execution still occurs when a user runs torch-checkpoint-shrink.py on that file.

The attacker needs the ability to influence or provide the .pt file that the script loads. They do not need shell access or direct code execution rights; they only need a path to get their checkpoint processed by a victim running the script.

The safest fix is to stop using generic, pickle-based deserialization for untrusted data and instead adopt restricted, schema-driven, or weights-only loading mechanisms. Code should ensure that only expected data structures (such as tensors and simple metadata) are loaded, not arbitrary Python objects.

Prevention focuses on design and process: treat all serialized artifacts as untrusted, avoid generic deserialization on external inputs, prefer safer serialization formats (JSON, protobuf, or restricted tensor formats), and enforce secure coding patterns via static analysis, code review, and internal guidelines for ML tooling.

Find out what's already
exploitable in your codebase.