Sandboxing is the practice of running code in an isolated environment that gets only the access it needs, so a crash, a bug or an attack stays inside the sandbox instead of reaching the host. The sandbox environment is the boundary: it decides which files, processes, network paths, system calls and resources the code can touch, and everything outside it stays a trusted environment.
Browsers sandbox the code that renders web pages, CI systems sandbox pull-request builds, and AI agents run generated code in a sandbox before it touches anything real. This page covers what a sandbox is, the two meanings of "sandbox environment", what sandbox testing involves, the layers of isolation from process permissions up to separate hardware, what a sandbox doesn't protect against, and how to pick one.
What is a sandbox?
A sandbox controls what the code inside it can see, what it can do and how much it can use. What it sees is the set of files, processes and network interfaces that exist from its point of view. What it can do is set by the system calls and privileges it has, and limits on CPU, memory and process count cap how much it uses.
The code inside is quarantined code. You don't trust it to behave, so you design the sandbox around the worst thing it could do. That might be a parser fed a malicious file, a dependency with a compromised release, a customer's script or a command an AI model wrote a second ago.
The strength of a sandbox comes down to its attack surface: how much host code the quarantined code can reach and try to break. A process with access to every Linux system call has a large surface. A virtual machine guest that only talks to a small device model has a much smaller one. Each isolation layer below trades that surface against performance and compatibility.
What is a sandbox environment?
A sandbox environment is an isolated place where code or changes run without affecting production systems or the host machine. Security teams use one to contain untrusted code and to analyze malware. Development teams use one to test changes, and payment and API providers offer sandboxes where test calls don't move real money or touch live accounts.
In security, the term means a sandbox that contains code you don't trust, such as a web page's scripts, a suspicious email attachment, a build from an outside pull request or a command an AI agent just generated. Sandbox security comes from the isolation layers described further down this page, from seccomp filters up to separate hardware.
Everywhere else, a sandbox is somewhere to try things without consequences. For developers that's usually a non-production copy of an application, often built per branch from containers or a VM and deleted afterwards. Salesforce sandboxes, for example, are copies of a production org that range from metadata only to a full copy of production data. API vendors run test versions of their APIs with separate keys and test accounts: payments created in a Stripe sandbox aren't processed by card networks, and the PayPal sandbox uses fictitious accounts in place of real PayPal users. Cloud teams give engineers a separate account or project to experiment in, and AWS's multi-account guidance groups these under a Sandbox OU, typically disconnected from internal networks and given a spending budget.
The words "sandbox" and "staging" are often used loosely, and some vendors call a full copy of production a sandbox. Where a team has both, a testing sandbox is usually for experiments: it's often disposable and may run on fake or partial data. Staging is usually kept as close to production as possible and used for the final checks before a release.
Security sandbox vs. test sandbox
"Sandbox environment" means two different things depending on who says it.
Security sandbox | Test sandbox | |
|---|---|---|
Purpose | Contain untrusted or risky code | Try changes without touching production |
What it isolates | Privileges, kernel access, network, resources | Data, side effects, third-party calls |
Typical threat | Malicious or compromised code | Bugs, bad migrations, real payments sent by accident |
Examples | Browser renderer, gVisor, microVMs | Payment provider test mode, a per-branch copy of an app |
They overlap more than the names suggest. A test sandbox is a sandboxed environment for data: fake card numbers, a copy of the database, outbound email captured instead of sent. A security sandbox is the same idea applied to privileges. A preview environment that builds and runs every pull request needs both, because it holds a copy of your app and runs code nobody has reviewed yet.
Sandbox testing
Sandbox testing means running software or files inside a sandbox and watching what they do before they get near anything real. In security it usually means malware detonation: a suspicious file is opened in an instrumented VM that holds no real data, and the sandbox records the processes it starts, the files it writes and the hosts it tries to contact.
In development it means isolated test runs. That might be integration tests against a vendor's API sandbox, or a test suite that runs in a throwaway environment so a failed migration or a stray email never reaches production or a customer.
The rest of this page is about security sandboxes: the layers of isolation they're built from, how to sandbox a command on Linux, where each kind is used, and what a sandbox doesn't protect against.
Layers of sandboxing, from weakest to strongest
Each layer adds a boundary the code has to get through. Stronger layers usually wrap the weaker ones rather than replace them.
The smallest sandbox is a process with less power: a non-root user, dropped capabilities, NoNewPrivileges so setuid binaries can't raise it back, and a seccomp filter that blocks system calls the program never needs. The kernel documentation states the limit directly: "System call filtering isn't a sandbox." A seccomp filter reduces the kernel surface the process can reach, but it doesn't hide files or the network.
Namespaces change what a process can see. The namespaces(7) man page lists separate namespaces for mounts, process IDs, network stacks, users, hostnames, IPC, cgroups and clocks.
Cgroups limit how much it can use. With cgroups v2, memory.max caps memory and pids.max stops a fork bomb. Tools like bubblewrap and nsjail are built from these pieces.
A container runtime combines namespaces, cgroups, seccomp and capability drops into one package with an image format. Every container still shares the host kernel, so a kernel bug or a runtime bug can become an escape.
CVE-2019-5736 let a malicious container overwrite the host's runc binary and run commands on the host. Container security basics and container hardening shrink that risk without removing it.
gVisor puts an "application kernel" between the workload and the host. Its Sentry component implements Linux system calls itself, so the workload's calls never go straight to the host kernel. gVisor's docs list the cost: "reduced application compatibility and higher per-system call overhead."
A microVM is a virtual machine with almost everything removed. Firecracker uses KVM and, in the project's words, "excludes unnecessary devices and guest-facing functionality to reduce the memory footprint and attack surface area of each microVM." Firecracker's specification gives 125 ms or less from the InstanceStart API call to the guest's /sbin/init, measured with a minimal kernel and root filesystem and the serial console off. It also gives 5 MiB or less of VMM memory overhead for a microVM with 1 vCPU and 128 MiB of RAM, plus the guest's own memory.
Cloud Hypervisor lists minimal emulation as a design goal. Kata Containers runs workloads in lightweight VMs that, per its README, "feel and perform like containers." The guest gets its own kernel, so a guest kernel bug stays in the guest.
A virtual machine under a hypervisor such as QEMU with KVM emulates a complete machine. The hardware boundary is the same as a microVM's, but the larger device model means more host-side code for an attacker to probe, and a full guest boots more slowly.
Dedicated bare metal hosts, or air-gapped machines for the worst cases, take shared CPUs, caches and memory out of the picture, which none of the other layers do.
Layer | Boundary | Shares host kernel | Startup cost | Typical use |
|---|---|---|---|---|
Process permissions, seccomp | Kernel permission checks | Yes | None | Hardening your own services |
Namespaces, cgroups | Kernel views and limits | Yes | Negligible | Build tools, desktop app sandboxes |
Containers | Namespaces, cgroups, seccomp combined | Yes | Low | Packaging and running trusted apps |
User-space kernel (gVisor) | Application kernel in user space | No direct syscalls | Low | Multi-tenant containers |
MicroVM (Firecracker, Cloud Hypervisor) | Hardware virtualization, minimal devices | No | Low (Firecracker spec: 125 ms or less to guest init) | Serverless, untrusted code, AI agents |
Full VM (QEMU/KVM) | Hardware virtualization, full devices | No | Higher (full guest boot) | Mixed OS workloads, malware analysis |
Separate hardware | Physical | No | Provisioning time | Regulated or hostile workloads |
The stronger layers add defense in depth of their own. On top of its application kernel, gVisor's docs say it "employs rule-based execution to provide defense-in-depth." Firecracker's production guide says to start it through its jailer, which adds namespace isolation and drops privileges, and to keep its default seccomp filters.
Sandboxing on Linux in practice
The examples below ran on Ubuntu 24.04 with systemd 255. Both share the host kernel, so they suit limiting your own code. Hostile code needs one of the stronger layers above.
Namespaces with unshare
unshare runs a command in new namespaces. In a new network namespace, the host's interfaces disappear and only a loopback device is left, and it's down:
sudo unshare --net ip -brief linklo DOWN 00:00:00:00:00:00 <LOOPBACK>Anything that needs the network fails, even DNS:
sudo unshare --net curl -sS --max-time 5 https://example.comcurl: (6) Could not resolve host: example.comA new PID namespace hides every other process on the host. The command becomes PID 1 of its own process tree:
sudo unshare --pid --fork --mount-proc ps -e -o pid,comm PID COMMAND 1 ps--fork is required so the command runs as a child inside the new PID namespace, and --mount-proc mounts a fresh /proc so ps reads the new namespace instead of the host's.
A sandboxed command with systemd-run
systemd exposes the same kernel features, plus seccomp and cgroup limits, as unit properties. systemd-run applies them to a one-off command:
sudo systemd-run --unit=sbx-demo --quiet --pipe --wait \ -p PrivateNetwork=yes \ -p ProtectSystem=strict \ -p ProtectHome=yes \ -p PrivateTmp=yes \ -p NoNewPrivileges=yes \ -p SystemCallFilter=@system-service \ -p SystemCallErrorNumber=EPERM \ -p MemoryMax=256M \ -p TasksMax=64 \ bash -c ' ip -brief link touch /etc/sbx-test touch /tmp/sbx-test && echo "/tmp is writable" mount -t tmpfs none /mnt grep -E "NoNewPrivs|Seccomp:" /proc/self/status cat /sys/fs/cgroup/system.slice/sbx-demo.service/memory.max cat /sys/fs/cgroup/system.slice/sbx-demo.service/pids.max 'lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>touch: cannot touch '/etc/sbx-test': Read-only file system/tmp is writablemount: /mnt: permission denied. dmesg(1) may have more information after failed mount system call.NoNewPrivs: 1Seccomp: 226843545664The command runs as root, and it still can't write to /etc or mount a filesystem. Each line maps to one property:
PrivateNetwork=yesgives it a network namespace with only loopback.ProtectSystem=strictmounts the whole filesystem read-only, apart from/dev,/procand/sys.PrivateTmp=yesgives it a private, writable/tmp, andProtectHome=yesmakes/home,/rootand/run/userempty and inaccessible.SystemCallFilter=@system-serviceis a seccomp allowlist of the calls a typical service needs.mountisn't in it, so the call fails withEPERM.Seccomp: 2means a filter is active.MemoryMaxandTasksMaxset the cgroup'smemory.maxandpids.max.
To check your existing services, run systemd-analyze security. It gives each service an exposure estimate from 0.0 to 10.0, and systemd-analyze security <unit> lists every setting it checked for that unit.
Where sandboxes are used
Use case | What runs inside | Common layer |
|---|---|---|
Web browsers | Page rendering and scripts from any site | OS process sandbox |
Malware analysis | Samples that are known or suspected to be malicious | Full VM or separate hardware |
CI/CD | Builds and tests from pull requests, including forks | Containers or VMs per job |
Serverless | Customer functions from many tenants on shared hosts | MicroVMs or user-space kernels |
Plugins and extensions | Third-party code inside your app | WebAssembly or process sandbox |
AI agents | Code and shell commands a model generated | MicroVMs or containers with strict egress |
Chromium's sandbox design doc describes the browser case: renderers are the sandboxed "target" processes, and the browser process is the "broker" that sets policy and performs approved actions for them. Firecracker came from the serverless case. Its README says it was developed at Amazon Web Services for services like AWS Lambda and AWS Fargate.
AI agents are the most recent of these use cases. The code is written seconds before it runs, nobody reviews it, and a prompt injection in a web page or a file can steer what it does. Run the agent as a quarantined agent: an AI sandbox should be built on the assumption that the model will at some point run a command you wouldn't approve.
What a sandbox doesn't protect against
Isolation can be escaped. Kernel bugs can break namespace and container isolation. Container runtime bugs can too: CVE-2019-5736 let a malicious container overwrite the host's runc binary. Bugs in a hypervisor or VMM can break virtual machines. A stronger layer gives an attacker less to work with, but it doesn't rule out an escape, so keep the host kernel and the runtime patched.
Side channels are a separate gap. Meltdown and Spectre let code read memory it was never given by measuring how the CPU behaves. The Meltdown researchers list cloud hosts that run containers on one shared kernel among the setups Meltdown affected. For hosts that run several tenants, Firecracker's production guide recommends disabling simultaneous multithreading and Kernel Samepage Merging, and running each tenant in its own Firecracker process.
A sandbox also does nothing about what you put inside it. Code in a sandbox can use any API token, mounted directory or database password it finds there, and it works just as well for the code you didn't mean to run, so give each sandbox short-lived credentials scoped to its task, or none at all. If a sandbox can reach the internet, the code inside it can send out anything it can read; block outbound traffic by default and allow only the hosts the workload needs. And quarantined code that never leaves the sandbox can still use up the host's memory, CPU or process table, so set cgroup limits on memory, CPU and process count for every sandbox.
How to choose a sandbox
Start with who wrote the code and who else shares the machine. Then check what the workload needs from the kernel, and what startup time and density you can afford.
For your own services and other trusted code, systemd sandboxing properties and hardened containers are a good place to start. Your team's CI jobs can run in containers with seccomp, no privileged mode and one job per container. Once the code comes from customers or the internet and many tenants share the hosts, move to gVisor or microVMs with one tenant per sandbox. AI agents running generated code need microVMs, default-deny egress and no long-lived credentials. Malware samples go in full VMs or on separate hardware, with a simulated network or none at all.
When two options fit, compare how much host code each one exposes. A container with a tight seccomp profile can expose less than a VM with a large device model. For multi-tenant code from strangers, though, a shared kernel is the boundary most likely to fail. Base the choice on the most dangerous code the sandbox could end up running, including code that arrives through a compromised dependency or a prompt injection.