Linux - Using bubblewrap to Run Code Safely

Linux - Using bubblewrap to Run Code Safely

Leader ●1 ●4 ●22
calendar_today ago • schedule6 min read

I run small language models (SLMs) locally, CPU-only, on an HP ENVY 17m with Debian 12 and llama.cpp. My reasoning model is Qwen3.5-9B, and I test it the obvious way: give it a coding task, then run the code it writes and see whether it works.

Running code you didn't write and haven't read is exactly what every security tech tells you not to do. An SLM isn't malicious, but it is confidently wrong on a regular basis, and a confidently wrong bash script can rm the wrong folder just as easily as the right one. I wanted every script Qwen wrote to run where it couldn't touch my files, my network or anything else on the machine.

I considered Podman or some similar tool that I could install. It turned out the solution is already there in the Linux OS. It's called bubblewrap, the command is bwrap, and I never installed it. It came in as a dependency of WebKitGTK, and it's the same sandbox that Flatpak uses for every Flatpak app.

This article covers what bubblewrap is, what Linux feature it's built on, and the one-line sandbox I use to test AI-written code.


Why code from a local model needs a box

A model's code fails in two ways. The first is the obvious one: it doesn't run, or it gives the wrong answer. That's fine; that's what testing is for. The second is the one that bites: it runs and does something you didn't ask for. It writes into your home folder, deletes a path built from an empty variable, loops forever, or reaches out to the network.

You don't fix that by reading every script first. The point of testing a model is to run a lot of its output, quickly and repeatedly. You fix it by making sure the worst a script can do is damage a throwaway folder.


What bubblewrap is

bubblewrap describes itself as a "utility for unprivileged chroot and namespace manipulation". In plain terms, it builds a private little world for one program, and the program can't see outside it.

It does that with a Linux kernel feature called namespaces. A namespace gives a process its own private view of one part of the system:

  • Mount namespace: its own view of the filesystem. Folders can be hidden, swapped out or made read-only, for that process only.
  • PID namespace: its own list of processes. It can't see or signal anything else running on the machine.
  • Network namespace: its own network stack. A new one has nothing in it but loopback, so there's no internet.
  • User, IPC, UTS and cgroup namespaces: the rest of the isolation, covering user IDs, shared memory, hostname and resource groups.

Containers (Docker, Podman, LXC) are built on these same namespaces. bubblewrap is the small, no-daemon version: one 72 KB binary, no images, no service running in the background, and it runs as your normal user.


The sandbox, one line

This is the command I use:

bwrap --ro-bind / / --dev /dev --proc /proc --tmpfs /tmp \
      --bind "$work" "$work" --chdir "$work" \
      --unshare-all --die-with-parent \
      timeout 30 bash test.sh

Read it left to right:

  • --ro-bind / /: the whole real filesystem, read-only. The script can still run find, sort, python3 and everything else installed, but it can't change a single file.
  • --dev /dev --proc /proc: a fresh, minimal /dev (so /dev/null works) and a /proc that shows only the sandbox's own processes.
  • --tmpfs /tmp: a blank, empty /tmp that vanishes when the sandbox ends. The real /tmp is hidden.
  • --bind "$work" "$work": the only writable place, a temporary folder made for this one test run.
  • --unshare-all: new namespaces for everything bubblewrap can unshare: user, IPC, PID, network, UTS and cgroup.
  • --die-with-parent: if the test harness dies, everything inside the sandbox is killed with it, so nothing is left running.
  • timeout 30: a script that loops forever is stopped after 30 seconds.

There's no config file and nothing to set up. You type the command and the box exists for as long as the command runs.

What it looks like from inside

Here's the same sandbox running a few ordinary commands on my laptop (B is the bwrap line above, minus the work folder):

$ $B touch ~/hello
touch: cannot touch '/home/ben/hello': Read-only file system

$ $B curl -sS -m5 https://example.com
curl: (6) Could not resolve host: example.com

$ $B ip -br link
lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>

$ $B ps -e
    PID TTY          TIME CMD
      1 ?        00:00:00 bwrap
      2 ?        00:00:00 ps

$ $B ls -A /tmp | wc -l
0

It can't write to my home folder. It has no network, only a loopback interface. It sees exactly two processes in the whole system, itself and ps. And its /tmp is empty, while my real one had 14 entries in it.


The setup: model writes, sandbox runs, script scores

My test loop has three steps:

  1. Ask. Qwen gets a task through my slm launcher (llama.cpp, cool mode on).
  2. Extract. A few lines of Python take the last code block from its answer.
  3. Run it boxed. The code goes into a fresh temp folder, along with a checker script I wrote. bubblewrap runs the checker against the model's code, and the checker prints a score.

The checker for a bash task builds its test files inside the sandbox, runs the model's script against them, and checks the results. Nothing it creates survives, and nothing outside the work folder can be touched, however wrong the script is.

Before trusting the checker, I ran a correct reference script through the same sandbox. It scored 12/12. That matters: if the checker is wrong, every result after it is wrong too.


The results

The task, in the kind of shape I'd put in ~/bin:

Write a bash script bigfiles DIR [N] that prints the N largest files under DIR (subfolders included), largest first, as a human-readable size, a tab, then the path. Handle file names with spaces. A missing directory or a bad N must print an error to stderr and exit 1.

Qwen thought for about 1,500 tokens, then wrote a clean, well-commented script. It took 9 minutes in cool mode, at 3.3 tokens/s. The sandbox ran it against 12 checks:

Check Result
Runs, prints 5 lines by default pass
Largest first, names with spaces kept intact pass
Tab between size and path pass
N=2 prints 2 lines pass
Missing directory: message on stderr, exit 1 pass
Bad N (abc, 0, none at all): exit 1 pass
Human-readable size fail

11 out of 12. The hard parts were all right: it used find -printf '%s\t%p\n' with a tab-separated read, which is exactly the right way to keep spaces in file names intact, and its input checks were tight.

The one failure is a classic. Inside the output loop it left in a stray line:

human_readable "$size"
echo "$(human_readable "$size")$filepath"

The first call prints the size with no newline, then the echo prints it again, so every line came out as 293.0K293.0K<tab>path. It's a one-line leftover, the kind of thing a person would catch on the first run, and the kind of thing you only catch at all if you actually run the code. The sandbox made running it a non-event.


A few things to know

  • Read-only isn't invisible. With --ro-bind / / the script can still read your files, including anything in your home folder. For model-written test code that's fine, because it has no network to send anything anywhere. If you want it to see less, bind only what it needs (--ro-bind /usr /usr and so on) instead of all of /.
  • It's a sandbox, not a VM. It shares your kernel. That's plenty for "the model wrote a bad script", but it's not the tool for code you think is actively hostile.
  • Unprivileged user namespaces must be on. They are by default on Debian 12 (/proc/sys/kernel/unprivileged_userns_clone reads 1). Some hardened setups turn them off.
  • This isn't only for AI. Any script from a forum post, a gist or a README can be run the same way the first time.

If you test local models on code, check whether you have bwrap before you reach for Docker or Podman - you may not need them.


Ben Santora - December 2026

Want more #security & #linux deep dives?

Join 6,129+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

1 Comment

0 votes

More Posts

Comparison: Universal Import vs. Plaid/Yodlee

Pocket Portfolio - Mar 12

The Audit Trail of Things: Using Hashgraph as a Digital Caliper for Provenance

Ken W. Algerverified - Apr 28

MCP Is the USB-C of AI. So Why Are You Plugging Everything In?

Ken W. Algerverified - Jun 10

Hermes Agent Telegram Setup on Windows — Control Your Local AI From Phone

TheCodeStreet - Nov 19

The Interface of Uncertainty: Designing Human-in-the-Loop

Pocket Portfolio - Mar 10
chevron_left
1.3k Points • 27 Badges
Montserrat MA • xiralinux.pages.dev
7Posts
18Comments
10Connections
Linux and AI Advocate - Creator of xiralinux - a lightweight Debian 12 + Openbox live distro built t... Show more

Related Jobs

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!