VAITP Dataset

← Back to the dataset

CVE-2023-36258

Arbitrary code execution in Langchain v0.0.199 via PALChain in python exec method

  • CVSS 9.8
  • CWE-94 Improper Control of Generation of Code ('Code Injection')
  • Input Validation and Sanitization
  • Remote

An issue in langchain v.0.0.199 allows an attacker to execute arbitrary code via the PALChain in the python exec method.

CVSS base score
9.8
Published
2023-07-03
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.200 or higher.

Vulnerable code sample

class PALChain:
    def execute(self, code):
        # Vulnerable to arbitrary code execution
        exec(code)

# Example of how to use the PALChain
vulnerable_chain = PALChain()
vulnerable_chain.execute("__import__('os').system('ls')")  # Arbitrary code execution

Patched code sample

class SafePALChain:
    def __init__(self, allowed_functions=None):
        self.allowed_functions = allowed_functions or {}

    def execute(self, code):
        # Use a restricted execution environment
        exec_globals = {"__builtins__": {}}
        exec_globals.update(self.allowed_functions)

        try:
            exec(code, exec_globals)
        except Exception as e:
            print(f"Execution failed: {e}")

# Example of how to use the SafePALChain
allowed_funcs = {
    'print': print,
    # Add other safe functions as needed
}

safe_chain = SafePALChain(allowed_functions=allowed_funcs)
safe_chain.execute("print('Hello, World!')")  # Safe execution
safe_chain.execute("__import__('os').system('ls')")  # This will fail

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.

  • Mitigate issue #5872 (Prompt injection -> RCE in PAL chain) (#6003) (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._call, langchain/utilities/python.py, langchain/utilities/python.py · PythonREPL.run
  • 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
  • remove code (#8425) (langchain-ai/langchain)
    libs/experimental/langchain_experimental/pal_chain/base.py, libs/experimental/langchain_experimental/pal_chain/base.py · PALChain.input_keys, libs/langchain/langchain/agents/load_tools.py, libs/langchain/langchain/chains/loading.py, libs/langchain/langchain/chains/loading.py · _load_pal_chain, libs/langchain/langchain/chains/loading.py · _load_sql_database_chain, libs/langchain/langchain/chains/__init__.py, libs/langchain/langchain/prompts/loading.py, libs/langchain/langchain/prompts/loading.py · _load_prompt_from_file, libs/langchain/langchain/__init__.py

Cite this entry

@misc{vaitp:cve202336258,
  title        = {{Arbitrary code execution in Langchain v0.0.199 via PALChain 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-36258},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-36258/}}
}

Download this entry as JSON Download the whole dataset

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 ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.