Skip to main content

Blog / AI & security

How the sandbox is wired

Abhimanyu Selvan7 min read

This is the third and last post in a short series about our eval harness, a tool that runs test packs against a model and grades the answers. A pack can ship a check written in Python, and the harness runs it. That is a real feature, and it is also a way to run a stranger's code next to your API keys.

Parts one and two are the why. A pack could read my keys out of a hardened container and ship them abroad, and the fix was to run pack code in a second container with no network, no keys, and no repository. You don't need either post to follow this one. This post is the how, read straight off the config. The isolation isn't a program you have to take on faith. It is a Compose file and a Makefile, and I want to show you that you can read the whole thing.

The short version:

  • One command. make up picks a free port, checks Docker is running, starts the containers, and hands you a keyed link. No arguments needed.
  • One block of restrictions. A single YAML anchor carries read-only root, every capability dropped, no new privileges, and memory and process caps. Every service inherits it.
  • Two kinds of container. The clients, the dashboard and CLI, hold the keys and the pack. A separate checks supervisor runs the pack's code, with no network and under a throwaway user.
  • A socket, not a mount. Clients reach the checks container over a Unix socket on an in-memory volume, locked down by uid, gid, and mode. No shared disk.
  • Opt-in by default. Untrusted packs get recorded tool responses and no live network calls, unless you deliberately turn a live tool on.

One command, a fresh port each time

The whole thing starts with make up. There are flags, but the bare command is the one I use:

make up                                  # just the sandboxmake up MODELS=1                         # add local modelsmake up PACK_DIR=../other-pack/evals     # point at a different pack

Two small decisions in that target matter more than they look. First, the port. By default make up does not pin one. It picks a random free port in the ephemeral range, tries to open it, and only keeps it if nothing answers. A fixed default port sounds friendlier, but a fixed port is one another program may already be holding, and then your first run fails for a reason that has nothing to do with the tool. If you want a specific port, you pass it. Otherwise the tool finds one.

Second, it fails closed on setup. Before it starts anything, it checks that Docker is actually running, and with MODELS=1 that the model runner is enabled too. If Docker is down you get one plain sentence telling you to start it, not a stack trace from deep inside Compose. When it does come up, it reads the dashboard's link out of the logs, including the access key for this run, and opens it. The dashboard answers only on 127.0.0.1, and only to a request that carries that key.

One block of restrictions, applied everywhere

Container hardening is easy to get half-right, because it is a list of flags and it is easy to set them on one service and forget them on the next. So there is exactly one list, written once as a YAML anchor, and every service pulls it in:

x-hardened: &hardened  image: ${EVAL_IMAGE:-evalbox:local}  init: true             # reap orphaned child processes on shutdown  read_only: true        # the container's own filesystem is read-only  tmpfs:    - /tmp               # the one writable path, and it lives in memory  cap_drop: [ALL]        # drop every Linux capability  security_opt:    - no-new-privileges:true  mem_limit: 2g  pids_limit: 512

Then each service starts from it with one line:

services:  dashboard:    <<: *hardened        # inherit the whole locked-down base    command: [serve, /pack, --no-browser, --no-tool-commands]  checks:    <<: *hardened        # same base, plus the few extras below

None of this stops the attack from parts one and two on its own. Reading a file and opening a socket need no capability and no writable disk. That is the point of the series. These flags are the floor, not the fix. They shrink what a container can do to the host, so that when something does go wrong it has less to work with. The actual fix is the next two sections.

Two kinds of container

There are two roles here, and the split is the whole security model. The clients, the dashboard and the one-off CLI, are the trusted side. They hold the pack and the keys and talk to the models. They never run a pack's code themselves. The checks service is the untrusted side. It exists only to run those Python checks, and it is built to hold nothing worth stealing:

checks:  <<: *hardened  network_mode: none                # no network interface at all  user: "0:20000"                   # starts privileged, drops per check  cap_add: [SETUID, SETGID, CHOWN, DAC_OVERRIDE, KILL]  ipc: none  entrypoint: [python, -c, "from checks.server import main", /sandbox]

network_mode: none is the line that defeats exfiltration. There is no network in that container, so the "open a socket and POST the keys" step has nothing to open. The user and cap_add lines look like the opposite of hardening, and they are the one place the supervisor is allowed more than the clients. It starts able to switch users, so that it can run each check under a private throwaway UID and clean up after it, then give nothing of its own to the check. The clients keep cap_drop: [ALL] with no additions. Only the supervisor, the thing that actually runs the stranger's code, carries the few capabilities it needs to cage that code, and it pays for them with having no network and no keys.

The trusted clients hold the pack and the keys and send each check over a Unix socket on an in-memory volume. The checks supervisor has no network and runs each check under a throwaway user, so the pack's code executes with nothing to read and nowhere to send it.

A socket, not a mount

The clients and the supervisor have to talk, and the obvious way, a shared folder, is the wrong way. A shared writable folder is a place for one side to drop a file the other reads later, which is exactly the kind of channel I was trying to close. So they share a socket instead, and nothing else. The socket lives on its own in-memory volume with the permissions written out by hand:

volumes:  checks-socket:    driver_opts: { type: tmpfs, device: tmpfs, o: "mode=0750,uid=0,gid=20000" }

That gid=20000 is the hinge. The supervisor owns the socket. A client gets to reach it only because the compose file adds it to that one group, and nothing more:

dashboard:  group_add: ["20000"]                      # the only reason it can reach the socket  volumes:    - checks-socket:/sandbox/socket:ro      # and it holds the read-only end

A check arrives over that socket carrying its declared inputs, runs in the networkless container, and the result comes back the same way. The pack's files never get mounted into the supervisor as a live directory. Only the dashboard and CLI ever see the pack.

Recorded tools by default, live only on purpose

A test pack can call tools, which usually means reaching out to a real service over the network. For an untrusted pack that is the same problem wearing a different hat, so the base setup does not allow it. Evaluations run in replay mode. The pack gets recorded tool responses, captured ahead of time, and makes no live calls at all.

Turning on a real tool is a separate, deliberate step. It is its own overlay file and its own service, off unless you ask:

services:  dashboard:    environment:      EVAL_MCP_MODE: live       # the base file defaults this to "replay"    depends_on:      mcp-live: { condition: service_healthy }

So the dangerous mode exists, but you reach it by naming the file and the server out loud, with make up LIVE_MCP=1, never by default and never by accident. There is a matching record service whose only job is to capture a live server's responses once so that later runs can replay them. The safe path is the one you fall into; the live path is the one you have to choose.

Local models without the cloud

If you don't want to point the tool at a hosted model, it can run one on your machine. With MODELS=1 an overlay hands the job to Docker's model runner, which pulls an open model and serves it on an OpenAI-compatible address:

models:  llama3_2:    model: ai/llama3.2:3B-Q4_K_M    context_size: 8192   # the model's own default is far larger and can OOM a laptop GPU

That context_size line is a scar from the first time I tried it. The model's default context is much larger, and on a laptop GPU the larger default runs you out of memory before the first answer. Pinning it to 8192 is the difference between a model that runs and one that dies on load.

How the files stack

None of these modes are forks of the setup. They are overlays, and the Makefile just decides which ones to lay on top of the base:

docker compose -f compose.yaml \  $(MODELS   -> -f compose.models.yaml) \  $(LIVE_MCP -> -f compose.live.yaml)

The base file, compose.yaml, is always there and holds the whole security model: the hardened anchor, the two roles, the socket. compose.models.yaml adds local models. compose.live.yaml adds one live tool. You can read each file on its own and know exactly what turning it on changes, because each one only adds.

The base Compose file holds the sandbox and is always applied. Local models and a live tool are separate overlay files, each added only when you ask for it, so the dangerous options are opt-in and legible.

Takeaways

  • Put your hardening flags in one place and inherit them. A list of security flags copied per service is a list you will eventually copy wrong.
  • Split trusted from untrusted at the container line, and give the untrusted side the privileges it needs to cage code in exchange for having nothing worth stealing, no network and no keys.
  • Make two processes talk over a socket, not a shared folder, when one of them is running code you don't trust.
  • Make the safe mode the default and the dangerous mode a file you have to name. Opt-in beats a warning in the docs.
  • Keep it one command. The best hardening is the hardening someone will actually run, and make up is easier to run than a page of docker flags.

Keep reading

AI & security

How I actually sandboxed untrusted code

A few days ago I showed that a test pack could read my API keys out of a hardened container and ship them abroad. The container had every flag set and none of them mattered, because the code I was running wanted none of them. So I stopped hardening the room the code runs in and gave it a different room: no network, no keys, no repository. Here is how it is built, and the cases it still doesn't cover.

AI & security

My agent hardened the container "perfectly." The API keys walked out anyway.

A coding agent containerized our internal eval tool and did a clean job: read-only filesystem, every Linux capability dropped, no new privileges. It also let any eval pack I run read my API keys and ship them to a server in another country. Both are true at once. The agent hardened the box and never asked who the box was meant to keep out.

On-device AI

My fine-tuned search model scored better. I'm not using it yet.

I fine-tuned a small on-device search model for voice notes. If search always shows five results, the right note is among them 54.7% of the time, up from 46.0%. Counting only what would appear on screen, it was ahead by less than one point, so I'm not using it yet.

Build in Public

I asked whether ArchByte still worked. Mostly it did, and that was the problem.

ArchByte draws your codebase's architecture with AI agents. I tested it like a stranger would, found what was broken, slow or pretending, and fixed it. Here is what it does today, with real numbers.

All posts