CVE-2023-29374
LangChain 0.0.131's LLMMathChain is vulnerable to arbitrary code execution via Python exec method through prompt injection attacks
- CVSS 9.8
- CWE-74 Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
- Input Validation and Sanitization
- Remote
In LangChain through 0.0.131, the LLMMathChain chain allows prompt injection attacks that can execute arbitrary code via the Python exec method.
- CVSS base score
- 9.8
- Published
- 2023-04-05
- 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.132 or higher.
Vulnerable code sample
class LLMMathChain:
def __init__(self, model):
self.model = model
def execute(self, prompt):
# Directly execute the prompt using exec, which can be dangerous
exec(prompt)
# Example usage
model = SomeLLMModel() # Replace with actual model
chain = LLMMathChain(model)
chain.execute("print('This is an arbitrary code execution!')") # Vulnerable inputPatched code sample
import re
class SafeLLMMathChain:
def __init__(self, model):
self.model = model
def execute(self, prompt):
# Sanitize the prompt to prevent code injection
sanitized_prompt = self.sanitize_prompt(prompt)
# Execute the model with the sanitized prompt
return self.model.run(sanitized_prompt)
def sanitize_prompt(self, prompt):
# Remove any potentially dangerous code patterns
safe_prompt = re.sub(r'[^a-zA-Z0-9\s\+\-\*/\(\)\.]+', '', prompt)
return safe_prompt
# Example usage
model = SomeLLMModel() # Replace with actual model
chain = SafeLLMMathChain(model)
result = chain.execute("2 + 2") # Safe inputPublished 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.
- Patch LLMMathChain exec vulnerability (hwchase17/langchain)
langchain/chains/llm_math/base.py,langchain/chains/llm_math/base.py · LLMMathChain._process_llm_result,langchain/chains/llm_math/prompt.py
Cite this entry
@misc{vaitp:cve202329374,
title = {{LangChain 0.0.131's LLMMathChain is vulnerable to arbitrary code execution via Python exec method through prompt injection attacks}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2023-29374},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-29374/}}
}
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 ::
