Skip to content

presets/wireguard: accept REST connections only on wg-nb and require the firewall - #61

Merged
seberm merged 2 commits into
bitcoinix:masterfrom
ostermayer:wireguard-guards
Oct 18, 2026
Merged

seberm merged 2 commits into
bitcoinix:masterfrom
ostermayer:wireguard-guards

Conversation

@ostermayer

@ostermayer ostermayer commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The WireGuard preset binds the REST APIs of lnd, clnrest and clightning-rest to 0.0.0.0. Only the iptables rules in networking.firewall.extraCommands protect them. Four gaps:

  • The accept rules match on -s ${wgSubnet}.0/24 only. A packet with a spoofed WireGuard-subnet source address arrives on any interface, such as the LAN, and the rule accepts it.
  • With networking.firewall.enable = false, the rules don't exist and the REST APIs are open. The build gives no warning.
  • The restrictPeer REJECT is appended to nixos-fw and FORWARD. NixOS puts its own ACCEPT rules for allowedTCPPorts in nixos-fw before extraCommands run, so a peer still reaches any globally opened port, SSH for example, on the server's other addresses. The option promises the peer reaches only serverAddress.
  • The firewall assertion didn't cover restrictPeer on its own. With the connect helpers off and the firewall disabled, restrictPeer = true evaluated fine and installed nothing.

Change

  • Add -i wg-nb and -d ${serverAddress} to the three REST accept rules. Traffic must arrive through the tunnel and be addressed to the server's tunnel address. Without -d an authenticated peer could reach the ports through another local address of the server, which restrictPeer is meant to rule out (the accept rules precede the REJECT). The -i part mirrors the ostermayer/nix-bitcoin fork. The -s ${wgSubnet}.0/24 match is unchanged.
  • Add an assertion: if lndconnect, lnconnect-clnrest, lndconnect-clightning or restrictPeer is enabled, networking.firewall.enable must be true and the backend must be iptables. extraCommands run 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.
  • Insert the restrictPeer REJECT at position 1 of nixos-fw and FORWARD instead of appending it. The rule matches only peer traffic that isn't addressed to serverAddress, so tunnel flows aren't touched. extraStopCommands is unchanged since -D matches by rule spec.
  • Under nftables the pinned nixpkgs (firewall-nftables.nix) already fails evaluation on a non-empty extraCommands, so that case needed no new check.

Testing

test/wireguard-lndconnect.nix has a new subtest, "REST accept rules only match traffic from wg-nb". It does these checks:

  • iptables -S nixos-fw on the server contains a -i wg-nb accept rule for the lnd REST port. The port comes from lndconnect-wg --url.
  • The client adds 10.10.0.3/32 to eth1, which is the plain test network and not the tunnel. Then it runs curl --interface 10.10.0.3 to https://server:<lnd rest port>/v1/getinfo. The request must fail.
  • During the spoof attempt, the server's rpfilter jump is removed. Without this, rpfilter drops the spoofed packet first, and the test wouldn't exercise the -i pin.
  • A counting rule must show that the spoofed SYNs reached nixos-fw. The wg-nb accept rule must have a packet count of 0.
  • The spoof curl fails on the old rules too, because the server's reply goes into wg-nb. A comment in the test says so. Only the packet counters show the difference.
  • Teardown restores the address, the counting rule and rpfilter. Then the test checks that the tunnel still pings 10.10.0.1.

A second new subtest, "restrictPeer rejects peer traffic to globally opened ports", checks the REJECT position:

  • The server opens port 2222 in allowedTCPPorts and runs a small HTTP listener on it.
  • iptables -S nixos-fw 1 and iptables -S FORWARD 1 on the server show the peer REJECT.
  • The client reaches port 2222 over the plain test network.
  • The server gets a second address, 192.0.2.1, on eth1. The client adds it to the peer's allowed IPs and routes it through the tunnel. curl to port 2222 and to the lnd REST port on 192.0.2.1 must both fail, and the REJECT rule's packet counter must rise for each. The test doesn't route the server's eth1 address itself, because that can be the tunnel's own endpoint.
  • lnd REST on 10.10.0.1 still answers afterwards.
Run Result
run-tests.sh -s wireguard-lndconnect build, final revision, on a KVM box PASS, all six subtests. Spoofed SYNs reached nixos-fw, the wg-nb rule counted 0, the REJECT counted the probe to 192.0.2.1, tunnel pinged after teardown

The -d match also changes what an authenticated peer reaches when restrictPeer is off. Say if you want a modules/versioning.nix entry 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 restrictPeer covers 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 on 10.10.0.1. That's what the option promises, so nothing changed for it.

@ostermayer

Copy link
Copy Markdown
Collaborator Author

Added -d ${serverAddress} to the three accept rules as well: they precede the restrictPeer REJECT, so without it an authenticated peer could still reach the REST ports via another local address of the server. VM test re-run on a KVM box, all subtests pass.

@ostermayer ostermayer changed the title presets/wireguard: pin REST accept rules to wg-nb, require the firewall presets/wireguard: accept REST connections only on wg-nb and require the firewall Oct 17, 2026
@seberm seberm added type: security Hardening or vulnerability (public-safe only) area: modules modules/ and pkgs/lib.nix labels Oct 17, 2026
@seberm

seberm commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Thanks, the change looks good. What I tested, with this commit on top of current master:

  • wireguard-lndconnect passes, all subtests.
  • Negative control with the old rules: a spoofed 10.10.0.3 source over eth1 is accepted by the REST accept rule, and with this PR it isn't. An authenticated peer reaching the lnd REST port through the tunnel at another local address of the server connects with the old rules and is refused with this PR.

What's needed from your side before merging:

  • The commit message describes only the -i wg-nb part. Please add the -d ${serverAddress} match and why it's needed (the accept rules precede the restrictPeer REJECT).
  • If AI tooling helped with this change, please add an Assisted-by: commit trailer naming at least the tool and the primary model with its version, as AGENTS.md ("AI-assisted contributions") asks.
  • Please add a test for the -d case: give the server a second local address (e.g. 192.0.2.1/32 on lo), route it through the tunnel on the client (wg set ... allowed-ips + ip route add), and check that the peer can't reach the REST port there.
  • The curl in the spoof check fails on the old rules too, because the server's reply goes into wg-nb, so only the packet counters show the difference. It's fine to keep, but a comment there saying so would help.
  • Please rebase onto master when you push. It's behind, and the ruleset needs it up to date.

@ostermayer

ostermayer commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

Pushed and rebased onto master. The branch has two commits.

  • 4fbc8e1 is the original change. Its message now covers the -d ${serverAddress} match and why the accept rules need it.
  • c4ae497 fixes two gaps the review found while I worked on your points. The restrictPeer REJECT was appended, so NixOS's own ACCEPT rules for allowedTCPPorts came first and a peer still reached SSH on the server's other addresses. It's now inserted first in nixos-fw and FORWARD. The firewall assertion now also fires for restrictPeer, and it requires the iptables backend, since firewalld ignores extraCommands.

The test for the -d case is in the new subtest "restrictPeer rejects peer traffic to globally opened ports". The server gets 192.0.2.1 on eth1. The client adds it to the peer's allowed IPs and routes it through the tunnel. curl to the lnd REST port and to a globally opened port on that address both fail, and the REJECT counter rises for each. REST on 10.10.0.1 still works after. The spoof curl now has the comment about the reply going into wg-nb.

The -d match also changes what an authenticated peer reaches when restrictPeer is off. Say if you want a versioning.nix entry for that.

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.
@seberm
seberm merged commit ec8b8c7 into bitcoinix:master Oct 18, 2026
6 checks passed
@seberm

seberm commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks. Could you please take a look at running tests for wireguard-lndconnect in the CI? I am not sure why they are not enabled by default already (probably due to resources?)

Please review following agent text and help me to decide whether we should cover it.


A follow-up request: CI runs only the default, netns and netnsRegtest scenarios (a runtime budget inherited from upstream), so wireguard-lndconnect isn't covered and every change to the WireGuard preset needs a local VM run. Could you look into adding it to the test_scenario matrix in .github/workflows/test.yml, as a separate PR?

  • test/ci/test-info.nix reads the scenario names from that matrix, so the test drivers should be picked up automatically. Please check that build_test_drivers and the "skip when already cached" logic handle it.
  • Please check that it runs reliably on GitHub-hosted runners (KVM, two VMs) and report the job's runtime. A few runs to rule out flakiness would help.
  • ci_gate already depends on test_scenario, so no change is needed there.

If other buildable scenarios (regtest, clightning-replication, …) turn out to be cheap enough, mention it, but wireguard-lndconnect is the one we need first.

@ostermayer

Copy link
Copy Markdown
Collaborator Author

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 wireguard-lndconnect job took 1 min 30 s to 1 min 48 s, all six subtests green each time. KVM handled the two VMs fine. That makes it the cheapest job in the matrix. default takes about 3 min 30 s and netns about 5 min 45 s in the same runs.

I also ran every buildable scenario once in the fork. All green. lndPruned, trustedcoin and clightning-replication are in the same 1 to 2 minute band as WireGuard. The others take about 6 minutes, like netns. The table is in #106.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: modules modules/ and pkgs/lib.nix type: security Hardening or vulnerability (public-safe only)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants