From 37194b5e9f2420e9abde5aa4088c91a1df319a41 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 19:41:59 +0000 Subject: egress through squid with an allowlist managed by fc-egress Instead of forwarding to addresses dnsmasq resolved for allowed names, nothing gets forwarded from fcbr0 any more (and no NAT). The guests use squid on 172.16.0.1:3128 as their proxy, which only allows CONNECT to port 443 of the domains in /var/lib/fc-egress/allowed. That list can be changed at any time with fc-egress (list, allow, deny, log); a path unit makes squid re-read it. DNS answers NXDOMAIN for everything. host.nix no longer takes allowedDomains. --- default.nix | 4 +- host.nix | 182 ++++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 138 insertions(+), 48 deletions(-) diff --git a/default.nix b/default.nix index 445ddfd..dcd337a 100644 --- a/default.nix +++ b/default.nix @@ -65,8 +65,10 @@ let networking.useDHCP = false; networking.resolvconf.enable = false; - # dnsmasq on the host, see host.nix. + # The way out is squid on the host, and DNS only says NXDOMAIN, see + # host.nix. environment.etc."resolv.conf".text = "nameserver 172.16.0.1\n"; + networking.proxy.default = "http://172.16.0.1:3128"; systemd.network.enable = true; systemd.network.wait-online.enable = false; systemd.network.networks.eth = { diff --git a/host.nix b/host.nix index 304e279..d190711 100644 --- a/host.nix +++ b/host.nix @@ -1,25 +1,100 @@ # Host side of the guests' network: a bridge with tap devices fctap0, -# fctap1, ... owned by the user running the VMs, a DHCP server and DNS -# resolver, and NAT to the outside world. Each running VM takes one tap +# fctap1, ... owned by the user running the VMs, a DHCP server, and an +# HTTP proxy as the guests' only way out. Each running VM takes one tap # device, so taps is the number of VMs that can run at the same time. # -# Egress: the guests can only resolve the names in allowedDomains (and -# their subdomains), and only reach the addresses those resolved to, and -# only on tcp port 443. Everything else from fcbr0 gets rejected. +# Egress: nothing gets forwarded from fcbr0. The guests go through squid +# at 172.16.0.1:3128 (their https_proxy), which only allows CONNECT to +# port 443 of the domains (and their subdomains) listed in +# /var/lib/fc-egress/allowed. The user can change that list at any time +# with fc-egress (see fc-egress help); squid picks up changes right away. +# DNS only answers NXDOMAIN, so the guests can't use it as a way out. # # usage (in the host's configuration.nix): -# imports = [ -# (import ./host.nix { -# user = "tv"; -# allowedDomains = [ "cache.nixos.org" "github.com" ]; -# }) -# ]; -# The host's firewall has to let the guests in on fcbr0: DHCP (udp 67) and -# DNS (udp and tcp 53) as input, and anything from fcbr0 (plus the return -# traffic) as forward; the table firecracker below does the filtering. -{ user, taps ? 8, allowedDomains ? [ ] }: { lib, pkgs, ... }: let +# imports = [ (import ./host.nix { user = "tv"; }) ]; +# The host's firewall has to let the guests in on fcbr0: DHCP (udp 67), +# DNS (udp and tcp 53) and the proxy (tcp 3128). +{ user, taps ? 8 }: { lib, pkgs, ... }: let tapNames = map (i: "fctap${toString i}") (lib.range 0 (taps - 1)); - domains = "/${lib.concatStringsSep "/" allowedDomains}/"; + dir = "/var/lib/fc-egress"; + allowed = "${dir}/allowed"; + + # The list holds squid dstdomain entries: .example.com matches the domain + # and its subdomains. fc-egress takes and shows plain domain names. + fc-egress = pkgs.writeShellScriptBin "fc-egress" '' + set -efu + export PATH=${lib.makeBinPath [ pkgs.bash pkgs.coreutils pkgs.gnugrep pkgs.gnused pkgs.systemd ]} + list=${allowed} + usage() { + echo "usage: fc-egress list" + echo " fc-egress allow DOMAIN..." + echo " fc-egress deny DOMAIN..." + echo " fc-egress log [JOURNALCTL-OPTION...]" + echo "Allowing a domain allows its subdomains, too." + } + check() { + for d; do + case $d in + ""|.*|*.|*..*|*[!a-z0-9.-]*) + echo "fc-egress: bad domain: $d" >&2 + exit 1 + esac + done + } + # Rewrite the list in place, as the path unit watches for writes. + update() { + tmp=$(mktemp) + trap 'rm -f "$tmp"' EXIT + "$@" > "$tmp" + cat "$tmp" > "$list" + } + cmd=''${1-help} + shift || : + case $cmd in + list) + sed 's/^\.//' "$list" + ;; + allow) + check "$@" + update sh -c '{ cat "$0"; printf ".%s\n" "$@"; } | sort -u' "$list" "$@" + ;; + deny) + check "$@" + update sh -c 'grep -vxF -e "$1" "$0" || :' "$list" "$(printf '.%s\n' "$@")" + ;; + log) + # squid's access log: time, duration, client, result/status, bytes, + # method, URL + journalctl -u fcbr0-squid -o short-iso "$@" | grep --line-buffered ' TCP_' + ;; + help|-h|--help) + usage + ;; + *) + usage >&2 + exit 1 + esac + ''; + + squidConfig = pkgs.writeText "fcbr0-squid.conf" '' + http_port 172.16.0.1:3128 + pid_filename /run/fcbr0-squid/squid.pid + cache deny all + coredump_dir none + access_log syslog:daemon.info + cache_log /dev/null + logfile_rotate 0 + shutdown_lifetime 1 seconds + + # -n: don't look up the names of IP addresses, else a guest could get + # to any address whose PTR record names an allowed domain. + acl allowed dstdomain -n "${allowed}" + acl https port 443 + http_access deny !CONNECT + http_access deny !https + http_access allow allowed + http_access deny all + ''; in { networking.interfaces = lib.genAttrs tapNames (_: { virtual = true; @@ -30,48 +105,31 @@ in { }; networking.bridges.fcbr0.interfaces = tapNames; - networking.nat = { - enable = true; - internalInterfaces = [ "fcbr0" ]; - }; - - # dnsmasq adds the addresses of allowed names to the set allowed when it - # resolves them. The set never forgets, so an address stays reachable - # until the table gets reloaded. This chain only rejects; for the - # packets it lets through, the host's own forward chain decides. + # Only rejects; for anything else the host's own forward chain decides. networking.nftables.enable = true; networking.nftables.tables.firecracker = { family = "inet"; content = '' - set allowed { - type ipv4_addr - } - chain forward { type filter hook forward priority filter; policy accept; - iifname "fcbr0" ct state { established, related } accept - iifname "fcbr0" ip daddr @allowed tcp dport 443 accept iifname "fcbr0" reject with icmpx admin-prohibited } ''; }; # A dnsmasq of its own, so it doesn't get mixed up with one the host might - # run otherwise. It forwards DNS queries for allowed names to the servers - # in the host's /etc/resolv.conf (server=/.../#) and answers NXDOMAIN for - # everything else (address=/#/). No AAAA records, as the guests only have - # IPv4. No cache, as dnsmasq only adds to the nftables set when it gets - # an answer from upstream, and the set is empty again after a reload. - # It only listens on fcbr0 (except-interface=lo, else it would also take - # 127.0.0.1:53 from a resolver the host runs there); bind-dynamic copes - # with fcbr0 not being there yet. + # run otherwise. DHCP, and DNS that answers NXDOMAIN for everything, so + # tools that resolve names fail fast. It only listens on fcbr0 + # (except-interface=lo, else it would also take 127.0.0.1:53 from a + # resolver the host runs there); bind-dynamic copes with fcbr0 not being + # there yet. systemd.services.fcbr0-dnsmasq = { wantedBy = [ "multi-user.target" ]; - after = [ "network.target" "nftables.service" ]; + after = [ "network.target" ]; serviceConfig = { RuntimeDirectory = "fcbr0-dnsmasq"; Restart = "on-failure"; - ExecStart = lib.escapeShellArgs ([ + ExecStart = lib.escapeShellArgs [ "${pkgs.dnsmasq}/bin/dnsmasq" "--keep-in-foreground" "--conf-file=/dev/null" @@ -81,13 +139,43 @@ in { "--dhcp-authoritative" "--dhcp-range=172.16.0.2,172.16.0.254,1h" "--dhcp-leasefile=/run/fcbr0-dnsmasq/leases" - "--cache-size=0" - "--filter-AAAA" + "--no-resolv" + "--no-hosts" "--address=/#/" - ] ++ lib.optionals (allowedDomains != [ ]) [ - "--server=${domains}#" - "--nftset=${domains}4#inet#firecracker#allowed" - ]); + ]; }; }; + + systemd.tmpfiles.rules = [ + "d ${dir} 0755 ${user} - -" + "f ${allowed} 0644 ${user} - -" + ]; + + # squid binds to 172.16.0.1, so it has to wait for fcbr0's address + # (network-addresses-fcbr0.service with scripted networking; else + # Restart= gets it there). It re-reads its config, including the list, + # on SIGHUP. + systemd.services.fcbr0-squid = { + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" "network-addresses-fcbr0.service" "systemd-tmpfiles-setup.service" ]; + serviceConfig = { + ExecStart = "${pkgs.squid}/bin/squid -N -d 1 -f ${squidConfig}"; + ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; + DynamicUser = true; + RuntimeDirectory = "fcbr0-squid"; + Restart = "on-failure"; + RestartSec = 1; + }; + }; + + systemd.paths.fcbr0-squid-reload = { + wantedBy = [ "multi-user.target" ]; + pathConfig.PathChanged = allowed; + }; + systemd.services.fcbr0-squid-reload = { + serviceConfig.Type = "oneshot"; + serviceConfig.ExecStart = "${pkgs.systemd}/bin/systemctl try-reload-or-restart fcbr0-squid.service"; + }; + + environment.systemPackages = [ fc-egress ]; } -- cgit v1.3.1