Repository navigation
rtl: keep RTL's port held by systemd while RTL is down - #293
Conversation
d45018c to
08ef376
Compare
|
Should
Assisted-by: Claude Code (Claude Opus 5.5) |
08ef376 to
30a1c8d
Compare
|
Thanks for the review. All in 30a1c8d.
|
viberaven
left a comment
There was a problem hiding this comment.
The systemd-owned listener addresses the original port-ownership issue for the default configuration. I’m requesting changes because the proxy can start without its secrets mask, an environment file can move RTL off its Unix socket, and the migration notice can direct operators to an address systemd does not hold.
I built module-config and test-runner locally on 30a1c8d8, evaluated the configuration cases in the inline comments, and checked the generated listener syntax with systemd-analyze verify. The VM results I used are from the passing GitHub Actions run. CI results
Reviewed with Codex (gpt-6.2-astra).
|
Like in the other PR - I suggest to drop IP address validation from the PR. Let's just pass them as string. We can add PR in the future that replaces all such usage with types in nbLib - I already have such PR in the making. |
30a1c8d to
6671559
Compare
|
Thanks for the reviews. All in 6671559.
|
viberaven
left a comment
There was a problem hiding this comment.
I verified five fixes from the previous review on 66715593. The proxy now requires both directory masks, rtl.service clears PORT and HOST from its final environment, the tunnel notice uses the correct destination for custom numeric binds, and systemd accepts the generated IPv4-mapped IPv6 listener. The default VM log also shows six distinct proxy PIDs killed and restart counters 1 through 6. Local module-config and test-runner checks passed. Default VM log
The port-zero finding is still open. RTL 0.15.17 previously treated a JSON port of 0 as its default of 3000. This revision passes evaluation and generates ListenStream=127.0.0.1:0, which systemd-analyze verify rejects. A nonzero-port assertion scoped to the proxy, or preserving the old fallback, would address this independently of the planned IP address types. Original port-zero thread
I found two migration cases in the revised code and notes, detailed inline. Both can be addressed while keeping services.rtl.address a string.
Reviewed with Codex (gpt-6.2-astra).
sebsgent
left a comment
There was a problem hiding this comment.
No must/should items from me beyond viberaven's open threads; the localhost and scoped-IPv6 cases matter beyond the notice wording, since systemd then holds no port and the attack this PR closes stays open for those configs.
Checked: the proxy's user has no extra groups, so with RTL's data dir and the secrets dir masked it can't reach the lnd macaroon or the CLN rune; the socket's IP filter matches RTL's in both tor.enforce cases; nodeinfo and the onion target are unaffected; the PrivateNetwork skip in the tor-enforce test doesn't drop any unit currently on master. Merges cleanly with master; relied on CI for the VM scenarios.
Assisted-by: Claude Code (Claude Opus 5.5)
6671559 to
1bf81ae
Compare
|
Thanks for the reviews. All in 1bf81ae.
|
viberaven
left a comment
There was a problem hiding this comment.
All three findings from my last review are addressed. Port zero is rejected when the proxy owns the listener. Hostnames and scoped IPv6 addresses now fail the system build, and the migration notice explains how to change them before connecting. I built the x86_64-linux module-config and test-runner checks locally. The default VM and CI gate also passed on GitHub. CI run
I’m requesting changes on the new build check. It accepts Unix-domain listeners where the module promises a TCP listener, and the same valid IPv6 unit can pass or fail depending on the build machine. Both findings are detailed inline.
Reviewed with Codex (gpt-6.2-astra).
RTL's web interface is plain HTTP with one password. Without netns-isolation, any local user could bind RTL's address and port while RTL was stopped, for example while lnd or clightning was stopped, serve a fake login page and collect the password. RTL now listens on a unix socket in its runtime dir, which only RTL's user can reach. A socket unit holds services.rtl.address and port from boot on, and rtl-proxy runs systemd-socket-proxyd to forward each connection to RTL. While RTL is stopped, a connection reaches the proxy and fails. The socket unit has RTL's IP filter, since a service's own filter doesn't apply to the connections that systemd passes in. Like RTL's own listener, it turns IPV6_V6ONLY off, so that :: also holds IPv4 when net.ipv6.bindv6only is set. A switch doesn't restart a changed socket unit, so the socket unit's name carries a hash of the address and port. A change then stops the old unit and starts a new one. A switch that changes only the proxy, like a systemd update, keeps the socket. The proxy has stopIfChanged = false, so the switch restarts it after the daemon-reload. Otherwise the switch would stop it before the reload and start it after. A connection before the reload would then start the old unit, which would keep running. The proxy restarts after a crash without a start limit, so systemd never closes the socket for it. The proxy can't see RTL's data dir or the secrets dir, and fails to start while one of them is missing. RTL's unit unsets PORT and HOST, which RTL would prefer to its config file. systemd skips a ListenStream= that it can't parse, like one with a host name or a zone ID. Without a listener it refuses the socket unit, but neither the switch nor the boot fails, and nothing holds the port. A path or a name that starts with @ gives a unix socket, which holds no port either. So the system build fails unless the address is a numeric IP address without a zone ID. Python's ipaddress module checks it, and reads no system state. systemd-analyze verify on the socket unit isn't enough. It takes a unix socket, and it refuses an IPv6 address when the build host's kernel has no IPv6. An assertion rejects port 0, which systemd can't listen on. A 0.2.0 notice and upgrade notes cover the first switch, which frees the port once, and the address. With netns-isolation, RTL's address exists only in RTL's namespace, which only root can enter, so RTL keeps listening on it directly. Assisted-by: Claude Code (Claude Opus 5.5)
1bf81ae to
6e06292
Compare
|
Thanks for the review. All in 6e06292.
|
viberaven
left a comment
There was a problem hiding this comment.
Both findings from my previous review are fixed in 6e06292f. I ran the module’s address check with /proc and /sys hidden. It accepted ::1 and ::ffff:127.0.0.1, and rejected both a filesystem socket path and an abstract Unix socket name. IPv6 validation is now independent of the builder’s network support.
The local module-config and test-runner checks passed. I also ran the full rtlIPv6 KVM scenario. It passed, including an authenticated UI request over IPv6, the port-ownership check with RTL stopped, and recovery after six proxy restarts. The GitHub CI gate is green. CI run
Reviewed with Codex (gpt-6.2-astra).
Fixes #262.
RTL's web interface is plain HTTP with one password. Without netns-isolation, any local user can bind RTL's address and port while RTL is stopped, and since #250 RTL stops each time lnd or clightning stops. The user can then serve a fake login page and collect the password that the operator types through the SSH tunnel.
RTL 0.15.17 can listen on a unix socket without a patch. It takes a
portthat isn't a number as a path, and listens there whenhostisn't set. So without netns-isolation, this PR:/run/rtl/rtl.sock./run/rtlis RTL's runtime dir with mode 0700, which systemd creates for each start and removes at each stop. No other user can create the path, and a stale socket never blocks a start.rtl-proxy-<hash>.socketthat listens onservices.rtl.addressandportfrom boot on and doesn't stop with RTL. It has RTL's IP filter, sotor.enforcestill applies, andFreeBind, so it can start before an address like a WireGuard IP exists. Like RTL's own listener, it turnsIPV6_V6ONLYoff (BindIPv6Only=both), so::also holds IPv4 and a v4-mapped address binds whennet.ipv6.bindv6onlyis set. The filter has to be on the socket unit, because systemd doesn't apply a service's IP filter to sockets that it passes in.rtl-proxy.service, which runssystemd-socket-proxydas RTL's user, because only that user can reach the socket. It hasdefaultHardening, no network of its own, onlyAF_UNIX, and no access to RTL's data dir, the secrets dir or the local resolvers. Like RTL, it starts only after the secrets are set up. While the data dir or the secrets dir is missing, it fails to start and retries, so it never runs without their masks.rtl-proxy.socketand noSockets=.stopIfChanged = falsemakes the switch restart the proxy after its daemon-reload. Otherwise the switch stops it before the reload and starts it after, and a connection before the reload starts the old unit, which then keeps running. The proxy restarts with a back-off and no start limit, because systemd closes a socket whose service hits the limit.PORTandHOSTfor RTL, also when they come from anEnvironmentFile. RTL prefers them to its config file, so they would move it off its unix socket.services.rtl.addresswhen the system is built. systemd skips aListenStream=that it can't parse, like one with a host name or a zone ID, and without a listener it refuses the unit. Neither the switch nor the boot fails then, and nothing holds the port. A path or a name that starts with@gives a unix socket, which holds no port either. So asystem.checksentry runs Python'sipaddressmodule frompkgs.buildPackages.python3on the address, and the build fails unless it's a numeric IP address without a zone ID. The error names the address. The address reaches Python through the environment, not through the code. The check reads no system state, so the build host's kernel doesn't matter. The option stays a string, with no parser in Nix.port = 0with an assertion, since systemd can't listen on it. RTL used to take 0 as 3000.docs/release-notes/0.2.0/anddocs/upgrading/0.2.0/, and a paragraph indocs/services.md.While RTL is stopped, a connection reaches the proxy, which can't reach RTL and closes it. A user who tries to bind the port gets
Address already in use. The SSH tunnel, the onion service andnodeinfokeep working with no config change.With netns-isolation nothing changes. RTL's address exists only in its namespace, which only root can enter. Joining the socket unit to that namespace would need hand-written boot ordering around
sockets.targetandnetns-rtl, which is more risk for no gain.Testing
test_rtllogs in through RTL's address and port and makes an authenticated API call, and does the same at the onion service's target. Then it stops RTL and checks thatnobodycan't bind the port and that a connection gets no answer. Without netns-isolation it also checks that 20 failed connections and six proxy kills leave the socket listening and held by systemd, and that the UI works after RTL's restart. Each kill must succeed and hit a new proxy. The test config gives the proxy a fixed 100 ms restart delay, so the six restarts would hit systemd's default start limit if the proxy had one. Its 2FA checks now use RTL's address and port too, not127.0.0.1:3000.nobodybinds127.0.0.1:3000in thedefaultscenario.rtlIPv6scenario runstest_rtlwith RTL on::1and lnd.run-tests.sh buildableruns it, and the CI matrix doesn't. So the node's kernel must let systemd listen on an IPv6 address, andnobodymust fail to bind it.default,netns,regtest,netnsRegtestandrtlIPv6pass on a KVM host. One earlierregtestrun failed because an outgoing connection took Fulcrum's port 50002, which is in the ephemeral range, and the rerun passed.module-configpasses with new cases for IPv6,BindIPv6Only, the unit name,UnsetEnvironment, port 0 with and without netns-isolation, the notice and that it doesn't name a host name, the build check, the socket's IP filter with and withouttor.enforce, and the proxy's sandbox. The last two fail when the socket gets another IP list or none, or when the proxy losesPrivateNetwork, its user, theAF_UNIX-only list or a mask, or gets a-on a mask. The address check must fail with its message, which names the address, forlocalhost,fe80::1%lo,/run/rtl/frontend.sock,@rtl-uiand1.2.3, and pass for127.0.0.1,::1,::ffff:127.0.0.1,0.0.0.0and::.module-configfails when the check takes a zone ID, ignores Python's exit status, runs with netns-isolation or is gone, and when the socket unit losesBindIPv6Only.test-runner, the release notes check andtest/shellcheck.shpass too. Thetor-enforcetest now skips units without a network of their own, likertl-proxy.nobodyloop that tries to bind the port during the switch never gets it. With a unit that takes 5 seconds to stop and a connection every 0.1 seconds during the switch, the proxy runs the new unit after the switch, and withoutstopIfChanged = falseit runs the old one. A port change moves the socket with no failed units, and so does the switch back.address = "::"serves IPv4 and IPv6. Withtor.enforce, the socket drops a client from127.0.0.2. RTL comes back afterkill -9. From inside the proxy's namespaces, RTL's data dir and/proc/<rtl pid>/rootaren't reachable. WithsecretsSetupMethod = "manual"and no secrets dir at boot, a connection makes the proxy fail with226/NAMESPACEand retry, while the socket keeps the port. Once the dir exists, the proxy starts and sees it empty, and the UI works. With-masks, the proxy started and later saw all secret files. In the normal setup, a connection beforesetup-secretswaits for the secrets, and the proxy starts once. WithPORT=3001from anEnvironmentFileandHOSTfromenvironment, RTL keeps its unix socket, and withoutUnsetEnvironmentit listens on0.0.0.0:3001. With systemd's default start limit on the proxy, the crash loop closes the socket and the test fails. A burst of 1000 connections fromnobodywhile the proxy fails or waits to restart leaves the socket active, since systemd holds the queued start until the restart. Without the build check and withaddress = "localhost"or"fe80::1%lo", at boot and after a switch from master's rtl.nix, the socket unit isbad-setting, the switch exits 0, no unit fails, nothing listens on port 3000, andnobodycan listen there. With::1and::ffff:127.0.0.1, the socket unit isloadedandactive, systemd listens on the address, andnobodycan't bind it. With the v4-mapped address,nobodycan't bind127.0.0.1:3000either. Withnet.ipv6.bindv6only = 1and withoutBindIPv6Only=both,::held only IPv6, andnobodycould listen on0.0.0.0:3000, and::ffff:127.0.0.1failed to bind withInvalid argument. With it,nobodycan't bind::,0.0.0.0,127.0.0.1or::1on port 3000, and the v4-mapped socket listens and serveshttp://127.0.0.1:3000.nixos-rebuild buildon the KVM host, not in this PR. The system build fails with the check's message forlocalhost,/run/rtl/frontend.sock,@rtl-ui,fe80::1%loand1.2.3, and port 0 fails at evaluation with the assertion. It builds for127.0.0.1,::1,::ffff:127.0.0.1,0.0.0.0,::and port 8443, and with netns-isolation, where the system has no check. The check's build command passes for::1and::ffff:127.0.0.1and fails forlocalhostin a sandbox with no/procand no/sys. Under strace, it opens nothing under/proc/netor/sys.Known limits. The first switch to this version, a switch back, and a switch that changes the address or port free the port for a moment, so the notice suggests
nixos-rebuild bootand a reboot. RTL sees no client addresses through the proxy, so five failed logins lock out every client for 30 minutes. With the default localhost setup that was already the case, since every client came from127.0.0.1. The proxy shares RTL's user, so a compromised proxy could kill RTL, but it can't read RTL's files or memory.[::1]:3000stays free as before, and the docs and the notice say to use the address and port that systemd holds in the tunnel. The PR doesn't parseaddressin Nix, so a bad address passes evaluation and fails only when the system is built. The node's kernel still decides if systemd can listen on an IPv6 address. On a node booted withipv6.disable=1, systemd refuses the socket unit for one, and nothing holds the port. The upgrade note'ssystemctl list-unitscheck shows that. The planned nbLib types can check the address at evaluation and replace the build check and the port 0 assertion.Written with Claude Opus 5.5, reviewed with Claude Fable 5.2, GPT-6 Astra, GPT-5.6 Sol, Kimi K3.1 Pro (twice), DeepSeek V4.1 Flash, GLM 5.3 and Qwen4-Max.