In Part 1 I laid out the idea: a fresh, kernel-locked helper process per call, a WebAssembly guest inside it for model-written Python, and a database that checks the kernel’s view of the sandbox before it hands over any input. This post is about what happened when I started measuring and attacking it. A caveat on the numbers first: they come from one arm64 box, usually under load, so read them as orders of magnitude rather than benchmarks. The code path also hasn’t run on x86_64 yet.
Every step up the ladder costs something#
I built, spiked or measured a handful of ways to run untrusted code. This is roughly how they stacked up.
| Approach | Trivial call | Where it ended up |
|---|---|---|
| Inside the backend (a trusted PL, or a parser linked in) | not built | Rejected on day one: a crash takes the server with it |
| Helper process, native parser code | about 0.65 ms | Default for parsers and network fetches |
| Helper + CPython on WebAssembly (wasmi) | 30 to 36 ms | Default for model-written Python |
| Helper + QuickJS-ng on WebAssembly | about 12.5 ms | Promising probe for JavaScript |
| Helper inside gVisor | 139 to 148 ms, about 110 ms over the default | Lab only |
| microVM or a separate executor service | not measured here | Designed as options, not built |
One footnote on the gVisor row: about 46 ms of that gap was the backend hashing its copy of the 19 MB guest, so the sandbox itself costs less than the row suggests.
The pattern is hard to miss. Every step up in isolation costs roughly an order of magnitude in time, or a piece of infrastructure, or both. gVisor worked, but launching it from a Postgres backend meant widening the seccomp profile of the whole Postgres container, which is a strange trade to make in the name of security. A microVM needs /dev/kvm, which a lot of cloud VMs and containers simply don’t have.
The Python guest has its own costs. It’s 19.3 MB, compute runs about 20 times slower than native CPython, and there are no native wheels, so numpy and pandas are off the table at the default tier. For an agent that needs to reshape a few hundred rows of JSON that’s fine. For an analyst who expects a dataframe, it isn’t yet.
Things I expected to work that didn’t#
Wasmtime in its usual compiled mode. Compiling WebAssembly to native code needs executable memory, and the whole seccomp profile is built around killing executable mappings. Its epoch-based interruption also wanted a second thread or a signal handler, which broke the one-thread rule the backend checks for. wasmi is slower, but as a pure interpreter it asks the kernel for almost nothing. (Wasmtime now ships an interpreter of its own, which I haven’t tried here.)
The import allowlist as a boundary. I had a list of modules the guest was allowed to import. A spike walked object.__subclasses__() straight back to the real os module and loaded builtins the list was supposed to hide. Nothing on the host was reachable, because the WASI build has no preopened paths, no sockets and no processes to reach. The real boundary was the build, so the list is now documented as a convenience and an early diagnostic, nothing more.
Fuel as a time limit. wasmi meters execution with fuel, and fuel is beautifully deterministic: the same call burns the same count under any load, which makes runaway-code tests repeatable. As a clock it is useless. Fuel per CPU second varied about 250-fold inside CPython between a tight while True: pass and print-heavy code. The kernel’s CPU limit is the real time bound now, and fuel is set high enough that it never fires first.
The fast interpreter dispatch. A loop of failing memory.grow calls ate a little native stack each time, and after about 44,000 of them the helper fell over. Switching wasmi to its portable dispatch fixed it, at a price: 694 ms instead of 264 ms on the reference workload. I took the slower, boring option.
Bounds built from estimates get beaten#
The most repeated lesson across more than 30 rounds of internal adversarial review was about limits. Whenever I bounded something with an estimate, an attacker found an input that blew through it.
A 240 KB PDF took 141 seconds to extract. A 30-byte input could ask for a 180 MiB answer, and the backend dutifully parsed it. My memory model for answers was beaten over and over; at one point a single float cost 1.2 KB inside jsonb_in against the 100 bytes I had estimated. A JSON-schema pattern check pushed a backend to about 4.1 GB before the container got OOM-killed.
What held were bounds by construction. Accept only known answer shapes and rebuild them. Allow no floats at all. Derive every output cap from the input that produced it, and refuse oversized answers at the frame header before allocating anything. Count regex patterns and their nodes before compiling a single one. After that change, the same 30-byte trick peaked the backend at 26 MB instead of 745 MB. I’ve watched the same thing play out in database capacity planning for decades: the estimate you’re proud of is the one that pages you.
A sandbox test that passes proves very little#
This took more of my time than any single layer, and I think it’s the part most worth stealing.
A missing file, a skipped test group or a typo in an expected string all look exactly like “blocked.” So the rules became:
- Judge from the host, never the guest. A test tries something real, like reading a file, creating a flag file or connecting to a port the test owns. The verdict comes from what the host observed. A guest that prints “blocked” proves nothing.
- Pair every block with a control that really succeeds. If the control can’t leak on purpose, the observer is broken, and every “blocked” next to it is void.
- Prove the check can fail. A fix isn’t done until a test fails on the old build and passes on the new one.
- No silent skips. A run that skips half its groups can still print
fail=0, so the suite now fails when fewer checks pass than expected.
The proof that this matters: a 29-case hostile test set came back with 24 cases blocked, 4 contained, 1 not applicable and zero gaps against the guest. Run against plain, unsandboxed python3, the same cases flagged 21 gaps. That second number is the one that tells you the first one means something.
My favorite cautionary tale is from the other direction. Every suite was green while the real end-to-end path failed every single time. A real model call left eight idle threads behind, the one-thread check rightly refused them, and the suites never noticed because they all used fake models.
What is still hard#
A few problems don’t have an answer that keeps things simple and also matches a hosted sandbox.
- One shared kernel. Once the WebAssembly engine is beaten, a single kernel bug is the last wall, and on a database host that is a bigger deal than on a disposable VM.
- Isolation versus nothing to deploy. My answer so far is a ladder: a simple default for everyone and stronger tiers as options, with each app declaring the minimum tier it needs and the database recording which tier every call actually ran on. That per-app minimum is designed, not built.
- Types across the boundary. Postgres
bigintandnumericarrive in the guest as JSON numbers, so anything past 2^53 loses precision. The fix is to send them as text and rebuild them on the other side. - Accounting per backend, not per cluster. Memory budgets and the crash circuit breaker work per connection. A burst across many backends could still reach the OS OOM killer.
- Independent review. Nobody outside the project has audited this yet, and the guest snapshot isn’t byte-for-byte reproducible, so its hash can’t be rebuilt independently. Both need fixing before anyone should trust it in production.
Where I think this goes#
The ideal is a sandbox with the strength of a hosted one and the footprint of a single extension. I’m close on the default and the verification story, partway on the reviewability, and furthest on speed and libraries, where every gain so far has cost a piece of the simplicity.
I still think the direction is right. Agents are going to live next to data, and the code they write should be governed where the data lives, by the same system that already governs everything else touching it. The database already knows who you are and what you’re allowed to read. Teaching it to ask the kernel what its sandbox is actually doing is a fairly small step from there.
If you’re working on the same problem, I’d love to compare hostile test cases. That’s the one thing I’m sure gets better when more people share it.
In Part 3: what happens when the code isn’t a one-off script but a function you deploy, and why “it ran safely” and “it gave the right answer” are two different problems.
Part 2 of 3. Next: Part 3, could the database be your serverless runtime?
