Skip to content

lndconnect: mount lnd's REST socket dir nosymfollow for tor - #346

Merged
seberm merged 1 commit into
bitcoinix:masterfrom
sebsgent:lnd-rest-nosymfollow
Nov 30, 2026
Merged

seberm merged 1 commit into
bitcoinix:masterfrom
sebsgent:lnd-rest-nosymfollow

Conversation

@sebsgent

@sebsgent sebsgent commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #343, suggested by @ostermayer.

Tor must only reach lnd's REST socket in /run/lnd-rest. lnd owns this dir and can create symlinks in it, so this mounts the dir on itself with nosymfollow. Tor's read-only bind mount of the dir copies the flag, so tor's connect() fails on a symlink there. lnd and tor still share the same dir and socket.

  • New mount unit run-lnd\x2drest.mount (bind,nosymfollow). It has no default dependencies, so it can start after systemd-tmpfiles-setup.service, which creates the dir at boot.
  • tor gets RequiresMountsFor=/run/lnd-rest, so it starts after the mount. If the mount fails, tor doesn't start.
  • The mount asserts that the dir exists. Otherwise systemd would create it as root:root 0755, and tor would start while lnd can't create its socket.
  • lnd is PartOf and ordered after the mount, so it stops or restarts with the mount and stops first. When lnd starts after the mount, its socket keeps lnd's copy of the mount busy, so the mount can't stop while lnd runs. This also covers a stop or restart of the mount alone, e.g. a switch that changes the mount unit, and a switch that turns off the onion service while lnd has restartIfChanged or stopIfChanged off. lnd doesn't require the mount.
  • The release note of Forward lnd's REST onion service to a Unix socket #343 mentions the mount.

Tests (lndconnect-onion-lnd, lndconnect-onion-lnd-restart):

  • The mount has nosymfollow in the host's view and in tor's mount namespace (there also ro).
  • From tor's namespace as the tor user, a symlink that lnd creates in the dir to lnd's socket fails to connect with ELOOP. lnd's socket still works.
  • tor requires the mount and started after it, lnd is PartOf and ordered after it, and systemd logged no ordering cycle in this boot.
  • lnd started after the mount. Stopping only the mount stops lnd and tor.
  • Without the dir, the mount and tor fail to start, and the dir isn't created.
  • Starting tor mounts the dir again first.

New VM test lndconnect-onion-switch, added to the CI matrix: lnd starts after the mount (checked at boot), then a restart of only the mount restarts lnd and tor (checked by tor's invocation ID). A switch turns the onion service off while lnd is slow to stop, and, after removing /run/lnd-rest, on again. Neither leaves failed units. Without PartOf, the restart of the mount fails with target is busy.

Assisted-by: Claude Code (Claude Opus 5.5, claude-opus-5-5)

@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: S 10–29 changed lines (additions + deletions) labels Nov 28, 2026
@seberm
seberm force-pushed the lnd-rest-nosymfollow branch from 28adcb2 to ae2d62c Compare November 28, 2026 21:42
@seberm
seberm marked this pull request as ready for review November 28, 2026 21:42
@seberm
seberm requested review from ostermayer and prusnak November 28, 2026 21:42
@seberm seberm added this to the v0.2.1 milestone Nov 28, 2026
@viberaven

Copy link
Copy Markdown
Contributor

Reviewed commit ae2d62c. One P2 finding to fix before merging.

[P2] Decode escaped unit names in the dependency assertion — test/suite/networking.py:44–45

systemctl show -P Requires tor emits the mount name as "run-lnd\\x2drest.mount": quoted, with the backslash escaped. .split() retains those quotes and both backslashes, so it cannot match lnd_rest_mount, which contains a single backslash. After has the same formatting. The dependency is actually present; this assertion fails before the mount/security checks can run. This explains the current default CI failure (AssertionError: Requires) and the failing gate.

shlex is already imported by the shared test helpers. Replacing the assertion with the following handles the observed output:

assert lnd_rest_mount in shlex.split(
    succeed(f"systemctl show -P {prop} tor")
), prop

Validation:

  • On the unmodified PR, a focused diagnostic VM confirmed that both dependencies exist, while .split() fails to find them and shlex.split() succeeds.
  • That VM passed the socket and restart assertions: host/Tor nosymfollow flags, Tor's read-only mount, ELOOP for a symlink, access to the real socket, stale-socket recovery, and stopping/starting Tor with the mount.
  • With only the assertion changed in a temporary copy, a focused VM running both original tests (lndconnect-onion-lnd and lndconnect-onion-lnd-restart) passed.

No other actionable findings. I did not independently test a live configuration switch or rerun the full scenario matrix.

Assisted-by: Codex (GPT-6 Astra)

@sebsgent

Copy link
Copy Markdown
Contributor Author

Thanks, fixed in 2d13e92 with your shlex.split suggestion: systemctl show quotes and escapes the mount's name in tor's Requires/After. I also confirmed this in a minimal VM on the PR's pinned nixpkgs (systemd 262.2): the dependency was there, only the parsing was wrong.

Assisted-by: Claude Code (Claude Opus 5.5)

@ostermayer ostermayer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@sebsgent I ran our gate on 2d13e92 with Kimi K3.1 Pro twice, DeepSeek V4.1 Flash, Gemini 4 Carbon and GPT-6 Astra twice, and checked each finding against the code. The mount does what it should. Tor's view has ro,nosymfollow, a symlink fails with ELOOP, and a hard link or a rename from another dir into the self-bind fails with EXDEV, so lnd can't point tor at another socket. Three small points are left.

  1. lnd's socket keeps the mount busy. When lnd starts after the mount, for example on any lnd restart, its namespace gets a copy of the self-bind, and the bound rest.sock holds a reference to that copy. While lnd runs, umount /run/lnd-rest fails with target is busy. I reproduced this with a self-bind and a listener in a slave mount namespace. A switch that turns off lndconnect.onion stops lnd, tor and the mount at the same time, so the mount's stop can fail and the switch exits with an error. after = [ "run-lnd\\x2drest.mount" ] on lnd makes lnd stop first. The restart test doesn't catch this, because it stops lnd before the mount.
  2. If /run/lnd-rest is missing when the mount starts, systemd creates it as root:root 0755, and the self-bind still succeeds. Tor then starts, but lnd can't create its socket. Before this PR, tor's BindReadOnlyPaths failed on the missing dir. For example, run systemctl stop lnd 'run-lnd\x2drest.mount', remove the dir and run systemctl start tor. unitConfig.AssertPathIsDirectory = lndRestSocketDir on the mount makes the start fail before systemd creates the dir, and tor fails with it.
  3. The boot check at test/suite/networking.py:51 fails when tor restarted before the test, for example with run_test("lndconnect-onion-lnd-restart") and then run_test("lndconnect-onion-lnd") in a debug VM. It also fails when the test runs before multi-user.target is active, because the target's timestamp is still 0. Line 50 passes when the mount never started, because the mount's timestamp is 0 too. The Requires and After checks already cover the order, so line 51 can go, and 0 < on line 50 catches a mount that never started.

@ostermayer ostermayer added status: changes needed Cannot proceed until suggested changes are implemented type: bug Behaviour is wrong vs. documented or intended labels Nov 28, 2026
@sebsgent
sebsgent force-pushed the lnd-rest-nosymfollow branch from 2d13e92 to 486415b Compare November 29, 2026 11:37
@github-actions github-actions Bot added size: M 30–99 changed lines (additions + deletions) and removed size: S 10–29 changed lines (additions + deletions) labels Nov 29, 2026
@sebsgent

Copy link
Copy Markdown
Contributor Author

@ostermayer Thanks, all three are in, amended into the single commit.

  1. Added after on lnd. The busy mount reproduces, but before this change a switch only failed in some cases: lnd is already ordered after onion-addresses, which is ordered after tor and so after the mount. That chain only holds while onion-addresses is active. With it stopped and lnd slow to stop, the switch failed with target is busy. The new CI test lndconnect-onion-switch covers this case, and the restart test now stops onion-addresses before it stops lnd and the mount together. lnd doesn't require the mount, because it doesn't need it.
  2. Added AssertPathIsDirectory. The test moves the dir away and checks that tor fails to start and the dir isn't created.
  3. Removed the multi-user.target check and changed the mount check to 0 <. In its place, the test checks the boot journal for ordering cycles.

Assisted-by: Claude Code (Claude Opus 5.5)

@seberm seberm removed the status: changes needed Cannot proceed until suggested changes are implemented label Nov 29, 2026

@ostermayer ostermayer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@sebsgent I reran our gate on 486415b with DeepSeek V4.1 Flash 1017 (max), Gemini 4 Carbon (high), Gemini 4 Neon (high), Claude Opus 5.5 (high) and GPT-6 Astra (max), and checked each finding against the code. The three changes do what they should, and AssertPathIsDirectory fails the mount before systemd creates the dir, so tor fails with it. A few points are left, and the first one matters most.

  1. after = [ lndRestMount ] orders lnd's stop only when lnd stops in the same transaction. A stop or restart of the mount alone doesn't stop lnd, so the unmount fails with target is busy and tor stays down. That happens with systemctl stop 'run-lnd\x2drest.mount', on a later switch that changes the mount unit (switch-to-configuration restarts a changed mount), and on a switch that turns the onion service off while lnd has restartIfChanged = false or stopIfChanged = false, because lnd then isn't in the stop phase. All five models found this. partOf = [ lndRestMount ] on lnd fixes it, because the mount's stop or restart then includes lnd, and lnd still doesn't require the mount. The restart test could then stop the mount alone at test/suite/networking.py:82 and check that lnd stopped too.
  2. systemctl is-active lnd tor at test/lndconnect-onion-switch.nix:99 succeeds when either unit is active. Two separate checks would catch a stopped lnd.
  3. The VM boots with the onion service, so /run/lnd-rest is still there from boot when the last subtest switches it back on. rmdir /run/lnd-rest before switch_to_system("machine") would test that the switch creates the dir before the mount's assertion runs.
  4. journalctl -u tor -u lnd -u <mount> at test/suite/networking.py:52 misses most cycle messages. systemd logs a cycle under the unit where it found it, often local-fs.target or sysinit.target, and lines from before journald started have no unit field. journalctl -b -t systemd catches both.
  5. The order through onion-addresses exists only when lnd's onion service is public (modules/onion-services.nix:89-94). The switch test doesn't set public, so the onion-addresses stop at test/lndconnect-onion-switch.nix:84 and the comments there and at modules/lndconnect.nix:314 describe an order that isn't there. The main suite sets it at test/tests.nix:307.
  6. Line 71 of test/suite/networking.py stops lnd before wait_for_lnd_server(). When config-replace has just restarted lnd, that stop can land between RPC start and server start, which is the panic that wait_for_lnd_server's docstring warns about. Starting lnd and waiting before the restart avoids it.

@ostermayer ostermayer added the status: changes needed Cannot proceed until suggested changes are implemented label Nov 29, 2026
@sebsgent
sebsgent force-pushed the lnd-rest-nosymfollow branch from 486415b to 9dd4f4c Compare November 29, 2026 16:12
@sebsgent

Copy link
Copy Markdown
Contributor Author

@ostermayer Thanks, all six points are addressed in 9dd4f4c (squashed):

  1. lnd now has partOf = [ lndRestMount ] next to after, so a stop or restart of the mount alone stops lnd first. The restart test stops only the mount and checks that lnd and tor stopped. The switch test restarts only the mount and checks that lnd restarted and no unit failed. Without partOf, that subtest fails with umount: /run/lnd-rest: target is busy. On a switch that removes the mount, switch-to-configuration stops it before daemon-reload, so lnd's old unit still has PartOf, also with restartIfChanged or stopIfChanged off.
  2. Split into two is-active checks.
  3. The last subtest removes /run/lnd-rest before the switch. The switch restarts systemd-tmpfiles-resetup through sysinit-reactivation.target and waits for it before it starts units, so the mount's assertion passes.
  4. The cycle check uses journalctl -b -o cat -t systemd.
  5. The switch test no longer stops onion-addresses. The comments say the order through it exists only when lnd's onion service is public.
  6. The restart test no longer stops lnd before wait_for_lnd_server(). It starts lnd, waits, checks that lnd started after the mount, and checks that lnd's namespace has the nosymfollow mount.

Could you rerun your gate on the new head? If anything else needs changing, could you push it as fixup! commits on a branch in your fork? Then I can apply them directly.

Assisted-by: Claude Code (Claude Opus 5.5, claude-opus-5-5)

@sebsgent
sebsgent force-pushed the lnd-rest-nosymfollow branch from 9dd4f4c to 67325a5 Compare November 29, 2026 16:16
@seberm
seberm requested a review from ostermayer November 29, 2026 16:19

@ostermayer ostermayer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@sebsgent I reran our gate on 67325a5 with DeepSeek V4.1 Flash 1017 (max), Gemini 4 Carbon (high), Gemini 4 Neon (high), Claude Opus 5.5 (high) and GPT-6 Astra (max), and checked each finding against the code. All six points are fixed. partOf stops lnd before the mount also when only the mount stops or restarts, and tor follows the mount through Requires. Two optional test points are left.

  1. The mount-restart subtest at test/lndconnect-onion-switch.nix:80 checks only that lnd restarted. Comparing tor's InvocationID before and after would test the "and tor" part.
  2. The main suite makes lnd's onion service public at test/tests.nix:307, so lndconnect-onion-switch is the only test where lnd's own after is its only order to the mount. Its first subtest restarts lnd at line 76 before it checks anything, so it doesn't test the boot order. A timestamp check like the one at test/suite/networking.py:83, before the restart, would.

@ostermayer ostermayer removed the status: changes needed Cannot proceed until suggested changes are implemented label Nov 29, 2026
@sebsgent
sebsgent force-pushed the lnd-rest-nosymfollow branch from 67325a5 to fa9351e Compare November 30, 2026 19:06
@sebsgent

Copy link
Copy Markdown
Contributor Author

@ostermayer Thanks, both are in, amended into the single commit and rebased onto master.

  1. The mount-restart subtest compares tor's InvocationID before and after. Without tor's Requires on the mount, the subtest fails at that check.
  2. The first subtest checks that lnd started after the mount before it restarts lnd. Without lnd's after, the check still passes, because lnd starts long after the mount at boot anyway. The slow-stop switch subtest is what catches a missing after.

Assisted-by: Claude Code (Claude Opus 5.5, claude-opus-5-5)

Tor must only reach lnd's REST socket in /run/lnd-rest. lnd owns this
dir and can create symlinks in it, and tor's connect() would follow
them inside tor's root dir.

Mount the dir on itself with `nosymfollow`. lnd and tor still share
the same dir and socket. Tor's bind mount of the dir copies the flag,
so tor requires the mount and starts after it. If the mount fails, tor
doesn't start. The mount has no default dependencies, so that it can
start after tmpfiles creates the dir at boot.

If the dir is missing, systemd would create it as root:root 0755, and
tor would start while lnd can't create its socket. Assert the dir
instead, so that the mount and tor fail to start.

When lnd starts after the mount, its mount namespace gets a copy of it,
which lnd's socket keeps busy, so the mount can't stop while lnd runs.
Make lnd part of the mount and order it after the mount, so that lnd
stops or restarts with the mount and stops first. This also holds when
only the mount stops or restarts, e.g. on a switch that changes the
mount unit, or when a switch that turns off the onion service doesn't
stop lnd itself. lnd doesn't need the mount, so it doesn't require it.

Test that the mount has `nosymfollow` in the host's and tor's view,
that tor can't connect through a symlink in the dir while lnd's socket
still works, that tor starts after the mount, that stopping only the
mount stops lnd and tor, that the mount and tor don't start without the
dir, and that tor remounts the dir when it starts. A new VM test in CI,
`lndconnect-onion-switch`, restarts only the mount, switches the onion
service off while lnd runs and is slow to stop, and on again after
removing the dir.

Assisted-by: Claude Code (Claude Opus 5.5, claude-opus-5-5)
@seberm
seberm force-pushed the lnd-rest-nosymfollow branch from fa9351e to 892ee61 Compare November 30, 2026 19:12
@seberm
seberm enabled auto-merge November 30, 2026 19:14
@seberm
seberm merged commit ef46b5b into bitcoinix:master Nov 30, 2026
40 checks passed
@sebsgent sebsgent mentioned this pull request Nov 30, 2026
3 tasks done
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.

4 participants