Summary
The torch-checkpoint-shrink.py utility in the ml-engineering project unsafely deserializes PyTorch checkpoint (.pt) files using torch.load() without the security-focused weights_only=True option.
When a user processes an untrusted or tampered checkpoint, a malicious file can trigger arbitrary Python object loading via pickle, potentially leading to code execution under the user's account.
Affected context
This vulnerability affects environments where the ml-engineering torch-checkpoint-shrink.py script is used to manipulate or shrink PyTorch checkpoint files.
It becomes relevant when:
- Checkpoints originate from external, shared, or otherwise untrusted sources (e.g., downloads, artifact exchanges, CI artifacts).
- Users execute the script on their local machine, development workstation, or server without isolating the process.
- The runtime Python environment has access to sensitive files, credentials, or network resources that could be abused by arbitrary code running during deserialization.
Exploit preconditions
- Attack surface: The script's checkpoint-loading interface (torch.load on .pt files).
- Required access: Ability to supply or influence the checkpoint file processed by torch-checkpoint-shrink.py (e.g., via shared storage, download links, or repository contributions).
- Trigger point: Invocation of torch-checkpoint-shrink.py that calls torch.load() on the attacker-controlled checkpoint.
- Why exploitation is possible: torch.load() relies on Python's pickle mechanism, and without weights_only=True or equivalent restrictions, it will reconstruct arbitrary Python objects. Pickle allows constructors and special methods that can execute code at load time, enabling execution of attacker-provided logic instead of purely loading tensor weights.
Why this happens
The root cause lies in treating serialized model checkpoints as trusted data and using a generic, feature-rich deserialization mechanism (pickle via torch.load()) without restrictions.
This aligns with CWE-502: Untrusted data is deserialized into live Python objects, allowing attacker-controlled state and behavior to be instantiated. The torch-checkpoint-shrink.py script does not enforce a "weights-only" or schema-constrained loading mode and does not isolate or sandbox the deserialization process, making it possible for malicious pickle payloads to execute arbitrary code during loading.
Potential impact
- Execution of arbitrary Python code with the privileges of the user running torch-checkpoint-shrink.py.
- Access to and exfiltration of local files, including API keys, configuration files, or training data.
- Installation of backdoors or persistence mechanisms in the environment where the script runs.
- Lateral movement within the network if the process has access to internal systems or sensitive services.
- Manipulation or corruption of ML artifacts, leading to poisoned or backdoored models.
General mitigation guidance
Mitigation generally focuses on restricting deserialization behavior and limiting trust in model artifacts:
- Prefer safe or restricted loading modes (e.g., weights_only-style behavior) that deserialize only raw tensors or well-defined structures instead of arbitrary Python objects.
- Treat all externally sourced checkpoint files as untrusted input; apply verification steps, such as integrity checks, digital signatures, or trusted distribution channels, before processing.
- Refactor scripts to separate file parsing logic from execution logic so that untrusted data never directly controls object construction or code paths.
- Run model-processing utilities with least privilege, in isolated or containerized environments that limit filesystem, network, and credential access.
- Establish internal guidelines that disallow generic pickle-based loading paths for untrusted artifacts and recommend safer interchange formats or serialization patterns.
How teams detect issues like this
Organizations can detect and monitor similar unsafe deserialization patterns through multiple approaches:
- Static analysis: Configure rules to flag use of torch.load(), pickle.load(), or related APIs when invoked on potentially untrusted inputs or when safer alternatives (such as weights-only loading) are not specified.
- Code review: During peer review, look for scripts and utilities that handle external model files or checkpoints and ensure they do not use generic pickle-based deserialization without constraints.
- Policy enforcement: Introduce coding standards that require explicit justification when using general-purpose deserialization mechanisms and mandate safer defaults for ML tooling.
- Runtime monitoring: Optionally run these utilities in monitored sandboxes where unexpected child processes, file modifications, or outbound connections can be detected and investigated.

