Skip to content

feat(host): share package caches with Firecracker sandboxes - #430

Merged
kalaspuff merged 31 commits into
mainfrom
fctl/802-firecracker-package-caches
Nov 7, 2026
Merged

kalaspuff merged 31 commits into
mainfrom
fctl/802-firecracker-package-caches

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Projects that share package caches got none on Firecracker hosts: Firecracker has no virtio-fs or virtio-9p, so the VMs share no filesystem with the host. Now each project's caches are a directory in the host's data directory, caches/<project>, as with Apple's container, and the host serves it to each VM over its vsock. Once a VM is up, beside its passt, the host runs fctl-agent share, a small 9P2000.L server of that directory on the VM's vsock port 1026, and fctl-agent vm-mount in the VM mounts the connection at /var/cache/fctl with the kernel's own 9p client (trans=fd, as WSL2 does over Hyper-V sockets). Several sandboxes of a project share the caches at once, and the existing sweep, measuring and emptying work on the directory unchanged. A server that fails to start, or a mount that fails, only leaves the session without caches, which its log says, and a running VM whose server is gone gets another and mounts it again.

Anything in a VM can reach that port, so the server serves nothing but the directory's files. It walks one name at a time with O_NOFOLLOW and refuses ., .. and names with /, so symlinks are never followed on the host and the VM resolves them against its own files. It opens only regular files and directories, and makes neither device nodes nor setuid or setgid files. Files belong to the host's user, and the VM sees them as the image user's (the uid and gid that vm-mount passes), because git and package managers check that their caches are theirs; changes of owner do nothing. Locks are the host's, on the file opened again for reading and writing (F_OFD_SETLK, which needs that), so two VMs' cargo runs wait for each other as two containers' do. The server outlives running out of descriptors or threads, losing only that connection, and serves at most 16 connections.

This needs 9p in the VMs' kernel, which hyperstructure/sandbox#15 added (merged) and its Kernel workflow published as sandbox-kernel:6.18.59-1, the tag this pins. A kernel without 9p (an older one in host.toml) still boots; the session's log then says the caches couldn't be mounted, and its package managers keep their own downloads.

I tested it in Firecracker nested in a QEMU VM on aarch64, and CI's Firecracker job runs it on x86_64 with the published kernel. The new ignored test, sandboxes_of_a_project_share_its_package_caches, boots two sandboxes of one project at once and checks what one writes the other reads, that the host has it, that the files are the image user's, that a lock one holds keeps the other waiting, and that a sandbox started again mounts the caches again. Unit tests cover the server's confinement, paged directory listings, renames, links, truncation, locks between two connections and its socket. CI's Firecracker job runs the new test, and runs it as root too, where the VMs run through the jailer (#434): a jailed VM's Firecracker connects from its chroot, as the jail's user, so the server listens there, on a socket given to that user. That passes as root in the lab as well.

Worth confirming

  • Decision: the server is a hidden fctl-agent share command, run with the host's static fctl-agent (the one it boots VMs with), so that it outlives the host's process as passt does and a host restart leaves the VMs' mounts working.
  • Decision: the VM mounts with cache=mmap: no page cache for reads and writes, so what one VM writes, another reads at once, at the cost of a trip over the vsock per read of up to 512 KiB.
  • Assumption: confining the server in code is enough without Landlock or seccomp around it; a VM's process that speaks 9P to it can reach only the project's caches, which its sandbox mounts anyway.

Refs #345

fctl session


Note

Medium risk. It adds a 9P file server on the host that exposes a project's cache directory to every Firecracker VM, and changes how VMs start, resume and mount, though any failure only leaves a session without caches.

What it does. Firecracker sandboxes now share their project's package caches like Docker and Apple container ones do. The host runs a new fctl-agent share process beside each VM that serves the project's caches/<project> directory over 9P on the VM's vsock, and a new fctl-agent vm-mount in the VM mounts it at /var/cache/fctl. A server or mount that fails, or a resumed VM whose server stopped with it, is served and mounted again at the next start, and otherwise the session keeps its own downloads. Docs, the kernel pin and CI are updated to match.

Kernel with 9p. The VMs need the sandbox-kernel:6.18.59-1 kernel from hyperstructure/sandbox#15; an older kernel still boots but the caches can't be mounted. Anything in a VM can reach the project's cache directory, which docs/security.md now says.

fctl review's first look at 48c260a · 20 files, +2292 −54 · Review agent settings

fctl-sh Bot added 17 commits November 6, 2026 18:11
The firecracker driver's test that boots a VM only ran by hand, on
aarch64 under QEMU. GitHub's x86_64 runners have KVM: a CI job builds a
static fctl-agent and runs the driver's ignored tests there, with
Ubuntu's passt and its AppArmor profile, and the kernel hosts pull,
which the test now takes unless FCTL_TEST_FIRECRACKER_KERNEL names one.

Refs #345
… lacks

Ubuntu 24.04's passt (0.0~git20240220) refuses --hostname, which came
later, so no Firecracker VM got a network there. The VM names itself, so
passt's DHCP needs no hostname.
…Docker does

Ubuntu's /etc/resolv.conf names only systemd-resolved's 127.0.0.53, and
the passt Ubuntu 24.04 has forwards no DNS to a resolver on the host's
loopback with --no-map-gw, so VMs resolved nothing there. Where the
host's resolv.conf names only loopback resolvers, the host passes passt
the servers systemd-resolved asks, as Docker does for its containers.
…t reach

systemd-resolved lists a link-local IPv6 server with its interface
(fe80::1%eth0), which --dns can't carry: without it, passt finds a
server itself rather than get one it can't reach.
…resolved's stub

The --dns passt took doesn't set where its forwarder sends queries in
Ubuntu 24.04's passt, so VMs still resolved nothing there. As Docker
gives its containers, the VM now asks the host's nameservers itself
(fctl.dns= on its kernel's command line), but for those on the host's
loopback, and where only those are left, the servers systemd-resolved
asks. With none, it asks passt's forwarder, as before.
…r vsock

Firecracker's VMs share no filesystem with the host, so projects that
share package caches got none there. The host now runs fctl-agent share
beside each VM, as it runs passt: a 9P2000.L server of the project's
cache directory on the VM's vsock port 1026, confined to that directory
(one name at a time, no symlinks followed, no devices or setuid files,
locks as the host's). Once the VM is up, fctl-agent vm-mount mounts the
connection at /var/cache/fctl with the kernel's 9p client (trans=fd), as
the image's user's files, so several sandboxes of a project share their
caches at once.
…u's passt takes

On Ubuntu 24.04 no Firecracker VM got a network: passt's AppArmor
profile there refuses to accept the connection Firecracker makes for the
VM (accept4 fails with EACCES, and nothing is logged), so the VM's frames
waited in passt's backlog. CI's x86_64 runners showed it.

Now the VM's init listens on its vsock port 1025, and the host, once the
VM is up, connects to it and hands passt the connection with --fd, which
passt reads without accepting anything. passt exits when the VM closes
the connection, as it stops, rather than linger until the next start,
and the host links a running VM again when its passt is gone.

The test's diagnostics that found it (passt under strace, its trace and
capture) are gone; on a failure it still says what passt, the VM's
console and its network say.
…makes the file

passt makes its pid's file as it reads its options and writes the pid
only once it has set itself up, so a passt that failed in between left
the VM without a network while the host took it as linked. The host now
waits for the pid; the test's stand-in passt makes the file first, as
passt does, and one that fails then gets no link.
…fctl/802-firecracker-package-caches

# Conflicts:
#	crates/fctl-host/src/driver/firecracker/vm.rs
#	crates/fctl-proto/src/vm.rs
hyperstructure/sandbox#15 publishes it; the VMs mount their project's
package caches with its 9p client.
@fctl-review

This comment has been minimized.

Comment thread crates/fctl-agent/src/share/mod.rs Resolved
Comment thread crates/fctl-agent/src/share/mod.rs Resolved
…ers and full fd tables

- A write lock on a file the VM opened to read failed (fcntl's locks take
  a file open for writing), as flock -x on one does: the server now takes
  a file's locks on it opened again for reading and writing, where the
  host's user may.
- A server out of file descriptors returned from its loop and exited, and
  nothing served the VM again. It now refuses that connection, pauses
  after errors, serves at most 16 connections, and the host serves a
  running VM again, which mounts the new server, when its server is gone.
- A server that couldn't start failed the VM's start. The host now serves
  the caches once the VM is up, as it links its network, and a failure
  only costs the session its caches, which its log says.
- The test that checks a lock between two VMs held its lock's program only
  until the host dropped its handle, which stopped it: it passed or failed
  by timing.
- cli.md, sandbox-images.md and security.md say Firecracker's VMs get the
  caches, and how the server keeps to their directory.
@fctl-sh

fctl-sh Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

On the docs finding in the review's summary: fixed in 299b3de. docs/cli.md says sessions get caches with the Docker, Apple container or Firecracker driver (not Lima), and that Firecracker's VMs mount them as they boot; docs/sandbox-images.md names Firecracker among the drivers that mount them (over 9P, as the image's user's files); and docs/security.md now says the VMs share no filesystem with the host but that, in projects with caches, the host serves the project's cache directory to each VM over 9P on the vsock, which anything in the VM can reach, and how the server keeps to that directory.

@fctl-review

This comment has been minimized.

Comment thread crates/fctl-agent/src/share/mod.rs Resolved
…nd never fail its start for them

- A running VM's start fetched the static fctl-agent before it checked
  whether its share ran, so a fetch that failed failed a healthy VM's
  start; the host now checks first, and a failure costs only the caches.
- A server that didn't make its socket in time, or whose mount failed,
  stayed, so the VM never got its caches until it stopped; the host now
  stops it, and the next start serves them again.
- A session that panicked kept its connection's slot, and a thread the
  system refused ended the server; the slot now goes back however the
  session ends, and a refused thread costs that connection.
fctl-sh Bot added 4 commits November 6, 2026 20:30
… as root in CI

passt opens its pid file as it starts, as root but for its capabilities
on a host that runs as root, then becomes nobody: it can write only in a
directory root owns, and, under Ubuntu's AppArmor profile, only under
/tmp or home. Its pid now goes beside the VM's socket directory, in the
host's user's directory in /tmp, which both allow, and CI boots a
sandbox as root too.
… make first

Ubuntu's passt of 2024 drops to nobody before it opens its pid's file, and later ones open it as root without capabilities: on a host that runs as root, only /tmp lets both write. The name has a random part, recorded in the VM's directory.
… told

A failed link now forgets where passt's pid went, so the test reads the path from the stand-in, which says where it was told to write.
@fctl-sh

fctl-sh Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

The "Firecracker sandboxes" failure at 5d72ba9 is the one this draft expects: both VM tests fail pulling the kernel this pull request pins, ghcr.io/hyperstructure/sandbox-kernel:6.18.59-1 ("ghcr.io answered 404 … MANIFEST_UNKNOWN"). The registry has 6.18.59 but not 6.18.59-1 yet: that tag is the kernel with 9p, which hyperstructure/sandbox#15 adds and its Kernel workflow publishes once it merges. Going back to 6.18.59 would only move the failure to the cache test, whose kernel can't mount 9p. So this needs no change here: once sandbox#15 is merged and the tag is published, re-run the job (or push anything) and it should pass, as it does in the nested lab with that kernel built locally. Every other check passes.

kalaspuff pushed a commit to hyperstructure/sandbox that referenced this pull request Nov 6, 2026
## Summary

fctl's Firecracker sandboxes get no package caches, because Firecracker
has no virtio-fs or virtio-9p and the VMs share no filesystem with the
host. fctl's host will serve a project's caches over 9P2000.L on each
VM's vsock, and fctl-agent in the VM mounts that connection with the
kernel's own 9p client (`trans=fd`, as WSL2 does over Hyper-V sockets).
This adds 9p and its fd transport to `kernel/fctl.config`, as
`6.18.59-1` (the same Linux release, built differently), and
`kernel/check.sh` now checks both: 9p in `/proc/filesystems`, and a
mount over `/dev/null` that fails without the kernel saying it can't
find the transport.

I built this kernel for aarch64 and booted it in Firecracker nested in a
QEMU VM: `kernel/check.sh` passes with it and reports `no 9p` with
`6.18.59`, and two sandboxes of one project share their caches through
it, locks included, with fctl's change below. The Kernel workflow's run
on this pull request builds and boots both architectures.

The fctl pull request pins `sandbox-kernel:6.18.59-1`, so this one has
to merge first, and the Kernel workflow has to publish the tag, before
that one can.

- hyperstructure/fctl#430

## Worth confirming

- **Decision:** the version is `6.18.59-1` rather than a newer Linux
release, so that this change is only the configuration; the README keeps
`6.18.59` in its table as the tag fctl pinned before.

Refs hyperstructure/fctl#345

[fctl session](https://app.fctl.sh/s/ses_01M6S0DK83842CC62AW3X74BR4)

<!-- FCTL_REVIEW_SUMMARY -->
---

> [!NOTE]
> **Low risk.** It only adds kernel options and a boot check, and
publishes a new kernel tag that nothing uses until an fctl release pins
it; the existing `6.18.59` tag stays as it is.
>
> **What it does.** Adds 9p with its fd transport to the Firecracker
guest kernel, so that fctl can mount a project's package caches over the
VM's vsock. `kernel/VERSION` becomes `6.18.59-1`, so the Kernel workflow
publishes a new tag instead of changing the published one.
`kernel/check.sh` now also boots the kernel and checks that 9p and its
fd transport are there, and AGENTS.md and README.md describe the new
tag.
>
> **New kernel tag.** Merging publishes `sandbox-kernel:6.18.59-1`, and
moves the `6.18` tag to it. fctl has to pin `6.18.59-1` before its hosts
get 9p.
>
> <sub>[fctl
review](https://app.fctl.sh/s/ses_01M6S3XV411FX00HJD66HFM8Q7)'s first
look at
[`3db7282`](3db7282)
· 5 files, +30 −11 · [Review agent
settings](https://app.fctl.sh/p/fctl?tab=reviews)</sub>

<!-- /FCTL_REVIEW_SUMMARY -->

Co-authored-by: fctl-sh[bot] <343971779+fctl-sh[bot]@users.noreply.github.com>
@fctl-sh
fctl-sh Bot marked this pull request as ready for review November 6, 2026 22:23
@fctl-sh
fctl-sh Bot requested a review from kalaspuff as a code owner November 6, 2026 22:23
Base automatically changed from fctl/802-firecracker-hosts-package-caches-builds to main November 6, 2026 23:32
kalaspuff pushed a commit that referenced this pull request Nov 6, 2026
## Summary

A host that runs as root now runs each Firecracker VM through
Firecracker's jailer, so that a VM escape lands as a user of its own in
a chroot rather than as the host's user, who can read the runner's
token. Each VM gets a uid and gid of its own (from 1,900,000,000 up,
picked under a lock so that no other running VM's jail has it), a chroot
under `firecracker/jail/`, and a cgroup of its own (`fctl/<VM key>`, at
most 256 processes):

- **What the jail holds.** Hard links of the VM's disks (the images'
filesystems readable by all) and of its kernel and initramfs (copies
where they're private or on another filesystem, and links resolved
first). Its writable disk is lent to the jail's user while it runs,
noted beside the chroot where that user can't change the note, and given
back to root as the VM stops, so a later jail with the same uid owns
nothing of the host's; jails that a stopped or crashed host left go
before the next VM starts.
- **How the host reaches it.** The jail's user owns the chroot, so the
host never goes through it: once Firecracker has made its API and vsock
sockets there, the host opens each without following links, checks it's
a socket, and hard-links that inode into a directory of root's beside
the chroot, which the VM's socket path leads to. A jail can't send the
host to another VM's sockets.
- **What it needs.** The jailer comes with the pinned Firecracker, or
sits beside the one `host.toml` names (as `jailer` or as the release
names it). It needs cgroup v2 and a data directory on a filesystem that
allows device files; without either, or without a jailer, the driver
reports itself not ready with what to change. `jailer = false` in
`[firecracker]` turns it off, and hosts that don't run as root are
unchanged.

I verified it in Firecracker nested in a QEMU VM on aarch64:
`boots_a_sandbox` as root passes with both Ubuntu 24.04's passt and
Debian 13's, checks that Firecracker runs as a jail's user, and that the
sandbox's disk and the images' filesystems are root's again once their
VMs stop. CI's root step runs it jailed on x86_64, and runs the
stale-jail test as root, where it lends and takes back a disk. Unit
tests cover the jail's users, the jailer's arguments and where it's
found, the chroot's links and copies, linking only the jail's own
sockets, and removing stale jails. The docs (security, architecture,
`host.toml`, ADR 0070's consequences) say what a jailed VM can reach.

- #429

## Worth confirming

- **Decision:** the jailer is on by default wherever the host runs as
root; only `jailer = false` turns it off, and a root host without cgroup
v2 must set it to run VMs at all.
- **Decision:** the jails' users are bare uids and gids from
1,900,000,000 up, with no entries in `/etc/passwd`, chosen far from the
ranges people, system users and user namespaces take.
- **Decision:** passt stays outside the jail, as `nobody` (passt drops
to it itself when started as root), rather than as the jail's user.
- **Assumption:** a host runs as root by root installing the host
service (its systemd user unit in root's manager, with lingering);
nothing else here changes for that.
- **Follow-up:** package caches (#430) and snapshots (#431) don't know
about jails yet: a jailed VM's Firecracker can't connect to a cache
server's socket that root made, and a snapshot written inside the chroot
goes with it when the VM stops. Whichever of these merges after the
other needs that integration (or turns caches or snapshots off for
jailed VMs).

Refs #345

[fctl session](https://app.fctl.sh/s/ses_01M6S0DK83842CC62AW3X74BR4)

<!-- FCTL_REVIEW_SUMMARY -->
---

> [!WARNING]
> **High risk.** It changes how every Firecracker VM starts on hosts
that run as root, on by default, and it chowns disks and creates uids,
chroots, links and cgroups on the host, so a mistake could stop VMs from
booting or weaken the isolation it sets out to add.
>
> **What it does.** On hosts that run as root, each Firecracker VM now
starts through Firecracker's jailer as its own uid and gid in a chroot
and cgroup, so that a VM escape no longer lands as the host's user. The
jailer comes with the pinned Firecracker or from beside a configured
one, and `jailer = false` in `[firecracker]` turns it off. The host
links the VM's sockets, after checking them, into a root-owned directory
beside the chroot that the VM's socket directory leads to, hands
writable disks back when a jail ends, picks uids under a lock, and
removes stale jails. Since the last review, the stale-jail test also
lends a disk to a jail uid when run as root, and CI runs it as root.
>
> **On by default.** The jailer is on for every host running as root
unless `jailer = false`; it needs cgroup v2 and a data directory not
mounted `nodev`, and package caches and snapshots don't yet work with
jailed VMs.
>
> <sub>[fctl
review](https://app.fctl.sh/s/ses_01M6SDFTHBA4P30V9TM3Q6ZCD2)'s first
look at
[`6dca881`](6dca881)
· 9 files, +856 −36 · [Review agent
settings](https://app.fctl.sh/p/fctl?tab=reviews)</sub>

<!-- /FCTL_REVIEW_SUMMARY -->

---------

Co-authored-by: fctl-sh[bot] <343971779+fctl-sh[bot]@users.noreply.github.com>
kalaspuff
kalaspuff previously approved these changes Nov 6, 2026
… a jailed VM's caches in its jail

#429, this branch's base, was squash-merged into main with #434, the jailer: the tree is main's with this pull request's own changes applied again. A jailed VM's Firecracker connects to the cache server from its chroot, as the jail's user, so the server now listens there on a socket given to that user; CI's root step runs the caches test, jailed.
Main took #432 (builds) and #433 (images over their bases). Both pull requests' VM tests are kept, the caches test beside the build test, fctl-agent depends on hex and libc, and docs/cli.md's Firecracker paragraph says both that sandboxes mount their caches and that the host builds images.
@fctl-review

This comment has been minimized.

…, and link it into the jail

Under a long data directory, the cache server's socket in a jail's chroot passed the 108 bytes a socket's path may have, so jailed sessions ran without caches. The server now listens in the VM's socket directory, which is on the jail's filesystem, and the host links that socket into the chroot, given to the jail's user, in place of whatever the jail put there. The security docs' caches threat model names Firecracker's.
@fctl-sh

fctl-sh Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

On the review's summary finding (the package caches threat model leaves out Firecracker): fixed in bb99da2. docs/security.md now says that with Firecracker the caches are a directory on the runner that the host serves to each VM over 9P, so what it says about one session poisoning what the project's later sessions build with applies there too.

@fctl-review

This comment has been minimized.

kalaspuff
kalaspuff previously approved these changes Nov 7, 2026
@fctl-review

This comment has been minimized.

With snapshots (#431), a stopped sandbox resumes with its cache mount on a connection whose server stopped with the VM, so its package managers got 'Connection reset by peer'. A resume now serves the caches again and the VM mounts them in place of the old mount, as a running VM whose server is gone gets. docs/cli.md says that Firecracker keeps its caches in the same directory as Apple's container.
@kalaspuff
kalaspuff merged commit ab5e7ff into main Nov 7, 2026
11 of 12 checks passed
@kalaspuff
kalaspuff deleted the fctl/802-firecracker-package-caches branch November 7, 2026 00:19
@fctl-sh

fctl-sh Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

On the review's two summary findings, both fixed in 48c260a:

  • A resumed sandbox's dead cache mount: right, and it's what failed CI's Firecracker job once main had snapshots (feat(host): resume stopped Firecracker sandboxes where they were #431): "cat: /var/cache/fctl/npm/index: Connection reset by peer" in the second start. ensure now serves the caches again after a successful resume (serve_caches_again, the step a running VM whose server is gone already took), and vm-mount mounts them in place of the stale mount, as its comment says. The job now passes; its log shows "Serving the package caches of … again" after the resume, and the cache test passes both as the runner's user (resumed) and as root (jailed).
  • Cache sweep docs: docs/cli.md now says that with Apple's container and Firecracker the caches are a directory of the data directory, caches/<project id>, which Firecracker's sandboxes mount over 9P on their vsock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant