Repository navigation
Forward lnd's REST onion service to a Unix socket - #343
Conversation
sebsgent
left a comment
There was a problem hiding this comment.
No must or should items from my side. Approving.
clnrest's onion service and the WireGuard URLs for both daemons still connect to the TCP ports, so #342 stays open for them.
Tested: not locally. CI's default and netns jobs run both new tests. I checked tor's sandbox in the pinned nixpkgs (RootDirectory, PrivateUsers, AF_UNIX allowed) and that switch-to-configuration runs tmpfiles before it restarts tor.
Assisted-by: Claude Code (Claude Opus 5.5)
|
Reviewed commit 2d29e9a. No actionable findings. The protected, persistent socket directory, Tor bind mount, stale-socket cleanup, and socket permissions fit together correctly. The existing TCP listener and onion address/port are preserved. I also checked the pinned LND and NixOS sources: LND creates its REST listeners before sending READY, and switch-to-configuration waits for tmpfiles through sysinit-reactivation before restarting the affected services. Local validation on x86_64-linux passed:
CI is also green at this head. The local socket tests do not exercise an end-to-end public Tor circuit. Assisted-by: Codex (GPT-6 Astra) |
With `services.lnd.lndconnect.onion`, lnd now also serves REST on the Unix socket /run/lnd-rest/rest.sock, and the `lnd-rest` onion service forwards to it instead of lnd's REST port. Only lnd can create entries in /run/lnd-rest, so the onion service reaches only lnd, also while lnd restarts. Only lnd and tor can enter the dir. - tmpfiles creates /run/lnd-rest with mode 2750, owned by lnd and group tor. The setgid bit gives the socket the tor group. The dir stays while lnd restarts, so tor's bind mount of it stays valid. tmpfiles runs before tor starts, at boot and on a switch. - The new internal option `services.lnd.restSocket` adds a second `restlisten`. Without it, lnd.conf doesn't change. - lnd gets the dir in ReadWritePaths. At the end of preStart, it removes a socket that a crash left behind, because lnd can't listen on an existing path. - lnd creates the socket with its umask 0022, and an ExecStartPost gives the group write access, which connect() needs. lnd sends READY only after its REST listeners are up. - tor gets the dir as a read-only bind mount, because it runs in a root dir of its own. connect() works on a read-only mount. lnd keeps its TCP REST listener for RTL, the WireGuard preset and local clients. The onion address and port stay the same, so wallets don't need to pair again. `lndconnect-onion-lnd` checks the torrc target, the dir and socket modes, REST through the socket from tor's mount namespace as the tor user, and that nobody and the operator can't enter the dir. The new `lndconnect-onion-lnd-restart` runs in the disruptive phase. It checks that a clean stop removes the socket and that lnd starts with a socket left behind. Assisted-by: Claude Code (Claude Opus 5.5)
2d29e9a to
aeff9b5
Compare
…ST and Electrum ports (#347) Without netns-isolation, clnrest, electrs and Fulcrum listen on the host's loopback address, and their onion services forward there. While a service is down, for example during a restart or an update, another local user can listen on its port and receive the clnrest rune from ZEUS or a wallet's addresses. With netns-isolation, the services listen on their namespace's address, which no process in the host namespace can take. Document this on the ZEUS and electrs/Fulcrum pages and in the netns-isolation section, and add a 0.2.1 release note. lnd's onion service forwards to a Unix socket since #343, and RTL and Orchard keep their port through a systemd socket once it has started. The WireGuard preset and direct REST connections can't use netns-isolation, so the ZEUS page says not to give untrusted users an account on such a node. Fixes #342
Tor must only reach lnd's REST socket in /run/lnd-rest, but lnd owns that dir and can create symlinks in it. This mounts the dir on itself with `nosymfollow`. Tor's read-only bind mount of the dir copies the flag, so tor can't connect through a symlink there. Tor requires the mount and starts after it. The mount asserts that the dir exists, so a missing dir fails the mount and tor, instead of systemd creating the dir as root:root 0755. lnd's socket keeps lnd's copy of the mount busy, so lnd is part of the mount and ordered after it. lnd then stops first when the mount stops or restarts, also on a switch that changes the mount or turns off the onion service. Follow-up to #343, suggested by ostermayer. The new VM test `lndconnect-onion-switch` runs in CI. It restarts only the mount, and it switches the onion service off while lnd is slow to stop and then on again. Claude Opus 5.5 wrote this. DeepSeek V4.1 Flash 1017 (max), Gemini 4 Carbon (high), Gemini 4 Neon (high), Claude Opus 5.5 (high) and GPT-6 Astra (max) reviewed it.
Part of #342.
With
services.lnd.lndconnect.onion, lnd now also serves REST on the Unix socket/run/lnd-rest/rest.sock, and thelnd-restonion service forwards to it instead of lnd's REST port. Only lnd can create entries in/run/lnd-rest, so the onion service reaches only lnd, also while lnd restarts. lnd keeps its TCP listener for RTL, the WireGuard preset and local clients. The onion address and port stay the same, so wallets don't need to pair again. clnrest isn't part of this PR.This PR:
/run/lnd-restwith tmpfiles, mode 2750, owned by lnd and group tor. Only lnd can create or remove entries, and only lnd and tor can enter. The setgid bit gives the socket the tor group. The dir stays while lnd restarts, so tor's bind mount of it stays valid. tmpfiles runs before tor starts, at boot and on a switch, because switch-to-configuration restartssysinit-reactivation.targetbefore it starts other units.restlisten=unix:///run/lnd-rest/rest.sockthrough the new internal optionservices.lnd.restSocket. Without the onion, lnd.conf and the lnd unit don't change.ReadWritePaths. At the end of preStart, lnd removes a socket that a crash left behind. Go removes the socket only when lnd stops cleanly, and lnd can't listen on an existing path.ExecStartPost. lnd creates it with its umask 0022, but connect() needs write access. lnd sends READY only after its REST listeners are up, so the socket is there. WhenextraConfigturns REST off, there's no socket, so a failed chmod doesn't fail lnd.lndconnect-onion-lndVM test and addslndconnect-onion-lnd-restartto the disruptive phase, which stops lnd and starts it again with a socket left behind. It also adds a release note draft.netns-isolation needs nothing extra, because a Unix socket on the filesystem doesn't depend on the network namespace.
Testing
default,netnsandhardenedpass. The tests check the torrc target, the owner, group and mode of the dir and the socket, REST through the socket from tor's mount namespace as the tor user, thatnobodyand the operator can't create a file in the dir or connect, the TCP listener, that a clean stop removes the socket, and a start with a socket left behind.lndconnect-onion-lndfails because torrc'slnd-restservice forwards to127.0.0.1:8080.systemctl kill -s KILL lnd, lnd restarts and serves REST on the socket again.regtestare the same as on master.module-config,test-runner,docs-options,test/shellcheck.shandhelper/check-release-notes.shpass.Written with Claude Opus 5.5 and reviewed with GPT-6 Astra, Kimi K3.1 Pro, DeepSeek V4.1 Flash and Gemini 4 Carbon.