diff options
| author | tv <tv@krebsco.de> | 2026-11-16 19:34:15 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 20:57:38 +0100 |
| commit | ac5a3985620a59651e7c5bcb676f883e3fedd39b (patch) | |
| tree | 0cbb52fdee1ccd3724755a81731e7e79bebd5ebb /host.nix | |
| parent | 95c23de0d7fa75ab6eda02ec197b851e36125fbd (diff) | |
restrict the guests' egress to https to allowed domains
host.nix takes allowedDomains (default none). The guests' dnsmasq only
resolves those names and their subdomains, answers NXDOMAIN for anything
else, and adds the addresses it resolves to an nftables set. The new
table inet firecracker rejects everything from fcbr0 except tcp port 443
to addresses in that set. dnsmasq runs without cache, so it refills the
set after an nftables reload, and without AAAA records, as the guests
only have IPv4.
Diffstat (limited to 'host.nix')
| -rw-r--r-- | host.nix | 69 |
1 files changed, 55 insertions, 14 deletions
@@ -1,14 +1,25 @@ # 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 device, so taps is the -# number of VMs that can run at the same time. +# resolver, and NAT to the outside world. 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. +# # usage (in the host's configuration.nix): -# imports = [ (import ./host.nix { user = "tv"; }) ]; +# 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. -{ user, taps ? 8 }: { lib, pkgs, ... }: let +# 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 tapNames = map (i: "fctap${toString i}") (lib.range 0 (taps - 1)); + domains = "/${lib.concatStringsSep "/" allowedDomains}/"; in { networking.interfaces = lib.genAttrs tapNames (_: { virtual = true; @@ -24,19 +35,43 @@ in { 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. + 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 to the servers in the host's - # /etc/resolv.conf (and answers from its /etc/hosts). 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. 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. systemd.services.fcbr0-dnsmasq = { wantedBy = [ "multi-user.target" ]; - after = [ "network.target" ]; + after = [ "network.target" "nftables.service" ]; serviceConfig = { RuntimeDirectory = "fcbr0-dnsmasq"; Restart = "on-failure"; - ExecStart = lib.escapeShellArgs [ + ExecStart = lib.escapeShellArgs ([ "${pkgs.dnsmasq}/bin/dnsmasq" "--keep-in-foreground" "--conf-file=/dev/null" @@ -46,7 +81,13 @@ 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" + "--address=/#/" + ] ++ lib.optionals (allowedDomains != [ ]) [ + "--server=${domains}#" + "--nftset=${domains}4#inet#firecracker#allowed" + ]); }; }; } |
