For an operator who serves a site's engines (docs/plan.md D71): the machine that runs a
language package's workers when the reader's browser cannot. The wire is docs/protocol.md
§8; this is the machine's side. The language packages come from anywhere: a flake of their
own, a directory built by playgrounds engines. Nothing here names a language.
Not built yet: the page's side (D71, S2), which connects a playground on a page to a
remote. Today a remote serves the conformance kit (playgrounds check <dir> --remote <url>).
What you are taking on
A remote runs code a stranger typed, on your machine. The jail makes that safe by stacking fences, and none of them is a virtual machine: every jail shares your kernel. A bug in the kernel's namespaces, cgroups, seccomp, Unix sockets, filesystems or any system call a jail may still make can give a reader the whole machine. The filter shrinks that surface; it does not remove it.
So:
- Run the remote on a machine that holds nothing else of value, or a virtual machine of its own, with no secrets, no credentials for other systems and no route to your private network. A machine you can wipe.
- Keep the kernel current. Most jail escapes in the world were kernel bugs, fixed in a point release.
- Do not start it on a host that runs other people's workloads without a VM between.
- gVisor, which puts a second kernel between a jail and yours, is planned (D71, S5) and not built.
Requirements
- Linux with cgroups v2 (the unified hierarchy) and unprivileged user namespaces
(
kernel.unprivileged_userns_cloneoruser.max_user_namespacesnot zero; Debian, Ubuntu 24.04+ and others have an AppArmor rule to lift too). The jails use namespaces and nothing needs root. - systemd, to delegate a cgroup to the service (
Delegate=). Without it the remote can run as a plain command with--cgroup <dir>naming a directory it owns. vm.panic_on_oomleft at 0 (the default). A jail that runs out of memory is killed by its own cgroup; a machine set to panic on any out-of-memory kill goes down with it.- Swap does not matter: each jail's cgroup has
memory.swap.maxat 0.
With NixOS
{
inputs.playgrounds.url = "github:playgroundkit/code-playgrounds";
inputs.my-language.url = "…"; # a flake that builds a language package
outputs = { nixpkgs, playgrounds, my-language, ... }: {
nixosConfigurations.remote = nixpkgs.lib.nixosSystem {
modules = [
playgrounds.nixosModules.default
({ pkgs, ... }: {
services.playgrounds-remote = {
enable = true;
engines = [
(playgrounds.lib.mkRemoteEngines pkgs [
my-language.packages.${pkgs.system}.default
])
];
origins = [ "https://example.org" ];
publicUrl = "wss://example.org/remote/";
tokenSecretFile = "/var/lib/secrets/playgrounds-ticket"; # openssl rand -hex 32
nginx.virtualHost = "example.org";
};
})
];
};
};
}
engines takes directories of <id>.<build>/ from anywhere. mkRemoteEngines writes one
from built packages (hosted: the package's own browser build runs under Node). A package that
brings a native program writes its own with playgrounds engines and lists its closure
(--closure). Several directories may be listed, so the old build stays while a site switches.
The module runs the service as a DynamicUser with no capabilities, a read-only system and a
private /tmp, IPAddressDeny=any (only allowFrom is reachable), Delegate= with
DelegateSubgroup=supervisor, and MemoryMax, TasksMax and CPUWeight for the jails
together (memoryMax, tasksMax, cpuWeight). It leaves out the hardening that bubblewrap
cannot live with (modules/_remote/service.nix lists it); do not add it, and expect
checks.remote-vm to fail when you do. nginx.virtualHost adds the proxy location below.
Options: listen (default 127.0.0.1:8081), trustedProxies (CIDRs whose X-Forwarded-For
is believed; loopback by default), limits (below), extraInstances (more instances of the
service on other addresses).
Without NixOS
playgrounds serve (the framework's CLI) is the built site and the remote on one port;
playgrounds-remote (packages.remote) is the remote alone. Both are the same program:
playgrounds-remote engines/ --sandbox bubblewrap --seccomp seccomp.bpf \
--listen 127.0.0.1:8081 --origin https://example.org \
--public-url wss://example.org/remote/ --secret-file ticket-secret \
--trusted-proxy 127.0.0.1/32
It needs bwrap (bubblewrap 0.13 or later) and the seccomp filter (packages.remote-seccomp,
built from packages/remote/jail/seccomp-gen.c with libseccomp). Run it as an unprivileged
user whose cgroup it may divide: under systemd, a unit with Delegate=memory pids cpu,
DelegateSubgroup=supervisor, and no hardening that blocks namespaces (the list in
modules/_remote/service.nix). The command works out which cgroup is its own, moves itself
into a supervisor leaf, and makes jail-<id> siblings for the jails; at start it removes
any jail-* left by a crashed predecessor.
--sandbox unsafe-process runs a plain process: it contains nothing, so it listens on
loopback only and is for developing against the wire.
The proxy
Put TLS and a rate-limited front in front. The remote reads Origin, a ticket if you require
one, and X-Forwarded-For. A proxy must:
- upgrade WebSockets (
UpgradeandConnection), withHTTP/1.1upstream; - drop
Cookie: the remote reads none, and the site's must not reach it; - set
X-Forwarded-Forto the client's address as the proxy saw it, never one the client sent (a remote believes it only from--trusted-proxy); - not buffer, and keep a read timeout above the remote's 25 s ping.
The rules for nginx and Caddy are in docs/hosts.md. The module's nginx.virtualHost writes
the nginx ones.
What a jail is
One per connection, made for the session and gone with it. bubblewrap makes:
- every namespace: user, mount, PID, network, IPC, UTS, cgroup. The network namespace is empty, with only a loopback;
--disable-userns: the jail cannot make a namespace of its own;- a root that is a read-only tmpfs holding the worker's closure (the store paths it needs),
the build's directory, a fresh
/procand/dev, and two small tmpfs mounts,/workand/tmp, whose pages count against the memory limit. Nothing of the host's/etc, home, sockets or other store paths is there; - no capabilities,
no_new_privs, a session of its own, and--die-with-parent.
Then a seccomp filter (seccomp-gen.c) that allows what Node, WebAssembly and native
compilers use and refuses the rest: namespaces (unshare, setns, clone with namespace
flags, clone3), mount and its relatives, ptrace, bpf, perf_event_open,
io_uring, keyctl, reboot, module and kexec calls, and every socket family except
AF_UNIX. Then a cgroup per jail that the supervisor owns (the jail cannot write it).
Limits
An engine may ask for less than the operator's caps (remote.json); what it does not ask
gets the defaults. All are enforced from outside the jail. A jail that crosses one is ended,
and the page is told which: close code 4507 with the word memory, cpu, processes,
disk, output, time or idle (busyMs and the wall clock are both time).
| Limit | Default | Cap | How |
|---|---|---|---|
| memory | 512 MiB | 4 GiB | memory.max, no swap, memory.oom.group; includes /work and /tmp |
| CPU | 300 s | 3600 s | the cgroup's usage, read every monitorMs; one CPU at a time (cpu.max) |
busyMs |
60 s | 600 s | the jail uses CPU that long and sends no frame |
| processes | 128 | pids.max, threads included |
|
| disk | /work 64 MiB, /tmp 16 MiB, one file 64 MiB |
tmpfs sizes, RLIMIT_FSIZE |
|
| open files | 256 | RLIMIT_NOFILE |
|
| output | 1 MiB/s for 5 s, or 32 MiB in one second | counted at the supervisor | |
| wall clock | 30 min | 2 h | per session |
| idle | 10 min | 1 h | no frame either way |
Admission is one decision per hello: jails at once (default the lesser of twice the CPU
count and the memory over 512 MiB, and never more than MemoryMax allows), perClient 8 and
perMinute 30 per address, a queue of 16 that waits at most 20 s (the page is told its place),
and sockets before their hello capped per address and in all. Set any of them in limits
(NixOS) or --limits-file: the fields of RemoteLimits in packages/remote/server.ts, with
defaults and caps as partial objects.
What a jail cannot do, and what tests it
checks.remote-vm starts two NixOS machines, server running the module behind nginx and
client as a reader. It needs kvm and fails loudly without. It runs a worker that tries
to get out, through the proxy, and fails unless every attempt is refused and nothing is left.
The attempts: gaining capabilities or privileges; reading the host's files, /proc/1/root,
the supervisor's process, the store beyond the closure, the supervisor's environment and open
file descriptors, the cgroup files; seeing another process; writing outside /work and
/tmp; following a symlink out; a socket to the outside, to the supervisor's port, to an
abstract Unix socket, or to DNS; seeing a network interface; and 23 system
calls the filter refuses. It also ends each limit (memory, a fork bomb, output, busy), checks
that two sessions share nothing, that the queue admits in order, that nothing outlives a
session, and that killing the supervisor leaves no process and no cgroup. A second instance
with a filter that allows sockets shows that the empty namespace and IPAddressDeny= hold
without it.
The check uses the framework's echo fixture and a test package, not a language, so a language
package's own checks decide that it runs hosted (playgrounds check <dir> --engines).
Running it well
- Logs are one line per session: package, build, worker, time, CPU, peak memory, bytes each way,
how it ended and what the process wrote to stderr (the engine's, up to 4 KiB). Never an
address, a ticket, a session id or a message.
GET /infosays what the remote serves. - Updates.
systemctl stopdrains: no new sessions, then 4512 to the rest after a few seconds. A new build of an engine is a new directory; list both while pages that name the old build exist. - The ticket secret signs what the site's backend hands a page. Rotate it by changing the
file and restarting; a ticket's
audispublicUrl, so one remote's tickets do not open another. - Without tickets any page whose origin is listed may use the remote, so anyone can, by
sending that
Originfrom a script. Require tickets when the machine's CPU is worth taking. - Watch the machine's memory,
pids.eventsof the unit, and the kernel's security announcements.