From b5edf2b27f18f6576d3bcc00f65f95754aec1317 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 25 Nov 2026 23:06:46 +0000 Subject: rigs: the host's names, rig-egress, rigbr0, rigtapN, ... The host side loses the fc and firecracker, too: rig-egress (the command, also in the guests, and in log lines; /var/lib/rig-egress for the lists, /run/rig-egress for the VMs' choices of list), rig-proxy (the proxy, its user, /run/rig-proxy, and /var/lib/rig-proxy with the CA), rig-firewall, rig-dnsmasq, /run/rig-hosts, /run/rig-addr, the bridge rigbr0 and taps rigtapN, /etc/rig-taps and /etc/rig-vms, config.rig.vms, the nftables tables rigs and rigs-bridge, and egress.lua's RIG_EGRESS_*. The host's own configuration needs the new names, too: its firewall's trusted interface rigbr0, and config.rig.vms. --- README.md | 54 ++++----- modules/host/default.nix | 278 +++++++++++++++++++++++----------------------- modules/host/egress.lua | 16 +-- pkgs/run-rig/default.nix | 64 +++++------ tests/qemu/firecracker.sh | 4 +- tests/vm-host.nix | 66 +++++------ 6 files changed, 241 insertions(+), 241 deletions(-) diff --git a/README.md b/README.md index 66e9ed1..0156f0c 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ imports = [ ``` `users` says who may run VMs, and how many at the same time: each gets -that many tap devices. This sets up a bridge `fcbr0` (172.16.0.1/24, +that many tap devices. This sets up a bridge `rigbr0` (172.16.0.1/24, and fdfc:fc::1/64) with the taps, DHCP and DNS for the guests, the egress proxy, and nftables rules that keep the guests from reaching anything else, on the host, beyond, or each other. Each running VM has a fixed address, and @@ -47,11 +47,11 @@ Optional arguments: them run at the same time as the user has taps. Taps have addresses of their own, for VMs without one: from 172.16.0.200 on, so they stand out (but for VMs' there; at most 55 taps in all). The VMs' - addresses are also in `config.fc.vms.NAME` (`user`, `ip`, `ip6`): + addresses are also in `config.rig.vms.NAME` (`user`, `ip`, `ip6`): ```nix services.nginx.virtualHosts."hermes.example".locations."/".proxyPass = - "http://${config.fc.vms.hermes.ip}:9120"; + "http://${config.rig.vms.hermes.ip}:9120"; ``` - `proxyPort` (default 10443): where the proxy listens; the guests' port 443 gets redirected there. @@ -70,7 +70,7 @@ Optional arguments: second): limits per guest, so one user can't starve the others. If the host has a firewall of its own, it has to let the guests in on -`fcbr0`: udp 67 (DHCP), udp and tcp 53 (DNS), tcp 10443 (the proxy), and +`rigbr0`: udp 67 (DHCP), udp and tcp 53 (DNS), tcp 10443 (the proxy), and tcp 10080 (the checks). ## Running a VM @@ -201,7 +201,7 @@ Who may log in goes in a layer, e.g.: ``` A guest's address is its VM's, by name (`vms`, see host setup), or else -its tap's own (see `fc-egress vms`). +its tap's own (see `rig-egress vms`). Secrets, like API keys (and that host key), come from the host at boot, never through the store or `/xchg` (which other VMs given the same @@ -262,21 +262,21 @@ nix-build pkgs/run-rig \ The guests resolve every name to the host, and all their HTTPS goes to the proxy, which decides by the server name and the VM's whitelist. Each user has any number of named whitelists, in -`/var/lib/fc-egress/USER/LIST`, and manages them with `fc-egress` +`/var/lib/rig-egress/USER/LIST`, and manages them with `rig-egress` (`-l LIST`; without it, the list `default`): ```sh -fc-egress list -fc-egress allow https://cache.nixos.org # the domain and its subdomains -fc-egress allow https://github.com/NixOS/ # only paths starting with /NixOS/ -fc-egress allow https://api.github.com/search/issues # that path and below, any query -fc-egress allow tcp://192.0.2.7:22 # plain TCP to that address and port -fc-egress allow tcp://git.example.org:22 # or name (resolved by the host) -fc-egress allow udp://192.0.2.0/24:5000-5100 -fc-egress deny https://github.com/NixOS/ -fc-egress -l work allow https://example.com # the list "work" -fc-egress lists -fc-egress log -f +rig-egress list +rig-egress allow https://cache.nixos.org # the domain and its subdomains +rig-egress allow https://github.com/NixOS/ # only paths starting with /NixOS/ +rig-egress allow https://api.github.com/search/issues # that path and below, any query +rig-egress allow tcp://192.0.2.7:22 # plain TCP to that address and port +rig-egress allow tcp://git.example.org:22 # or name (resolved by the host) +rig-egress allow udp://192.0.2.0/24:5000-5100 +rig-egress deny https://github.com/NixOS/ +rig-egress -l work allow https://example.com # the list "work" +rig-egress lists +rig-egress log -f ``` Each VM can have a name (also its host name; unique among your running @@ -284,19 +284,19 @@ VMs), and uses one list: the one given with `--egress`, else the one named like the VM, else (no name) `default`: ```sh -fc-egress -l box allow https://cache.nixos.org +rig-egress -l box allow https://cache.nixos.org ./result/bin/run-rig --name box # list box ./result/bin/run-rig --name box2 --egress box # list box, too -fc-egress vms # running VMs: tap (as in the log), address, name, list -fc-egress -v box allow https://example.org # the list box uses -fc-egress use box default # switch box to another list +rig-egress vms # running VMs: tap (as in the log), address, name, list +rig-egress -v box allow https://example.org # the list box uses +rig-egress use box default # switch box to another list ``` A list that doesn't exist allows nothing. Changes to lists and switches apply to the next connection. Whole domains get passed through encrypted. Domains with path rules get decrypted to check each request, with certificates from a CA the host creates on first start -(`/var/lib/fcbr0-egress/ca.pem`). The guest trusts that CA if it exists +(`/var/lib/rig-proxy/ca.pem`). The guest trusts that CA if it exists when the guest gets built, so build the guest on the host after the proxy has started once. Everything else, and anything that isn't HTTPS, gets refused. @@ -309,18 +309,18 @@ those (IPv4 and IPv6) and tells the guests the same addresses, so `ssh git.example.org` works. They get resolved again every 5 minutes. (Every guest gets those addresses for those names, but only the VMs whose list has the rule can connect.) Those connections get forwarded, NATed to the host's address, and logged -(rate-limited, kernel log; `fc-egress log` shows them), as are refused +(rate-limited, kernel log; `rig-egress log` shows them), as are refused ones. If the host has a forward chain of its own, it has to let them through, too (anything from -`fcbr0`; this module rejects what isn't allowed). +`rigbr0`; this module rejects what isn't allowed). For `nix-shell` and the like to download, allow `cache.nixos.org`. Guests can check what their egress allows, before relying on it, with -`fc-egress check` (the guest's own, read-only one), which asks the host: +`rig-egress check` (the guest's own, read-only one), which asks the host: ```sh -fc-egress check https://github.com/krebs/kartei tcp://172.16.0.1:22 +rig-egress check https://github.com/krebs/kartei tcp://172.16.0.1:22 ``` prints `true` or `false` and the URI, a line each (exit status 0 if all @@ -341,7 +341,7 @@ the list its own VM uses. - `layers/console.nix`: a shell on the serial console, to import into a layer. - `layers/switch.nix`: switching a running guest, to import into a layer. - `modules/host/default.nix`: the host's side: network, DHCP/DNS, egress - proxy, `fc-egress`. + proxy, `rig-egress`. - `modules/host/egress.lua`: the egress policy, for the proxy (OpenResty). ## Caveats diff --git a/modules/host/default.nix b/modules/host/default.nix index cfb050a..dbc02c4 100644 --- a/modules/host/default.nix +++ b/modules/host/default.nix @@ -1,5 +1,5 @@ -# Host side of the guests' network: a bridge with tap devices fctap0, -# fctap1, ..., each owned by one of the users that run VMs, a DHCP server, +# Host side of the guests' network: a bridge with tap devices rigtap0, +# rigtap1, ..., each owned by one of the users that run VMs, a DHCP server, # and nginx (OpenResty, i.e. with Lua) as the guests' only way out. Each # running VM takes one tap device, so a user's number of taps is the number # of VMs they can run at the same time. @@ -7,17 +7,17 @@ # Addresses are the VMs' (see vms below), not the taps': a VM takes any of # its user's taps, and gets its address there. dnsmasq hands out every # address by MAC address (06:00 and the address, which the run script -# gives the guest, see /etc/fc-vms and /etc/fc-taps), and the bridge drops +# gives the guest, see /etc/rig-vms and /etc/rig-taps), and the bridge drops # anything coming from a tap with another source address than its VM's, # so a guest's address says which VM, and whose, it is. Which address a -# tap has is up to fcbr0-firewall (see firewallUpdate): the VM's, by its -# name (/run/fc-egress/TAP.name, see below) and the tap's user, or, for a +# tap has is up to rig-firewall (see firewallUpdate): the VM's, by its +# name (/run/rig-egress/TAP.name, see below) and the tap's user, or, for a # VM without a name in vms, the tap's own. It puts the addresses into # the tap's nftables sets, in both the bridge's table and the other, and -# into /run/fcbr0-addr (TAP: its address, ADDRESS: its tap), for nginx +# into /run/rig-addr (TAP: its address, ADDRESS: its tap), for nginx # and the run script. # -# Egress, per VM: nothing gets forwarded from fcbr0. The guests' DNS +# Egress, per VM: nothing gets forwarded from rigbr0. The guests' DNS # resolves every name to 172.16.0.1, and their TLS connections (tcp port # 443, whatever the address) get redirected to nginx on # 172.16.0.1:proxyPort (not 443 itself, as the host may run a web server @@ -28,20 +28,20 @@ # from our own CA, which the guests trust, see pkgs/run-rig) # and only matching requests forwarded. # -# Whitelists are per user and named: /var/lib/fc-egress/USER/LIST, changed -# with fc-egress (see fc-egress help) at any time; changes apply to the -# next connection. Which list a VM uses is in /run/fc-egress/TAP (one +# Whitelists are per user and named: /var/lib/rig-egress/USER/LIST, changed +# with rig-egress (see rig-egress help) at any time; changes apply to the +# next connection. Which list a VM uses is in /run/rig-egress/TAP (one # line, the list's name, writable by the tap's user), which the run script # writes when it takes the tap (--egress LIST, default: the VM's name # (--name), or "default") and # empties when the VM exits. No list, or one that doesn't exist, allows -# nothing. Likewise /run/fc-egress/TAP.name, the VM's name (--name), for -# its address (see above), and so fc-egress can take names instead of +# nothing. Likewise /run/rig-egress/TAP.name, the VM's name (--name), for +# its address (see above), and so rig-egress can take names instead of # taps. # # Users can also allow plain TCP or UDP to given addresses and ports, like # ssh to some host: rules like tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] in the -# same lists. fcbr0-firewall puts a VM's into its tap's nftables set +# same lists. rig-firewall puts a VM's into its tap's nftables set # allow_TAP whenever the list or the tap's choice of list changes, and # those connections get forwarded (and NATed). # @@ -51,11 +51,11 @@ # VMs get static addresses by name and user (vms = { hermes = { user = # "tv"; ip = "172.16.0.2"; }; }), any number of them, whichever tap they # run on, e.g. for the host to reach them with a reverse proxy. The -# addresses are also in config.fc.vms.NAME (user, ip, ip6), e.g. -# proxyPass = "http://${config.fc.vms.hermes.ip}:9120"; +# addresses are also in config.rig.vms.NAME (user, ip, ip6), e.g. +# proxyPass = "http://${config.rig.vms.hermes.ip}:9120"; # 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), +# The host's firewall has to let the guests in on rigbr0: DHCP (udp 67), # DNS (udp and tcp 53), the proxy (tcp proxyPort, after the redirect), # and the checks (tcp checkPort, likewise). { users @@ -66,7 +66,7 @@ # 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 + # The guests' IPv6 /64 (on rigbr0, 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 # IPv4. @@ -102,7 +102,7 @@ # [ { vm = "hermes"; user = ...; ip = ...; ip6 = ...; ll6 = ...; # mac = ...; } ... ] vmList = lib.mapAttrsToList (vm: v: { inherit vm; inherit (v) user; } // address v.ip) vms; - # [ { name = "fctap0"; user = ...; ip = ...; ip6 = ...; ll6 = ...; + # [ { name = "rigtap0"; user = ...; ip = ...; ip6 = ...; ll6 = ...; # mac = ...; } ... ]: each user's taps, in order, with their own # addresses, for VMs without one in vms: from 172.16.0.200 on, so they # stand out, those that aren't any VM's, in order. @@ -112,17 +112,17 @@ (lib.genList (k: "172.16.0.${toString (k + tapsFrom)}") (255 - tapsFrom)); userTaps = lib.concatMap (u: lib.genList (_: u) users.${u}) userNames; in lib.imap0 (i: user: { - name = "fctap${toString i}"; + name = "rigtap${toString i}"; inherit user; } // address (if i < lib.length free then lib.elemAt free i else "172.16.0.0")) userTaps; # see assertions # Everyone's addresses: taps' and VMs'. addresses = taps ++ vmList; - dir = "/var/lib/fc-egress"; - vmDir = "/run/fc-egress"; # TAP: the name of the VM's list + dir = "/var/lib/rig-egress"; + vmDir = "/run/rig-egress"; # TAP: the name of the VM's list # List names: no slashes, no dot files. listName = "[A-Za-z0-9_-]+"; # VM names (run-rig --name, also the guest's host name): - # /run/fc-egress/TAP.name, for fc-egress. + # /run/rig-egress/TAP.name, for rig-egress. vmName = "[A-Za-z0-9][A-Za-z0-9-]{0,62}"; userNames = lib.attrNames users; deny4 = lib.filter (a: !lib.hasInfix ":" a) denyDestinations; @@ -135,11 +135,11 @@ # For the run script: the taps' own addresses, TAP USER IP MAC, and the # VMs', VM USER IP MAC, a line each. - tapsFile = "/etc/fc-taps"; - vmsFile = "/etc/fc-vms"; + tapsFile = "/etc/rig-taps"; + vmsFile = "/etc/rig-vms"; # Which address each tap has, and which tap each address: TAP: ADDRESS, - # ADDRESS: TAP, written by fcbr0-firewall (see firewallUpdate). - addrDir = "/run/fcbr0-addr"; + # ADDRESS: TAP, written by rig-firewall (see firewallUpdate). + addrDir = "/run/rig-addr"; # tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT], tcp://[ADDRESS6[/PREFIXLEN]]:..., # tcp://NAME:PORT[-PORT], or udp://... (nft checks the numbers.) @@ -151,7 +151,7 @@ # The addresses of the names in the tcp:// and udp:// rules, as the # firewall update resolves them, a hosts file per tap, for the guests' # dnsmasq, so a guest connects to the addresses the rules allow. - hostsDir = "/run/fcbr0-hosts"; + hostsDir = "/run/rig-hosts"; # How often the names get resolved again. refreshInterval = "5min"; @@ -163,8 +163,8 @@ # keep the others' out, with names resolved by the host (IPv4 and IPv6; # a name that doesn't resolve allows nothing), and those names' # addresses into the taps' hosts files. Doesn't follow symlinks (it - # runs with CAP_NET_ADMIN, though not as root, see fcbr0-firewall). - firewallUpdate = pkgs.writeShellScript "fcbr0-firewall-update" '' + # runs with CAP_NET_ADMIN, though not as root, see rig-firewall). + firewallUpdate = pkgs.writeShellScript "rig-firewall-update" '' set -fu export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.gawk pkgs.getent pkgs.gnugrep pkgs.nftables ]} # first line of a file, at most 256 bytes, or nothing @@ -183,7 +183,7 @@ ${lib.concatMapStrings (u: " ${u}) targets=\"$targets ${lib.concatMapStri done for tap in $targets; do case $tap in -${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ip=${t.ip} ip6=${t.ip6} ll6=${t.ll6} others=\"${lib.concatMapStringsSep " " (o: o.name) (lib.filter (o: o.name != t.name) (tapsOf t.user))}\" ;;\n") taps} *) echo "fcbr0-firewall: no tap $tap" >&2; status=1; continue +${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ip=${t.ip} ip6=${t.ip6} ll6=${t.ll6} others=\"${lib.concatMapStringsSep " " (o: o.name) (lib.filter (o: o.name != t.name) (tapsOf t.user))}\" ;;\n") taps} *) echo "rig-firewall: no tap $tap" >&2; status=1; continue esac # The tap's address: its own, unless its VM's name is one of the @@ -199,24 +199,24 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ ip=$vip ip6=$vip6 ll6=$vll6 for t in $others; do if test "$(line ${vmDir}/$t.name)" = "$name"; then - echo "fcbr0-firewall: $tap: $user's VM $name is on $t, too: no address" >&2 + echo "rig-firewall: $tap: $user's VM $name is on $t, too: no address" >&2 ip= ip6= ll6= status=1 fi done fi { - for table in "inet firecracker" "bridge firecracker-bridge"; do + for table in "inet rigs" "bridge rigs-bridge"; do echo "flush set $table addr_$tap" echo "flush set $table addr6_$tap" done if test -n "$ip"; then - echo "add element inet firecracker addr_$tap { $ip }" - echo "add element inet firecracker addr6_$tap { $ip6 }" - echo "add element bridge firecracker-bridge addr_$tap { $ip }" - echo "add element bridge firecracker-bridge addr6_$tap { $ip6, $ll6 }" + echo "add element inet rigs addr_$tap { $ip }" + echo "add element inet rigs addr6_$tap { $ip6 }" + echo "add element bridge rigs-bridge addr_$tap { $ip }" + echo "add element bridge rigs-bridge addr6_$tap { $ip6, $ll6 }" fi - } | nft -f - || { echo "fcbr0-firewall: address of $tap ($user: ''${name:--}) not applied" >&2; status=1; continue; } + } | nft -f - || { echo "rig-firewall: address of $tap ($user: ''${name:--}) not applied" >&2; status=1; continue; } # The address's tap first, then the tap's address, which the run # script waits for. An address's file that names another tap is # stale (that tap's own file says). @@ -261,15 +261,15 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ done done { - echo "flush set inet firecracker allow_$tap" - echo "flush set inet firecracker allow6_$tap" + echo "flush set inet rigs allow_$tap" + echo "flush set inet rigs allow6_$tap" if test -n "$elements"; then - echo "add element inet firecracker allow_$tap { $elements }" + echo "add element inet rigs allow_$tap { $elements }" fi if test -n "$elements6"; then - echo "add element inet firecracker allow6_$tap { $elements6 }" + echo "add element inet rigs allow6_$tap { $elements6 }" fi - } | nft -f - || { echo "fcbr0-firewall: rules of $tap ($user: $list) not applied" >&2; status=1; continue; } + } | nft -f - || { echo "rig-firewall: rules of $tap ($user: $list) not applied" >&2; status=1; continue; } # dnsmasq reads new and changed files, but not dot files. printf %s "$hosts" | sort -u > ${hostsDir}/.$tap mv ${hostsDir}/.$tap ${hostsDir}/$tap @@ -282,12 +282,12 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ # upstream). workerConnections = 5 * maxConnections * lib.length taps + 1024; - # How fcbr0-firewall runs: not as root, users' files are input. A user + # How rig-firewall runs: not as root, users' files are input. A user # of its own (not DynamicUser), for the hosts files and addrDir. firewallService = { Type = "oneshot"; - User = "fcbr0-firewall"; - Group = "fcbr0-firewall"; + User = "rig-firewall"; + Group = "rig-firewall"; AmbientCapabilities = [ "CAP_NET_ADMIN" ]; CapabilityBoundingSet = [ "CAP_NET_ADMIN" ]; NoNewPrivileges = true; @@ -299,26 +299,26 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ }; # Where nginx keeps the CA: ca.key, and ca.pem for the guests (see - # caCert in pkgs/run-rig). - stateDir = "/var/lib/fcbr0-egress"; - runDir = "/run/fcbr0-egress"; + # security.pki in pkgs/run-rig). + stateDir = "/var/lib/rig-proxy"; + runDir = "/run/rig-proxy"; - fc-egress = pkgs.writeShellScriptBin "fc-egress" '' + rig-egress = pkgs.writeShellScriptBin "rig-egress" '' set -efu export PATH=${lib.makeBinPath [ pkgs.bash pkgs.coreutils pkgs.findutils pkgs.gawk pkgs.gnugrep pkgs.gnused pkgs.systemd ]} user=$(id -un) if ! test -d ${dir}/$user; then - echo "fc-egress: $user has no VMs (see modules/host)" >&2 + echo "rig-egress: $user has no VMs (see modules/host)" >&2 exit 1 fi usage() { - echo "usage: fc-egress [-l LIST | -v VM] list" - echo " fc-egress [-l LIST | -v VM] allow RULE..." - echo " fc-egress [-l LIST | -v VM] deny RULE..." - echo " fc-egress lists" - echo " fc-egress vms" - echo " fc-egress use VM LIST" - echo " fc-egress log [JOURNALCTL-OPTION...]" + echo "usage: rig-egress [-l LIST | -v VM] list" + echo " rig-egress [-l LIST | -v VM] allow RULE..." + echo " rig-egress [-l LIST | -v VM] deny RULE..." + echo " rig-egress lists" + echo " rig-egress vms" + echo " rig-egress use VM LIST" + echo " rig-egress log [JOURNALCTL-OPTION...]" echo "LIST is a whitelist's name (default: default); a VM uses the one" echo " given to run-rig --egress (default: its --name," echo " or default)" @@ -337,13 +337,13 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ } # the tap of a running VM of the user's, by name or tap tap_of() { - for f in $(find ${vmDir} -mindepth 1 -maxdepth 1 -user "$user" -size +0 -name 'fctap*' ! -name '*.*' -printf '%f\n'); do + for f in $(find ${vmDir} -mindepth 1 -maxdepth 1 -user "$user" -size +0 -name 'rigtap*' ! -name '*.*' -printf '%f\n'); do if test "$f" = "$1" || test "$(head -n 1 ${vmDir}/$f.name 2>/dev/null)" = "$1"; then echo "$f" return fi done - echo "fc-egress: no running VM $1 of $user's" >&2 + echo "rig-egress: no running VM $1 of $user's" >&2 exit 1 } name=default @@ -358,7 +358,7 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ shift 2 esac if ! printf %s "$name" | grep -qxE '${listName}'; then - echo "fc-egress: bad list name: $name" >&2 + echo "rig-egress: bad list name: $name" >&2 exit 1 fi list=${dir}/$user/$name @@ -384,12 +384,12 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ }'; then continue fi - echo "fc-egress: bad rule: $r" >&2 + echo "rig-egress: bad rule: $r" >&2 exit 1 fi if ! printf %s "$r" | grep -qxE 'https://[a-z0-9-]+(\.[a-z0-9-]+)*(/[A-Za-z0-9._~!$&()*+,;=:@%/-]*)?' || printf %s "$r" | grep -qE '/\.\.?(/|$)'; then - echo "fc-egress: bad rule: $r" >&2 + echo "rig-egress: bad rule: $r" >&2 exit 1 fi done @@ -421,7 +421,7 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ find ${dir}/$user -mindepth 1 -maxdepth 1 -type f -printf '%f\n' | sort ;; vms) - find ${vmDir} -mindepth 1 -maxdepth 1 -user "$user" -size +0 -name 'fctap*' ! -name '*.*' -printf '%f\n' | sort -V | + find ${vmDir} -mindepth 1 -maxdepth 1 -user "$user" -size +0 -name 'rigtap*' ! -name '*.*' -printf '%f\n' | sort -V | while read -r tap; do vm=$(head -n 1 ${vmDir}/$tap.name 2>/dev/null) || : ip=$(head -n 1 ${addrDir}/$tap 2>/dev/null) || : @@ -432,11 +432,11 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ if test $# != 2; then usage >&2; exit 1; fi tap=$(tap_of "$1") if ! printf %s "$2" | grep -qxE '${listName}'; then - echo "fc-egress: bad list name: $2" >&2 + echo "rig-egress: bad list name: $2" >&2 exit 1 fi if ! test -f ${dir}/$user/"$2"; then - echo "fc-egress: no list $2, $1 gets no egress" >&2 + echo "rig-egress: no list $2, $1 gets no egress" >&2 fi printf '%s\n' "$2" > ${vmDir}/$tap ;; @@ -445,14 +445,14 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ # guest, upstream, status, bytes sent and received (from the guest's # view: received, sent), seconds. Decrypted requests: ALLOW|DENY, # user, tap, list, method, https://HOST/URI, status, bytes sent. - # Other connections (kernel log, rate-limited): fc-egress + # Other connections (kernel log, rate-limited): rig-egress # ALLOW|DENY, user, tap, tcp://DST:DPT (or udp://; added here), and # the packet. The URIs are as rules take them, for copying. - journalctl -o short-iso _SYSTEMD_UNIT=fcbr0-egress.service + _TRANSPORT=kernel "$@" | + journalctl -o short-iso _SYSTEMD_UNIT=rig-proxy.service + _TRANSPORT=kernel "$@" | grep --line-buffered -E " (ALLOW|DENY) $user " | sed -u -E \ - -e 's/( fc-egress (ALLOW|DENY) [^ ]+ [^ ]+ )(.* DST=([0-9.]+) .*PROTO=(TCP|UDP) .*DPT=([0-9]+).*)$/\1\L\5\E:\/\/\4:\6 \3/' \ - -e 's/( fc-egress (ALLOW|DENY) [^ ]+ [^ ]+ )(.* DST=([0-9a-f:]+) .*PROTO=(TCP|UDP) .*DPT=([0-9]+).*)$/\1\L\5\E:\/\/[\4]:\6 \3/' + -e 's/( rig-egress (ALLOW|DENY) [^ ]+ [^ ]+ )(.* DST=([0-9.]+) .*PROTO=(TCP|UDP) .*DPT=([0-9]+).*)$/\1\L\5\E:\/\/\4:\6 \3/' \ + -e 's/( rig-egress (ALLOW|DENY) [^ ]+ [^ ]+ )(.* DST=([0-9a-f:]+) .*PROTO=(TCP|UDP) .*DPT=([0-9]+).*)$/\1\L\5\E:\/\/[\4]:\6 \3/' ;; help|-h|--help) usage @@ -466,20 +466,20 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ # nginx's resolver from the host's resolv.conf (nginx can't use that # itself), the CA on first start, and a certificate that nginx wants # but never gets to use (egress.lua sets one for every connection). - egressSetup = pkgs.writeShellScript "fcbr0-egress-setup" '' + egressSetup = pkgs.writeShellScript "rig-proxy-setup" '' set -efu export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.gawk pkgs.openssl ]} awk ' $1 == "nameserver" { s = s " " ($2 ~ /:/ ? "[" $2 "]" : $2) } END { - if (s == "") { print "fcbr0-egress-setup: no nameserver in /etc/resolv.conf" > "/dev/stderr"; exit 1 } + if (s == "") { print "rig-proxy-setup: no nameserver in /etc/resolv.conf" > "/dev/stderr"; exit 1 } print "resolver" s " ipv6=off;" } ' /etc/resolv.conf > ${runDir}/resolver.conf cd ${stateDir} if ! test -e ca.key; then openssl req -x509 -new -newkey rsa:2048 -nodes -days 3650 \ - -subj /CN=fc-egress -keyout ca.key.tmp -out ca.crt \ + -subj /CN=rig-egress -keyout ca.key.tmp -out ca.crt \ -addext basicConstraints=critical,CA:TRUE \ -addext keyUsage=critical,keyCertSign,cRLSign 2>/dev/null mv ca.key.tmp ca.key @@ -499,7 +499,7 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ + " },\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 + # on rigbr0, 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" ]) @@ -519,18 +519,18 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ # the server name, verifying its certificate. The original request URI # goes upstream unchanged (proxy_pass with variables and no URI), which # is what egress.lua checks. - nginxConfig = pkgs.writeText "fcbr0-egress.conf" '' + nginxConfig = pkgs.writeText "rig-proxy.conf" '' daemon off; pid ${runDir}/nginx.pid; error_log stderr warn; worker_processes 1; worker_rlimit_nofile ${toString (workerConnections + 1024)}; - env FC_EGRESS_RULES=${dir}; - env FC_EGRESS_STATE=${stateDir}; - env FC_EGRESS_RUN=${runDir}; - env FC_EGRESS_VMS=${vmDir}; - env FC_EGRESS_ADDR=${addrDir}; - env FC_EGRESS_HOSTS=${hostsDir}; + env RIG_EGRESS_RULES=${dir}; + env RIG_EGRESS_STATE=${stateDir}; + env RIG_EGRESS_RUN=${runDir}; + env RIG_EGRESS_VMS=${vmDir}; + env RIG_EGRESS_ADDR=${addrDir}; + env RIG_EGRESS_HOSTS=${hostsDir}; events { worker_connections ${toString workerConnections}; } @@ -539,7 +539,7 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${ lua_package_path "${egressLua}/?.lua;;"; include ${runDir}/resolver.conf; log_format egress '$egress_log $remote_addr $upstream_addr $status $bytes_sent $bytes_received $session_time'; - access_log syslog:server=unix:/dev/log,nohostname,tag=fc_egress egress if=$egress_log; + access_log syslog:server=unix:/dev/log,nohostname,tag=rig_egress egress if=$egress_log; # Per user (not per VM): open connections, at most maxConnections per tap, so one # user can't use up the connections everyone's egress goes through. @@ -568,7 +568,7 @@ ${lib.concatMapStrings (u: " limit_conn user_${u} ${toString (users.${u} log_format egress '$egress_verdict $egress_guest $request_method https://$host$request_uri $status $body_bytes_sent'; # Only requests that got to egress.lua (others, like a TLS # handshake without request, have no verdict). - access_log syslog:server=unix:/dev/log,nohostname,tag=fc_egress egress if=$egress_verdict; + access_log syslog:server=unix:/dev/log,nohostname,tag=rig_egress egress if=$egress_verdict; uninitialized_variable_warn off; server { @@ -626,7 +626,7 @@ ${lib.concatMapStrings (t: " listen unix:${runDir}/decrypt-${t.name}.sock ''; in { imports = [{ - options.fc.vms = lib.mkOption { + options.rig.vms = lib.mkOption { type = lib.types.attrsOf (lib.types.attrsOf lib.types.str); default = lib.listToAttrs (map (v: lib.nameValuePair v.vm { inherit (v) user ip ip6; @@ -668,14 +668,14 @@ in { virtualType = "tap"; virtualOwner = t.user; }) taps) // { - fcbr0.ipv4.addresses = [{ address = "172.16.0.1"; prefixLength = 24; }]; - fcbr0.ipv6.addresses = [{ address = "${prefix6}1"; prefixLength = 64; }]; + rigbr0.ipv4.addresses = [{ address = "172.16.0.1"; prefixLength = 24; }]; + rigbr0.ipv6.addresses = [{ address = "${prefix6}1"; prefixLength = 64; }]; }; - networking.bridges.fcbr0.interfaces = map (t: t.name) taps; - # With networkd, fcbr0 would only get its address while it has carrier, + networking.bridges.rigbr0.interfaces = map (t: t.name) taps; + # With networkd, rigbr0 would only get its address while it has carrier, # i.e. while a VM runs, so nginx couldn't bind to it. (Unused # with scripted networking, which assigns it regardless.) - systemd.network.networks."40-fcbr0".networkConfig.ConfigureWithoutCarrier = true; + systemd.network.networks."40-rigbr0".networkConfig.ConfigureWithoutCarrier = true; # 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 @@ -690,15 +690,15 @@ in { # guest anywhere else either. Each guest gets at most maxConnections # open and maxNewConnections new per second. Allowed and refused # connections get logged (rate-limited), with user and tap, as - # "fc-egress ALLOW USER TAP" and "fc-egress DENY USER TAP" (kernel log). These + # "rig-egress ALLOW USER TAP" and "rig-egress DENY USER TAP" (kernel log). These # chains only reject; for what they let through, the host's own chains # decide, too. Forwarded connections get NATed to the host's address. networking.nftables.enable = true; - # The build's ruleset check runs where fcbr0-egress doesn't exist. + # The build's ruleset check runs where rig-proxy doesn't exist. networking.nftables.preCheckRuleset = '' - sed -i 's/skuid "fcbr0-egress"/skuid 0/' ruleset.conf + sed -i 's/skuid "rig-proxy"/skuid 0/' ruleset.conf ''; - networking.nftables.tables.firecracker = { + networking.nftables.tables.rigs = { family = "inet"; content = '' ${lib.concatMapStrings (t: '' @@ -766,20 +766,20 @@ ${lib.concatMapStrings (t: '' # nothing. 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 + iifname "rigbr0" meta nfproto ipv4 tcp dport 443 redirect to :${toString proxyPort} + iifname "rigbr0" ip daddr 172.16.0.1 tcp dport 80 redirect to :${toString checkPort} + iifname "rigbr0" meta nfproto ipv4 meta l4proto { tcp, udp } dnat ip to ip daddr + iifname "rigbr0" meta nfproto ipv6 meta l4proto { tcp, udp } dnat ip6 to ip6 daddr } chain postrouting { type nat hook postrouting priority srcnat - 1; - iifname "fcbr0" masquerade + iifname "rigbr0" masquerade } chain input { type filter hook input priority filter; policy accept; - iifname != "fcbr0" accept + iifname != "rigbr0" accept ct state { established, related } accept # IPv6 neighbour and router discovery (dnsmasq sends the RAs) icmpv6 type { nd-router-solicit, nd-neighbor-solicit, nd-neighbor-advert, mld2-listener-report } accept @@ -792,7 +792,7 @@ ${lib.concatMapStrings (t: '' chain forward { type filter hook forward priority filter; policy accept; - iifname != "fcbr0" accept + iifname != "rigbr0" accept ct state { established, related } accept jump limits ip daddr @deny4 jump refused @@ -812,14 +812,14 @@ ${lib.concatMapStrings (t: '' chain vms { ${lib.concatMapStrings (t: '' - ip saddr @addr_${t.name} ip daddr . meta l4proto . th dport @allow_${t.name} limit rate 10/second burst 50 packets log prefix "fc-egress ALLOW ${t.user} ${t.name} " level info + ip saddr @addr_${t.name} ip daddr . meta l4proto . th dport @allow_${t.name} limit rate 10/second burst 50 packets log prefix "rig-egress ALLOW ${t.user} ${t.name} " level info ip saddr @addr_${t.name} ip daddr . meta l4proto . th dport @allow_${t.name} accept - ip6 saddr @addr6_${t.name} ip6 daddr . meta l4proto . th dport @allow6_${t.name} limit rate 10/second burst 50 packets log prefix "fc-egress ALLOW ${t.user} ${t.name} " level info + ip6 saddr @addr6_${t.name} ip6 daddr . meta l4proto . th dport @allow6_${t.name} limit rate 10/second burst 50 packets log prefix "rig-egress ALLOW ${t.user} ${t.name} " level info ip6 saddr @addr6_${t.name} ip6 daddr . meta l4proto . th dport @allow6_${t.name} accept '') taps} } chain refused { -${lib.concatMapStrings (t: " ip saddr @addr_${t.name} limit rate 10/second burst 50 packets log prefix \"fc-egress DENY ${t.user} ${t.name} \" level info\n") taps}${lib.concatMapStrings (t: " ip6 saddr @addr6_${t.name} limit rate 10/second burst 50 packets log prefix \"fc-egress DENY ${t.user} ${t.name} \" level info\n") taps} reject with icmpx admin-prohibited +${lib.concatMapStrings (t: " ip saddr @addr_${t.name} limit rate 10/second burst 50 packets log prefix \"rig-egress DENY ${t.user} ${t.name} \" level info\n") taps}${lib.concatMapStrings (t: " ip6 saddr @addr6_${t.name} limit rate 10/second burst 50 packets log prefix \"rig-egress DENY ${t.user} ${t.name} \" level info\n") taps} reject with icmpx admin-prohibited } # Only nginx's packets: others, including those the kernel sends on @@ -827,7 +827,7 @@ ${lib.concatMapStrings (t: " ip saddr @addr_${t.name} limit rate 10/secon # no skuid matches them at all), pass. chain output { type filter hook output priority filter; policy accept; - meta skuid "fcbr0-egress" jump nginx + meta skuid "rig-proxy" jump nginx } chain nginx { @@ -844,7 +844,7 @@ ${lib.concatMapStrings (t: " ip saddr @addr_${t.name} limit rate 10/secon # and addr6_TAP, see firewallUpdate; or 0.0.0.0, for DHCP and ARP probes, # or ::, for IPv6 duplicate address detection), IPv4, ARP and IPv6 only, # and nothing to other taps. - networking.nftables.tables.firecracker-bridge = { + networking.nftables.tables.rigs-bridge = { family = "bridge"; content = '' ${lib.concatMapStrings (t: '' @@ -858,7 +858,7 @@ ${lib.concatMapStrings (t: '' '') taps} chain prerouting { type filter hook prerouting priority filter; policy accept; - iifname != "fctap*" accept + iifname != "rigtap*" accept ${lib.concatMapStrings (t: '' iifname "${t.name}" ether type ip ip saddr @addr_${t.name} accept iifname "${t.name}" ether type arp arp saddr ip @addr_${t.name} accept @@ -871,7 +871,7 @@ ${lib.concatMapStrings (t: '' chain forward { type filter hook forward priority filter; policy accept; - iifname "fctap*" oifname "fctap*" drop + iifname "rigtap*" oifname "rigtap*" drop } ''; }; @@ -883,22 +883,22 @@ ${lib.concatMapStrings (t: '' # an empty answer instead of REFUSED, which can fail whole lookups), but # for the names in the VMs' tcp:// and udp:// rules, from the hosts files # in hostsDir (which win over address=; for every guest, there's no - # telling guests apart here). It only listens on fcbr0 + # telling guests apart here). It only listens on rigbr0 # (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 + # resolver the host runs there); bind-dynamic copes with rigbr0 not being # there yet. - systemd.services.fcbr0-dnsmasq = { + systemd.services.rig-dnsmasq = { wantedBy = [ "multi-user.target" ]; after = [ "network.target" "systemd-tmpfiles-setup.service" ]; serviceConfig = { - RuntimeDirectory = "fcbr0-dnsmasq"; + RuntimeDirectory = "rig-dnsmasq"; Restart = "on-failure"; ExecStart = lib.escapeShellArgs ([ "${pkgs.dnsmasq}/bin/dnsmasq" "--keep-in-foreground" "--conf-file=/dev/null" "--bind-dynamic" - "--interface=fcbr0" + "--interface=rigbr0" "--except-interface=lo" "--dhcp-authoritative" "--dhcp-range=172.16.0.0,static" @@ -906,7 +906,7 @@ ${lib.concatMapStrings (t: '' # addresses from their MACs. "--enable-ra" "--dhcp-range=${prefix6},ra-only" - "--dhcp-leasefile=/run/fcbr0-dnsmasq/leases" + "--dhcp-leasefile=/run/rig-dnsmasq/leases" "--no-resolv" "--no-hosts" "--address=/#/172.16.0.1" @@ -922,8 +922,8 @@ ${lib.concatMapStrings (t: '' systemd.tmpfiles.rules = [ "d ${dir} 0755 root root -" "d ${vmDir} 0755 root root -" - "d ${hostsDir} 0755 fcbr0-firewall fcbr0-firewall -" - "d ${addrDir} 0755 fcbr0-firewall fcbr0-firewall -" + "d ${hostsDir} 0755 rig-firewall rig-firewall -" + "d ${addrDir} 0755 rig-firewall rig-firewall -" ] ++ lib.concatMap (u: [ "d ${dir}/${u} 0755 ${u} - -" @@ -942,11 +942,11 @@ ${lib.concatMapStrings (t: '' # Fills the addr_TAP and allow_TAP sets: all of them at start and # whenever nftables gets reloaded (which empties them), a tap's - # (fcbr0-firewall@TAP) whenever its VM's name or choice of list changes, - # and all of a user's (fcbr0-firewall@USER) whenever one of their lists + # (rig-firewall@TAP) whenever its VM's name or choice of list changes, + # and all of a user's (rig-firewall@USER) whenever one of their lists # changes. By path units of each tap's and user's own, so a user who # keeps changing things can't make it stop for others. - systemd.services.fcbr0-firewall = { + systemd.services.rig-firewall = { wantedBy = [ "multi-user.target" ]; after = [ "nftables.service" "systemd-tmpfiles-setup.service" ]; partOf = [ "nftables.service" ]; @@ -958,33 +958,33 @@ ${lib.concatMapStrings (t: '' }; }; # Names get resolved again every refreshInterval. - systemd.services.fcbr0-firewall-refresh = { - after = [ "fcbr0-firewall.service" ]; + systemd.services.rig-firewall-refresh = { + after = [ "rig-firewall.service" ]; serviceConfig = firewallService // { ExecStart = firewallUpdate; }; }; - systemd.timers.fcbr0-firewall-refresh = { + systemd.timers.rig-firewall-refresh = { wantedBy = [ "timers.target" ]; timerConfig.OnUnitActiveSec = refreshInterval; timerConfig.OnActiveSec = refreshInterval; }; - users.users.fcbr0-firewall = { + users.users.rig-firewall = { isSystemUser = true; - group = "fcbr0-firewall"; + group = "rig-firewall"; }; - users.groups.fcbr0-firewall = { }; - systemd.services."fcbr0-firewall@" = { - after = [ "fcbr0-firewall.service" ]; + users.groups.rig-firewall = { }; + systemd.services."rig-firewall@" = { + after = [ "rig-firewall.service" ]; serviceConfig = firewallService // { ExecStart = "${firewallUpdate} %i"; }; }; systemd.paths = let - path = name: instance: file: lib.nameValuePair "fcbr0-firewall-${name}" { + path = name: instance: file: lib.nameValuePair "rig-firewall-${name}" { wantedBy = [ "multi-user.target" ]; pathConfig.PathChanged = file; # a directory: any file in it - pathConfig.Unit = "fcbr0-firewall@${instance}.service"; + pathConfig.Unit = "rig-firewall@${instance}.service"; }; in lib.listToAttrs (map (u: path u u "${dir}/${u}") userNames ++ map (t: path t.name t.name "${vmDir}/${t.name}") taps @@ -992,32 +992,32 @@ ${lib.concatMapStrings (t: '' # A user of its own rather than DynamicUser, as ca.pem has to be readable # by anyone (DynamicUser state lives under /var/lib/private, mode 0700). - # nginx binds to 172.16.0.1, so it has to wait for fcbr0's address - # (network-addresses-fcbr0.service with scripted networking; with + # nginx binds to 172.16.0.1, so it has to wait for rigbr0's address + # (network-addresses-rigbr0.service with scripted networking; with # networkd, nothing to order after, so it keeps restarting, without start # limit, until the address is there). - users.users.fcbr0-egress = { + users.users.rig-proxy = { isSystemUser = true; - group = "fcbr0-egress"; + group = "rig-proxy"; }; - users.groups.fcbr0-egress = { }; - systemd.services.fcbr0-egress = { + users.groups.rig-proxy = { }; + systemd.services.rig-proxy = { wantedBy = [ "multi-user.target" ]; startLimitIntervalSec = 0; - after = [ "network.target" "network-addresses-fcbr0.service" "systemd-tmpfiles-setup.service" ]; + after = [ "network.target" "network-addresses-rigbr0.service" "systemd-tmpfiles-setup.service" ]; serviceConfig = { ExecStartPre = egressSetup; ExecStart = "${pkgs.openresty}/bin/nginx -e stderr -p ${runDir} -c ${nginxConfig}"; - User = "fcbr0-egress"; - Group = "fcbr0-egress"; - StateDirectory = "fcbr0-egress"; + User = "rig-proxy"; + Group = "rig-proxy"; + StateDirectory = "rig-proxy"; StateDirectoryMode = "0755"; - RuntimeDirectory = "fcbr0-egress"; + RuntimeDirectory = "rig-proxy"; UMask = "0077"; Restart = "always"; RestartSec = 1; }; }; - environment.systemPackages = [ fc-egress ]; + environment.systemPackages = [ rig-egress ]; } diff --git a/modules/host/egress.lua b/modules/host/egress.lua index fe626b2..9bcae41 100644 --- a/modules/host/egress.lua +++ b/modules/host/egress.lua @@ -21,12 +21,12 @@ local _M = {} -local RULES = os.getenv("FC_EGRESS_RULES") -- RULES/USER/LIST -local VMS = os.getenv("FC_EGRESS_VMS") -- VMS/TAP: LIST -local STATE = os.getenv("FC_EGRESS_STATE") -- ca.key, ca.pem -local RUN = os.getenv("FC_EGRESS_RUN") -- RUN/decrypt-TAP.sock: the http server -local ADDR = os.getenv("FC_EGRESS_ADDR") -- ADDR/TAP: ADDRESS, ADDR/ADDRESS: TAP -local HOSTS = os.getenv("FC_EGRESS_HOSTS") -- HOSTS/TAP: ADDRESS NAME, the tap's rules' names +local RULES = os.getenv("RIG_EGRESS_RULES") -- RULES/USER/LIST +local VMS = os.getenv("RIG_EGRESS_VMS") -- VMS/TAP: LIST +local STATE = os.getenv("RIG_EGRESS_STATE") -- ca.key, ca.pem +local RUN = os.getenv("RIG_EGRESS_RUN") -- RUN/decrypt-TAP.sock: the http server +local ADDR = os.getenv("RIG_EGRESS_ADDR") -- ADDR/TAP: ADDRESS, ADDR/ADDRESS: TAP +local HOSTS = os.getenv("RIG_EGRESS_HOSTS") -- HOSTS/TAP: ADDRESS NAME, the tap's rules' names -- { addresses = { [ADDRESS] = USER }, taps = { [TAP] = USER }, -- deny = { ADDRESS/PREFIXLEN, ... } } local owners = require("egress_guests") @@ -345,7 +345,7 @@ local function within(a, prefix) end -- The guest's tcp:// and udp:// rules: { proto, host, from, to }, host an --- ADDRESS[/PREFIXLEN] or a name, as fcbr0-firewall takes them. +-- ADDRESS[/PREFIXLEN] or a name, as rig-firewall takes them. local function port_rules(guest, name) local list = {} local f = name and io.open(RULES .. "/" .. guest.user .. "/" .. name) @@ -365,7 +365,7 @@ local function port_rules(guest, name) return list end --- What the tap's guest gets for names in rules (fcbr0-firewall's hosts +-- What the tap's guest gets for names in rules (rig-firewall's hosts -- file for the tap): { [NAME] = { ADDRESS, ... } }; any other name, the -- guest gets 172.16.0.1. local function hosts(guest) diff --git a/pkgs/run-rig/default.nix b/pkgs/run-rig/default.nix index 16af39d..cc16879 100644 --- a/pkgs/run-rig/default.nix +++ b/pkgs/run-rig/default.nix @@ -44,18 +44,18 @@ 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 + # The guest's side of rig-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... + rig-egress = pkgs.writeShellScriptBin "rig-egress" '' + # usage: rig-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. + # errors. The whitelist itself is the host's, see rig-egress there. set -eu usage() { - echo "usage: fc-egress check URI..." >&2 + echo "usage: rig-egress check URI..." >&2 exit 2 } test "''${1-}" = check || usage @@ -65,21 +65,21 @@ let body= for uri; do case $uri in *$'\n'* | *$'\r'*) - echo "fc-egress: bad URI" >&2 + echo "rig-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 + echo "rig-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 + echo "rig-egress: the host answered ''${status:-nothing}" >&2 cat <&3 >&2 exit 2 fi @@ -92,7 +92,7 @@ let i=$((i + 1)) done if test $i != $#; then - echo "fc-egress: $i answers for $# URIs" >&2 + echo "rig-egress: $i answers for $# URIs" >&2 exit 2 fi exit $all @@ -264,8 +264,8 @@ let (pkgs.runCommand "mount.vsock9p" { } "install -D ${mountVsock9p} $out/bin/mount.vsock9p") ]; - # fc-egress check URI..., see fc-egress above. - environment.systemPackages = [ fc-egress ]; + # rig-egress check URI..., see rig-egress above. + environment.systemPackages = [ rig-egress ]; # sshd, started on demand (a layer may turn that off), with the host # key a secret (--secret ssh_host_ed25519_key=FILE; see rig-secrets), @@ -326,7 +326,7 @@ let # (if it's there; another one: security.pki.certificateFiles). environment.etc."resolv.conf".text = "nameserver 172.16.0.1\n"; security.pki.certificateFiles = - lib.optional (builtins.pathExists /var/lib/fcbr0-egress/ca.pem) /var/lib/fcbr0-egress/ca.pem; + lib.optional (builtins.pathExists /var/lib/rig-proxy/ca.pem) /var/lib/rig-proxy/ca.pem; systemd.network.enable = true; systemd.network.wait-online.enable = false; systemd.network.networks.eth = { @@ -584,8 +584,8 @@ pkgs.writeShellScriptBin "run-rig" /* sh */ '' # layer) doesn't start without --home. # --mem-size sets the guest's memory, in MiB (default 1024). # The guest's egress goes by the user's whitelist LIST (default: the - # VM's name, or without one, default), see modules/host and fc-egress. - # --name names the VM, for fc-egress and as the guest's host name; only + # VM's name, or without one, default), see modules/host and rig-egress. + # --name names the VM, for rig-egress and as the guest's host name; only # one running VM of the user's can have a given name. --vcpus: the # guest's number of CPUs (default 2; Firecracker takes 1 to 32). # --secret NAME=FILE: a secret for the guest, NAME letters, digits, '.', @@ -776,12 +776,12 @@ pkgs.writeShellScriptBin "run-rig" /* sh */ '' fi # Take the first tap device of the user's that no other VM holds, from - # the ones modules/host lists in /etc/fc-taps (TAP USER IP MAC, with the + # the ones modules/host lists in /etc/rig-taps (TAP USER IP MAC, with the # tap's own address). The lock is held until the script exits. The # guest's address, and the MAC address that DHCP hands it out by, is - # its VM's, if it has a name in /etc/fc-vms (VM USER IP MAC), else the + # its VM's, if it has a name in /etc/rig-vms (VM USER IP MAC), else the # tap's own; modules/host gives the tap that address once the VM's name - # is in /run/fc-egress/TAP.name (see below). + # is in /run/rig-egress/TAP.name (see below). # Lock files in a directory of the user's own: in /tmp, another user # could take them first. lockdir=''${XDG_RUNTIME_DIR:-/run/user/$(id -u)} @@ -789,8 +789,8 @@ pkgs.writeShellScriptBin "run-rig" /* sh */ '' echo "$0: $lockdir isn't a directory of $(id -un)'s; set XDG_RUNTIME_DIR" >&2 exit 1 fi - if ! test -r /etc/fc-taps; then - echo "$0: no /etc/fc-taps, see modules/host" >&2 + if ! test -r /etc/rig-taps; then + echo "$0: no /etc/rig-taps, see modules/host" >&2 exit 1 fi me=$(id -un) @@ -807,25 +807,25 @@ pkgs.writeShellScriptBin "run-rig" /* sh */ '' break fi exec {lock}>&- - done < /etc/fc-taps + done < /etc/rig-taps if test -z "$tap"; then - echo "$0: no free tap device fctap* of $me's, see modules/host" >&2 + echo "$0: no free tap device rigtap* of $me's, see modules/host" >&2 exit 1 fi - if test -n "$name" && test -r /etc/fc-vms; then + if test -n "$name" && test -r /etc/rig-vms; then while read -r v u a m; do if test "$v $u" = "$name $me"; then addr=$a mac=$m fi - done < /etc/fc-vms + done < /etc/rig-vms fi # The VM's name: held by a lock, like the tap, so it's unique among the - # user's running VMs. Not like a tap's, so fc-egress can tell them + # user's running VMs. Not like a tap's, so rig-egress can tell them # apart. if test -n "$name"; then if ! printf %s "$name" | grep -qxE '[A-Za-z0-9][A-Za-z0-9-]{0,62}' || - printf %s "$name" | grep -qxE 'fctap[0-9]+'; then + printf %s "$name" | grep -qxE 'rigtap[0-9]+'; then echo "$0: bad name: $name (letters, digits and -, not like a tap)" >&2 exit 1 fi @@ -838,26 +838,26 @@ pkgs.writeShellScriptBin "run-rig" /* sh */ '' args="$args systemd.hostname=$name" fi # The guest's whitelist: modules/host goes by the name in the tap's file - # in /run/fc-egress (emptied again when the guest exits, below). - vm=/run/fc-egress/$tap + # in /run/rig-egress (emptied again when the guest exits, below). + vm=/run/rig-egress/$tap if test -w "$vm"; then if ! printf %s "$egress" | grep -qxE '[A-Za-z0-9_-]+'; then echo "$0: bad list name: $egress" >&2 exit 1 fi - if ! test -f "/var/lib/fc-egress/$(id -un)/$egress"; then - echo "$0: no list $egress (see fc-egress), no egress" >&2 + if ! test -f "/var/lib/rig-egress/$(id -un)/$egress"; then + echo "$0: no list $egress (see rig-egress), no egress" >&2 fi printf '%s\n' "$egress" > "$vm" printf '%s\n' "$name" > "$vm.name" # The tap's address, by the VM's name (see above): wait for it, up to # 10s, writing the name again every second, in case modules/host - # missed it (fcbr0-firewall@TAP was busy). + # missed it (rig-firewall@TAP was busy). n=0 - while test "$(head -n 1 /run/fcbr0-addr/$tap 2>/dev/null)" != "$addr"; do + while test "$(head -n 1 /run/rig-addr/$tap 2>/dev/null)" != "$addr"; do n=$((n + 1)) if test $n -gt 100; then - echo "$0: $tap didn't get $addr, see fcbr0-firewall@$tap on the host" >&2 + echo "$0: $tap didn't get $addr, see rig-firewall@$tap on the host" >&2 exit 1 fi if test $((n % 10)) = 0; then diff --git a/tests/qemu/firecracker.sh b/tests/qemu/firecracker.sh index 08f766d..fcc217c 100644 --- a/tests/qemu/firecracker.sh +++ b/tests/qemu/firecracker.sh @@ -2,7 +2,7 @@ # run script where there's no Firecracker: boots the same kernel, initrd, # drives, vsock (vhost-device-vsock does the same uds_path_PORT # convention) and network interface (qemu's user networking; guest port 22 -# forwarded to 127.0.0.1:${FC_TEST_SSH_PORT:-2222}) in qemu, with +# forwarded to 127.0.0.1:${RIG_TEST_SSH_PORT:-2222}) in qemu, with # virtio-pci instead of virtio-mmio (see debug.nix). set -efu config=$3 @@ -23,5 +23,5 @@ qemu-system-x86_64 -machine accel=kvm:tcg -nographic -no-reboot \ -initrd "$(get '."boot-source".initrd_path')" -append "$args" \ -chardev socket,id=vs,path="$vhost" -device vhost-user-vsock-pci,chardev=vs \ $(jq -r '.drives[] | "-drive file=\(.path_on_host),if=virtio,format=raw" + (if .is_read_only then ",readonly=on" else "" end)' "$config") \ - -netdev user,id=n,hostfwd=tcp:127.0.0.1:"${FC_TEST_SSH_PORT:-2222}"-:22 \ + -netdev user,id=n,hostfwd=tcp:127.0.0.1:"${RIG_TEST_SSH_PORT:-2222}"-:22 \ -device virtio-net-pci,netdev=n,mac="$(get '."network-interfaces"[0].guest_mac')" diff --git a/tests/vm-host.nix b/tests/vm-host.nix index 569d874..a0e5cda 100644 --- a/tests/vm-host.nix +++ b/tests/vm-host.nix @@ -1,8 +1,8 @@ # NixOS VM test of modules/host: the module for real (systemd units, path # units, timer, nftables, dnsmasq, nginx), with network namespaces on -# veths in place of tv's taps fctap1 and fctap2 as guests: +# veths in place of tv's taps rigtap1 and rigtap2 as guests: # g1 as the VM box, with box's address (172.16.0.10, MAC 06:00:ac:10:00:0a), -# g2 without a name, with fctap2's own (172.16.0.202, ...:ca); SLAAC gives +# g2 without a name, with rigtap2's own (172.16.0.202, ...:ca); SLAAC gives # them the IPv6 addresses the host expects from those MACs. Another netns # is an upstream with 42::1 and 198.51.100.1, there for the names *.test # (a dnsmasq on the host's lo, as nginx's resolver), with an HTTPS server @@ -20,10 +20,10 @@ let # Test certificates (keys in the store, for the test only): a CA, the # upstream's certificate for the names below from it, and a self-signed # one for untrusted.test. - certs = pkgs.runCommand "fc-test-certs" { nativeBuildInputs = [ pkgs.openssl ]; } '' + certs = pkgs.runCommand "rig-test-certs" { nativeBuildInputs = [ pkgs.openssl ]; } '' mkdir $out && cd $out openssl req -x509 -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \ - -days 3650 -subj /CN=fc-test-ca -keyout ca.key -out ca.pem \ + -days 3650 -subj /CN=rig-test-ca -keyout ca.key -out ca.pem \ -addext basicConstraints=critical,CA:TRUE -addext keyUsage=critical,keyCertSign openssl req -x509 -CA ca.pem -CAkey ca.key -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \ -days 3650 -subj /CN=allowed.test -keyout server.key -out server.pem \ @@ -35,9 +35,9 @@ let ''; in pkgs.testers.runNixOSTest { - name = "fc-host"; + name = "rig-host"; nodes.host = { pkgs, ... }: { - # alice: fctap0, tv: fctap1 and fctap2 + # alice: rigtap0, tv: rigtap1 and rigtap2 imports = [ (import ../modules/host { users = { tv = 2; alice = 1; }; vms.box = { user = "tv"; ip = "172.16.0.10"; }; @@ -60,7 +60,7 @@ pkgs.testers.runNixOSTest { }; security.pki.certificateFiles = [ "${certs}/ca.pem" ]; # As the host's firewall has to (see README.md). - networking.firewall.trustedInterfaces = [ "fcbr0" ]; + networking.firewall.trustedInterfaces = [ "rigbr0" ]; environment.systemPackages = [ pkgs.socat pkgs.dig pkgs.curl pkgs.openssl ]; virtualisation.memorySize = 1024; }; @@ -69,18 +69,18 @@ pkgs.testers.runNixOSTest { g2_ip6 = "fdfc:fc::400:acff:fe10:ca" host.wait_for_unit("multi-user.target") - host.wait_for_unit("fcbr0-dnsmasq.service") - host.wait_for_unit("fcbr0-firewall.service") - # box on fctap1, as the run script does it - host.succeed("su tv -c 'echo box > /run/fc-egress/fctap1.name'") - host.wait_until_succeeds("test \"$(head -n 1 /run/fcbr0-addr/fctap1)\" = 172.16.0.10", timeout=60) + host.wait_for_unit("rig-dnsmasq.service") + host.wait_for_unit("rig-firewall.service") + # box on rigtap1, as the run script does it + host.succeed("su tv -c 'echo box > /run/rig-egress/rigtap1.name'") + host.wait_until_succeeds("test \"$(head -n 1 /run/rig-addr/rigtap1)\" = 172.16.0.10", timeout=60) host.succeed(""" set -e guest() { # N TAP IP MAC ip link del $2 ip netns add g$1 ip link add $2 type veth peer name eth0 netns g$1 - ip link set $2 master fcbr0 up + ip link set $2 master rigbr0 up ip netns exec g$1 sysctl -qw net.ipv6.conf.all.use_tempaddr=0 net.ipv6.conf.default.use_tempaddr=0 ip netns exec g$1 ip link set eth0 address $4 ip netns exec g$1 ip link set lo up @@ -88,8 +88,8 @@ pkgs.testers.runNixOSTest { ip netns exec g$1 ip link set eth0 up ip netns exec g$1 ip route add default via 172.16.0.1 } - guest 1 fctap1 172.16.0.10 06:00:ac:10:00:0a - guest 2 fctap2 172.16.0.202 06:00:ac:10:00:ca + guest 1 rigtap1 172.16.0.10 06:00:ac:10:00:0a + guest 2 rigtap2 172.16.0.202 06:00:ac:10:00:ca ip netns add U ip link add up0 type veth peer name eth0 netns U ip link set up0 up @@ -122,11 +122,11 @@ pkgs.testers.runNixOSTest { print(f"{what}: {got}") assert got == want, f"{what}: got {got!r}, want {want!r}" - # rules via fc-egress and the path units - host.succeed("su tv -c 'fc-egress -l box allow tcp://[42::1]:22 tcp://[42::1]:443 tcp://sshhost6.test:22 tcp://198.51.100.1:22'") - host.fail("su tv -c 'fc-egress -l box allow tcp://[42::1/129]:22'") - host.succeed("su tv -c 'echo box > /run/fc-egress/fctap1'") - host.wait_until_succeeds("nft list set inet firecracker allow6_fctap1 | grep -q '42::1'", timeout=60) + # rules via rig-egress and the path units + host.succeed("su tv -c 'rig-egress -l box allow tcp://[42::1]:22 tcp://[42::1]:443 tcp://sshhost6.test:22 tcp://198.51.100.1:22'") + host.fail("su tv -c 'rig-egress -l box allow tcp://[42::1/129]:22'") + host.succeed("su tv -c 'echo box > /run/rig-egress/rigtap1'") + host.wait_until_succeeds("nft list set inet rigs allow6_rigtap1 | grep -q '42::1'", timeout=60) check("host reaches the upstream", host.succeed("timeout 5 socat -T3 - TCP6:[42::1]:22 /tmp/www/hello && echo ok > /tmp/www/ok/file && echo no > /tmp/www/no/file") host.succeed( "cd /tmp/www && ip netns exec U openssl s_server -quiet -WWW -accept 198.51.100.1:443" @@ -161,7 +161,7 @@ pkgs.testers.runNixOSTest { " -servername untrusted.test -cert2 ${certs}/untrusted.pem -key2 ${certs}/untrusted.key >&2 &" ) host.wait_until_succeeds("ip netns exec U ss -ltn | grep -q '198.51.100.1:443'", timeout=30) - host.wait_for_unit("fcbr0-egress.service") + host.wait_for_unit("rig-proxy.service") def https(g, url, ca, extra=""): name = url.split("/")[2] @@ -172,13 +172,13 @@ pkgs.testers.runNixOSTest { return "blocked" if out[-1] == "blocked" else f"{' '.join(out[:-1])} {out[-1]}" def status(g, url, extra=""): - return https(g, url, fc_ca, extra).split()[-1] + return https(g, url, proxy_ca, extra).split()[-1] - host.succeed("su tv -c 'fc-egress -l box allow https://allowed.test https://paths.test/ok/ https://untrusted.test/ok/'") + host.succeed("su tv -c 'rig-egress -l box allow https://allowed.test https://paths.test/ok/ https://untrusted.test/ok/'") check("g1 allowed.test, passed through", https("g1", "https://allowed.test/hello", test_ca), "hello 200") - check("g1 allowed.test, not with our CA", https("g1", "https://allowed.test/hello", fc_ca), "blocked") + check("g1 allowed.test, not with our CA", https("g1", "https://allowed.test/hello", proxy_ca), "blocked") check("g1 sub.allowed.test, subdomain", https("g1", "https://sub.allowed.test/hello", test_ca), "hello 200") - check("g1 paths.test/ok/, decrypted", https("g1", "https://paths.test/ok/file", fc_ca), "ok 200") + check("g1 paths.test/ok/, decrypted", https("g1", "https://paths.test/ok/file", proxy_ca), "ok 200") check("g1 paths.test/ok/, not the upstream's certificate", https("g1", "https://paths.test/ok/file", test_ca), "blocked") check("g1 paths.test/no/", status("g1", "https://paths.test/no/file"), "403") check("g1 paths.test/ok/../no/", status("g1", "https://paths.test/ok/../no/file", "--path-as-is"), "403") @@ -189,11 +189,11 @@ pkgs.testers.runNixOSTest { check("g1 untrusted.test, upstream not verified", status("g1", "https://untrusted.test/ok/file"), "502") check("g1 https checks", check_uris("g1", "https://allowed.test", "https://sub.allowed.test/x", "https://paths.test/ok/file", "https://paths.test/no/file", "https://denied.test"), ["true", "true", "true", "false", "false"]) - host.wait_until_succeeds("journalctl -t fc_egress --no-pager | grep -q 'ALLOW tv fctap1 box https://allowed.test'", timeout=30) - host.wait_until_succeeds("journalctl -t fc_egress --no-pager | grep -q 'DENY tv fctap1 box https://denied.test'", timeout=30) - host.wait_until_succeeds("journalctl -t fc_egress --no-pager | grep -qE 'DENY .*GET https://paths.test/no/file'", timeout=30) + host.wait_until_succeeds("journalctl -t rig_egress --no-pager | grep -q 'ALLOW tv rigtap1 box https://allowed.test'", timeout=30) + host.wait_until_succeeds("journalctl -t rig_egress --no-pager | grep -q 'DENY tv rigtap1 box https://denied.test'", timeout=30) + host.wait_until_succeeds("journalctl -t rig_egress --no-pager | grep -qE 'DENY .*GET https://paths.test/no/file'", timeout=30) # rules apply to the next connection, without reloading anything - host.succeed("su tv -c 'fc-egress -l box deny https://allowed.test && fc-egress -l box allow https://denied.test'") + host.succeed("su tv -c 'rig-egress -l box deny https://allowed.test && rig-egress -l box allow https://denied.test'") check("g1 allowed.test after deny", https("g1", "https://allowed.test/hello", test_ca), "blocked") check("g1 denied.test after allow", https("g1", "https://denied.test/hello", test_ca), "hello 200") ''; -- cgit v1.3.1