Skip to content

tor.enforce: allow only local addresses and hide resolver sockets - #264

Merged
seberm merged 8 commits into
masterfrom
tor-enforce-local-only
Nov 15, 2026
Merged

seberm merged 8 commits into
masterfrom
tor-enforce-local-only

Conversation

@seberm

@seberm seberm commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

tor.enforce is meant to keep a service off the clearnet except through Tor. Two paths were outside it:

  • The systemd IP filter allowed all of 169.254.0.0/16, not only loopback and the netns-isolation addresses. Without netns-isolation a service could reach any link-local address, including a cloud provider's metadata service.
  • Name lookups went through the host's nscd socket (and systemd-resolved or D-Bus, if enabled), which the IP filter doesn't cover.

Changes

  • allowLocalIPAddresses allows loopback only, plus the netns /24 (169.254.<addressblock>.0/24) when netns-isolation is on. This applies to every service with tor.enforce, to charge-lnd and to unbeholden-wallet.
  • These units can't reach /run/nscd, /run/systemd/resolve or /run/dbus, so glibc resolves names itself, from /etc/hosts, and a query to an IP resolver meets the filter. /run/avahi-daemon is masked too when Avahi is enabled.
  • The masks fail closed: a unit doesn't start when a masked directory is missing. security.nix creates the three directories on every node, so custom units that use allowLocalIPAddresses or allowTor start too.
  • nscd keeps its runtime directory across restarts. Without that, an nscd restart detached the mask in running units.
  • netns-isolation rejects addressblock 169 and 170.
  • Breaking: a 0.1.6 versioning notice and upgrade notes, for setups that relied on a link-local peer without netns-isolation, or on resolving a peer by host name. Evaluation can't detect custom units, so the notice says to check them by hand.
  • Release notes, service docs and the option description describe the new restriction and its limits.

Known limits

Also in the docs:

  • Without netns-isolation, a host service on 127.0.0.1 or ::1 stays reachable, because the filter can't tell ports apart.
  • Other local daemons that make network requests for their clients over a Unix socket aren't covered.
  • Settings that widen the filter on purpose still work: an operator's IPAddressAllow, tor.enforce = false. BTCPay Server isn't restricted by default.

Tests

  • module-config: cases for the allow list, the masks, the directories, the nscd setting, the rejected address blocks and the notice.
  • New VM test tor-enforce, in every scenario: it checks that the restricted units are exactly the expected ones, checks each unit's deny rule, whole allow list and masks, replays them in a transient unit against a listener on 169.254.169.254, probes the running units in their own cgroup and mount namespace, and repeats that after an nscd restart. A custom restricted unit must start in every scenario.
  • The hardened scenario now runs glibc's nscd, so the stale pid file cleanup is covered.

The first seven commits are by ostermayer. The last one adds unbeholden-wallet to the expected units of the VM test, and the release notes entry.

ostermayer and others added 8 commits November 15, 2026 23:26
A service with tor.enforce (and charge-lnd) now reaches only localhost
and, with netns-isolation, its namespace subnet
169.254.<addressblock>.0/24.

The systemd IP filter covers IP sockets only, so these services also
can't reach the local name-resolution sockets (nscd, systemd-resolved,
D-Bus, and Avahi when enabled). glibc then resolves names itself from
/etc/hosts, and a query to an IP resolver meets the filter.

The nscd, resolved and D-Bus masks fail closed: security.nix creates
their directories whenever a restricted service is enabled, matching
each resolver's own ownership, and keeps nscd's runtime directory
across restarts so a restart doesn't re-expose its socket. Avahi is
opt-in and owns its directory, so its mask is skipped when absent.

netns-isolation rejects an address block that would place a cloud
metadata address inside the allowed subnet. The docs note that a
resolver on an allowed loopback address stays reachable, because the
filter can't tell ports apart.

Services that need a further address allow it per service, and
resolve host names from networking.hosts or by address. Add a 0.1.6
migration entry, upgrade and service notes, module-config tests and a
VM test over each restricted unit.

Assisted-by: Claude Code (Claude Opus 5.5)
The Avahi mask had a `-` prefix, so it applied only if /run/avahi-daemon
existed when the unit started. If a user enabled Avahi later, the
restricted units kept running unchanged and could reach its socket. A
compromised service could then ask Avahi to resolve or browse names over
that socket, outside the IP filter.

Mask the directory only when `services.avahi.enable` is set, and without
the prefix. Avahi's module creates the directory with tmpfiles before
any service starts. Enabling Avahi now changes the restricted units, so
the switch restarts them with the mask.

Also say in the upgrade notes and the option description that
netns-isolation rejects addressblock 169 and 170.

Assisted-by: Claude Code (Claude Opus 5.5)
The fail-closed resolver masks of `nix-bitcoin.lib.allowLocalIPAddresses`
need /run/nscd, /run/systemd/resolve and /run/dbus to exist. security.nix
created them only when a service from a fixed list used the restriction.
A custom unit with `allowLocalIPAddresses`, or a future module missing
from the list, got the masks without the directories and failed to start
with 226/NAMESPACE.

Create the three directories, and keep nscd's runtime directory across
restarts, on every node. The service list is now only used by the 0.1.6
migration notice, so it lives in versioning.nix alone.

Every test scenario now has a custom unit with the restriction, and the
tor-enforce test checks that it starts. This covers scenarios without
tor.enforce, such as setup-secrets. The module-config tests check the
directories and the nscd setting without tor.enforce. The nbxplorer case
moves to the migration test, where its gating still matters.

Assisted-by: Claude Code (Claude Opus 5.5)
Without the resolver directories, the tor-enforce test failed at the
`touch` of a marker file while it set up its probes, before it checked
any unit. So on the previous modules it didn't fail on the missing
restriction itself. Check each unit's deny rule, allow list and masks
first.

The test also selected units by `127.0.0.1/32` in `IPAddressAllow`, so a
unit whose allow list was overridden dropped out without a failure.
Select units by either part of the restriction, the localhost entry or
the nscd mask. A unit then drops out only if it overrides both.

Assisted-by: Claude Code (Claude Opus 5.5)
…cted

Custom units that use `nix-bitcoin.lib.allowLocalIPAddresses` or
`allowTor` get the narrower restriction too. Say so in the 0.1.6 notice
and in the upgrade notes.

The notice still uses the list of services. The versioning check throws
from a `system` definition, and reading `systemd.services` there causes
infinite recursion, because units depend on `system.*` options. So a
node with only such a custom unit gets no notice, and the upgrade notes
tell users to check these units themselves.

Assisted-by: Claude Code (Claude Opus 5.5)
The tor-enforce test found units only by the restriction itself, so a
unit that lost both its localhost entry and its nscd mask dropped out
without a failure. It also checked only that `127.0.0.1/32` is allowed,
so a broadened list such as `127.0.0.1/32 ::/0` passed.

Compute the units that must be restricted independently in tests.nix:
the custom test unit, charge-lnd, nbxplorer with btcpayserver, and the
enabled services with tor.enforce. The test fails if one of them isn't
selected. The IPAddressAllow of each selected unit must equal the
loopback addresses, plus the namespace subnet with netns-isolation.

The hardened scenario now uses glibc's nscd, so the nscd restart check
also runs with the pid file that the ExecStartPre removes. The
module-config tests check that only glibc's nscd gets that ExecStartPre,
and that a host that only imports the modules gets the resolver
directories and the nscd setting.

Assisted-by: Claude Code (Claude Opus 5.5)
…un order

Fail when a restricted unit is missing or unexpected, so a new restricted
unit must be added to the expected list. Add tor-enforce to the runner's
phase order check. Say in the 0.1.6 notice that evaluation can't detect
custom units.

Assisted-by: Claude Code (Claude Opus 5.5)
unbeholden-wallet uses allowLocalIPAddresses unconditionally, like
charge-lnd. The tor-enforce test checks the exact set of restricted
units, so it failed in scenarios that enable the wallet.

Add the release notes entry for the fix.

Assisted-by: Claude Code (Claude Fable 5.2)
@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 area: packages pkgs/ and flake pins type: feature New capability or service labels Nov 15, 2026
@seberm seberm added the type: security Hardening or vulnerability (public-safe only) label Nov 15, 2026
@seberm
seberm requested a review from prusnak November 15, 2026 22:28
@seberm seberm added breaking Changes existing user config; needs a versioning.nix entry and removed type: feature New capability or service labels Nov 15, 2026
@seberm seberm added this to the v0.1.6 milestone Nov 15, 2026
@seberm

seberm commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

@ostermayer @prusnak could you take a look? Thanks!

@viberaven

Copy link
Copy Markdown
Contributor

Reviewed at b775958. No blocking findings.

I checked the narrowed address lists, reserved address-block validation, resolver masks, directory ownership/lifecycle, service integration, and migration documentation. The nscd directory preservation addresses the restart issue; the pinned systemd-resolved unit already sets RuntimeDirectoryPreserve=yes.

Validation on x86_64-linux:

  • module-config passed (116 configuration checks).
  • test-runner passed (9 tests).
  • An additional temporary NixOS VM scenario passed with glibc nscd, systemd-resolved, and Avahi enabled. It ran the PR's tor-enforce test, then checked all four socket paths before and after restarting nscd, systemd-resolved, Avahi, and D-Bus. The sockets remained hidden from the restricted unit's mount namespace, localhost resolution continued working, and the restricted process retained the same PID.

The existing PR CI is also green, including the default, netns, and netnsRegtest VM scenarios; I relied on CI for those full scenarios.

Optional test follow-up: retain a lightweight resolved/Avahi scenario with these restart checks. The built-in VM test currently exercises nscd restart and checks three mask paths; Avahi's mask has evaluation coverage but no corresponding runtime assertion.

Review assisted by Codex (GPT-6 Astra).

@seberm

seberm commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

No must-fix or should-fix items.

Tested: ./test/run-tests.sh -s hardened build on b775958 passes, including tor-enforce over 10 units and the glibc nscd restart. I also checked that switch-to-configuration recreates the resolver directories through sysinit-reactivation.target before it restarts the changed units, so upgrading an existing node doesn't stop the restricted units from starting.

Assisted-by: Claude Code (Claude Opus 5.5, Claude Fable 5.1 as advisor)

@seberm
seberm merged commit 772846d into master Nov 15, 2026
25 checks passed
@seberm
seberm deleted the tor-enforce-local-only branch November 15, 2026 23:10
unbeholdendev added a commit to unbeholdendev/infra that referenced this pull request Nov 17, 2026
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [bitcoinix/bitcoinix](https://github.com/bitcoinix/bitcoinix) | patch | `v0.1.5` → `v0.1.6` |

---

### Release Notes

<details>
<summary>bitcoinix/bitcoinix</summary>

### [`v0.1.6`](https://github.com/bitcoinix/bitcoinix/releases/tag/v0.1.6): bitcoinix 0.1.6

[Compare Source](bitcoinix/bitcoinix@v0.1.5...v0.1.6)

**Security release with breaking changes.** Narrows Tor enforcement to local addresses, closes a netns-isolation bypass, hardens RTL and the custom lnd macaroons, and updates lnd to 0.21.8-beta. All users should upgrade. Evaluation fails with a message for each breaking change that affects your config; act on it, then set `nix-bitcoin.configVersion = "0.1.6";`. Deploying restarts every netns-isolated service and each RTL backend once.

#### Security

- **Tor enforcement allowed more than Tor.** Services with `tor.enforce` and charge-lnd could reach every link-local address `169.254.0.0/16`, including a cloud provider's metadata service, and could resolve names through the host's resolver sockets. They now reach only localhost and, with netns-isolation, the namespace subnet, and can't use the nscd, systemd-resolved, D-Bus and Avahi sockets ([#&#8203;264](bitcoinix/bitcoinix#264), [GHSA-cgh8-4w2p-8fr8](GHSA-cgh8-4w2p-8fr8)).
- **netns-isolation:** with `br_netfilter` loaded, which Docker does, a service without `tor.enforce` could reach the listeners of namespaces it isn't connected to. Traffic between namespaces is no longer masqueraded ([#&#8203;239](bitcoinix/bitcoinix#239)).
- **RTL verifies its backends.** RTL sent the lnd admin macaroon and the clightning admin rune to any server on a backend's port. It now requires the backend's pinned TLS certificate ([#&#8203;258](bitcoinix/bitcoinix#258)) and stops when lnd or clightning stops, instead of polling a port that another local user could take over ([#&#8203;250](bitcoinix/bitcoinix#250)).
- **RTL two-factor authentication survives restarts.** Before, each restart turned 2FA off without a warning. `RTL-Config.json`, which holds the 2FA secret, is now readable only by RTL's user ([#&#8203;254](bitcoinix/bitcoinix#254)).
- **Custom lnd macaroons** (`services.lnd.macaroons`, used by BTCPay Server and charge-lnd) are now readable only by their `user`. Before, the user's primary group could read them, which for `operator` is `users`, so other normal users could. If other users log in to your node, [revoke all macaroons](https://docs.bitcoinix.net/services.html#revoke-all-macaroons) ([#&#8203;255](bitcoinix/bitcoinix#255)). Each custom macaroon now has its own root key, so one can be revoked without the others ([#&#8203;236](bitcoinix/bitcoinix#236)).
- **Root scripts no longer load config files from a service user's home.** The lnd user could make root's `curl` and `jq` load `~/.curlrc` and `~/.jq` from lnd's data dir ([#&#8203;241](bitcoinix/bitcoinix#241), [GHSA-cwmc-wjvh-3qp6](GHSA-cwmc-wjvh-3qp6)).
- **Backups fail when a database dump fails.** Before, duplicity succeeded without the database. With `services.postgresqlBackup.compression` set to `"zstd"` or `"none"`, backups left out the BTCPay and NBXplorer databases; they are now included, so run `systemctl start duplicity` after upgrading if you use one of these ([#&#8203;225](bitcoinix/bitcoinix#225)).
- **Multipath TCP:** service namespaces now copy the host's `net.mptcp.enabled`, so the setting of `hardened-extended` reaches netns-isolated services ([#&#8203;206](bitcoinix/bitcoinix#206)).
- **Secrets:** `setup-secrets` rejects empty secret files, and secret generation no longer replaces an empty TLS, WireGuard or replication SSH key ([#&#8203;244](bitcoinix/bitcoinix#244)).
- **WireGuard preset:** evaluation fails for firewall setups in which the preset's rules can't run or don't stay loaded ([#&#8203;188](bitcoinix/bitcoinix#188)).
- **lnd [0.21.8-beta](https://github.com/lightningnetwork/lnd/blob/master/docs/release-notes/release-notes-0.21.8.md)** fixes correctness bugs in channel state persistence, gossip validation, the sweeper and HTLC recovery ([#&#8203;263](bitcoinix/bitcoinix#263)).

#### Breaking changes

- **Tor enforcement:** a service with `tor.enforce` that connects to a non-local address or by host name needs a change. netns-isolation rejects `addressblock` 169 and 170 ([#&#8203;264](bitcoinix/bitcoinix#264)).
- **Onion address access:** `lndconnect.onion` and `clnrest.lnconnect.onion` give the operator user access only when `nix-bitcoin.operator.enable` is set. `nix-bitcoin.onionAddresses.access` accepts only users that are defined and enabled in `users.users` ([#&#8203;170](bitcoinix/bitcoinix#170)).
- **Secrets:** an empty or non-regular secret file now fails `setup-secrets`, and then no service can read its secrets. Check before deploying: `find /etc/nix-bitcoin-secrets -maxdepth 1 -type f -empty`. Set `nix-bitcoin.secrets.<name>.allowEmpty = true` for a secret that may be empty ([#&#8203;244](bitcoinix/bitcoinix#244)).
- **lnd wallet:** lnd stops when `wallet.db` isn't a regular file, or when its seed file is a symlink to a missing file while it has no wallet ([#&#8203;244](bitcoinix/bitcoinix#244)).
- **lnd macaroons:** bitcoinix deletes unused root keys with IDs `0x6e62000000000000` to `0x6e62ffffffffffff` on each lnd start; check `lncli listmacaroonids` before deploying. lnd's `no-macaroons` option isn't supported. Restart services that read a custom macaroon only at startup ([#&#8203;236](bitcoinix/bitcoinix#236)). A program that read a custom macaroon through the group needs its own macaroon ([#&#8203;255](bitcoinix/bitcoinix#255)).
- **RTL:** with both backends enabled, stopping one stops RTL for both ([#&#8203;250](bitcoinix/bitcoinix#250)). `LN_SERVER_URL`/`LND_SERVER_URL`, another `Settings.lnServerUrl`, a proxy without `no_proxy` for the backend, and servers with a self-signed certificate no longer work as before ([#&#8203;258](bitcoinix/bitcoinix#258)).
- **WireGuard preset:** fails evaluation with the nftables firewall package without iptables, with `networking.nftables.flushRuleset`, and with an `ip` or `ip6` table named `filter` in `networking.nftables.tables` ([#&#8203;188](bitcoinix/bitcoinix#188)).
- **extra-container [1.0.1](https://github.com/extra-container/extra-container/releases/tag/v1.0.1):** containers can no longer reach the host's Nix daemon socket, so `nix build`, `nix run`, `nix develop` and `nix-shell` fail inside them. `extra.hostNixDaemon = true;` restores the old behavior; read the security section of the extra-container README first ([#&#8203;211](bitcoinix/bitcoinix#211)).
- **Packages:** `rtl` and `trustedcoin` moved to `legacyPackages.<system>.pinned`. Trezor is `nix-bitcoin.pkgs.nbPython3Packages.trezor`. `nbPython3Packages` uses the Python interpreter of nixpkgs-unstable, so build custom Python environments with `nbPython3Packages.python` ([#&#8203;245](bitcoinix/bitcoinix#245)).
- **nodeinfo:** every bitcoinix update now produces a new system generation, because the revision is part of the script. Set `nix-bitcoin.revision = null;` to opt out ([#&#8203;243](bitcoinix/bitcoinix#243)).

See [Upgrading to config version 0.1.6](https://docs.bitcoinix.net/upgrading.html#upgrading-to-config-version-016).

#### New

- **[Unbeholden Wallet](https://docs.bitcoinix.net/services.html#connect-to-unbeholden-wallet):** a watch-only wallet that runs in the browser and hands transactions to an air-gapped signer over QR codes. Enable it with `services.unbeholden-wallet.enable = true;` ([#&#8203;230](bitcoinix/bitcoinix#230)).
- **nodeinfo** shows the bitcoinix revision and the package version of each service. `nodeinfo --version` prints only the revision, `nodeinfo NAME ...` only the given entries ([#&#8203;243](bitcoinix/bitcoinix#243)).

#### Fixes

- lnd announces its onion address with the port Tor forwards. With a `services.lnd.port` other than 9735, peers couldn't reach the node over Tor ([#&#8203;228](bitcoinix/bitcoinix#228)).
- clnrest replaces an empty or `null` admin rune on start. Generate connection URLs from `lnconnect-clnrest` again if they were created with such a rune ([#&#8203;217](bitcoinix/bitcoinix#217)).
- btcpayserver with the clightning backend joins `services.clightning.group`, so a custom group works ([#&#8203;238](bitcoinix/bitcoinix#238)).
- The lnd seed and electrs index migration steps keep the data when an earlier step fails ([#&#8203;242](bitcoinix/bitcoinix#242)).

#### Updates

| Package                       | 0.1.5       | 0.1.6                                                                            |
| ----------------------------- | ----------- | -------------------------------------------------------------------------------- |
| lnd                           | 0.21.6-beta | [0.21.8-beta](https://github.com/lightningnetwork/lnd/releases/tag/v0.21.8-beta) |
| RTL                           | 0.15.16     | [0.15.17](https://github.com/Ride-The-Lightning/RTL/releases/tag/v0.15.17)       |
| extra-container (flake input) | 0.17        | [1.0.1](https://github.com/extra-container/extra-container/releases/tag/v1.0.1)  |
| Unbeholden Wallet             | –           | [0.4.0](https://github.com/unbeholdendev/unbeholden-wallet/releases/tag/v0.4.0)  |

Also: mempool builds with Node.js 24 ([#&#8203;246](bitcoinix/bitcoinix#246)); the docs say what netns-isolation doesn't isolate ([#&#8203;237](bitcoinix/bitcoinix#237)) and how to close public SSH without losing access ([#&#8203;234](bitcoinix/bitcoinix#234)).

Contributors: [@&#8203;msgilligan](https://github.com/msgilligan), [@&#8203;ostermayer](https://github.com/ostermayer), [@&#8203;prusnak](https://github.com/prusnak), [@&#8203;seberm](https://github.com/seberm), [@&#8203;unbeholdendev](https://github.com/unbeholdendev), [@&#8203;viberaven](https://github.com/viberaven).

Full changes: <bitcoinix/bitcoinix@v0.1.5...v0.1.6>

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4yNDEuMCIsInVwZGF0ZWRJblZlciI6IjQ0LjI0MS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: agent <[email protected]>
Reviewed-on: https://git.home.unbeholden.dev/unbeholden/infra/pulls/140
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 area: packages pkgs/ and flake pins breaking Changes existing user config; needs a versioning.nix entry type: security Hardening or vulnerability (public-safe only)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants