Repository navigation
presets/wireguard: accept REST connections only on wg-nb and require the firewall - #61
Conversation
15f7748 to
95ce593
Compare
|
Added |
|
Thanks, the change looks good. What I tested, with this commit on top of current
What's needed from your side before merging:
|
95ce593 to
c4ae497
Compare
|
Pushed and rebased onto
The test for the The The description lists the models that helped with this PR, for the changes and for the review. |
The preset binds the REST APIs of lnd, clnrest and clightning-rest to
0.0.0.0 and protects them only with the iptables rules in
`networking.firewall.extraCommands`.
- Add `-i wg-nb` to the accept rules. With `-s` alone, a packet with a
spoofed WireGuard-subnet source address arriving on any other
interface (e.g. the LAN) was accepted. This mirrors the same change in
the ostermayer/nix-bitcoin fork.
- Add `-d ${serverAddress}` to the accept rules. They come before the
`restrictPeer` REJECT in `nixos-fw`, so without `-d` an authenticated
peer could reach the REST ports through another local address of the
server, such as its LAN address.
- Assert that `networking.firewall.enable` is set when any `*connect`
helper is enabled. Without the firewall the rules do not exist and the
REST APIs are open. No nftables assertion is needed: NixOS already
fails evaluation for a non-empty `extraCommands` under nftables.
The wireguard-lndconnect VM test now checks the generated rule and
sends spoofed-source requests over the plain test network.
…restrictPeer NixOS fills nixos-fw with its own ACCEPT rules, including the ones for allowedTCPPorts, before extraCommands run. The appended restrictPeer REJECT never saw peer traffic to a globally opened port, so the peer could still reach that port, SSH for example, on any other address of the server. Insert the REJECT at the head of nixos-fw and FORWARD. It only matches peer traffic that isn't addressed to the server's tunnel address, so tunnel traffic still passes. restrictPeer is enforced by extraCommands too, so the firewall assertion now also fires when restrictPeer is set. It also requires the iptables backend, because extraCommands run only there. NixOS already fails evaluation on a non-empty extraCommands under nftables, but firewalld silently ignores them, which left the REST ports without the accept rules and the peer without the REJECT. The VM test opens a global port on the server and gives the server a second address on eth1. Through the tunnel, the peer can't reach the global port or the lnd REST port on that address, and both attempts hit the REJECT rule. The test also checks that the REJECT is the first rule in both chains, and that REST on the tunnel address still works.
c4ae497 to
c79c1df
Compare
|
Merged, thanks. Could you please take a look at running tests for Please review following agent text and help me to decide whether we should cover it. A follow-up request: CI runs only the
If other buildable scenarios ( |
|
Thanks for the merge. The CI change is #106. It's one matrix line. The eval, the driver build and the cached-skip logic pick the scenario up with no script change, and Astra confirmed the wiring. I timed it on GitHub-hosted runners through my fork, which has no Cachix key, so every job built and ran its VM for real. Four runs of the I also ran every buildable scenario once in the fork. All green. |
Problem
The WireGuard preset binds the REST APIs of lnd, clnrest and clightning-rest to
0.0.0.0. Only the iptables rules innetworking.firewall.extraCommandsprotect them. Four gaps:-s ${wgSubnet}.0/24only. A packet with a spoofed WireGuard-subnet source address arrives on any interface, such as the LAN, and the rule accepts it.networking.firewall.enable = false, the rules don't exist and the REST APIs are open. The build gives no warning.restrictPeerREJECT is appended tonixos-fwandFORWARD. NixOS puts its own ACCEPT rules forallowedTCPPortsinnixos-fwbeforeextraCommandsrun, so a peer still reaches any globally opened port, SSH for example, on the server's other addresses. The option promises the peer reaches onlyserverAddress.restrictPeeron its own. With the connect helpers off and the firewall disabled,restrictPeer = trueevaluated fine and installed nothing.Change
-i wg-nband-d ${serverAddress}to the three REST accept rules. Traffic must arrive through the tunnel and be addressed to the server's tunnel address. Without-dan authenticated peer could reach the ports through another local address of the server, whichrestrictPeeris meant to rule out (the accept rules precede the REJECT). The-ipart mirrors theostermayer/nix-bitcoinfork. The-s ${wgSubnet}.0/24match is unchanged.lndconnect,lnconnect-clnrest,lndconnect-clightningorrestrictPeeris enabled,networking.firewall.enablemust be true and the backend must be iptables.extraCommandsrun only with that backend. firewalld ignored them, which left the REST ports without the accept rules and the peer without the REJECT. The message says to disable nftables and firewalld rather than set the backend by hand, because with nftables enabled the firewall package is nftables and the iptables unit has no binary.restrictPeerREJECT at position 1 ofnixos-fwandFORWARDinstead of appending it. The rule matches only peer traffic that isn't addressed toserverAddress, so tunnel flows aren't touched.extraStopCommandsis unchanged since-Dmatches by rule spec.firewall-nftables.nix) already fails evaluation on a non-emptyextraCommands, so that case needed no new check.Testing
test/wireguard-lndconnect.nixhas a new subtest, "REST accept rules only match traffic from wg-nb". It does these checks:iptables -S nixos-fwon the server contains a-i wg-nbaccept rule for the lnd REST port. The port comes fromlndconnect-wg --url.10.10.0.3/32toeth1, which is the plain test network and not the tunnel. Then it runscurl --interface 10.10.0.3tohttps://server:<lnd rest port>/v1/getinfo. The request must fail.-ipin.nixos-fw. Thewg-nbaccept rule must have a packet count of 0.curlfails on the old rules too, because the server's reply goes intowg-nb. A comment in the test says so. Only the packet counters show the difference.10.10.0.1.A second new subtest, "restrictPeer rejects peer traffic to globally opened ports", checks the REJECT position:
allowedTCPPortsand runs a small HTTP listener on it.iptables -S nixos-fw 1andiptables -S FORWARD 1on the server show the peer REJECT.192.0.2.1, oneth1. The client adds it to the peer's allowed IPs and routes it through the tunnel.curlto port 2222 and to the lnd REST port on192.0.2.1must both fail, and the REJECT rule's packet counter must rise for each. The test doesn't route the server'seth1address itself, because that can be the tunnel's own endpoint.10.10.0.1still answers afterwards.run-tests.sh -s wireguard-lndconnect build, final revision, on a KVM boxnixos-fw, thewg-nbrule counted 0, the REJECT counted the probe to192.0.2.1, tunnel pinged after teardownThe
-dmatch also changes what an authenticated peer reaches whenrestrictPeeris off. Say if you want amodules/versioning.nixentry for that.Models used on this PR: Fable 5.1 and Opus 5.5 for the changes. Kimi K3 (max) and GLM 5.3 (max) reviewed the first revision. DeepSeek V4.1 Flash 1017 (max), GLM 5.3 (max) and GPT-6 Astra (xhigh) reviewed the final one. Astra caught that the first test version routed the tunnel's own endpoint into the tunnel, fixed above with the separate address. GLM noted that
restrictPeercovers IPv4 only. The server accepts only the peer's one IPv4 address through the tunnel, so that isn't reachable. DeepSeek flagged that the peer reaches every port on10.10.0.1. That's what the option promises, so nothing changed for it.