CVE-2026-1839: HuggingFace Transformers allows for arbitrary code execution in the `Trainer` class
HuggingFace Transformers' `Trainer` class loads a checkpoint's `rng_state.pth` with `torch.load()` and no `weights_only=True`. Resuming training from a malicious checkpoint can run arbitrary code when PyTorch is older than 2.6. Fixed in transformers 5.0.0rc3. Upgrade it, or upgrade PyTorch to 2.6 or later.
Published Apr 7, 2026 · Updated Apr 8, 2026 · Analysis Oct 30, 2026
What an attacker can do
Trainer._load_rng_state() in src/transformers/trainer.py (line 3059) restores random-number state from rng_state.pth when you resume from a checkpoint. It calls torch.load() without weights_only=True. torch.load uses Python pickle, and a pickle can run code while it loads. The safe_globals() context manager that the library relies on gives no protection with PyTorch below 2.6, so all versions that support torch>=2.2 are affected when paired with those PyTorch releases.
The attacker must get a victim to resume training from a checkpoint directory they control. That could be a checkpoint downloaded from a model hub, a shared drive or a colleague. The CVSS vector is local with user interaction required and high attack complexity, which fits. Code runs as the user running the training job, so it can reach their files, cloud credentials in the environment, and GPU cluster access.
The weakness is deserialization of untrusted data (CWE-502). Only the checkpoint-resume path is described; loading a model for inference does not use this method.
Who is affected
The pip package transformers before 5.0.0rc3, when used with PyTorch below 2.6, and only if you resume Trainer runs from checkpoints you did not create. Check both versions.
pip show transformers torch
python -c "import torch, transformers; print(torch.__version__, transformers.__version__)"Search your jobs for resume_from_checkpoint.
transformers
- Ecosystem
- PyPI
- Affected
- < 5.0.0rc3
- Fixed in
- 5.0.0rc3
- Check
pip show transformers
How to fix it
Upgrade to transformers 5.0.0rc3 or later, which is the patched version in the advisory (commit 03c8082).
pip install --upgrade "transformers>=5.0.0rc3"The advisory ties the exposure to PyTorch below 2.6, so upgrading PyTorch to 2.6 or later also removes the unprotected path. Until then, resume only from checkpoints you created or verified, and inspect untrusted checkpoint folders before use.
How to tell if you were hit
Check which checkpoint directories your training jobs resumed from, and whether they came from outside your team. Look for rng_state.pth files that are unusually large or that were modified after creation. Review process activity and network connections from the training host around the time a resume started. Beam records agent shell commands and file access, which helps if an agent downloaded the checkpoint or started the run.
Original advisory
From the GitHub Advisory Database (CC BY 4.0).
A vulnerability in the HuggingFace Transformers library, specifically in the Trainer class, allows for arbitrary code execution. The _load_rng_state() method in src/transformers/trainer.py at line 3059 calls torch.load() without the weights_only=True parameter. This issue affects all versions of the library supporting torch>=2.2 when used with PyTorch versions below 2.6, as the safe_globals() context manager provides no protection in these versions. An attacker can exploit this vulnerability by supplying a malicious checkpoint file, such as rng_state.pth, which can execute arbitrary code when loaded. The issue is resolved in version v5.0.0rc3.
Questions
- Is transformers affected if I use PyTorch 2.6 or newer?
The advisory ties the issue to PyTorch below 2.6, where
safe_globals()gives no protection. Upgrade transformers anyway.- Which version fixes CVE-2026-1839?
transformers 5.0.0rc3 is the patched version listed in the advisory. Earlier releases are affected.
- Does loading a model for inference trigger it?
The advisory describes the
Trainerresume path, where_load_rng_state()readsrng_state.pth. Inference loading is not described.- Is it exploited in the wild?
No source says so. The EPSS score is 0.0038.
References
Read for this analysis:
How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. The analysis is written by an AI model from the sources listed above and checked by automated rules before it is published. Check versions against the vendor's advisory before you act. Methodology
More Transformers vulnerabilities
All Transformers entriesCVE-2026-80047
Hugging Face Transformers downloads custom generation code before trust consent
high7.8No fixSep 1, 2026CVE-2026-9856
Transformers save_pretrained path traversal allows arbitrary file writes through chat template names
high7.1Aug 2, 2026CVE-2026-5241
huggingface/transformers: Arbitrary Code Execution During Model Initialization in the LightGlue Model Loading Path
high8.0Jun 3, 2026CVE-2026-4372
HuggingFace transformers vulnerable to remote code execution
high7.8May 26, 2026CVE-2025-6921
Hugging Face Transformers vulnerable to Regular Expression Denial of Service (ReDoS) in the AdamWeightDecay optimizer
medium5.3Sep 23, 2025CVE-2025-6051
Hugging Face Transformers library has Regular Expression Denial of Service
medium5.3Sep 14, 2025
