From 4e480218c0777ba9342ce2edd3b00ff3488229b2 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 21 Nov 2026 13:08:29 +0000 Subject: host: guests can check what their egress allows, fc-egress check So a guest can know beforehand whether it gets somewhere, instead of finding out by timeouts and retries, and can tell which rule it lacks. nginx answers POST http://172.16.0.1/check (the guests' port 80 of 172.16.0.1 gets redirected to 172.16.0.1:checkPort, default 10080; plain HTTP anywhere else stays refused): URIs one per line, at most 100, and true or false for each, a line each, by the same rules as the egress: whole domains and path rules for https://HOST[/PATH] (port 443 only), and for tcp:// and udp://HOST:PORT the list's rules, with names as resolved for the guest's tap (its hosts file), but not to denyDestinations or the host's own addresses on fcbr0, and not to tcp port 443, which goes to the proxy. Read-only, about the asking guest's own list only (by its address, as for its egress), and rate-limited. In the guest, fc-egress check URI... asks that, over bash's /dev/tcp, as the guest may have no HTTP client, and prints true or false and the URI, a line each; exit status 0 if all are allowed, 1 if not, 2 on errors. --- pkgs/run-firecracker-vm/default.nix | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'pkgs') diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index c8a77e5..fa4abc2 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -42,6 +42,60 @@ let exec ${pkgs.socat}/bin/socat VSOCK-CONNECT:2:"$1" EXEC:"${pkgs.util-linux}/bin/mount -t 9p -o trans=fd -o rfdno=0 -o wfdno=1 -o version=9p2000.L -o msize=1048576$opts vsock9p $2",nofork ''; + # The guest's side of fc-egress (see modules/host): asks the host + # whether the guest's egress allows URIs, over bash's /dev/tcp, as the + # guest may have no HTTP client. + fc-egress = pkgs.writeShellScriptBin "fc-egress" '' + # usage: fc-egress check URI... + # Whether this guest's egress allows each URI (https://HOST[/PATH], + # tcp://HOST:PORT, udp://HOST:PORT): true or false, and the URI, a line + # each. Exit status 0 if all of them are allowed, 1 if not, 2 on + # errors. The whitelist itself is the host's, see fc-egress there. + set -eu + usage() { + echo "usage: fc-egress check URI..." >&2 + exit 2 + } + test "''${1-}" = check || usage + shift + test $# -gt 0 || usage + uris=("$@") + body= + for uri; do + case $uri in *$'\n'* | *$'\r'*) + echo "fc-egress: bad URI" >&2 + exit 2 + esac + body+=$uri$'\n' + done + export LC_ALL=C # for the length in bytes + if ! exec 3<>/dev/tcp/172.16.0.1/80; then + echo "fc-egress: can't reach the host" >&2 + exit 2 + fi + printf 'POST /check HTTP/1.0\r\nHost: 172.16.0.1\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s' "''${#body}" "$body" >&3 + read -r _ status _ <&3 || status= + while IFS= read -r line <&3 && test -n "''${line%$'\r'}"; do :; done + if test "$status" != 200; then + echo "fc-egress: the host answered ''${status:-nothing}" >&2 + cat <&3 >&2 + exit 2 + fi + all=0 + i=0 + while read -r verdict <&3; do + test $i -lt $# || break + printf '%s %s\n' "$verdict" "''${uris[i]}" + test "$verdict" = true || all=1 + i=$((i + 1)) + done + if test $i != $#; then + echo "fc-egress: $i answers for $# URIs" >&2 + exit 2 + fi + exit $all + ''; + # The system of the first n layers (the base system being layer 0), # with n + 1 images. evalSystem = n: import (pkgs.path + "/nixos") { @@ -134,6 +188,9 @@ let (pkgs.runCommand "mount.vsock9p" { } "install -D ${mountVsock9p} $out/bin/mount.vsock9p") ]; + # fc-egress check URI..., see fc-egress above. + environment.systemPackages = [ fc-egress ]; + # sshd, started on demand (a layer may turn that off), with the host # key the run script serves via 9P on vsock port 567 (--ssh-host-key; # not in the store), copied to /run/ssh before sshd (or its socket) -- cgit v1.3.1