diff --git a/deploy/nixos/module.nix b/deploy/nixos/module.nix index 34699d1..c10a36a 100644 --- a/deploy/nixos/module.nix +++ b/deploy/nixos/module.nix @@ -20,6 +20,14 @@ let pkg = cfg.package; stateRoot = "/var/lib/ianitor"; runProof = "/run/ianitor-proof"; + # Control's UI socket (G14 option b): its directory is control's RuntimeDirectory, 0750 + # ianitor-control:ianitor, so only the ianitor group (edge, Caddy) can reach it and no other + # user can put a socket there while control restarts. + uiSocket = "/run/ianitor-control/ui.sock"; + # The front proxy's socket (Caddy beside Ianitor): its directory is setgid ianitor-proxy, so + # the socket either engine makes there belongs to that group. + proxyDir = "/run/ianitor-proxy"; + proxySocket = "${proxyDir}/edge.sock"; toml = pkgs.formats.toml { }; isStore = p: lib.hasPrefix builtins.storeDir (toString p); @@ -42,7 +50,7 @@ let access_log = "/var/log/ianitor/access.log"; caddy_credentials_dir = "/run/credentials/caddy-fallback.service"; trusted_proxies = cfg.edge.trustedProxies; - }; + } // lib.optionalAttrs cfg.edge.proxySocket.enable { proxy_socket = proxySocket; }; controlToml = toml.generate "control.toml" ( { @@ -78,6 +86,7 @@ let static_dir = "${pkg}/share/ianitor/web"; } // lib.optionalAttrs (cfg.ui.hostname != null) { hostname = cfg.ui.hostname; } + // lib.optionalAttrs cfg.ui.socket { socket = uiSocket; } // lib.optionalAttrs (cfg.ui.logoutUrl != null) { logout_url = cfg.ui.logoutUrl; }; engine = { file = "${stateRoot}/engine"; @@ -147,11 +156,24 @@ in default = false; description = "Open edge's two ports in the firewall."; }; + proxySocket = { + enable = mkEnableOption '' + a Unix socket (${proxySocket}, 0660, group ianitor-proxy) on which edge (or the Caddy + fallback) serves the HTTPS hosts in plain HTTP to a front proxy that terminated TLS, + such as Caddy beside Ianitor. Only that group can connect, so its X-Forwarded-For is + believed; no loopback TCP port is involved''; + users = mkOption { + type = types.listOf types.str; + default = [ ]; + example = [ "caddy" ]; + description = "The front proxy's users, added to the ianitor-proxy group so they can connect."; + }; + }; trustedProxies = mkOption { type = types.listOf types.str; default = [ ]; example = [ "127.0.0.1" ]; - description = "Proxies in front of edge whose X-Forwarded-For names the client (beside Caddy: 127.0.0.1), for the share-link throttle and the access log. Off by default: on a loopback listener any local process could then claim any address (docs/deploy/beside-caddy.md)."; + description = "Proxies in front of edge whose X-Forwarded-For names the client, for the share-link throttle and the access log. Off by default: on a loopback listener any local process could then claim any address. Beside Caddy, use edge.proxySocket instead (docs/deploy/beside-caddy.md)."; }; }; @@ -229,6 +251,15 @@ in example = "ianitor.example"; description = "The admin host edge serves the UI on (a host in the routes repository with forward-auth and the X-Ianitor-Proof rule, docs/api.md)."; }; + socket = mkOption { + type = types.bool; + default = true; + description = '' + Serve the API for the admin host on ${uiSocket} as well, and trust the UI proof only + there (G14 option b): the admin host's upstream is then `unix:${uiSocket}`. Over the + loopback port, which every local process can reach, only API tokens work. + ''; + }; logoutUrl = mkOption { type = types.nullOr types.str; default = null; @@ -255,8 +286,19 @@ in # Only control (and Caddy itself) may reach Caddy's admin socket; edge is not in it. users.groups.ianitor-caddy-admin = { }; users.users.ianitor-control = { isSystemUser = true; group = "ianitor"; extraGroups = [ "ianitor-caddy-admin" ]; home = "${stateRoot}/control"; }; - users.users.ianitor-edge = { isSystemUser = true; group = "ianitor"; }; - users.users.caddy-fallback = { isSystemUser = true; group = "ianitor"; }; + users.users.ianitor-edge = { + isSystemUser = true; + group = "ianitor"; + extraGroups = lib.optional cfg.edge.proxySocket.enable "ianitor-proxy"; + }; + users.users.caddy-fallback = { + isSystemUser = true; + group = "ianitor"; + extraGroups = lib.optional cfg.edge.proxySocket.enable "ianitor-proxy"; + }; + users.groups.ianitor-proxy = mkIf cfg.edge.proxySocket.enable { + members = cfg.edge.proxySocket.users; + }; systemd.tmpfiles.rules = [ "d ${stateRoot} 0755 ianitor-control ianitor - -" @@ -268,7 +310,7 @@ in "d ${stateRoot}/caddy-state 0700 caddy-fallback ianitor - -" "f ${stateRoot}/engine 0644 ianitor-control ianitor - ${cfg.defaultEngine}" "d /var/log/ianitor 0770 ianitor-edge ianitor - -" - ]; + ] ++ lib.optional cfg.edge.proxySocket.enable "d ${proxyDir} 2770 root ianitor-proxy - -"; environment.systemPackages = [ pkg ]; # The non-secret config, so the CLI (token, pki) finds it: --config /etc/ianitor/control.toml. @@ -308,6 +350,9 @@ in ExecStopPost = "${pkg}/bin/ianitor-control caddy-revert --config ${controlToml}"; LoadCredential = loadCreds controlCreds ++ [ "ui-proof:${runProof}/ui-proof" ]; ReadWritePaths = [ stateRoot ]; + # The UI socket's directory: 0750, so the ianitor group may connect and nobody else. + RuntimeDirectory = "ianitor-control"; + RuntimeDirectoryMode = "0750"; Restart = "on-failure"; RestartSec = 2; }; @@ -334,7 +379,8 @@ in LoadCredential = loadCreds edgeCreds ++ [ "ui-proof:${runProof}/ui-proof" ]; RuntimeDirectory = "ianitor-edge"; RuntimeDirectoryMode = "0750"; - ReadWritePaths = [ "${stateRoot}/edge-state" "/var/log/ianitor" ]; + ReadWritePaths = [ "${stateRoot}/edge-state" "/var/log/ianitor" ] + ++ lib.optional cfg.edge.proxySocket.enable proxyDir; AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ]; # The first start may come before control has written edge.json. @@ -366,7 +412,8 @@ in LoadCredential = loadCreds cfg.credentials ++ [ "ui-proof:${runProof}/ui-proof" ]; RuntimeDirectory = "caddy-fallback"; RuntimeDirectoryMode = "0750"; - ReadWritePaths = [ "${stateRoot}/caddy-state" "/var/log/ianitor" ]; + ReadWritePaths = [ "${stateRoot}/caddy-state" "/var/log/ianitor" ] + ++ lib.optional cfg.edge.proxySocket.enable proxyDir; AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ]; # Go needs W^X for its runtime on some platforms. diff --git a/deploy/nixos/tests/default.nix b/deploy/nixos/tests/default.nix index f239d08..c0e06c8 100644 --- a/deploy/nixos/tests/default.nix +++ b/deploy/nixos/tests/default.nix @@ -226,6 +226,56 @@ in server.fail("find /var/lib/ianitor -name 'domi-root.key' | grep -q .") ''; + # G14 option b and the proxy socket (phase 4): another local user can neither open control's + # UI socket nor use the proof over the loopback port; the front proxy's socket is for the + # ianitor-proxy group only. + vm-sockets = pkgs.testers.runNixOSTest { + name = "ianitor-sockets"; + nodes.server = { + imports = [ server ]; + services.ianitor.edge.proxySocket = { enable = true; users = [ "front" ]; }; + services.ianitor.ui.hostname = "ui.test"; + users.users.front.isNormalUser = true; + users.users.mallory.isNormalUser = true; + }; + testScript = helpers + '' + start_all() + t = setup(server) + install_intermediate(server, t) + server.succeed("test -S /run/ianitor-control/ui.sock") + assert server.succeed("stat -c %a /run/ianitor-control/ui.sock").strip() == "660" + assert server.succeed("stat -c %a /run/ianitor-control").strip() == "750" + # Another local user cannot reach the UI socket ... + server.fail("runuser -u mallory -- curl -sf -m 5 --unix-socket /run/ianitor-control/ui.sock http://localhost/api/v1/overview") + # ... nor sign in over the loopback port, even with the real proof (read here as root). + proof = server.succeed("cat /run/ianitor-proof/ui-proof").strip() + code = server.succeed( + "runuser -u mallory -- curl -s -o /dev/null -w '%{http_code}' -H 'Remote-User: mallory' " + + "-H 'X-Ianitor-Proof: " + proof + "' http://127.0.0.1:7878/api/v1/overview" + ).strip() + assert code == "401", code + # The same over the socket, as edge (group ianitor): signed in. + code = server.succeed( + "runuser -u ianitor-edge -- curl -s -o /dev/null -w '%{http_code}' -H 'Host: ui.test' " + + "-H 'Remote-User: mark' -H 'X-Ianitor-Proof: " + proof + "' " + + "--unix-socket /run/ianitor-control/ui.sock http://ui.test/api/v1/overview" + ).strip() + assert code == "200", code + + # The front proxy's socket: the front user may connect, mallory may not. + r = api(server, t, "POST", "/hosts", {"host": { + "hostname": "app.test", "upstreams": ["http://127.0.0.1:8000"], "tls": "domi", "auth": "none"}}) + assert wait_apply(server, t, r["apply"]["id"])["state"] == "kept" + server.wait_until_succeeds("test -S /run/ianitor-proxy/edge.sock") + assert server.succeed("stat -c '%a %G' /run/ianitor-proxy/edge.sock").strip() == "660 ianitor-proxy" + code = server.succeed( + "runuser -u front -- curl -s -o /dev/null -w '%{http_code}' --unix-socket /run/ianitor-proxy/edge.sock http://app.test/" + ).strip() + assert code == "200", code + server.fail("runuser -u mallory -- curl -sf -m 5 --unix-socket /run/ianitor-proxy/edge.sock http://app.test/") + ''; + }; + vm-control-loopback = pkgs.testers.runNixOSTest { name = "ianitor-control-loopback"; nodes.server = server; diff --git a/docs/api.md b/docs/api.md index 8674ae6..a5751cb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -243,7 +243,7 @@ a host in the routes repository with forward-auth to Authelia and one header rul ```toml # hosts/ianitor.example.toml hostname = "ianitor.example" -upstreams = ["http://127.0.0.1:7878"] +upstreams = ["unix:/run/ianitor-control/ui.sock"] # [ui] socket (the NixOS module's default) tls = "domi" auth = "forward-auth" @@ -266,11 +266,18 @@ secret = "ui-proof" # listed in ianitor.toml: secrets = ["ui-proof"] - Any request with a `Remote-*` header but without the proof is `401 ui.no_proof`, so a local process on loopback cannot claim a user; the proof without `Remote-User` is `401 ui.no_user`. Tested in `crates/control/tests/ui_auth.rs`, including end to end through edge and a mock Authelia. +- **The UI socket (G14 option b, phase 4).** With `[ui] socket = "/run/ianitor-control/ui.sock"` + (the NixOS module sets it; `services.ianitor.ui.socket`, default on), control also serves the API + on that Unix socket, mode 0660, in its runtime directory (0750, ianitor-control:ianitor): only + the ianitor group (edge, caddy-fallback) can connect, and no other user can create a socket + there. The proof then counts **only on the socket**: over the loopback port it is + `401 ui.no_proof` even when it is right. Why: any local process can reach 127.0.0.1:7878, and + one that took that port while control restarted would receive edge's requests, proof and all. + The admin host's upstream is `unix:/run/ianitor-control/ui.sock`; control refuses a socket + upstream on any other host, and any socket other than its own (`host.upstream.unix_socket`). + The loopback port stays for API tokens (scripts, the CLI, the trial's SSH tunnel). - `[ui] logout_url` (Authelia's logout address) becomes `overview.site.auth_logout_url`, the UI's - "Sign out". A Unix socket only edge can reach is Phase 4 hardening (decision Q2). - -Why not the Unix socket now: edge's upstreams are `host:port` in the schema; a socket upstream is a -schema and compiler change in both engines, and the proof header needs neither. + "Sign out". ## Certificates (the Domi CA) diff --git a/docs/deploy/beside-caddy.md b/docs/deploy/beside-caddy.md index 1aa6e03..e0949a7 100644 --- a/docs/deploy/beside-caddy.md +++ b/docs/deploy/beside-caddy.md @@ -24,22 +24,24 @@ cut-over. This is **prepared, not applied**: Mark runs every command below himse ## The shape ```text -visitor ── Caddy :443 (as today) ── https://127.0.0.1:18443 Ianitor edge ── the app +visitor ── Caddy :443 (as today) ── unix:/run/ianitor-proxy/edge.sock ── Ianitor edge ── the app ``` -- Ianitor edge listens on **loopback only** (127.0.0.1:18080 and :18443), so D-21a holds: - nothing new is reachable from the LAN. Control is on 127.0.0.1:7878. -- Caddy keeps the public side for the two names and hands them to Ianitor over TLS; Ianitor - serves them with Domi certificates, which Caddy checks against the Domi root (a public file). +- Caddy reaches edge on edge's **proxy socket** (`services.ianitor.edge.proxySocket`, phase 4): + `/run/ianitor-proxy/edge.sock`, mode 0660, in a setgid directory of the group `ianitor-proxy`, + whose only members are edge, the Caddy fallback and Caddy (`proxySocket.users = [ "caddy" ]`). + Edge serves the HTTPS hosts there in plain HTTP (Caddy terminated TLS) and **believes the + X-Forwarded-For Caddy sends**, so the visit log and the share-link throttle see the visitor. + No other local user can connect, so none can claim an address; no loopback TCP port is + involved, and `trustedProxies` stays off. The Caddy fallback listens on the same socket + (compile option `proxy_socket`, a third Caddy server with `trusted_proxies_unix`), so an engine + switch keeps the two hosts reachable through Caddy. +- Edge's TCP listeners stay on **loopback only** (127.0.0.1:18080 and :18443), so D-21a holds: + nothing new is reachable from the LAN. Control is on 127.0.0.1:7878 (API tokens) and on its UI + socket `/run/ianitor-control/ui.sock` (the admin host, [../api.md](../api.md), "Sign-in"). - Every other host is untouched. Rollback: revert the patch and switch (below). -- Visitor addresses: Ianitor sees every visit coming from 127.0.0.1 (Caddy), so the share-link - throttle counts all visitors together and the visit log shows 127.0.0.0. The option - `services.ianitor.edge.trustedProxies = [ "127.0.0.1" ]` would make edge believe Caddy's - `X-Forwarded-For` instead, but edge listens on a TCP port on loopback that **any local user or - process can reach**, and any of them could then claim any address. It stays **off** for the - trial (the two hosts have no share links and no visit log); turn it on only on a machine where - every local account is trusted. (A Unix-socket listener for edge, which Caddy alone could reach, - is the proper fix; not built yet.) +- Until phase 4 this hop was TLS to 127.0.0.1:18443 with Domi certificates, and edge saw every + visitor as 127.0.0.1. The socket replaces it; the Domi root is no longer needed by Caddy. ## The patch diff --git a/docs/deploy/configuration-server.patch b/docs/deploy/configuration-server.patch index a003568..bf6c8e1 100644 --- a/docs/deploy/configuration-server.patch +++ b/docs/deploy/configuration-server.patch @@ -27,29 +27,26 @@ diff --git a/hyperv-infra/modules/caddy.nix b/hyperv-infra/modules/caddy.nix index e79f6b2..6fb5a49 100644 --- a/hyperv-infra/modules/caddy.nix +++ b/hyperv-infra/modules/caddy.nix -@@ -225,6 +225,21 @@ in +@@ -225,6 +225,18 @@ in Referrer-Policy "strict-origin-when-cross-origin" } ''; + # The Ianitor trial (modules/ianitor.nix): Caddy keeps the public side and hands the request -+ # to Ianitor edge on loopback, over TLS with a Domi certificate it checks against the Domi -+ # root. Host and SNI are the visitor's name. ++ # to Ianitor edge on its proxy socket, /run/ianitor-proxy/edge.sock (0660, group ++ # ianitor-proxy, which Caddy joins and no other local user is in). Edge believes the ++ # X-Forwarded-For Caddy sends there, so the visit log and the share-link throttle see the ++ # visitor; no loopback TCP port is involved. Host is the visitor's name. + viaIanitor = host: { + extraConfig = '' + tls internal + ${secureHeaders "SAMEORIGIN"} -+ reverse_proxy https://127.0.0.1:18443 { -+ transport http { -+ tls_server_name ${host} -+ tls_trusted_ca_certs ${../ianitor/domi-root.crt} -+ } -+ } ++ reverse_proxy unix//run/ianitor-proxy/edge.sock + ''; + }; simple = { frame ? "SAMEORIGIN", upstream }: { extraConfig = '' tls internal -@@ -252,7 +267,11 @@ in +@@ -252,7 +264,11 @@ in # flowers - was `localhost:8085`, which is PRODUCTION's port. On the mirror the app is # published on 127.0.0.1:8092 and fronted by nginx on 8091, matching production's topology # (nginx in front, not Caddy straight to the container). Proxying to 8085 reached nothing. @@ -67,7 +64,7 @@ new file mode 100644 index 0000000..cd23016 --- /dev/null +++ b/hyperv-infra/modules/ianitor.nix -@@ -0,0 +1,49 @@ +@@ -0,0 +1,52 @@ +{ config, lib, inputs, ... }: + +# Ianitor beside Caddy (Ianitor ROADMAP §1.12, Phase 3): two low-risk hosts, flowers and esp32, @@ -76,11 +73,12 @@ index 0000000..cd23016 +# Shape, chosen to keep decision D-21a ("Caddy is the only thing on 80/443 and the only service +# exposed beyond loopback"): +# -+# visitor -> Caddy :443 (tls as today) -> https://127.0.0.1:18443 Ianitor edge -> the app ++# visitor -> Caddy :443 (tls as today) -> unix socket /run/ianitor-proxy/edge.sock -> Ianitor edge -> the app +# -+# Ianitor edge listens on loopback only (18080/18443) and terminates TLS with a Domi certificate; -+# Caddy trusts the Domi root (a public certificate, ianitor/domi-root.crt) for that hop. Control -+# listens on 127.0.0.1:7878. Nothing new is reachable from the LAN. ++# Ianitor edge serves Caddy on its proxy socket (group ianitor-proxy: Caddy is the only member), ++# and believes the X-Forwarded-For Caddy sends there. Its TCP listeners stay on loopback only ++# (18080/18443, Domi certificates). Control listens on 127.0.0.1:7878 for API tokens and on its ++# UI socket for the admin host. Nothing new is reachable from the LAN. +# +# Secrets come from sops-nix (paths under /run/secrets, never the store); the module asserts it. +# Rollback: remove this import and the two `viaIanitor` lines in caddy.nix, nixos-rebuild switch. @@ -100,6 +98,8 @@ index 0000000..cd23016 + httpListen = "127.0.0.1:18080"; + httpsListen = "127.0.0.1:18443"; + openFirewall = false; ++ # Caddy reaches edge here; only Caddy joins the socket's group. ++ proxySocket = { enable = true; users = [ "caddy" ]; }; + }; + control.listen = "127.0.0.1:7878"; + shareLinkKeyFile = config.sops.secrets."ianitor/share-link-key".path;