Skip to content

lib: make /run and /dev read-only in defaultHardening - #51

Merged
prusnak merged 3 commits into
bitcoinix:masterfrom
bobberb:default-hardening-ro
Oct 25, 2026
Merged

prusnak merged 3 commits into
bitcoinix:masterfrom
bobberb:default-hardening-ro

Conversation

@bobberb

@bobberb bobberb commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#49: with nbLib.defaultHardening, ProtectSystem = "strict" leaves /run writable, and PrivateDevices bind-mounts the host's /dev/shm, /dev/mqueue and /dev/hugepages read-write.

  • Non-root services (bitcoind, clightning, lnd, ...) could create files in the host's /dev/shm and /dev/mqueue.
  • onion-addresses runs as root with CAP_DAC_OVERRIDE, so it could write anywhere in /run, including /run/keys and /run/wrappers.

Change

  • pkgs/lib.nix: defaultHardening gets ReadOnlyPaths = [ "/run" "/dev" ].
    • RuntimeDirectory (lnd's /run/lnd), StateDirectory and ReadWritePaths still nest inside it, also for a dataDir under /run.
    • Device nodes like /dev/null stay writable, and connecting to unix sockets under /run still works (postgres, mysqld, NOTIFY_SOCKET). A read-only mount doesn't block writes to device nodes or socket connects.
    • A user-set ReadOnlyPaths on one of these services is merged with the list; only mkForce drops it.
  • New VM test hardening-ro-paths, enabled in every scenario:
    • Eval: every service whose SystemCallFilter carries defaultHardening's blocklist entry must have /run and /dev in ReadOnlyPaths. This catches a module that later overrides the attr. It detects exactly the 15 services that use defaultHardening in full.
    • Runtime: root tries to create a file in /run, /run/keys, /dev/shm, /dev/mqueue and /dev/hugepages inside each service. Root ignores file permissions, so only a read-only mount stops it, and any error other than EROFS also fails the test.
      • Running services: the probe runs in the service's mount namespace via nsenter.
      • Active oneshot services (onion-addresses): the test adds the probe as ExecStartPost through a runtime drop-in in /run/systemd/system, restarts the unit, reads the probe output by the unit's InvocationID, then removes the drop-in.
    • It runs before backups, which stops bitcoind and the services that depend on it.
  • docs/configuration.md: new section "Write to /run or /dev/shm from a service hook". It says which paths are read-only, that a custom preStart/postStart/ExecStartPost writing there fails with Read-only file system, and the two ways around it: ReadWritePaths (merged with the service's own list) or a + prefix. Suggested by @ostermayer.

Testing

Run Result
ad hoc { lnd, clightning, tor, onion service }, fix PASS, runtime check covers bitcoind, clightning, lnd, onion-addresses
same, pkgs/lib.nix at master (test kept) FAIL: ReadOnlyPaths lacks /run or /dev: {'bitcoind': [], 'clightning': [], 'lnd': [], 'onion-addresses': []}
same, pkgs/lib.nix at master, eval check disabled FAIL: bitcoind: writable inside the service: ['/run', '/run/keys', '/dev/shm', '/dev/mqueue', '/dev/hugepages']
same, fix, but onion-addresses given ReadOnlyPaths = [ ], eval check disabled FAIL: onion-addresses: ['nb-ro-probe: writable /run', ... /run/keys, /dev/shm, /dev/mqueue, /dev/hugepages]
default, netns, hardened PASS, runtime check covers 13 services: bitcoind, btcpayserver, clightning, clightning-rest, fulcrum, lightning-loop, lightning-pool, liquidd, lnd, mempool, nbxplorer, onion-addresses, rtl (electrs is stopped earlier by stop-electrs)
full, regtest, netnsRegtest PASS, the same 13 plus electrs
lndPruned PASS (bitcoind, lnd, onion-addresses)
trustedcoin PASS (bitcoind, clightning)
wireguard-lndconnect, clightning-replication (own test scripts, without hardening-ro-paths; replication covers sshfs and gocryptfs), run on the same module change PASS
First round, test placed after backups full FAIL: backups had stopped bitcoind, so assert "bitcoind" in checked failed. The other 9 passed. Moved the test before backups.
default, rebased on master (1528ecf), with the docs commit PASS, runtime check covers the same 13 services
./test/shellcheck.sh PASS
nix flake check --all-systems PASS
Separate attacker review of the change PASS, 3 low findings, all fixed: /dev/hugepages added to the probe, a service restarting mid-check no longer fails the test, and oneshot services are now probed. Also checked: a unit given ReadWritePaths = [ "/dev/shm" ] passes the eval check and fails the runtime check; unix socket connect under /run works; onion-addresses ends Result=success.
Second separate review, of the reworked test PASS. Re-ran a half-fix (bitcoind with ReadWritePaths = [ "/run/keys" "/dev/shm" ]): the runtime check reports both. Fixed its findings: wait for journald before reading the probe output, wait for onion-addresses before the check, clean up the drop-in and probe file.

Not covered

  • charge-lnd is a timer-run oneshot that is inactive when the test runs, so only the eval check covers it.
  • Custom user hooks in these services (preStart, postStart, ExecStartPost) that write to /run or /dev/shm now fail with EROFS. No in-tree module does this. docs/configuration.md documents it and the workarounds.
  • No versioning.nix entry: whether a config breaks depends on user scripts, which no eval-time condition can see.
  • Mounts the host creates under /run after a service started (e.g. logind's /run/user/<uid>) can arrive writable through mount propagation, as the secrets: sandbox setup-secrets and lock down every unlisted entry #48 review found for setup-secrets. Not checked here.

🤖 Generated with Claude Code

@bobberb
bobberb force-pushed the default-hardening-ro branch from 8707809 to 88eceb0 Compare October 15, 2026 23:33
@seberm seberm added area: modules modules/ and pkgs/lib.nix type: security Hardening or vulnerability (public-safe only) labels Oct 16, 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.

I ran this branch on a KVM box: default and regtest pass. The runtime check covered bitcoind, btcpayserver, clightning, clightning-rest, fulcrum, lightning-loop, lightning-pool, liquidd, lnd, mempool, nbxplorer, onion-addresses and rtl, plus electrs on regtest. So the .NET services (nbxplorer, btcpayserver) and the Node services (rtl, mempool) work with /dev/shm read-only. That was my main concern.

The change itself is sound. RuntimeDirectory and ReadWritePaths nest inside ReadOnlyPaths, and device nodes and unix socket connects are not affected by a read-only mount. The eval check catches a module that drops the attribute later.

One note for the description, not blocking: a user preStart or postStart that writes under /run now fails with EROFS. The "Not covered" section says this. A line in docs/configuration.md or a versioning.nix note would help operators find it faster.

Approving.

bobberb added a commit to bobberb/bitcoinix that referenced this pull request Oct 17, 2026
With defaultHardening, /run and /dev are read-only, so a custom
preStart, postStart or ExecStartPost that writes there fails with
EROFS. Say so in docs/configuration.md and show the two ways around
it: ReadWritePaths (which merges with the service's own list) or a
`+` prefix.

Suggested by ostermayer in the bitcoinix#51 review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bobberb
bobberb force-pushed the default-hardening-ro branch from 88eceb0 to 1ac478a Compare October 17, 2026 01:03
@bobberb

bobberb commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator Author

@ostermayer thanks for running it on KVM. Added in 1ac478a: a new section in docs/configuration.md, "Write to /run or /dev/shm from a service hook". It says a custom preStart/postStart/ExecStartPost writing there fails with Read-only file system, and gives the two ways around it: ReadWritePaths or a + prefix. I checked by eval that a user's ReadWritePaths adds to the service's own list (lnd: ["/run/my-dir", "/var/lib/lnd"]), so the example doesn't drop the data dir.

I left out a versioning.nix entry: whether a config breaks depends on user scripts, which no eval-time condition can see. The branch is also rebased on master, so ci_gate runs now; default passes locally.

bobberb added a commit to bobberb/bitcoinix that referenced this pull request Oct 17, 2026
With defaultHardening, /run and /dev are read-only, so a custom
preStart, postStart or ExecStartPost that writes there fails with
EROFS. Say so in docs/configuration.md and show the two ways around
it: ReadWritePaths (which merges with the service's own list) or a
`+` prefix.

Suggested by ostermayer in the bitcoinix#51 review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bobberb
bobberb force-pushed the default-hardening-ro branch from 1ac478a to bf3186f Compare October 17, 2026 22:52
bobberb and others added 3 commits October 25, 2026 22:01
`ProtectSystem = "strict"` doesn't cover /run with the other settings in
defaultHardening, and `PrivateDevices` bind-mounts the host's /dev/shm,
/dev/mqueue and /dev/hugepages read-write. So non-root services could
write to the host's /dev/shm and /dev/mqueue, and onion-addresses (root,
CAP_DAC_OVERRIDE) to all of /run, including /run/keys and /run/wrappers.

`ReadOnlyPaths = [ "/run" "/dev" ]` closes both. RuntimeDirectory,
StateDirectory and ReadWritePaths still nest inside it, and device nodes
like /dev/null stay writable.

Add the hardening-ro-paths test. It checks that every service built from
defaultHardening keeps both paths, and that /run, /run/keys, /dev/shm,
/dev/mqueue and /dev/hugepages reject a root write inside each running
service and inside onion-addresses.

Fixes bitcoinix#49

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assisted-by: Claude Code (Claude Opus 5.5)
Assisted-by: Codex (GPT-6)
With defaultHardening, /run and /dev are read-only, so a custom
preStart, postStart or ExecStartPost that writes there fails with
EROFS. Say so in docs/configuration.md and show the two ways around
it: ReadWritePaths (which merges with the service's own list) or a
`+` prefix.

Suggested by ostermayer in the bitcoinix#51 review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assisted-by: Claude Code (Claude Opus 5.5)
Assisted-by: Codex (GPT-6)
Notify users of affected services before read-only /run and /dev break custom hooks or plugins. Document the 0.1.3 migration and writable path exceptions.

Assisted-by: Codex (GPT-6)
@prusnak
prusnak force-pushed the default-hardening-ro branch from bf3186f to b589c4c Compare October 25, 2026 21:08
@github-actions github-actions Bot added area: ci .github/, test/, dev/ area: docs docs/, README, examples/, SECURITY.md, book.toml, assets area: packages pkgs/ and flake pins labels Oct 25, 2026
@prusnak

prusnak commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

rebased PR on master, added versioning entry

@seberm

seberm commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

utACK

@prusnak
prusnak merged commit 73a39ad into bitcoinix:master Oct 25, 2026
12 checks passed
@seberm seberm mentioned this pull request Oct 25, 2026
msgilligan pushed a commit to msgilligan/bitcoinix that referenced this pull request Nov 8, 2026
With defaultHardening, /run and /dev are read-only, so a custom
preStart, postStart or ExecStartPost that writes there fails with
EROFS. Say so in docs/configuration.md and show the two ways around
it: ReadWritePaths (which merges with the service's own list) or a
`+` prefix.

Suggested by ostermayer in the bitcoinix#51 review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assisted-by: Claude Code (Claude Opus 5.5)
Assisted-by: Codex (GPT-6)
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 type: security Hardening or vulnerability (public-safe only)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants