From d6c5c010f3764e2a7bc7c6b654ef7986d704199b Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 02:28:41 +0000 Subject: 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. --- pkgs/run-firecracker-vm/default.nix | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'pkgs') 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 \ -- cgit v1.3.1