Summary
An undocumented feature in the PLY (Python Lex-Yacc) library allows unvalidated deserialization of .pkl files via the picklefile parameter to yacc(), using pickle.load() on attacker-controlled input.
Because Python's pickle format can trigger arbitrary code execution through object reducers, a crafted pickle file passed into this parameter can lead to remote code execution and stealthy backdoor behavior.
Affected context
This vulnerability affects Python applications that use PLY 3.11 and rely on the yacc() function, particularly in environments where grammar tables may be cached or loaded from pickle files.
The risk is most pronounced when:
- The application accepts or loads
.pklparser table files from outside its trusted deployment bundle - File paths or file-like objects passed to
yacc()can be influenced by a user, plugin, configuration, or remote service - Operational assumptions treat the PLY parser cache as benign, without explicit integrity checks
Because picklefile is undocumented in official PLY documentation and the GitHub repo but present in the PyPI build, it may be active without the awareness of maintainers or reviewers.
Exploit preconditions
- Attack surface: Any code path that calls
ply.yacc.yacc()(or wrappers around it) and allows control over: - The
picklefileargument, or - The source of the
.pklfile ultimately passed topickle.load() - Required access: Ability to:
- Supply or overwrite a
.pklfile used as parser tables, or - Influence configuration, environment variables, or file paths that determine which pickle file is loaded
- Trigger point: Execution of
yacc()when it: - Is invoked with the
picklefileparameter, or - Is configured to load pre-generated parsing tables from the specified pickle file
- Why exploitation is possible:
- PLY calls
pickle.load()on the provided file without validation or restriction. - Python's pickle mechanism allows arbitrary object construction, including objects whose
__reduce__()or related methods execute attacker-controlled code during deserialization. - The parameter is undocumented, making it less likely to be scrutinized during security review and allowing it to function as a stealthy deserialization backdoor.
Why this happens
The underlying bug class is unsafe deserialization of untrusted data using Python's pickle module, mapped to CWE-502.
PLY's yacc() function exposes a picklefile parameter that:
- Accepts a
.pklfile path or file-like object - Passes this directly to
pickle.load()without: - Verifying the file's origin or integrity
- Restricting the set of allowable types
- Applying sandboxing or execution guards
This pattern treats serialized data as implicitly trustworthy code, rather than opaque data, allowing attackers to embed execution logic in the serialized stream.
The risk is amplified because the parameter is undocumented, so developers may be unaware of the deserialization behavior and fail to constrain or audit it, enabling a form of stealthy backdoor and persistence vector.
Potential impact
- Execution of arbitrary OS commands under the privileges of the Python process
- Installation of backdoors or persistence mechanisms inside the application or host environment
- Exfiltration or tampering with sensitive data processed by the application (credentials, tokens, business records)
- Lateral movement to other services or infrastructure components reachable from the compromised environment
- Subversion of application logic, including manipulation of parsing behavior or grammar processing
- Use of the application as a foothold for broader supply-chain attacks if parser caches are redistributed
General mitigation guidance
Effective mitigation focuses on eliminating or isolating unsafe deserialization paths and reducing trust in external pickle data:
- Avoid using
picklefile(or any implicit pickle-based caching) with untrusted or externally supplied files. - Prefer safer serialization formats for parser tables or configuration, such as JSON or other data-only encodings, when custom caching is required.
- Treat all pickle input as untrusted by default; only deserialize files that:
- Are shipped read-only with the application, and
- Are protected by deployment and file-system integrity controls.
- Constrain file paths used for PLY parser tables to a known, non-writable directory under application control, and avoid deriving them from user input, environment variables, or editable configuration.
- Run parser-related code with least privilege, including:
- Non-root service accounts
- Minimal file-system write permissions
- Restricted network access where feasible
- Incorporate secure-by-default coding guidance in your codebase:
- Ban or discourage direct
pickle.load()usage, especially on external inputs. - Prefer libraries and patterns that default to data-only formats for configuration and caches.
How teams detect issues like this
Detection typically combines code-level analysis with configuration and runtime review:
- Static analysis and code search:
- Search for direct uses of
ply.yacc.yacc()and inspect: - Whether
picklefileis explicitly passed - How the file path is derived
- Search for
pickle.load()in the codebase and confirm that it is never applied to data that may be influenced externally. - Code review signals:
- Undocumented parameters or keyword arguments passed into third-party APIs.
- Parser table files (
*.pkl) stored in locations writable by the application or end users. - Configuration options or environment variables that control parser cache locations.
- Policy and tooling:
- Add a secure coding policy that either forbids or strictly gates
pickleusage, particularly for network or user-accessible data. - Use automated SAST tools or custom linters to flag:
- Deserialization via
pickle.load()and similar APIs - Writes to and reads from pickle files whose path can be influenced externally
- Operational monitoring:
- Monitor for unexpected creation or modification of
.pklfiles in application directories. - Alert on anomalous process activity (e.g., shell invocation) from processes that primarily perform parsing or compilation tasks.

