Repository navigation
lnd: give each custom macaroon its own root key - #236
Conversation
335dc4a to
b1114e5
Compare
|
Rebased onto master after #241. The macaroon script now lives in In #241's test I kept the Fable 5.2 (xhigh), GPT-6 Astra (max, two passes), Kimi K3.1 Pro (max, two passes) and GLM 5.3 (max) reviewed the rebase. The dead |
viberaven
left a comment
There was a problem hiding this comment.
Reviewed b1114e5. No blocking correctness findings.
Checked the deterministic root-key IDs and permission normalization, validation before deletion, macaroon installation, restart helper, BTCPay restart trigger, and migration documentation. The new tests cover the key revocation and unchanged-restart cases well.
Validation: built checks.x86_64-linux.module-config, checks.x86_64-linux.test-runner, and checks.x86_64-linux.lnd-macaroon-filters successfully from an isolated source snapshot of this commit. GitHub CI is also green. I did not independently rerun the VM scenarios or verify a live configuration switch / BTCPay credential reload; those remain coverage limitations as noted in the PR.
One minor documentation request below.
Review performed with Codex (GPT-6 Astra).
b1114e5 to
94bf5b5
Compare
|
Reviewed 94bf5b5 (rebased after #241). No blocking findings. Optional: a JSON syntax error in Checked: no new listener or netns connection; lncli runs as the lnd user with Tested: CI on 94bf5b5 is green, including the default, netns and netnsRegtest scenarios, which run lnd-macaroon-revocation with lnd, charge-lnd and BTCPay. No local runs. Assisted-by: Claude Code (Claude Fable 5.1) |
Custom macaroons were baked with lnd's root key 0, which they share with
the admin, readonly and invoice macaroons and which lnd can't delete. So
copies of a custom macaroon stayed valid after its permissions were
narrowed, its owner changed or it was removed.
Each macaroon in `services.lnd.macaroons` now gets a root key ID derived
from its name, user and canonical permissions, in the range
0x6e62000000000000 to 0x6e62ffffffffffff ("nb" in the top 16 bits). The
same definition keeps its ID, so lnd reuses the root key and copies
survive restarts. The ID is the read-only option
`services.lnd.macaroons.<name>.rootKeyId`.
On each lnd start, lnd-create-macaroons deletes the root keys in this
range that no configured macaroon uses. Then it bakes each macaroon with
its ID and checks the result with `lncli printmacaroon` before it hands
the macaroon out. Deleting a root key doesn't close the streams opened
with it, so after a deletion a transient unit try-restarts lnd once,
after the current start. A stop, also a queued one, stays in effect, and
if the restart fails, lnd is stopped. /run/lnd is kept across restarts,
so services can read their macaroons meanwhile.
The script now uses lncli over gRPC, run as the lnd user, instead of the
REST API. It runs on every start, also without custom macaroons, and the
REST address need not be in lnd's certificate. lncli runs with profiles
turned off, so a profile in lnd's home can't redirect it. Root only
parses lncli's output, with size limits and strict validation: one
object whose `root_key_ids` is an array of unique, canonical uint64
strings. The new `lnd-macaroon-filters` check runs these jq filters on
malformed output.
The script no longer runs curl, so the HOME test in test_lnd_macaroons
drops its `.curlrc` check. Its `.jq` check still covers the jq that
runs as root.
The option and the script move to modules/lnd-macaroons.nix. Macaroon
names must now be safe file names of at most 246 characters.
btcpayserver reads its macaroon only at startup, so it now restarts when
its ID changes.
Assisted-by: Claude Code (Claude Opus 5.5)
Add an LND section to the services guide: what the root key IDs are, when copies stop working, the exception for copies created before 0.1.6, and how to revoke one macaroon or all of them by hand. `lncli changepassword --new_mac_root_key` fails on bitcoinix, which unlocks the wallet when lnd starts. So the guide stops lnd and the units that can start it, deletes macaroons.db and the macaroon files as the lnd user, and checks `lncli getinfo` after the start. This applies to lnd's default bbolt database backend. Add a 0.1.6 notice for lnd users: lnd-create-macaroons deletes the root keys in the reserved range that no configured macaroon uses, and every lnd start now uses the admin macaroon over gRPC, so `no-macaroons` isn't supported. Assisted-by: Claude Code (Claude Opus 5.5)
94bf5b5 to
9280108
Compare
|
Rebased onto master 2362b11. #170 added two 0.1.6 notices to On the KVM host, #244 adds a 0.1.6 notice in the same place, so whichever of the two merges second needs the same small fix. I'll do it. |
viberaven
left a comment
There was a problem hiding this comment.
Reviewed the current head, 9280108. No new blocking correctness findings.
Checked deterministic root-key IDs and permission normalization, validation before deletion, credential handling and installation, the deferred restart and stop behavior, BTCPay's restart trigger, and the migration/revocation documentation against the implementation.
Validation: nix build --no-link .#checks.x86_64-linux.module-config .#checks.x86_64-linux.test-runner .#checks.x86_64-linux.lnd-macaroon-filters succeeded from a detached worktree at this head (the filter check was already available in the store). The diff passes whitespace checks, and GitHub CI is green. I did not independently rerun the VM scenarios or exercise a live configuration change and BTCPay credential reload; the static restart-trigger assertion does not establish that runtime behavior.
The existing nonblocking request to summarize this change in docs/release-notes.md under 0.1.6 remains applicable; no duplicate inline comment added.
Review performed with Codex (GPT-6 Astra).
|
Re-checked 9280108 (rebased after #170; the lnd commit is unchanged since 94bf5b5). No blocking findings. Should fix:
Tested: CI on 9280108 is green, including default, netns and netnsRegtest. No local runs. Assisted-by: Claude Code (Claude Opus 5.5, Claude Fable 5.1 as advisor) |
Assisted-by: Claude Code (Claude Opus 5.5)
|
Added the release note in 7bc20b3. It links to the "Custom macaroons" section and the 0.1.6 upgrade text. |
The restart after deleting stale root keys stopped lnd whenever `try-restart` failed. That also happened when the old process was already gone and only the new start failed, for example on a transient lncli error. The stop then cancelled lnd's `Restart=always`, and lnd stayed down. Now it stops lnd only while lnd still runs the invocation from before the restart, whose streams may still be open. lncli parses the profile file also with an empty `--profile`, so an empty profiles.json, for example after a crash right after its creation, made every lnd start fail. Rewrite an empty file too. Assisted-by: Claude Code (Claude Opus 5.5)
BTCPay Server's lnd client reads the macaroon file for each request (`MacaroonFileAuthentication` in BTCPayServer.Lightning.LND), so the restart trigger on the macaroon's root key ID only restarted BTCPay Server for nothing, once on each node at the upgrade. Remove it, its test and the docs that described it. The cleanup restart now exits at once when lnd is already stopping. Say so in its message, and let the slow-stop test wait for lnd to stop. Assisted-by: Claude Code (Claude Opus 5.5)
|
I pushed two fixes and one removal. This changes code after the approvals, so please take another look.
On the KVM host, |
viberaven
left a comment
There was a problem hiding this comment.
Reviewed f033896. No blocking correctness findings.
Rechecked the full change and the updates since the previous approval: the restart helper preserves automatic recovery when the old invocation is gone, empty lncli profile files are repaired as the lnd user, and the release note now covers the change. Removing BTCPay's restart trigger is consistent with the upstream MacaroonFileAuthentication implementation, which reads the file for each authenticated request.
Validation:
- Successfully built checks.x86_64-linux.module-config, checks.x86_64-linux.test-runner, and checks.x86_64-linux.lnd-macaroon-filters from an isolated source snapshot of this exact head (the filter check was already available in the store).
- Exercised the actual restart-helper shell body with a mocked systemctl: successful restart, failure with the old invocation active, failure after the invocation changed, and an already-stopping service all took the expected branches.
- Diff whitespace checks pass; GitHub CI, including ci_gate and the default/netns/netnsRegtest VM scenarios, is green.
I did not independently rerun the VM scenarios or test a live configuration transition / BTCPay credential reload. The mocked helper checks validate its branching, not systemd's runtime behavior.
Review performed with Codex (GPT-6 Astra).
4fabc87 to
f033896
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
Closes #235.
Each custom macaroon now gets its own lnd root key. The ID comes from the macaroon's name, user and permissions, in a range reserved for bitcoinix (
0x6e62in the top 16 bits). On every start,lnd-create-macaroons:An unchanged macaroon keeps its ID and root key, so BTCPay keeps working across restarts. BTCPay Server reads its macaroon file for each request to lnd, so it picks up a new macaroon without a restart. The script now calls lncli over gRPC as the lnd user instead of REST, so it no longer needs the REST address in the certificate. The option and the script moved to
modules/lnd-macaroons.nix, andlnd.nixis smaller than before.There's a 0.1.6 notice: lnd now deletes IDs in the reserved range, and every lnd start uses the admin macaroon over gRPC, so
no-macaroonsisn't supported. Copies baked before this change are on root key 0 and stay valid. The docs show how to rotate all root keys.Tested
On my KVM host, with lnd 0.21.6:
lnd-macaroon-filtersruns the jq validation against about 40 fixtures: duplicates, numbers, leading zeros, overflow, a trailing newline, missing fields, the range ends. A rejected listing never leads to a deletion.lnd-macaroon-revocationchecks that stale IDs in the range are deleted and their copies are rejected, IDs outside the range and root key 0 survive, a copy survives an unchanged restart, deleting and re-baking the same ID rejects the old copy, lnd restarts once after a deletion and not again, an operator's stop wins over a queued restart,/run/lndsurvives the internal restart, and a conflicting lncli profile doesn't redirect anything.lnd,charge-lnd,default,netnsandnetnsRegtestscenarios pass.Not covered: a real config change from one macaroon definition to another, and BTCPay Server reading a new macaroon at runtime.
Opus 5.5 wrote this. Codex GPT-6 Astra and Kimi K3.1 Pro reviewed the plan and every round of code, twice each. Their findings are fixed: a trailing newline in IDs, the REST certificate dependency, open streams, lncli profiles, the restart helper, IPv6 addresses, argument quoting and the revoke-all races. GPT-5.6 Sol found the name path check and the queued stop. Kimi K3.1 Pro (two passes) and DeepSeek V4.1 Flash ran over the branch, and Astra checked their report. The one finding in the moved code is older than this PR: the macaroon files are mode 0440, so the owner's primary group can read them, as on master. I'd change that separately. After that, Fable 5.2 and Astra found that a failed cleanup restart kept lnd down, that an empty lncli profile file blocked every lnd start, and that BTCPay Server doesn't need a restart for a new macaroon. Those are fixed too.