Summary
CosyVoice's model averaging tool insecurely deserializes PyTorch checkpoint files using torch.load() without enabling the weights_only=True safeguard, allowing arbitrary Python object loading via pickle.
If an attacker can place or replace checkpoint files in a directory that a user later passes to the averaging script, arbitrary code may execute on the user's system during model averaging.
Affected context
This vulnerability appears in CosyVoice installations that rely on the average_model.py script to combine multiple model checkpoints (e.g., epoch_*.pt) into an averaged model.
It is most relevant when checkpoints are sourced from shared directories, downloaded from external sources, or otherwise not strictly controlled.
The risk materializes when users run the averaging tool on directories containing attacker-controlled or unverified PyTorch checkpoint files.
Exploit preconditions
- Attack surface: Local execution environment where CosyVoice's
average_model.pyis run. - Required access: Ability to introduce or modify PyTorch checkpoint files (e.g.,
epoch_*.pt) in a directory that a victim will later use as input to the averaging script. - Trigger point: Invocation of the model averaging tool, which iterates over and deserializes checkpoint files using
torch.load()withoutweights_only=True. - Why exploitation is possible:
torch.load()delegates to Python's pickle machinery, and without restricting deserialization to weights only, arbitrary object graphs from untrusted files can be instantiated, enabling execution of attacker-defined code during loading.
Why this happens
The core issue is the use of a general-purpose, pickle-based deserialization API (torch.load) on data that may not be fully trusted, without constraining what can be deserialized.
This maps directly to CWE-502: Deserialization of Untrusted Data, where serialized input is treated as benign state but is actually interpreted as executable object construction instructions.
In this case, the absence of the weights_only=True parameter allows full object deserialization, so attacker-supplied checkpoint files can embed custom objects whose special methods execute code when instantiated during load.
Potential impact
- Arbitrary code execution in the context of the user running the averaging script.
- Installation of backdoors or persistence mechanisms on the victim's machine.
- Unauthorized access to local files, credentials, or environment secrets.
- Tampering with or poisoning model parameters and outputs, potentially undermining downstream ML workflows.
- Use of the compromised environment as a stepping stone for broader lateral movement.
General mitigation guidance
Effective mitigation centers on constraining deserialization and reducing trust in external artifacts:
- Prefer deserialization modes that restrict loaded content to plain tensors or weights, such as enabling security-focused options (e.g.,
weights_only=Trueintorch.load) when available. - Treat model checkpoints as untrusted input; validate provenance and integrity (e.g., signatures, checksums) before loading them.
- Separate code and data concerns: design serialization formats that store only numeric weights or structured metadata, avoiding arbitrary object graphs.
- Run tooling that processes external checkpoints with least-privilege accounts and in isolated environments (e.g., containers or sandboxes) to limit blast radius.
- Establish policies that discourage automatic or scripted loading of artifacts from uncurated directories or user-uploaded locations.
How teams detect issues like this
Detection typically involves a mix of code analysis and process monitoring:
- Static analysis: Search for uses of
torch.load()(or similar pickle-backed APIs) that operate on variable or user-specified paths, especially where security options likeweights_only=Trueare omitted. - Code review: Flag patterns where serialized artifacts come from shared, external, or user-provided locations and are loaded without validation or isolation.
- Policy enforcement: Introduce linting or CI checks that require explicit security parameters for deserialization functions and discourage direct pickle usage.
- Runtime monitoring: On high-value systems, monitor for unexpected process spawns or file modifications initiated while loading checkpoints, indicative of malicious payloads being executed.

