CVE-2023-36188
Remote code execution in Langchain v0.0.64 via PALChain parameter in Python exec method
- CVSS 9.8
- CWE-74 Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
- Input Validation and Sanitization
- Remote
An issue in langchain v.0.0.64 allows a remote attacker to execute arbitrary code via the PALChain parameter in the Python exec method.
- CVSS base score
- 9.8
- Published
- 2023-07-06
- OWASP
- A03 Injection
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Command Injection
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- langchain
- Fixed by upgrading
- Yes
Solution
Update Langchain to version 0.0.65 or higher.
Vulnerable code sample
class VulnerableExecutor:
def execute_code(self, code):
# Vulnerable to arbitrary code execution
exec(code)
# Example usage
executor = VulnerableExecutor()
# This code can be executed, potentially allowing for arbitrary code execution
user_input = "__import__('os').system('echo Vulnerable!')"
executor.execute_code(user_input)Patched code sample
class SafeExecutor:
def __init__(self, allowed_globals=None):
self.allowed_globals = allowed_globals or {}
def safe_exec(self, code, local_vars=None):
# Only allow a restricted set of globals
exec(code, self.allowed_globals, local_vars)
# Example usage
safe_globals = {
"__builtins__": {
"print": print, # Only allow print function
# Add other safe built-ins as needed
}
}
executor = SafeExecutor(allowed_globals=safe_globals)
# This code can be executed safely
safe_code = "print('Hello, safe world!')"
executor.safe_exec(safe_code)Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Some mitigations for RCE in PAL chain (#7870) (langchain-ai/langchain)
langchain/chains/pal/base.py,langchain/chains/pal/base.py · PALChain.from_colored_object_prompt,langchain/chains/pal/base.py · PALChain.from_math_prompt,langchain/chains/pal/base.py · PALChain.raise_deprecation,langchain/chains/pal/base.py · PALChain._call,langchain/utilities/python.py,langchain/utilities/python.py · PythonREPL.run
Cite this entry
@misc{vaitp:cve202336188,
title = {{Remote code execution in Langchain v0.0.64 via PALChain parameter in Python exec method}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2023-36188},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-36188/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
