GHSA-rhp5-r9x4-f5g2: Pip/nltk vulnerability
Summary
The NLTK library's TransitionParser.parse() method deserializes model files using pickleload() with the default restricted=False parameter, allowing arbitrary Python code execution when loading a malicious model file. The library provides a RestrictedUnpickler class for safe deserialization, but it is never used by production code paths, leaving the vulnerability unpatched.
Root Cause
File: nltk/parse/transitionparser.py (lines 542-557)
The parse() method calls pickleload(f) without restricted=True, routing through WarningUnpickler which inherits from pickle.Unpickler and does NOT override findclass(). This allows arbitrary class/function resolution during unpickling, enabling RCE via standard pickle gadgets (e.g., os.system, subprocess.Popen).
Vulnerability chain in nltk/picklesec.py:
python def pickleload(file, , context=None, restricted=False): if restricted: return RestrictedUnpickler(file).load() # Safe: blocks all globals return WarningUnpickler(file, context=context).load() # VULNERABLE PATH
WarningUnpickler only emits a warning but does NOT block unsafe class loading — it calls super().load() which is standard pickle.Unpickler.load().
Why this is not by design: - NLTK intentionally created RestrictedUnpickler to block unsafe deserialization - The restricted=True parameter exists in the API but is never used by any production code path - All call sites use the default restricted=False: transitionparser.py:557, parse/chartparserapp.py:816, parse/chartparserapp.py:2273, parse/chartparserapp.py:2311
Attack Surface
Entry point: TransitionParser().parse(depgraphs, modelFile) receives a filesystem path with no validation.
Exploitation path: 1. Attacker places a malicious pickle file at a known or attacker-controlled location 2. Victim calls parser.parse(sentences, "/path/to/maliciousmodel.pkl") 3. pickleload() deserializes the file with restricted=False (default) 4. Standard pickle gadget chain executes arbitrary Python code with victim's privileges
Impact: Remote code execution with the privileges of the user running the NLTK-dependent application. Affects researchers, data scientists, and automated ML pipelines using NLTK for parsing tasks.
Steps to Reproduce
Environment - NLTK version: 3.8.1+ (all versions with transitionparser.py) - Python 3.6+ - No special dependencies required
Reproduction
1. Create a malicious pickle file that uses reduce to execute a system command during deserialization.
2. Call TransitionParser().parse([], '/path/to/maliciousmodel.pkl').
3. The pickleload(f) call at transitionparser.py:557 uses restricted=False by default, routing through WarningUnpickler, which does not override findclass() and permits full class resolution — executing the embedded gadget.
4. Arbitrary code executes with the victim's privileges.
Proof That the Fix Works
Changing line 557 in transitionparser.py from: python model = pickleload(f) to: python model = pickleload(f, restricted=True) causes RestrictedUnpickler to raise an UnpicklingError and block execution, confirming the safe path prevents the attack.
Working PoC
python import pickle import os from nltk.parse.transitionparser import TransitionParser
Create malicious pickle with RCE payload class Exploit: def reduce(self): return (os.system, ('touch /tmp/nltkpoctriggered',))
with open('/tmp/maliciousmodel.pkl', 'wb') as f: pickle.dump(Exploit(), f)
Trigger the vulnerable code path (requires algorithm argument in ≤ 3.9.4) parser = TransitionParser('arc-standard') # or 'arc-eager' parser.parse([], '/tmp/maliciousmodel.pkl') # loads and unpickles unsafely
Exploit succeeds: file /tmp/nltkpoctriggered is created
On NLTK ≥ 3.10.0 (patched), the same code fails with:
pickle.UnpicklingError: global 'posix.system' is not in the pickle allowlist
This proves the vulnerability exists in versions ≤ 3.9.4 and is fixed in 3.10.0+.
Recommended Fix
Change all call sites to use restricted=True:
| File | Line | Before | After | |------|------|--------|-------| | nltk/parse/transitionparser.py | 557 | pickleload(f) | pickleload(f, restricted=True) | | nltk/parse/chartparserapp.py | 816 | pickleload(modeldatafile) | pickleload(modeldatafile, restricted=True) | | nltk/parse/chartparserapp.py | 2273 | pickleload(file) | pickleload(file, restricted=True) | | nltk/parse/chartparserapp.py | 2311 | pickleload(fp) | pickleload(fp, restricted=True) |
Note: This fix may affect loading older sklearn models. A more robust approach would implement a module allowlist in RestrictedUnpickler.findclass().
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
pip/nltkto a version that resolves this vulnerability.Fixed in 3.10.0 - Upgrade
Upgrade to a fixed release to a version that resolves this vulnerability.
Fixed in 3.10.0 - Configuration
In `nltk/parse/transitionparser.py` at the `TransitionParser().parse(depgraphs, modelFile)` deserialization call (line 557), change `pickle_load(f)` to `pickle_load(f, restricted=True)` to ensure the `RestrictedUnpickler` safe path is used instead of the vulnerable `WarningUnpickler` behavior.
NLTK TransitionParser (nltk/parse/transitionparser.py) pickle_load(..., restricted) = restricted=True - Configuration
Update all model-file deserialization call sites in `nltk/parse/chartparser_app.py` to use `restricted=True`: - line 2273: change `pickle_load(file)` to `pickle_load(file, restricted=True)` - line 2311: change `pickle_load(fp)` to `pickle_load(fp, restricted=True)` - line 816: change `pickle_load(model_data_file)` to `pickle_load(model_data_file, restricted=True)`
NLTK ChartParser application (nltk/parse/chartparser_app.py) pickle_load(..., restricted) = restricted=True
Event History
Frequently Asked Questions
Who is exposed to this issue?
Applications that use NLTK's TransitionParser.parse() to load model files are exposed when an attacker can supply or replace the model file being parsed. A malicious pickle can execute Python code during deserialization.
Is the default behavior affected?
Yes. TransitionParser.parse() calls pickle_load(f) without setting restricted=True, so pickle_load() uses the unrestricted WarningUnpickler path by default. That unpickler does not block class or function resolution.
What can be done if patching is not immediately possible?
Do not load untrusted or attacker-modifiable transition-parser model files. Where code can be changed locally, model deserialization must use the restricted=True path so that RestrictedUnpickler is used instead of WarningUnpickler.
How can I determine whether an application is affected?
Check whether it invokes nltk.parse.transitionparser.TransitionParser.parse() and whether the model file it loads can originate from, or be modified by, an untrusted party. The affected code path is the call to pickle_load(f) in transitionparser.py without restricted=True.