aboutsummaryrefslogtreecommitdiffstats
path: root/host.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 19:53:26 +0000
committertv <tv@krebsco.de>2026-11-16 20:57:42 +0100
commit9a6f2a20edd0c76a8fd3b58280d038a0aa3eec12 (patch)
tree56649bd2cc2b0db811022fb9a9e347e1d92e11b3 /host.nix
parent37194b5e9f2420e9abde5aa4088c91a1df319a41 (diff)
make egress transparent: SNI proxy instead of squid
The guests' DNS now resolves every name to 172.16.0.1, where nginx reads the server name from the TLS ClientHello and, if it's allowed, connects to port 443 of that name as resolved by the host. The guests need no proxy settings any more, and a faked server name can't get them to any other address. fc-egress keeps managing the list (now plain domains), and a reload regenerates nginx's map from it.
Diffstat (limited to 'host.nix')
-rw-r--r--host.nix143
1 files changed, 95 insertions, 48 deletions
diff --git a/host.nix b/host.nix
index d190711..d034ef3 100644
--- a/host.nix
+++ b/host.nix
@@ -1,26 +1,31 @@
# Host side of the guests' network: a bridge with tap devices fctap0,
-# 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.
+# fctap1, ... owned by the user running the VMs, a DHCP server, and a
+# transparent TLS 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: 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.
+# Egress: nothing gets forwarded from fcbr0. The guests' DNS resolves
+# every name to 172.16.0.1, where nginx takes TLS connections on port 443,
+# reads the server name (SNI) from the ClientHello, and if it's one of the
+# domains (or their subdomains) listed in /var/lib/fc-egress/allowed,
+# connects to port 443 of that name, as resolved by the host. The guests
+# need no proxy settings, and they can only ever reach the real addresses
+# of allowed names. The user can change the list at any time with
+# fc-egress (see fc-egress help); nginx picks up changes right away.
#
# usage (in the host's configuration.nix):
# 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).
+# DNS (udp and tcp 53) and TLS (tcp 443).
{ user, taps ? 8 }: { lib, pkgs, ... }: let
tapNames = map (i: "fctap${toString i}") (lib.range 0 (taps - 1));
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.
+ # One domain per line. fc-egress validates what it adds, sniConfig
+ # skips lines that are no domains, as the file belongs to the user.
+ domainPattern = "[a-z0-9-]+(\\.[a-z0-9-]+)*";
+
fc-egress = pkgs.writeShellScriptBin "fc-egress" ''
set -efu
export PATH=${lib.makeBinPath [ pkgs.bash pkgs.coreutils pkgs.gnugrep pkgs.gnused pkgs.systemd ]}
@@ -52,20 +57,20 @@
shift || :
case $cmd in
list)
- sed 's/^\.//' "$list"
+ cat "$list"
;;
allow)
check "$@"
- update sh -c '{ cat "$0"; printf ".%s\n" "$@"; } | sort -u' "$list" "$@"
+ 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' "$@")"
+ 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_'
+ # ALLOW|DENY, server name, guest, upstream, status, bytes sent and
+ # received (from the guest's view: received, sent), seconds
+ journalctl -u fcbr0-sni -o short-iso "$@" | grep --line-buffered -E ' (ALLOW|DENY) '
;;
help|-h|--help)
usage
@@ -76,24 +81,62 @@
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
+ # Generates nginx's map of allowed names (".example.com" matches the
+ # domain and its subdomains) from the list, and its resolver from the
+ # host's resolv.conf (nginx can't use that itself).
+ sniConfig = pkgs.writeShellScript "fcbr0-sni-config" ''
+ set -efu
+ export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.gawk pkgs.gnugrep ]}
+ cd /run/fcbr0-sni
+ grep -xE '${domainPattern}' ${allowed} |
+ awk '{ printf ".%s $ssl_preread_server_name:443;\n", $1 }' > allowed.map.tmp || :
+ mv allowed.map.tmp allowed.map
+ awk '
+ $1 == "nameserver" { s = s " " ($2 ~ /:/ ? "[" $2 "]" : $2) }
+ END {
+ if (s == "") { print "fcbr0-sni-config: no nameserver in /etc/resolv.conf" > "/dev/stderr"; exit 1 }
+ print "resolver" s " ipv6=off;"
+ }
+ ' /etc/resolv.conf > resolver.conf
+ '';
+
+ # Connections without an allowed server name go to the deny server,
+ # which closes them right away. That makes nginx log an error about the
+ # reset connection, so errors only get logged from crit up: what went
+ # wrong also shows in the access log, as $status (502: couldn't connect,
+ # 500: couldn't resolve, ...).
+ nginxConfig = pkgs.writeText "fcbr0-sni.conf" ''
+ daemon off;
+ pid /run/fcbr0-sni/nginx.pid;
+ error_log stderr crit;
+ worker_processes 1;
+ events {}
+ stream {
+ include /run/fcbr0-sni/resolver.conf;
+ log_format egress '$egress_verdict $ssl_preread_server_name $remote_addr $upstream_addr $status $bytes_sent $bytes_received $session_time';
+ access_log syslog:server=unix:/dev/log,nohostname,tag=fcbr0_sni egress;
- # -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
+ map $ssl_preread_server_name $egress_upstream {
+ hostnames;
+ default unix:/run/fcbr0-sni/deny.sock;
+ include /run/fcbr0-sni/allowed.map;
+ }
+ map $egress_upstream $egress_verdict {
+ unix:/run/fcbr0-sni/deny.sock DENY;
+ default ALLOW;
+ }
+
+ server {
+ listen 172.16.0.1:443;
+ ssl_preread on;
+ proxy_pass $egress_upstream;
+ }
+ server {
+ listen unix:/run/fcbr0-sni/deny.sock;
+ access_log off;
+ return "";
+ }
+ }
'';
in {
networking.interfaces = lib.genAttrs tapNames (_: {
@@ -118,8 +161,9 @@ in {
};
# A dnsmasq of its own, so it doesn't get mixed up with one the host might
- # run otherwise. DHCP, and DNS that answers NXDOMAIN for everything, so
- # tools that resolve names fail fast. It only listens on fcbr0
+ # run otherwise. DHCP, and DNS that resolves every name to 172.16.0.1
+ # without asking anyone else (local=/#/; it also makes AAAA queries get
+ # an empty answer instead of REFUSED, which can fail whole lookups). 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.
@@ -141,7 +185,8 @@ in {
"--dhcp-leasefile=/run/fcbr0-dnsmasq/leases"
"--no-resolv"
"--no-hosts"
- "--address=/#/"
+ "--address=/#/172.16.0.1"
+ "--local=/#/"
];
};
};
@@ -151,30 +196,32 @@ in {
"f ${allowed} 0644 ${user} - -"
];
- # squid binds to 172.16.0.1, so it has to wait for fcbr0's address
+ # nginx 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 = {
+ # Restart= gets it there). On reload, sniConfig regenerates the map and
+ # nginx re-reads its config on SIGHUP; if that fails, it keeps the old.
+ systemd.services.fcbr0-sni = {
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";
+ ExecStartPre = sniConfig;
+ ExecStart = "${pkgs.nginx}/bin/nginx -e stderr -p /run/fcbr0-sni -c ${nginxConfig}";
+ ExecReload = [ sniConfig "${pkgs.coreutils}/bin/kill -HUP $MAINPID" ];
DynamicUser = true;
- RuntimeDirectory = "fcbr0-squid";
+ AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
+ RuntimeDirectory = "fcbr0-sni";
Restart = "on-failure";
RestartSec = 1;
};
};
- systemd.paths.fcbr0-squid-reload = {
+ systemd.paths.fcbr0-sni-reload = {
wantedBy = [ "multi-user.target" ];
pathConfig.PathChanged = allowed;
};
- systemd.services.fcbr0-squid-reload = {
+ systemd.services.fcbr0-sni-reload = {
serviceConfig.Type = "oneshot";
- serviceConfig.ExecStart = "${pkgs.systemd}/bin/systemctl try-reload-or-restart fcbr0-squid.service";
+ serviceConfig.ExecStart = "${pkgs.systemd}/bin/systemctl try-reload-or-restart fcbr0-sni.service";
};
environment.systemPackages = [ fc-egress ];