aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/run-firecracker-vm/default.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-17 02:28:41 +0000
committertv <tv@krebsco.de>2026-11-17 02:28:41 +0000
commitd6c5c010f3764e2a7bc7c6b654ef7986d704199b (patch)
tree80fb6fb6f51618e2f81a150db2eb1cef998b9e21 /pkgs/run-firecracker-vm/default.nix
parentb050c6af6aedbe78201addb4abe2636ab075d14e (diff)
per-VM egress whitelists
Whitelists are now named, any number per user, in /var/lib/fc-egress/USER/LIST, and each VM uses one of them: run-firecracker-vm --egress LIST (default: default). The run script writes the list's name to /run/fc-egress/TAP when it takes the tap, and empties it when the VM exits; a list that doesn't exist allows nothing. A user's VMs can only use that user's lists. So the unit of policy is the tap (i.e. the running VM) rather than the user: nftables sets allow_TAP, nginx decrypt sockets decrypt-TAP.sock, egress_taps.lua maps guest addresses to tap and user. fcbr0-firewall updates a tap when its file in /run/fc-egress changes, and all of a user's taps when anything in the user's directory does. nginx's connection limit stays per user. fc-egress takes -l LIST (default: default), and has new commands lists and vms (running VMs' taps and their lists). Log lines now say user, tap and (nginx's) list: "ALLOW|DENY USER TAP LIST ...", kernel: "fc-egress ALLOW|DENY USER TAP". The old rules file, /var/lib/fc-egress/USER/allowed, is now just a list named "allowed"; rename it to "default" to keep using it.
Diffstat (limited to 'pkgs/run-firecracker-vm/default.nix')
-rw-r--r--pkgs/run-firecracker-vm/default.nix26
1 files changed, 23 insertions, 3 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix
index b6e5387..5b148da 100644
--- a/pkgs/run-firecracker-vm/default.nix
+++ b/pkgs/run-firecracker-vm/default.nix
@@ -306,6 +306,7 @@ in
pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# usage: run-firecracker-vm [--store FILE] [--store-size SIZE]
# [--nixpkgs DIR] [--nixpkgs-cache MODE]
+ # [--egress LIST]
# The guest gets the host's store, read-only, via 9P over vsock, and the
# directory xchg (in the current working directory) at /xchg, the same
# way. VMs started in the same directory share it. Also the same way,
@@ -319,15 +320,18 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# when the guest exits, or with --store, to FILE, which keeps it across
# boots (created if it doesn't exist, of --store-size, default 16G; the
# file is sparse, so that's only an upper limit).
+ # The guest's egress goes by the user's whitelist LIST (default:
+ # default), see modules/host and fc-egress.
set -efu
- export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]}
+ export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]}
usage() {
- echo "usage: $0 [--store FILE] [--store-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE]"
+ echo "usage: $0 [--store FILE] [--store-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST]"
}
args=
rw_store=
rw_store_size=16G
+ egress=default
nixpkgs=${lib.escapeShellArg (toString pkgs.path)}
while test $# -gt 0; do
case $1 in
@@ -336,6 +340,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
--store-size) rw_store_size=$2; shift 2 ;;
--nixpkgs) nixpkgs=$2; shift 2 ;;
--nixpkgs-cache) args="$args fc.9p566=cache=$2"; shift 2 ;;
+ --egress) egress=$2; shift 2 ;;
-h|--help) usage; exit ;;
*) usage >&2; exit 1
esac
@@ -396,6 +401,21 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
exit 1
fi
n=''${tap#fctap}
+ # The guest's whitelist: modules/host goes by the name in the tap's file
+ # in /run/fc-egress (emptied again when the guest exits, below).
+ vm=/run/fc-egress/$tap
+ if test -w "$vm"; then
+ if ! printf %s "$egress" | grep -qxE '[A-Za-z0-9_-]+'; then
+ echo "$0: bad list name: $egress" >&2
+ exit 1
+ fi
+ if ! test -f "/var/lib/fc-egress/$(id -un)/$egress"; then
+ echo "$0: no list $egress (see fc-egress), no egress" >&2
+ fi
+ printf '%s\n' "$egress" > "$vm"
+ else
+ vm=
+ fi
mac=$(printf '06:00:ac:10:%02x:%02x' $((n >> 8 & 255)) $((n & 255)))
# The 9P servers, for the store, /xchg, and nixpkgs, one each per VM.
@@ -415,7 +435,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
xchg=$!
RUST_LOG=off unpfs "unix!$tmp/vsock!_566" "$nixpkgs" > /dev/null &
nixpkgs_server=$!
- trap 'kill $store $xchg $nixpkgs_server; rm -rf "$tmp"' EXIT
+ trap 'kill $store $xchg $nixpkgs_server; rm -rf "$tmp"; if test -n "$vm"; then : > "$vm"; fi' EXIT
while ! test -S "$vsock"_565; do sleep 0.01; done
sed \