Potential Security Vulnerability: Arbitrary Code Execution via Direct Execution of LLM-Generated Content
Description
In the llm_agents project, a potential security vulnerability has been identified where content generated by a Large Language Model (LLM) is directly executed without sufficient filtering or sandboxing. This can lead to prompt injection attacks, allowing malicious users to execute arbitrary code.
Details
- In the
runmethod ofagent.py, thetool_inputgenerated by the LLM is passed to theusemethod of a tool: - Specifically, when
PythonREPLToolis used, itsusemethod receivesinput_textand passes it to therunmethod of aPythonREPLinstance:
# ... existing code ...
class PythonREPLTool(ToolInterface):
# ... existing code ...
def use(self, input_text: str) -> str:
input_text = input_text.strip().strip("```")
return self.python_repl.run(input_text)
# ... existing code ...
- The
runmethod of thePythonREPLclass uses theexec()function to execute the provided command:
# ... existing code ...
class PythonREPL(BaseModel):
# ... existing code ...
def run(self, command: str) -> str:
# ... existing code ...
exec(command, self.globals, self.locals)
# ... existing code ...
Risk
Because the exec() function executes the input string as Python code, if the LLM is manipulated via a malicious prompt (i.e., prompt injection), it may generate a string containing harmful Python code. This code would then be executed in the agent's runtime environment, potentially leading to:
- Data leakage
- System compromise
- Unauthorized operations
Recommended Mitigations
-
Sandboxed Execution Environment:
Execute Python code in a restricted sandboxed environment. Consider using thesubprocessmodule in an isolated process or a specialized sandboxing library. -
Input Validation and Filtering:
Apply strict validation and filtering to LLM-generatedtool_inputto ensure it does not contain potentially malicious code or commands. This may include whitelisting, blacklisting, or more advanced static code analysis. -
Principle of Least Privilege:
Ensure that the environment in which the Python REPL runs has the least privileges necessary. Even if code execution occurs, the potential damage is minimized.
Yes sure, that is a well known fact.
For my use case it doesn't matter, but if this is important to you, feel free to submit a pull request.