nsjail, bubblewrap and Firejail compared: three Linux process sandboxes, from their own docs
Not every piece of untrusted code needs a virtual machine. Sometimes you want something lighter on one Linux host: run this command with no network, a read-only view of the system, a time limit and a memory cap.
Three mature open-source tools do that: nsjail, bubblewrap and Firejail. They are built from the same kernel features, namespaces, seccomp and resource limits, but they were made for different jobs.
This comparison relies on each project's README and documentation. We have not benchmarked them, and we do not rank them; the right one depends on what you are sandboxing.
The shared building blocks
All three use features covered in earlier posts:
- Namespaces give the process its own view of mounts, process IDs, network, hostname and users.
- seccomp filters system calls. Our post on seccomp for application developers explains what it can and cannot do.
- Resource limits cap CPU time, memory, file descriptors and process counts, through rlimits or cgroups.
All three share the host kernel with the sandboxed process. That is the main difference from a microVM, and it applies to every tool here: a kernel bug reachable through an allowed system call is reachable from inside.
nsjail
nsjail's README calls it a "Linux process isolation tool using namespaces, resource limits, and seccomp-bpf syscall filters". Its feature list is the broadest of the three:
- Execution modes: a TCP listener that runs one jailed process per connection (inetd-style), a standalone single run, and continuous re-execution, which the README notes is useful for fuzzing.
- Namespaces: UTS, mount, PID, IPC, network, user, cgroup and time.
- Filesystem:
chroot(),pivot_root(), read-only mounts, custom/procandtmpfs. - Limits: CPU time, memory, file descriptors, process count, a wall-time limit, and cgroup controls (v1 and v2).
- Syscall policies written in Kafel, a policy language that compiles to seccomp filters.
- Networking: isolated interfaces, MACVLAN, or userland networking.
- Configuration on the command line or in protobuf config files.
A single run with the README's basic flags plus a wall-time and memory limit looks like this:
nsjail -Mo --chroot / --user 99999 --group 99999 \
--time_limit 30 --rlimit_as 512 -- /usr/bin/python3 /tmp/job/main.py
nsjail fits server-side jobs: running submissions on a judge, running untrusted handlers per connection, or fuzzing. The protobuf config makes policies reviewable files rather than long command lines.
bubblewrap
bubblewrap starts from a different problem. Its README explains that tools like Docker are not suitable to hand to unprivileged users, because that access can easily become root on the host. bubblewrap uses user namespaces so that any user can build a sandbox without privileges; the README notes the old setuid mode has been removed.
Its design is minimal by intent. Each sandbox starts in a new, completely empty mount namespace whose root is a tmpfs, and you build up the filesystem with command-line options. It turns off setuid binaries with PR_SET_NO_NEW_PRIVS. Flatpak is its best-known user.
The README is unusually clear about responsibility: bubblewrap "is not a complete, ready-made sandbox with a specific security policy", and the protection between sandbox and host "is entirely determined by the arguments passed to bubblewrap". Whatever builds those arguments owns the security model.
Its limitations section adds specifics, such as needing --new-session (unless a seccomp filter blocks TIOCSTI) to prevent command injection into the outer terminal, and noting that everything mounted into the sandbox, a D-Bus socket for example, can potentially be used to escalate privileges.
A minimal job sandbox:
bwrap --ro-bind /usr /usr --symlink usr/lib /lib --symlink usr/lib64 /lib64 --symlink usr/bin /bin \
--proc /proc --dev /dev --tmpfs /tmp --bind /tmp/job /job --chdir /job \
--unshare-all --die-with-parent --new-session --clearenv \
/usr/bin/python3 main.py
--unshare-all includes the network namespace, so the job has no network unless you add --share-net. --clearenv keeps your environment variables, and any secrets in them, out of the sandbox.
bubblewrap has no built-in time or memory limits; pair it with cgroups or systemd-run, or with a timeout in the caller. bubblewrap fits as a building block inside a larger program that knows exactly what it wants to expose: a desktop app framework, a build tool, or your own job runner.
Firejail
Firejail's README describes "a lightweight security tool intended to protect a Linux system by setting up a restricted environment for running (potentially untrusted) applications". It is an SUID program using namespaces, seccomp-bpf and Linux capabilities, works alongside SELinux or AppArmor, and integrates with cgroups.
Its distinguishing feature is a large library of ready-made profiles for common desktop applications such as Firefox, Chromium and VLC. You run firejail firefox and get a profile that someone has already tuned. Options like --net=none and the private-* family (private home, /tmp, /dev, /etc) restrict what a program sees.
The SUID design is the main trade-off to weigh for server use. A setuid-root program is itself attack surface for local users. Firejail has had privilege escalation bugs in that code, for example CVE-2022-31214, where a crafted fake container accepted as a join target by the setuid-root program in version 0.9.68 let a local attacker gain root. Bugs get fixed, but a design that needs root to start a sandbox carries that class of risk by construction.
Firejail fits desktop hardening: running a browser, a media player or a document viewer with less access than your user account has.
Side by side
| nsjail | bubblewrap | Firejail | |
|---|---|---|---|
| Built for | Server-side jobs, network services, fuzzing | A low-level building block for unprivileged sandboxes | Desktop application hardening |
| How it starts | A standalone binary you run with the options or config you choose | Unprivileged, via user namespaces | A SUID root program |
| Policy lives in | Command line or protobuf config; Kafel for syscalls | The caller's command line | Bundled and custom profiles |
| Resource limits | Built in (rlimits, cgroups, wall time) | Not built in | cgroup integration |
| Ships a ready-made policy | No | No, by design | Yes, per application |
For agent-written code
If you are running code an AI agent wrote on your own Linux hosts:
nsjailis the closest fit of the three: one jailed process per job, limits built in, policies in files you can review.bubblewrapis a good foundation if you are writing your own runner and want full control, as long as you also add limits and treat every bind mount as an exposure.- Firejail's profiles target desktop apps, and its SUID design is a poor fit for a multi-tenant job runner.
Whichever you choose, the kernel stays shared. For code nobody has read, from users you do not know, a boundary with its own kernel is worth considering; see containers, gVisor and Firecracker compared.
Sandscale runs each run in its own Firecracker microVM for that reason. The habits from this post still apply inside any sandbox: no secrets in the environment, no network unless the task needs it, and a time limit.
Originally published on the Sandscale blog.
Comments
No comments yet. Start the discussion.