Summary
A flaw in picklescan prior to version 0.0.28 fails to flag malicious pickle payloads that leverage the torch.utils.collect_env.run function inside custom reduce methods.
Under these conditions, attackers can embed undetected code into pickle files that executes remote commands when victims load the files in Python environments.
Affected context
This vulnerability appears in environments that rely on picklescan (before 0.0.28) to vet Python pickle files for malicious content.
It is most relevant in workflows where untrusted or semi-trusted pickle artifacts (models, datasets, cached states) are scanned prior to being loaded, particularly in machine learning pipelines using PyTorch.
The issue manifests when attackers craft pickle payloads that use the torch.utils.collect_env.run function in __reduce__ methods, bypassing picklescan's detection logic and remaining indistinguishable from benign files to the scanner.
Exploit preconditions
- Attack surface: Python applications that load pickle files, especially ML workflows using PyTorch and relying on picklescan for safety checks.
- Required access: Ability to supply or influence pickle files consumed by victims (e.g., model sharing platforms, artifact repositories, shared notebooks).
- Trigger point: Deserialization of the malicious pickle using
pickle.loador equivalent APIs in a Python process where the vulnerable detection logic was previously assumed to validate the file. - Why exploitation is possible: picklescan's analysis does not correctly interpret or flag the use of
torch.utils.collect_env.runin custom reduce methods, allowing attacker-controlled code paths to execute during unpickling.
Why this happens
The underlying issue is a variant of CWE-502: Unsafe Deserialization, compounded by incomplete detection coverage in a security scanning tool.
Pickle is inherently capable of reconstructing arbitrary Python objects and can execute attacker-influenced behavior during reconstruction (e.g., via __reduce__).
picklescan's logic failed to treat the torch.utils.collect_env.run invocation inside reduce methods as a potential execution sink, leaving a gap where malicious payloads could be crafted to run remote commands during deserialization while evading the scanner's heuristics.
Potential impact
- Remote command execution on systems that load attacker-crafted pickle files
- Compromise of development or production ML environments
- Theft or corruption of sensitive data, configuration, or model parameters
- Establishment of backdoors or persistence mechanisms via poisoned artifacts
- Lateral movement from compromised ML tooling into adjacent infrastructure
General mitigation guidance
Mitigation typically centers on reducing trust in pickle-based artifacts and constraining deserialization behavior.
Pattern-based strategies include:
- Avoiding pickle for untrusted or externally sourced artifacts, favoring safer interchange formats (e.g., JSON, protocol buffers, or domain-specific export formats).
- Enforcing strict separation between trusted and untrusted data sources, and treating all external pickle inputs as hostile unless independently verified.
- Running deserialization routines within sandboxed, least-privilege environments that limit potential damage from code execution (e.g., containers with restricted permissions and network egress controls).
- Applying whitelisting of allowed object types or controlled loaders where possible, rather than generic pickle deserialization.
- Incorporating defense-in-depth checks that look for deserialization sinks and reduce-method invocations, rather than relying on a single scanning tool.
How teams detect issues like this
Effective detection generally combines static and dynamic review of deserialization logic:
- Static analysis rules targeting use of
pickle.load,pickle.loads, and custom__reduce__/__setstate__implementations, especially when combined with external command execution or environment inspection utilities such astorch.utils.collect_env.run. - Code review practices that flag any deserialization of untrusted data, with reviewers explicitly checking for hidden execution paths in object reconstruction methods.
- Policy enforcement that prohibits generic pickle deserialization in high-risk contexts (e.g., CI pipelines, artifact ingestion services) or requires explicit justification and threat modeling.
- Supplemental automated tools capable of scanning artifacts and source code for known unsafe deserialization patterns, used as an adjunct to human review rather than a sole line of defense.

