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. --- modules/host/default.nix | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'modules/host/default.nix') diff --git a/modules/host/default.nix b/modules/host/default.nix index 144a23a..26120b8 100644 --- a/modules/host/default.nix +++ b/modules/host/default.nix @@ -56,12 +56,16 @@ # The taps' own addresses, for VMs without, are from 172.16.0.200 on (but # for vms'). # The host's firewall has to let the guests in on fcbr0: DHCP (udp 67), -# DNS (udp and tcp 53) and the proxy (tcp proxyPort, after the redirect). +# DNS (udp and tcp 53), the proxy (tcp proxyPort, after the redirect), +# and the checks (tcp checkPort, likewise). { users # VM name -> { user; ip; }: that VM's address (172.16.0.2 to .254), see # above. , vms ? { } , proxyPort ? 10443 + # Where nginx answers the guests' checks of their egress (their port 80 + # of 172.16.0.1 gets redirected there). +, checkPort ? 10080 # The guests' IPv6 /64 (on fcbr0, the host is ::1): each guest gets the # address from its MAC (SLAAC with EUI-64, see taps), for the tcp:// and # udp:// rules. Unique local, so forwarded connections get NATed, like @@ -492,6 +496,11 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ + lib.concatMapStrings (a: " [\"${a.ip}\"] = \"${a.user}\",\n") addresses + " },\n taps = {\n" + lib.concatMapStrings (t: " [\"${t.name}\"] = \"${t.user}\",\n") taps + # For checks: where no rule gets a guest (the host's own addresses + # on fcbr0, too; its others are out of reach as well, but unknown + # here). + + " },\n deny = {\n" + + lib.concatMapStrings (d: " \"${d}\",\n") (denyDestinations ++ [ "172.16.0.1" "${prefix6}1" ]) + " },\n}\n"; passAsFile = [ "guests" ]; } '' @@ -519,6 +528,7 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ env FC_EGRESS_RUN=${runDir}; env FC_EGRESS_VMS=${vmDir}; env FC_EGRESS_ADDR=${addrDir}; + env FC_EGRESS_HOSTS=${hostsDir}; events { worker_connections ${toString workerConnections}; } @@ -589,6 +599,27 @@ ${lib.concatMapStrings (t: " listen unix:${runDir}/decrypt-${t.name}.sock proxy_busy_buffers_size 128k; } } + + # The guests' checks of their egress (the guests' port 80 of + # 172.16.0.1 gets redirected here): POST /check, URIs one per line, + # true or false for each, a line each, by the same rules as their + # egress. Read-only, and only ever about the asking guest's own list. + limit_req_zone $binary_remote_addr zone=check:1m rate=10r/s; + server { + listen 172.16.0.1:${toString checkPort}; + access_log off; + client_max_body_size 64k; + client_body_buffer_size 64k; + limit_req zone=check burst=20 nodelay; + location = /check { + limit_except POST { deny all; } + content_by_lua_block { require("egress").check() } + } + location / { + default_type text/plain; + return 200 "POST /check: URIs (https://..., tcp://HOST:PORT, udp://HOST:PORT), one per line;\ntrue or false for each, whether this guest's egress allows it.\n"; + } + } } ''; in { @@ -646,7 +677,8 @@ in { # What the guests may do on the host's network: DHCP and DNS with our # dnsmasq, TLS to nginx (port 443 of any address, redirected to - # 172.16.0.1:proxyPort), and what their list's tcp:// and udp:// rules + # 172.16.0.1:proxyPort), checks of their egress (port 80 of 172.16.0.1, + # redirected to 172.16.0.1:checkPort), and what their list's tcp:// and udp:// rules # allow (set allow_TAP, see firewallUpdate), except to denyDestinations. # Nothing else, neither to the host (whose own services, like ssh, the # guests would otherwise reach at 172.16.0.1, where DNS sends every @@ -733,6 +765,7 @@ ${lib.concatMapStrings (t: '' chain prerouting { type nat hook prerouting priority dstnat - 1; iifname "fcbr0" meta nfproto ipv4 tcp dport 443 redirect to :${toString proxyPort} + iifname "fcbr0" ip daddr 172.16.0.1 tcp dport 80 redirect to :${toString checkPort} iifname "fcbr0" meta nfproto ipv4 meta l4proto { tcp, udp } dnat ip to ip daddr iifname "fcbr0" meta nfproto ipv6 meta l4proto { tcp, udp } dnat ip6 to ip6 daddr } @@ -751,7 +784,7 @@ ${lib.concatMapStrings (t: '' jump limits ip daddr { 172.16.0.1, 255.255.255.255 } udp dport 67 accept ip daddr 172.16.0.1 udp dport 53 accept - ip daddr 172.16.0.1 tcp dport { 53, ${toString proxyPort} } accept + ip daddr 172.16.0.1 tcp dport { 53, ${toString proxyPort}, ${toString checkPort} } accept jump refused } -- cgit v1.3.1