Summary
The _load_model() function in the optimate project's neural_magic_training.py script performs insecure deserialization of PyTorch model state dictionaries using torch.load() without enabling weights_only=True.
An attacker who can supply a crafted state_dict.pt file via the --model directory can cause arbitrary Python object deserialization, potentially leading to remote code execution on the system running the training script.
Affected context
This vulnerability occurs in environments where the optimate neural_magic_training.py script is used to load model state dictionaries from state_dict.pt files.
The risk is present when:
- Model checkpoints are loaded from directories provided via the --model argument
- The source of the state_dict.pt file is not fully trusted or is obtained from external or shared locations
- torch.load() is invoked without enabling safety features such as weights_only=True, allowing generic Pickle deserialization
Exploit preconditions
- Attack surface: The file loading path for model state_dict.pt used by neural_magic_training.py.
- Required access: Ability to place or control a state_dict.pt file in a directory that will be referenced by the --model argument.
- Trigger point: Execution of the _load_model() function, which calls torch.load() on the untrusted state_dict.pt file.
- Why exploitation is possible: torch.load() defaults to Pickle-based deserialization of arbitrary Python objects, and without weights_only=True or similar restrictions, any embedded object constructors or deserialization hooks can be executed during loading.
Why this happens
The root cause is the use of a general-purpose deserialization mechanism for untrusted model checkpoints without restricting what is deserialized.
In this case, _load_model() relies on torch.load() with its default behavior, which leverages Python's Pickle module to reconstruct arbitrary Python objects from the state_dict.pt file.
This maps directly to CWE-502: Insecure Deserialization, where application logic deserializes data that may contain executable object payloads, without enforcing a safe subset (such as tensor-only loading) or validating the structure before instantiation.
The underlying insecure pattern is treating externally supplied model files as trusted inputs and deserializing them with full interpreter capabilities.
Potential impact
- Arbitrary code execution on the host running the optimate training script.
- Execution of malicious payloads embedded in the crafted state_dict.pt file during model loading.
- Compromise of the user account or environment executing the script, including access to local files and credentials available to that account.
- Establishment of persistence mechanisms or backdoors through code executed at deserialization time.
- Lateral movement within the environment if additional credentials or network access are available.
General mitigation guidance
Mitigation focuses on constraining deserialization behavior and tightening trust boundaries around model files:
- Prefer deserialization modes that restrict loading to tensors or simple data structures, such as using torch.load() with weights_only=True or equivalent safe-loading flags where appropriate.
- Treat model checkpoints and state_dict.pt files as untrusted by default; allow loading only from vetted, authenticated sources (e.g., signed artifacts, tightly controlled storage).
- Introduce explicit schema or structural validation of loaded data before using it, ensuring the content matches expected types and shapes rather than arbitrary objects.
- Separate privileges for model-loading processes from sensitive system resources, employing least privilege and isolation (e.g., containers, restricted users) to limit the blast radius if deserialization is abused.
- Reduce reliance on Pickle-based serialization for untrusted data, favoring formats that do not support arbitrary code execution, such as pure tensor archives or structured formats like JSON where feasible.
How teams detect issues like this
Detection can be approached by both static and operational techniques:
- Static analysis and code review:
- Search for calls to torch.load(), pickle.load(), or similar APIs used on data that may come from external or user-controlled locations.
- Flag usages that omit safety parameters such as weights_only=True or that lack prior validation of the input path and content.
- Review training and inference pipelines to identify any deserialization of files obtained from shared repositories, user uploads, or network locations.
- Policy and tooling:
- Enforce coding guidelines that prohibit generic Pickle-based deserialization for untrusted data, and add automated checks in CI to detect unsafe patterns.
- Use security-focused linters or static analysis tools that have rules for detecting insecure deserialization and unsafe PyTorch loading patterns.
- Runtime monitoring:
- Monitor model-loading workflows for unexpected outbound connections, file modifications, or subprocess creation that may indicate malicious code executed during deserialization.

