Summary
Stanza's model loading logic may fall back to unsafe deserialization, allowing arbitrary pickle execution when loading attacker-controlled .pt model or pretrain files.
If a user or system loads untrusted or tampered Stanza models, an attacker can execute arbitrary code in the Python runtime under the privileges of the calling application.
Affected context
This vulnerability occurs in Stanza NLP environments that load pretrain or model .pt files via the standard pipeline or model loader interfaces.
It is most relevant when models are sourced from shared storage, external distribution channels, or environments where files can be modified by other tenants or processes.
The issue manifests when torch.load(..., weights_only=True) raises an pickle.UnpicklingError, causing the loader to silently fall back to torch.load(..., weights_only=False) and thus deserialize arbitrary pickle content embedded in the model file.
Exploit preconditions
- Attack surface: Stanza model loading routines that call torch.load on
.ptpretrain or model files. - Required access: Ability to supply or modify the
.ptfile being loaded (e.g., write access to model directories, control over download sources, or influence over packaging/distribution). - Trigger point: Invocation of Stanza pipeline or model loader (such as
stanza.models.common.pretrain.Pretrain.load()) that attempts to load the crafted file. - Why exploitation is possible: The loader treats attacker-controlled data as a trusted pickle source when the initial safe path fails, enabling execution of Python code embedded within the serialized object graph.
Why this happens
The core bug class is unsafe deserialization of untrusted data, mapping directly to CWE-502.
Stanza's loader logic attempts a more restricted torch.load(..., weights_only=True) but, on errors such as pickle.UnpicklingError, falls back to torch.load(..., weights_only=False), which fully unpickles the content.
Because pickle is a general-purpose serialization format that can encode executable behaviors, treating model files as inherently trusted artifacts allows attackers to smuggle arbitrary code into the deserialization process.
The behavior also touches CWE-676 (Use of Potentially Dangerous Function) by relying on a dangerous deserialization primitive (torch.load with pickle) without enforcing trust boundaries or validating the provenance of the model file.
Potential impact
- Execution of arbitrary Python code in the application process during model loading.
- Complete compromise of the NLP application environment, including access to configuration, data, and credentials available to the process.
- Installation of backdoors or malicious logic in ML pipelines, potentially influencing downstream analysis, classification, or NER results.
- Facilitation of lateral movement if the compromised process has network access or integration with other internal systems.
- Tampering with machine learning workflows and datasets, enabling data poisoning or subtle integrity attacks on NLP outputs.
General mitigation guidance
Effective mitigation focuses on reducing trust in model artifacts and eliminating unsafe deserialization paths.
Organizations can prioritize:
- Using deserialization methods or formats that do not execute code by design (e.g., formats that encode tensors and parameters only, without arbitrary Python objects).
- Applying strict provenance controls for model files: only load artifacts from authenticated, integrity-checked sources; treat externally obtained
.ptfiles as untrusted. - Enforcing file system permissions so that only authorized processes can write to locations where Stanza models are loaded from.
- Running NLP workloads with least-privilege accounts and isolating them via containers or virtual environments, reducing blast radius if deserialization is abused.
- Incorporating security review of model loading code paths to avoid fallback behaviors that return to unsafe primitives when safer options fail.
How teams detect issues like this
Detection commonly combines static, dynamic, and process-level approaches:
- Static analysis: Configure rules to flag usage of generic pickle-based APIs (including
torch.loadwithout restrictive options) in model loading paths, especially where input paths or files may be influenced externally. - Code review: Examine loader logic for fallbacks from safe to unsafe deserialization modes, and for assumptions that model artifacts are inherently trusted.
- Runtime monitoring: Track and alert on unexpected model file changes, especially in shared or networked storage, and correlate these with subsequent load operations.
- Policy enforcement: Define organizational policies that disallow loading models from uncontrolled locations or without integrity verification such as checksums or signatures.
- Automated tooling: Use application security testing tools and CI hooks that scan for unsafe deserialization patterns, highlighting high-risk usage of pickle-backed mechanisms in ML libraries.

