Repository navigation
rtl: pin the TLS certificates of lnd and clnrest - #258
Conversation
viberaven
left a comment
There was a problem hiding this comment.
Reviewed fe91473f28dae202e9a83b0758bbda46f03d0303 against #151. I found no new blocking implementation defects in the certificate-pinning change. It addresses the backend credential-disclosure attack, but it does not implement everything outlined in #151.
- Stop RTL with its backends: already implemented by merged PR #250 and retained here.
- Verify backend certificates: implemented here. Pinning clnrest's server certificate rather than its CA is the right distinction: another certificate issued by that CA must not receive the rune. The URL-override guards also prevent the documented configuration paths from silently bypassing the pins.
- Scoped lnd macaroon: still outstanding.
rtl-copy-macaroonstill copiesadmin.macaroon; this PR does not reduce RTL's lnd permissions. - TLS or a Unix socket for the web UI: still outstanding. The UI remains HTTP, so the separate login-page impersonation attack described in #151 remains possible without namespace isolation. Backend TLS verification does not protect that listener.
- Document the netns mitigation: the service guide already describes how to connect with netns isolation, but does not explicitly explain that it prevents an unprivileged process in the host namespace from taking over these listeners. That sentence would still be useful, particularly for the remaining UI threat.
Keeping the scoped-macaroon and UI work separate is reasonable. Please create and link follow-up issues for them before retaining Fixes #151, or describe this as partially addressing #151 so the outstanding work is not lost when GitHub closes it.
The existing rebase request also still applies: GitHub reports merge conflicts. Move the migration notice into a separate file under modules/versioning/0.1.6/ and add descriptive #### headings to the new release/upgrade entries when rebasing. This is a merge prerequisite, not a new finding.
Validation on the reviewed head:
module-configandtest-runnerpassed locally (9 runner tests).- The full
regtestVM scenario passed locally, includingrtl-credentials,rtl-backend-certs, andrtl-stops-with-backend. The PR's requiredci_gateis also green. - Additional local probes using the flake's RTL package and Node runtime:
request.js,ws, andsocket.io-clientall sent requests with the correct pin and sent zero application bytes with a different leaf certificate from the same CA. The HTTP CONNECT proxy probe also failed certificate verification with zero application bytes, consistent with the documented restriction.
Review performed with Codex (GPT-6 Astra).
fe91473 to
5f64230
Compare
|
Rebased onto The scoped credentials and the UI TLS are now #261 and #262, linked in the PR body. #262 also has the netns sentence. So On the new head, |
seberm
left a comment
There was a problem hiding this comment.
LGTM. I will rebase and merge this PR so we can finish the v0.1.6 version. Thanks!
Reviewed 5f64230. Nothing blocking; all earlier requests are done (versioning file under modules/versioning/0.1.6/, #### headings, follow-ups #261/#262).
Checked beyond CI:
hardenedVM scenario on this head: passes, includingrtl-credentials,rtl-backend-certsandrtl-stops-with-backend.- The proxy note in
docs/upgrading.md, with RTL 0.15.17'sbackend/utils/request.jsand the preload against a pinned fake backend: through an HTTP CONNECT proxy the request fails withDEPTH_ZERO_SELF_SIGNED_CERTand the backend receives 0 bytes (https-proxy-agentdrops host and port beforetls.connect, so no pin matches); withno_proxyit succeeds. Without the preload, the macaroon header reaches the backend through the proxy. So the documented behaviour is right.
Assisted-by: Claude Code (Claude Fable 5.1)
There was a problem hiding this comment.
@ostermayer It would be nice if this could be covered upstream. What do you think?
One follow-up on the TODO-EXTERNAL in modules/rtl/pin-backend-certs.cjs: the right fix is upstream. RTL hardcodes rejectUnauthorized: false in request.js, common.js and both webSocketClient.js, with no option for a CA or a pinned cert (that goes back to Ride-The-Lightning/RTL#20). There's no upstream issue or PR for backend certificate verification yet, and upstream has been merging hardening PRs this year.
Could you open an upstream issue (and a PR if you have the time) for a per-node cert option, e.g. Authentication.lnCertPath, used as ca with rejectUnauthorized: true in all four places? Then add the issue URL to the TODO-EXTERNAL comments here and in rtl.nix, like modules/lndconnect.nix does, so we know when the preload can go. Not blocking for this PR; a follow-up commit is fine if the issue isn't open yet.
Assisted-by: Claude Code (Claude Fable 5.1)
RTL sends the lnd admin macaroon and the clightning admin rune to its backends with `rejectUnauthorized: false`, and it has no option for a CA. Since bitcoinix#250, RTL stops with its backends. But clightning's start only waits until clnrest's port accepts a connection, and any listener passes that check. A local user who takes the port first gets the rune. Preload a small Node module into RTL that wraps `tls.connect`. A connection to a backend's address and port must present the backend's own cert: lnd's cert, or clnrest's server cert from the cert dir that clightning reports. Pinning clnrest's server cert instead of its CA keeps out other certs from that CA. Every other connection gets `rejectUnauthorized: true` and the normal checks against the system CAs. So a backend reached through another address or a proxy fails, because the system CAs don't trust its cert, and fiat rate requests still work. Node's own check drops the connection before any request data goes out. RTL doesn't start when the pins are missing or malformed, or when `LN_SERVER_URL` or `LND_SERVER_URL` is set: RTL would use them instead of the configured URLs, even over plain HTTP. For the same reason, an assertion rejects `Settings.lnServerUrl` in a node's `extraConfig`. Another assertion keeps the preload in `NODE_OPTIONS`. Assisted-by: Claude Code (Claude Opus 5.5)
5f64230 to
7d5ca8f
Compare
This PR contains the following updates: | Package | Update | Change | |---|---|---| | [bitcoinix/bitcoinix](https://github.com/bitcoinix/bitcoinix) | patch | `v0.1.5` → `v0.1.6` | --- ### Release Notes <details> <summary>bitcoinix/bitcoinix</summary> ### [`v0.1.6`](https://github.com/bitcoinix/bitcoinix/releases/tag/v0.1.6): bitcoinix 0.1.6 [Compare Source](bitcoinix/bitcoinix@v0.1.5...v0.1.6) **Security release with breaking changes.** Narrows Tor enforcement to local addresses, closes a netns-isolation bypass, hardens RTL and the custom lnd macaroons, and updates lnd to 0.21.8-beta. All users should upgrade. Evaluation fails with a message for each breaking change that affects your config; act on it, then set `nix-bitcoin.configVersion = "0.1.6";`. Deploying restarts every netns-isolated service and each RTL backend once. #### Security - **Tor enforcement allowed more than Tor.** Services with `tor.enforce` and charge-lnd could reach every link-local address `169.254.0.0/16`, including a cloud provider's metadata service, and could resolve names through the host's resolver sockets. They now reach only localhost and, with netns-isolation, the namespace subnet, and can't use the nscd, systemd-resolved, D-Bus and Avahi sockets ([#​264](bitcoinix/bitcoinix#264), [GHSA-cgh8-4w2p-8fr8](GHSA-cgh8-4w2p-8fr8)). - **netns-isolation:** with `br_netfilter` loaded, which Docker does, a service without `tor.enforce` could reach the listeners of namespaces it isn't connected to. Traffic between namespaces is no longer masqueraded ([#​239](bitcoinix/bitcoinix#239)). - **RTL verifies its backends.** RTL sent the lnd admin macaroon and the clightning admin rune to any server on a backend's port. It now requires the backend's pinned TLS certificate ([#​258](bitcoinix/bitcoinix#258)) and stops when lnd or clightning stops, instead of polling a port that another local user could take over ([#​250](bitcoinix/bitcoinix#250)). - **RTL two-factor authentication survives restarts.** Before, each restart turned 2FA off without a warning. `RTL-Config.json`, which holds the 2FA secret, is now readable only by RTL's user ([#​254](bitcoinix/bitcoinix#254)). - **Custom lnd macaroons** (`services.lnd.macaroons`, used by BTCPay Server and charge-lnd) are now readable only by their `user`. Before, the user's primary group could read them, which for `operator` is `users`, so other normal users could. If other users log in to your node, [revoke all macaroons](https://docs.bitcoinix.net/services.html#revoke-all-macaroons) ([#​255](bitcoinix/bitcoinix#255)). Each custom macaroon now has its own root key, so one can be revoked without the others ([#​236](bitcoinix/bitcoinix#236)). - **Root scripts no longer load config files from a service user's home.** The lnd user could make root's `curl` and `jq` load `~/.curlrc` and `~/.jq` from lnd's data dir ([#​241](bitcoinix/bitcoinix#241), [GHSA-cwmc-wjvh-3qp6](GHSA-cwmc-wjvh-3qp6)). - **Backups fail when a database dump fails.** Before, duplicity succeeded without the database. With `services.postgresqlBackup.compression` set to `"zstd"` or `"none"`, backups left out the BTCPay and NBXplorer databases; they are now included, so run `systemctl start duplicity` after upgrading if you use one of these ([#​225](bitcoinix/bitcoinix#225)). - **Multipath TCP:** service namespaces now copy the host's `net.mptcp.enabled`, so the setting of `hardened-extended` reaches netns-isolated services ([#​206](bitcoinix/bitcoinix#206)). - **Secrets:** `setup-secrets` rejects empty secret files, and secret generation no longer replaces an empty TLS, WireGuard or replication SSH key ([#​244](bitcoinix/bitcoinix#244)). - **WireGuard preset:** evaluation fails for firewall setups in which the preset's rules can't run or don't stay loaded ([#​188](bitcoinix/bitcoinix#188)). - **lnd [0.21.8-beta](https://github.com/lightningnetwork/lnd/blob/master/docs/release-notes/release-notes-0.21.8.md)** fixes correctness bugs in channel state persistence, gossip validation, the sweeper and HTLC recovery ([#​263](bitcoinix/bitcoinix#263)). #### Breaking changes - **Tor enforcement:** a service with `tor.enforce` that connects to a non-local address or by host name needs a change. netns-isolation rejects `addressblock` 169 and 170 ([#​264](bitcoinix/bitcoinix#264)). - **Onion address access:** `lndconnect.onion` and `clnrest.lnconnect.onion` give the operator user access only when `nix-bitcoin.operator.enable` is set. `nix-bitcoin.onionAddresses.access` accepts only users that are defined and enabled in `users.users` ([#​170](bitcoinix/bitcoinix#170)). - **Secrets:** an empty or non-regular secret file now fails `setup-secrets`, and then no service can read its secrets. Check before deploying: `find /etc/nix-bitcoin-secrets -maxdepth 1 -type f -empty`. Set `nix-bitcoin.secrets.<name>.allowEmpty = true` for a secret that may be empty ([#​244](bitcoinix/bitcoinix#244)). - **lnd wallet:** lnd stops when `wallet.db` isn't a regular file, or when its seed file is a symlink to a missing file while it has no wallet ([#​244](bitcoinix/bitcoinix#244)). - **lnd macaroons:** bitcoinix deletes unused root keys with IDs `0x6e62000000000000` to `0x6e62ffffffffffff` on each lnd start; check `lncli listmacaroonids` before deploying. lnd's `no-macaroons` option isn't supported. Restart services that read a custom macaroon only at startup ([#​236](bitcoinix/bitcoinix#236)). A program that read a custom macaroon through the group needs its own macaroon ([#​255](bitcoinix/bitcoinix#255)). - **RTL:** with both backends enabled, stopping one stops RTL for both ([#​250](bitcoinix/bitcoinix#250)). `LN_SERVER_URL`/`LND_SERVER_URL`, another `Settings.lnServerUrl`, a proxy without `no_proxy` for the backend, and servers with a self-signed certificate no longer work as before ([#​258](bitcoinix/bitcoinix#258)). - **WireGuard preset:** fails evaluation with the nftables firewall package without iptables, with `networking.nftables.flushRuleset`, and with an `ip` or `ip6` table named `filter` in `networking.nftables.tables` ([#​188](bitcoinix/bitcoinix#188)). - **extra-container [1.0.1](https://github.com/extra-container/extra-container/releases/tag/v1.0.1):** containers can no longer reach the host's Nix daemon socket, so `nix build`, `nix run`, `nix develop` and `nix-shell` fail inside them. `extra.hostNixDaemon = true;` restores the old behavior; read the security section of the extra-container README first ([#​211](bitcoinix/bitcoinix#211)). - **Packages:** `rtl` and `trustedcoin` moved to `legacyPackages.<system>.pinned`. Trezor is `nix-bitcoin.pkgs.nbPython3Packages.trezor`. `nbPython3Packages` uses the Python interpreter of nixpkgs-unstable, so build custom Python environments with `nbPython3Packages.python` ([#​245](bitcoinix/bitcoinix#245)). - **nodeinfo:** every bitcoinix update now produces a new system generation, because the revision is part of the script. Set `nix-bitcoin.revision = null;` to opt out ([#​243](bitcoinix/bitcoinix#243)). See [Upgrading to config version 0.1.6](https://docs.bitcoinix.net/upgrading.html#upgrading-to-config-version-016). #### New - **[Unbeholden Wallet](https://docs.bitcoinix.net/services.html#connect-to-unbeholden-wallet):** a watch-only wallet that runs in the browser and hands transactions to an air-gapped signer over QR codes. Enable it with `services.unbeholden-wallet.enable = true;` ([#​230](bitcoinix/bitcoinix#230)). - **nodeinfo** shows the bitcoinix revision and the package version of each service. `nodeinfo --version` prints only the revision, `nodeinfo NAME ...` only the given entries ([#​243](bitcoinix/bitcoinix#243)). #### Fixes - lnd announces its onion address with the port Tor forwards. With a `services.lnd.port` other than 9735, peers couldn't reach the node over Tor ([#​228](bitcoinix/bitcoinix#228)). - clnrest replaces an empty or `null` admin rune on start. Generate connection URLs from `lnconnect-clnrest` again if they were created with such a rune ([#​217](bitcoinix/bitcoinix#217)). - btcpayserver with the clightning backend joins `services.clightning.group`, so a custom group works ([#​238](bitcoinix/bitcoinix#238)). - The lnd seed and electrs index migration steps keep the data when an earlier step fails ([#​242](bitcoinix/bitcoinix#242)). #### Updates | Package | 0.1.5 | 0.1.6 | | ----------------------------- | ----------- | -------------------------------------------------------------------------------- | | lnd | 0.21.6-beta | [0.21.8-beta](https://github.com/lightningnetwork/lnd/releases/tag/v0.21.8-beta) | | RTL | 0.15.16 | [0.15.17](https://github.com/Ride-The-Lightning/RTL/releases/tag/v0.15.17) | | extra-container (flake input) | 0.17 | [1.0.1](https://github.com/extra-container/extra-container/releases/tag/v1.0.1) | | Unbeholden Wallet | – | [0.4.0](https://github.com/unbeholdendev/unbeholden-wallet/releases/tag/v0.4.0) | Also: mempool builds with Node.js 24 ([#​246](bitcoinix/bitcoinix#246)); the docs say what netns-isolation doesn't isolate ([#​237](bitcoinix/bitcoinix#237)) and how to close public SSH without losing access ([#​234](bitcoinix/bitcoinix#234)). Contributors: [@​msgilligan](https://github.com/msgilligan), [@​ostermayer](https://github.com/ostermayer), [@​prusnak](https://github.com/prusnak), [@​seberm](https://github.com/seberm), [@​unbeholdendev](https://github.com/unbeholdendev), [@​viberaven](https://github.com/viberaven). Full changes: <bitcoinix/bitcoinix@v0.1.5...v0.1.6> </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4yNDEuMCIsInVwZGF0ZWRJblZlciI6IjQ0LjI0MS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119--> Co-authored-by: agent <[email protected]> Reviewed-on: https://git.home.unbeholden.dev/unbeholden/infra/pulls/140
lnconnect-clnrest always passed `server.pem` from clightning's network dir to lndconnect. clnrest reads its cert dir from `clnrest-certs`, which can also come from an included or per-network config file. With that option set, lnconnect-clnrest failed or put the wrong cert into the URL. Ask clightning for the effective `clnrest-certs`, like RTL does since clnrest.nix that both use. lnconnect-clnrest only asks when the URL includes the cert and the caller doesn't pass `--tlscertpath`. When clightning doesn't answer, for example because it's stopped, it prints a warning and uses the network dir, as before. RTL still fails then, because it pins the cert. `netnsRegtest` now sets a relative `clnrest-certs` dir, which `lnconnect-clnrest-cert` and the RTL tests check. Follow-up to bitcoinix#258. Assisted-by: Claude Code (Claude Opus 5.5)
Follow-up to #258. `lnconnect-clnrest` always passed `server.pem` from clightning's network dir to lndconnect. clnrest reads its cert dir from `clnrest-certs`, which can also come from an `include` file or the per-network config. With that option set, `lnconnect-clnrest` failed, or it put a stale cert from the network dir into the URL. #258 solved the same problem for RTL at runtime. ## What this does - `lnconnect-clnrest` now asks clightning for the effective `clnrest-certs` with `lightning-cli listconfigs clnrest-certs`, like RTL. It runs the query as its caller: root or a member of the `clightning` group. - The jq query and the path resolution (unset means the network dir, a relative dir starts there) move into one script, the internal read-only option `services.clightning.plugins.clnrest.serverCertPathFromConfigs`. It reads the `listconfigs` output on stdin and prints the path of `server.pem`. RTL and `lnconnect-clnrest` both use it, and each runs `lightning-cli` its own way. RTL's script does the same thing as before. - The query only runs when the URL includes the cert from this path. `clnrest.lnconnect.onion`, `--nocert` and the caller's own `--tlscertpath` skip it. So `lnconnect-clnrest-wg` doesn't depend on clightning at all. - When clightning doesn't answer, `lnconnect-clnrest` prints a warning and uses `server.pem` from the network dir, as before. That happens when clightning is stopped, or when the caller can't read a config file that `lightning-cli` reads, for example an `include` file that only the clightning user can read. The warning says to run the command while clightning runs, or to pass `--tlscertpath`, when `clnrest-certs` is set. RTL doesn't fall back. It pins the cert, so it still fails and `Restart=on-failure` tries again. - `nix-bitcoin.mkLndconnect` gets an optional `getCertPathCmd` argument. Existing calls work as before. There's no versioning notice. Nothing that worked before fails now: without an answer from clightning, `lnconnect-clnrest` does what it did before, with a warning. ## Testing - `netnsRegtest` now sets `clnrest-certs=rest-certs`, a relative dir. There, `lnconnect-clnrest-cert` compares the URL's cert with clnrest's TLS handshake, which fails without this change. `rtl-credentials` and `rtl-backend-certs` use the cert and CA from that dir. - `regtest` still uses the default dir for the same tests. - `wireguard-lndconnect` stops clightning and checks that `lnconnect-clnrest-wg --url` prints the same URL as before. `lnconnect-clnrest --url` then prints the warning and a URL with a cert. That URL is the same as the one with `--tlscertpath=<network dir>/server.pem`, and that call prints no warning. - I ran the new script against `listconfigs` output with no value, a relative dir, an absolute dir with a space, invalid JSON and empty input. I also ran shellcheck on the generated `lnconnect-clnrest`. On the KVM host, `netnsRegtest`, `wireguard-lndconnect`, `ciTestInfo`, `module-config` and `test-runner` pass on the final head. `regtest` passed on the previous head, which only differed in the `lnconnect-clnrest` fallback, its test and the docs. ## Models Claude Opus 5.5 wrote the change. Codex GPT-5.6 Sol, Codex GPT-6 Astra (two passes plus rechecks), Kimi K3.1 Pro (two passes), DeepSeek V4.1 Flash 1017 and GLM 5.3 reviewed it.
Fixes #151.
RTL sends the lnd admin macaroon and the clightning admin rune to its backends with
rejectUnauthorized: false, and it has no option for a CA. RTL 0.15.17 still does this. #250 made RTL stop with its backends, but clightning's start only waits until clnrest's port accepts a connection, and any listener passes that check. So a local user who takes the port first still gets the rune.What this does
bitcoinix now preloads a small Node module into RTL (
NODE_OPTIONS=--require=...). It wrapstls.connect, where all of RTL's HTTPS and WebSocket connections start:services.lnd.certPathfor lnd, or clnrest'sserver.pem. The pinned cert is the only trust anchor, so the hostname check is skipped. CLN's certs don't always name the address RTL connects to.client.pem.rejectUnauthorized: trueand the normal checks against the system CAs. So a backend reached through another address or a proxy fails, and fiat rate requests still work.LN_SERVER_URLorLND_SERVER_URLis set. RTL would use those instead of the configured URLs, even over plain HTTP. For the same reason, evaluation fails when a node'sextraConfigchangesSettings.lnServerUrl.The rune script asks clightning for the effective
clnrest-certs(listconfigs), because CLN also readsincludefiles and the per-network config. Unset means the network dir.I didn't patch RTL itself. It builds its requests in eight places with three clients (axios, ws, socket.io-client), and a patch would need rework on most RTL updates. All of them end in
tls.connect. The module carries aTODO-EXTERNALnote to remove it once RTL can verify its backends' certs itself.A few setups stop working:
LN_SERVER_URL/LND_SERVER_URL, another backend URL inextraConfig, a proxy withoutno_proxyfor the backend, and a self-signed non-backend server set throughextraConfig. For the last one,NODE_EXTRA_CA_CERTSin RTL's environment is the fix. There's a 0.1.6 versioning entry (modules/versioning/0.1.6/rtl-backend-certs.nix) and an upgrading entry for this.Not in this PR
#151 also lists two hardening ideas that don't fix the leak. They're now separate issues:
admin.macaroonand the unrestricted rune.Testing
rtl-backend-certs(new, intest/suite/security.py) uses RTL's ownbackend/utils/request.js:NODE_EXTRA_CA_CERTSsucceeds.httppin, orLN_SERVER_URL/LND_SERVER_URL.rtl-credentialsalso checks the copy of clnrest's cert. Eval tests check that the preload stays inNODE_OPTIONS(testRtlNodeOptions) and that anotherlnServerUrlfails (testRtlServerUrlOverride).On the KVM host,
regtest,netnsRegtest,default,netnsandhardenedpass, as domodule-config,test-runnerandciTestInfo. The last change only added thelnServerUrlassertion, so for the final head I reranregtest,netnsand the eval checks. After the rebase onto #255,regtest,netnsand the eval checks pass again. After the rebase onto #257, #259 and #260,regtest,module-config,test-runnerandciTestInfopass. I also checked by hand, against the real clnrest plugin and RTL'srequest.js, that the old CA pin let a cert from CLN's CA through (the fake server got the request with the credential header), and theserver.pempin doesn't.Models
Claude Opus 5.5 wrote the change. Claude Fable 5.2 and Codex GPT-6 Astra reviewed it over several rounds.