|
| 1 | +--- |
| 2 | +slug: "forevervm-minimal" |
| 3 | +title: "Minimal example of ForeverVM" |
| 4 | +date: 2025-03-01T00:00:00+00:00 |
| 5 | +draft: true |
| 6 | +authors: [abeeshake] |
| 7 | +tags: [ python, gvisor ] |
| 8 | +--- |
| 9 | + |
| 10 | + |
| 11 | +# Building ForeverVM |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +In the rapidly evolving landscape of Large Language Models (LLMs), one of the most powerful capabilities is code generation and execution. However, executing untrusted code generated by LLMs poses significant security risks. This blog post explores a solution: a gVisor-based Python REPL that provides a secure, isolated environment for executing untrusted code in LLM-based workflows. |
| 16 | + |
| 17 | +## The Challenge of Code Execution in LLM Applications |
| 18 | + |
| 19 | +LLMs like GPT-4, Claude, and others have demonstrated remarkable capabilities in generating functional code across various programming languages. This has led to the development of "agentic" applications where LLMs can: |
| 20 | + |
| 21 | +1. Generate code to solve specific problems |
| 22 | +2. Execute that code to obtain results |
| 23 | +3. Analyze the results and iterate on the solution |
| 24 | +4. Interact with external systems and APIs |
| 25 | + |
| 26 | +However, executing code generated by LLMs introduces several challenges: |
| 27 | + |
| 28 | +- **Security Risks**: LLM-generated code might contain vulnerabilities, either accidentally or through prompt injection attacks |
| 29 | +- **Resource Management**: Unconstrained execution could lead to resource exhaustion |
| 30 | +- **Stateful Execution**: Many tasks require maintaining state between multiple code executions |
| 31 | +- **Isolation**: Code execution needs to be isolated from the host system and other users' code |
| 32 | + |
| 33 | +## Introducing gVisor-based Python REPL |
| 34 | + |
| 35 | +Our solution addresses these challenges through a multi-layered security approach: |
| 36 | + |
| 37 | +1. **Docker Containerization**: Provides basic isolation from the host system |
| 38 | +2. **gVisor Sandbox**: Adds an additional security layer by intercepting and filtering system calls |
| 39 | +3. **TCP Interface**: Limits interaction to a simple TCP API, reducing attack surface |
| 40 | +4. **Session Management**: Maintains stateful execution while ensuring isolation between sessions |
| 41 | + |
| 42 | +### Architecture Overview |
| 43 | + |
| 44 | +The system consists of several key components: |
| 45 | + |
| 46 | +1. **TCP Server**: A Python TCP server that accepts code execution requests and maintains stateful sessions |
| 47 | +2. **Docker Container**: Provides containerization for the Python environment |
| 48 | +3. **gVisor Runtime**: Adds an additional layer of isolation by intercepting and filtering system calls |
| 49 | + |
| 50 | +### How It Works |
| 51 | + |
| 52 | +1. The server runs inside a Docker container with gVisor's runsc runtime |
| 53 | +2. Clients connect to the server via TCP and send Python code to execute |
| 54 | +3. The server executes the code in a session-specific environment |
| 55 | +4. Results are returned to the client |
| 56 | +5. Sessions persist between requests, allowing for stateful execution |
| 57 | + |
| 58 | +## Setting Up Your Own Secure Python REPL |
| 59 | + |
| 60 | +### Prerequisites |
| 61 | + |
| 62 | +- Docker |
| 63 | +- gVisor (runsc) |
| 64 | + |
| 65 | +### Installation Steps |
| 66 | + |
| 67 | +1. Install gVisor: |
| 68 | + ```bash |
| 69 | + sudo apt-get update && \ |
| 70 | + sudo apt-get install -y \ |
| 71 | + apt-transport-https \ |
| 72 | + ca-certificates \ |
| 73 | + curl \ |
| 74 | + gnupg |
| 75 | + |
| 76 | + # Install runsc |
| 77 | + curl -fsSL https://gvisor.dev/archive.key | sudo gpg --dearmor -o /usr/share/keyrings/gvisor-archive-keyring.gpg |
| 78 | + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/gvisor-archive-keyring.gpg] https://storage.googleapis.com/gvisor/releases release main" | sudo tee /etc/apt/sources.list.d/gvisor.list > /dev/null |
| 79 | + sudo apt-get update && sudo apt-get install -y runsc |
| 80 | + ``` |
| 81 | + |
| 82 | +2. Configure Docker to use gVisor: |
| 83 | + ```bash |
| 84 | + sudo runsc install |
| 85 | + sudo systemctl restart docker |
| 86 | + ``` |
| 87 | + |
| 88 | +3. Clone the repository: |
| 89 | + ```bash |
| 90 | + git clone https://github.com/username/gvisor-based-python-repl.git |
| 91 | + cd gvisor-based-python-repl |
| 92 | + ``` |
| 93 | + |
| 94 | +4. Run the server: |
| 95 | + ```bash |
| 96 | + ./run.sh |
| 97 | + ``` |
| 98 | + |
| 99 | +## Integrating with LLM Applications |
| 100 | + |
| 101 | +### Client-Side Integration |
| 102 | + |
| 103 | +To integrate this secure Python REPL with your LLM application, you'll need to: |
| 104 | + |
| 105 | +1. Establish a TCP connection to the server |
| 106 | +2. Send Python code generated by your LLM |
| 107 | +3. Receive and process the execution results |
| 108 | +4. Optionally maintain session state for multi-step workflows |
| 109 | + |
| 110 | +Here's a simple Python example: |
| 111 | + |
| 112 | +```python |
| 113 | +import socket |
| 114 | +import json |
| 115 | + |
| 116 | +def send_code_to_repl(code, session_id=None): |
| 117 | + # Connect to the server |
| 118 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 119 | + sock.connect(("localhost", 8000)) |
| 120 | + |
| 121 | + # Skip initial greeting |
| 122 | + length_bytes = sock.recv(4) |
| 123 | + message_length = int.from_bytes(length_bytes, byteorder='big') |
| 124 | + sock.recv(message_length) |
| 125 | + |
| 126 | + # Prepare request |
| 127 | + request = { |
| 128 | + "code": code |
| 129 | + } |
| 130 | + if session_id: |
| 131 | + request["session_id"] = session_id |
| 132 | + |
| 133 | + # Send request |
| 134 | + request_json = json.dumps(request) |
| 135 | + request_bytes = request_json.encode('utf-8') |
| 136 | + length = len(request_bytes) |
| 137 | + sock.sendall(length.to_bytes(4, byteorder='big')) |
| 138 | + sock.sendall(request_bytes) |
| 139 | + |
| 140 | + # Receive response |
| 141 | + length_bytes = sock.recv(4) |
| 142 | + message_length = int.from_bytes(length_bytes, byteorder='big') |
| 143 | + response_bytes = sock.recv(message_length) |
| 144 | + response = json.loads(response_bytes.decode('utf-8')) |
| 145 | + |
| 146 | + sock.close() |
| 147 | + return response |
| 148 | + |
| 149 | +# Example usage |
| 150 | +response = send_code_to_repl("print('Hello, world!')") |
| 151 | +print(response["output"]) |
| 152 | + |
| 153 | +# Use the session_id for subsequent requests |
| 154 | +session_id = response["session_id"] |
| 155 | +response = send_code_to_repl("x = 42\nprint(f'x = {x}')", session_id) |
| 156 | +print(response["output"]) |
| 157 | +``` |
| 158 | + |
| 159 | +### LLM Workflow Integration |
| 160 | + |
| 161 | +Here's how you might integrate this into an LLM-based workflow: |
| 162 | + |
| 163 | +1. User provides a problem description |
| 164 | +2. LLM generates Python code to solve the problem |
| 165 | +3. Your application sends the code to the secure REPL |
| 166 | +4. Execution results are returned to the LLM |
| 167 | +5. LLM analyzes the results and iterates if needed |
| 168 | + |
| 169 | +This approach allows for powerful agentic workflows while maintaining security. |
| 170 | + |
| 171 | +## Security Considerations |
| 172 | + |
| 173 | +While this system provides strong isolation, it's important to understand its limitations: |
| 174 | + |
| 175 | +- Side-channel attacks might still be possible |
| 176 | +- Resource exhaustion could affect container performance |
| 177 | +- New vulnerabilities in gVisor or Docker could compromise security |
| 178 | + |
| 179 | +Regular updates and security audits are recommended. |
| 180 | + |
| 181 | +## Use Cases |
| 182 | + |
| 183 | +This secure Python REPL is particularly useful for: |
| 184 | + |
| 185 | +1. **Educational Platforms**: Allow students to execute code safely |
| 186 | +2. **AI Coding Assistants**: Enable LLMs to execute and test generated code |
| 187 | +3. **Data Analysis Workflows**: Run data processing scripts in a secure environment |
| 188 | +4. **Automated Code Testing**: Test user-submitted code without security risks |
| 189 | +5. **Interactive Documentation**: Create interactive code examples that users can modify and run |
| 190 | + |
| 191 | +## Conclusion |
| 192 | + |
| 193 | +The gVisor-based Python REPL provides a secure foundation for executing untrusted code in LLM-based workflows. By combining Docker containerization with gVisor's sandboxing capabilities, it offers a robust solution to the security challenges of code execution in AI applications. |
| 194 | + |
| 195 | +As LLMs continue to evolve and become more integrated into software development workflows, tools like this will be essential for balancing the power of code generation with the necessary security constraints. |
| 196 | + |
| 197 | +Whether you're building an AI coding assistant, an educational platform, or any application that involves executing untrusted code, this approach provides a practical and secure solution. |
| 198 | +Footer |
0 commit comments