CVE-2023-39662
Llama_index v.0.7.13 and earlier: Remote code execution via 'exec' parameter in PandasQueryEngine function
- 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 llama_index v.0.7.13 and before allows a remote attacker to execute arbitrary code via the `exec` parameter in PandasQueryEngine function.
- CVSS base score
- 9.8
- Published
- 2023-08-15
- 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
- llama_index
- Fixed by upgrading
- Yes
Solution
Update llama_index to version 0.7.14 or higher.
Vulnerable code sample
import pandas as pd
class PandasQueryEngine:
def __init__(self, data_frame):
self.data_frame = data_frame
def query(self, query, exec_param=None):
# Vulnerable code allowing execution of arbitrary code
if exec_param is not None:
exec(exec_param) # This is the vulnerability
# Execute the query
return self.data_frame.query(query)
# Example usage
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
engine = PandasQueryEngine(df)
# Arbitrary code can be executed via exec_param
engine.query("a > 1", exec_param="print('This could be malicious code!')")Patched code sample
import pandas as pd
class SafePandasQueryEngine:
def __init__(self, data_frame):
self.data_frame = data_frame
def safe_query(self, query, exec_param=None):
# Validate the exec_param to prevent arbitrary code execution
if exec_param is not None:
raise ValueError("Execution of arbitrary code is not allowed.")
# Execute the query safely
return self.data_frame.query(query)
# Example usage
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
engine = SafePandasQueryEngine(df)
# This will raise an error if exec_param is provided
result = engine.safe_query("a > 1")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.
- Remediate RCE vulnerability CVE-2023-39662 (#8890) (run-llama/llama_index)
llama_index/query_engine/pandas_query_engine.py,llama_index/query_engine/pandas_query_engine.py · default_output_processor - Remediate RCE vulnerability CVE-2023-39662 – part 2 (#9423) (run-llama/llama_index)
llama_index/exec_utils.py,llama_index/exec_utils.py · safe_eval,llama_index/exec_utils.py · safe_exec
Cite this entry
@misc{vaitp:cve202339662,
title = {{Llama_index v.0.7.13 and earlier: Remote code execution via 'exec' parameter in PandasQueryEngine function}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2023-39662},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-39662/}}
}
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 ::
