Hardware Virtualization for AI CLI Security

AI plugins and command-line tools are incredibly practical—but they also open exciting attack vectors for data theft. You don’t have to look far for examples of problems that arise when the security mechanisms of AI models and their integrations into IDEs and command lines fail.

What alternatives are available if you simply cannot do without them?

Surprisingly, containers are not the right answer this time. Let me explain why.

Architectural Security: Virtualization for AI CLI and MCP Workflows

In AI-assisted software development, the architectural setup of an AI CLI client dictates the security boundary. While the Model Context Protocol (MCP) enables AI agents to execute tools and interact with local services, it introduces specific attack vectors that require a robust isolation strategy.

The Host Level: Direct Access and Prompt Injection

Installing an AI CLI directly on the host operating system is the simplest path with the lowest barrier to entry. It requires no additional configuration and provides native performance.

In this setup, the AI process operates within the host’s user space and inherits the logged-in user’s permissions and environment. The primary risk is Indirect Prompt Injection. If an AI processes untrusted data—such as a README from a public repository containing hidden malicious instructions—the model can be “hijacked.”

Because no system-level boundary exists, the manipulated model can execute commands in the local shell, accessing private SSH keys, environment variables, and local databases.

The Container: Dependency Isolation and the Socket Compromise

To isolate dependencies, AI tools are often operated within Docker containers. This ensures a consistent environment and prevents “dependency drift” on the host system.

However, a technical conflict arises when using MCP: many MCP servers are distributed as Docker images to ensure cross-platform compatibility. An AI CLI inside a container must therefore be able to start and manage “sibling containers.” The standard technical solution for this is mounting the Docker socket (-v /var/run/docker.sock:/var/run/docker.sock).

The Risk Profile: The Docker socket provides administrative access to the host’s Docker daemon. If an MCP tool or the CLI environment is compromised, the socket allows for lateral movement. An attacker can command the host daemon to launch a new, privileged container that mounts the host’s root filesystem. While the container isolates application libraries, the socket bridge bypasses the container’s security boundary entirely.

But Gemini has a sandbox mode that runs the CLI inside a container

Beyond the socket risk, this setup creates a significant “Trust Gap.” By configuring a containerized sandbox for the Gemini CLI, you are essentially trusting the NPM package—and by extension, Google — to respect that boundary. You relinquish granular control over what the CLI actually executes, trusting that the software won’t attempt to “jump out of its cage.” If the package itself is compromised or contains a supply-chain vulnerability, the software-level isolation of a container offers little resistance against an actor determined to probe the host via the mounted socket.

The Virtual Machine (VM): Hardware-Enforced Isolation

A third approach utilizes a dedicated Virtual Machine (VM). This shifts the security boundary from software-based mechanisms (namespaces and cgroups) to the hardware level (hypervisor).

Technical Advantages of the VM:

Conclusion

Deciding where to run AI orchestration tools involves a trade-off between setup speed and specific security requirements. While direct host installations and containers offer convenience, the VM architecture provides the most robust protection for the underlying operating system. By isolating the AI and its tools at the hardware level, you can leverage the potential of the Model Context Protocol while maintaining a strictly controlled environment.

Practical Example: Isolation with macOS & Lima

The technical implementation of this architecture is achieved using Lima (Linux Machines). This tool leverages the native Virtualization.framework (vz) on macOS to run Linux guests with minimal overhead.

My project lima-ai-cli-sandbox implements the described isolation through the following mechanisms:

Filesystem Restriction

The decisive security factor of this setup is the deliberate restriction of file access by the user. In the lima-gemini.yaml, access to the host is explicitly limited:

Identity and Kernel Isolation

Workflow and Integrity

Through this structure, the attack surface is reduced compared to a host-level Gemini CLI installation, while the functionality of AI CLIs and MCP workflows remains fully preserved.

👋 Hello, I am Denis,

I am a freelance software architect, full-stack developer, and co-founder of DenktMit, a network of independent IT experts.

Got questions or a project in mind? Get in touch via LinkedIn or email.

back to blog list