/

CVE-2026-31221

Severity:

HIGH RISK

(7.8)

Unsafe Deserialization in PyTorch Lightning

CVE-2026-31221

Modified

Vulnerability type

Category:

Unsafe Deserialization

CWE:

CWE-502

Impact:

Arbitrary Code Execution

Severity:

HIGH RISK

(7.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

PyTorch Lightning up to version 2.6.0 contains an unsafe deserialization flaw in its checkpoint loading mechanism, where LightningModule.load_from_checkpoint() calls torch.load() without restricting deserialization to weights only.

If an attacker can supply a malicious checkpoint file, this behavior can deserialize arbitrary Python objects via Pickle and may result in arbitrary code execution when the checkpoint is loaded.

Affected context

This vulnerability arises in environments that use PyTorch Lightning's checkpoint loading APIs, especially LightningModule.load_from_checkpoint() for restoring training state or deploying models.

It is most relevant when:

  • Checkpoints come from sources that are not strictly controlled or validated
  • Teams share model files across machines, services, or collaborators
  • Automated pipelines load checkpoints without human review

The risk increases in multi-tenant, research, or ML-ops setups where model artifacts may be exchanged or downloaded dynamically.

Exploit preconditions

  • Attack surface: Any code path that calls LightningModule.load_from_checkpoint() or indirectly invokes torch.load() on untrusted checkpoint files
  • Required access: Ability to cause the application to read a crafted checkpoint (e.g., upload, share, or place a file where the process will load it)
  • Trigger point: Execution of model loading logic that deserializes the attacker-supplied checkpoint
  • Why exploitation is possible: The underlying torch.load() call uses Pickle-style deserialization without weights_only=True or equivalent constraints, allowing arbitrary object construction and associated code execution during loading

Why this happens

The core issue is an unsafe deserialization pattern: treating serialized model checkpoints as inherently trustworthy and deserializing them using a general-purpose, code-capable mechanism.

PyTorch Lightning's LightningModule.load_from_checkpoint() delegates to torch.load() without enforcing a safe mode such as weights_only=True.

This maps directly to CWE-502: Deserialization of Untrusted Data, where user-controllable serialized input is passed to a deserializer that can instantiate arbitrary Python objects, some of which can execute code when constructed or restored.

Potential impact

  • Execution of arbitrary Python code with the privileges of the process loading the checkpoint
  • Compromise of the ML training or inference environment (e.g., exfiltrating data, credentials, or model IP)
  • Modification or backdooring of models for data poisoning or model behavior sabotage
  • Use of compromised systems as a foothold for lateral movement within the network

General mitigation guidance

Effective mitigation focuses on limiting trust in serialized artifacts and constraining deserialization behavior:

  • Prefer safer checkpoint handling modes (e.g., options that load only tensor weights and not arbitrary objects) where available
  • Treat checkpoints and model artifacts as untrusted input; only load files from authenticated, integrity-verified, and access-controlled sources
  • Introduce a validation or staging step for model artifacts (e.g., out-of-band scanning, signature verification, or checksum validation) before loading them into production workloads
  • Run training and inference workloads with the principle of least privilege so that, even if deserialization is abused, system-level impact is reduced
  • Separate model loading components from highly sensitive resources (e.g., isolate in containers or dedicated service accounts) to limit blast radius

How teams detect issues like this

Organizations can look for this issue through both preventive and detective controls:

  • Static analysis / code scanning:
  • Flag calls to torch.load() or LightningModule.load_from_checkpoint() where the source of the checkpoint is not clearly trusted or validated
  • Identify deserialization of files or streams that originate from user input, network locations, or shared storage
  • Code review signals:
  • Model loading utilities that accept arbitrary file paths, URLs, or user-uploaded artifacts
  • Pipelines that automatically fetch and load checkpoints from external registries, experiment tracking systems, or object storage without integrity checks
  • Policy and tooling:
  • Enforce internal guidelines that serialized artifacts must be signed or verified before loading
  • Use automated review tools or CI checks to highlight unsafe deserialization patterns and require explicit justification or safer alternatives

Sources

SourceLink
github.comgithub.com/Lightning-AI/pytorch-lightning
notion.sonotion.so/CVE-2026-31221-35d1e1393188815f8db7c4fd08076639

Frequently asked

questions

It can be, if an attacker can get a malicious checkpoint file into a location that the application or user will load—such as via file upload features, shared storage, or convincing someone to open an attacker-supplied model file.

The attacker needs the ability to influence which checkpoint file is loaded or to provide a crafted checkpoint that the victim's code will deserialize. Direct shell access is not required; control over the model artifact is sufficient.

Use deserialization modes that limit loading to weights or simple data structures only, and restrict the use of generic Pickle-based loading for untrusted files. Combine this with strict trust and integrity controls for all checkpoint sources.

Establish a policy that all serialized artifacts are treated as untrusted input by default, avoid general-purpose deserializers for attacker-controlled data, and incorporate secure patterns into ML frameworks and internal libraries. Integrate security review and automated checks into ML pipeline and code changes that introduce or modify deserialization logic.

Find out what's already
exploitable in your codebase.