Skip to content

Forward lnd's REST onion service to a Unix socket - #343

Merged
seberm merged 1 commit into
bitcoinix:masterfrom
ostermayer:lnd-rest-onion-socket
Nov 28, 2026
Merged

seberm merged 1 commit into
bitcoinix:masterfrom
ostermayer:lnd-rest-onion-socket

Conversation

@ostermayer

Copy link
Copy Markdown
Collaborator

Part of #342.

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. 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:

  • creates /run/lnd-rest with 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 restarts sysinit-reactivation.target before it starts other units.
  • adds restlisten=unix:///run/lnd-rest/rest.sock through the new internal option services.lnd.restSocket. Without the onion, lnd.conf and the lnd unit don't change.
  • gives lnd the dir in 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.
  • makes the socket 0660 in an 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. When extraConfig turns REST off, there's no socket, so a failed chmod doesn't fail lnd.
  • bind-mounts the dir read-only into tor's root dir. connect() works on a read-only mount.
  • extends the lndconnect-onion-lnd VM test and adds lndconnect-onion-lnd-restart to 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, netns and hardened pass. 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, that nobody and 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.
  • With master's modules, lndconnect-onion-lnd fails because torrc's lnd-rest service forwards to 127.0.0.1:8080.
  • Without the bind mount, the chmod or the removal of a leftover socket, or with the dir at mode 0755, the tests fail.
  • A switch from a config without the onion to one with it restarts tor after tmpfiles created the dir, and tor reaches lnd through the socket. After systemctl kill -s KILL lnd, lnd restarts and serves REST on the socket again.
  • Without the onion, the lnd and tor units of regtest are the same as on master.
  • module-config, test-runner, docs-options, test/shellcheck.sh and helper/check-release-notes.sh pass.

Written with Claude Opus 5.5 and reviewed with GPT-6 Astra, Kimi K3.1 Pro, DeepSeek V4.1 Flash and Gemini 4 Carbon.

@github-actions github-actions Bot added area: ci .github/, test/, dev/ area: docs docs/, README, examples/, SECURITY.md, book.toml, assets area: modules modules/ and pkgs/lib.nix size: M 30–99 changed lines (additions + deletions) labels Nov 27, 2026
@ostermayer ostermayer added the type: bug Behaviour is wrong vs. documented or intended label Nov 27, 2026
@prusnak prusnak added this to the v0.2.1 milestone Nov 27, 2026

@sebsgent sebsgent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@viberaven

Copy link
Copy Markdown
Contributor

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:

  • module-config and test-runner flake checks.
  • Full default and netns VM scenarios, including lndconnect-onion-lnd and lndconnect-onion-lnd-restart. These exercise socket ownership/modes, access from Tor's mount namespace as the Tor user, denied unprivileged access, continued TCP access, clean-stop removal, and recovery with a stale socket.
  • git diff --check.

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)
@seberm
seberm force-pushed the lnd-rest-onion-socket branch from 2d29e9a to aeff9b5 Compare November 28, 2026 20:47
@seberm
seberm enabled auto-merge November 28, 2026 20:58
@seberm
seberm merged commit d555692 into bitcoinix:master Nov 28, 2026
39 checks passed
seberm added a commit that referenced this pull request Nov 29, 2026
…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
seberm added a commit that referenced this pull request Nov 30, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ci .github/, test/, dev/ area: docs docs/, README, examples/, SECURITY.md, book.toml, assets area: modules modules/ and pkgs/lib.nix size: M 30–99 changed lines (additions + deletions) type: bug Behaviour is wrong vs. documented or intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants