Repository navigation
lib: make /run and /dev read-only in defaultHardening - #51
Conversation
8707809 to
88eceb0
Compare
ostermayer
left a comment
There was a problem hiding this comment.
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.
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]>
88eceb0 to
1ac478a
Compare
|
@ostermayer thanks for running it on KVM. Added in 1ac478a: a new section in I left out a |
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]>
1ac478a to
bf3186f
Compare
`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)
bf3186f to
b589c4c
Compare
|
rebased PR on master, added versioning entry |
|
utACK |
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)
Problem
#49: with
nbLib.defaultHardening,ProtectSystem = "strict"leaves/runwritable, andPrivateDevicesbind-mounts the host's/dev/shm,/dev/mqueueand/dev/hugepagesread-write./dev/shmand/dev/mqueue.onion-addressesruns as root withCAP_DAC_OVERRIDE, so it could write anywhere in/run, including/run/keysand/run/wrappers.Change
pkgs/lib.nix:defaultHardeninggetsReadOnlyPaths = [ "/run" "/dev" ].RuntimeDirectory(lnd's/run/lnd),StateDirectoryandReadWritePathsstill nest inside it, also for adataDirunder/run./dev/nullstay writable, and connecting to unix sockets under/runstill works (postgres, mysqld,NOTIFY_SOCKET). A read-only mount doesn't block writes to device nodes or socket connects.ReadOnlyPathson one of these services is merged with the list; onlymkForcedrops it.hardening-ro-paths, enabled in every scenario:SystemCallFiltercarriesdefaultHardening's blocklist entry must have/runand/devinReadOnlyPaths. This catches a module that later overrides the attr. It detects exactly the 15 services that usedefaultHardeninginfull./run,/run/keys,/dev/shm,/dev/mqueueand/dev/hugepagesinside each service. Root ignores file permissions, so only a read-only mount stops it, and any error other than EROFS also fails the test.nsenter.onion-addresses): the test adds the probe asExecStartPostthrough a runtime drop-in in/run/systemd/system, restarts the unit, reads the probe output by the unit'sInvocationID, then removes the drop-in.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 custompreStart/postStart/ExecStartPostwriting there fails withRead-only file system, and the two ways around it:ReadWritePaths(merged with the service's own list) or a+prefix. Suggested by @ostermayer.Testing
{ lnd, clightning, tor, onion service }, fixpkgs/lib.nixat master (test kept)ReadOnlyPaths lacks /run or /dev: {'bitcoind': [], 'clightning': [], 'lnd': [], 'onion-addresses': []}pkgs/lib.nixat master, eval check disabledbitcoind: writable inside the service: ['/run', '/run/keys', '/dev/shm', '/dev/mqueue', '/dev/hugepages']onion-addressesgivenReadOnlyPaths = [ ], eval check disabledonion-addresses: ['nb-ro-probe: writable /run', ... /run/keys, /dev/shm, /dev/mqueue, /dev/hugepages]default,netns,hardenedstop-electrs)full,regtest,netnsRegtestlndPrunedtrustedcoinwireguard-lndconnect,clightning-replication(own test scripts, withouthardening-ro-paths; replication covers sshfs and gocryptfs), run on the same module changebackupsfullFAIL:backupshad stopped bitcoind, soassert "bitcoind" in checkedfailed. The other 9 passed. Moved the test beforebackups.default, rebased onmaster(1528ecf), with the docs commit./test/shellcheck.shnix flake check --all-systems/dev/hugepagesadded to the probe, a service restarting mid-check no longer fails the test, and oneshot services are now probed. Also checked: a unit givenReadWritePaths = [ "/dev/shm" ]passes the eval check and fails the runtime check; unix socket connect under/runworks;onion-addressesendsResult=success.ReadWritePaths = [ "/run/keys" "/dev/shm" ]): the runtime check reports both. Fixed its findings: wait for journald before reading the probe output, wait foronion-addressesbefore the check, clean up the drop-in and probe file.Not covered
charge-lndis a timer-run oneshot that is inactive when the test runs, so only the eval check covers it.preStart,postStart,ExecStartPost) that write to/runor/dev/shmnow fail with EROFS. No in-tree module does this.docs/configuration.mddocuments it and the workarounds.versioning.nixentry: whether a config breaks depends on user scripts, which no eval-time condition can see./runafter 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 forsetup-secrets. Not checked here.🤖 Generated with Claude Code