Repository navigation
Description
Problem
nbLib.defaultHardening (pkgs/lib.nix:9) sets ProtectSystem = "strict", which is expected to make everything outside a unit's own state/runtime dirs read-only. On the systemd in current nixpkgs (260.5) two areas stay writable:
/run: withProtectKernelTunables,ProtectControlGroups,ProtectProc = "invisible"orProcSubset = "pid"(all indefaultHardening),/runand the mounts below it (/run/keys,/run/wrappers) are not remounted read-only./dev:PrivateDevices = truebind-mounts the host's/dev/shm,/dev/mqueueand/dev/hugepagesinto the private/devread-write. These are the host's mounts, shared with every other process, not private copies.
Whether a unit can use this depends on DAC:
- Non-root units (bitcoind, clightning, lnd, ...) can create files in the host's
/dev/shmand/dev/mqueue(both mode 1777). onion-addressesruns as root withPrivateUsers = falseandCAP_DAC_OVERRIDE CAP_CHOWN CAP_FOWNER(modules/onion-addresses.nix:53-60), so it can write anywhere in/run,/run/keysand/run/wrappers.
This was found while hardening setup-secrets in #48 (for #25): there a declared secret symlinked into /run or /dev/shm still got its target chowned.
Evidence
VM test on master (af32606), with a probe added as ExecStartPost to each unit. It runs inside the unit's own sandbox and tries touch on every mount point in /proc/self/mountinfo.
| Unit | uid | Writable outside its own dirs and private /tmp, /var/tmp |
|---|---|---|
| bitcoind | 999 | /dev/shm, /dev/mqueue |
| clightning | 998 | /dev/shm, /dev/mqueue |
| lnd | 996 | /dev/shm, /dev/mqueue (/run/lnd is its RuntimeDirectory, expected) |
| onion-addresses | 0 | /run, /run/keys, /run/systemd, /run/wrappers, /dev/shm, /dev/mqueue, /dev/hugepages |
Probe scenario (run with ./test/run-tests.sh --extra-scenarios probe.nix -s hardeningProbe)
{ scenarios, pkgs, lib, nix-bitcoin }: with lib; let
probe = name: pkgs.writeShellScript "hp-probe-${name}" ''
export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.gawk ]}
echo "HPROBE[${name}] uid=$(id -u)"
while read -r mp; do
mp=$(printf '%b' "$mp")
[[ -d $mp ]] || continue
f="$mp/.hprobe.$$"
if touch "$f" 2>/dev/null; then rm -f "$f"; echo "HPROBE[${name}] RW $mp"; fi
done < <(awk '{print $5}' /proc/self/mountinfo | sort -u)
'';
addProbe = name: { systemd.services.${name}.serviceConfig.ExecStartPost = mkAfter [ "${probe name}" ]; };
in {
hardeningProbe = { imports = [
{
services.bitcoind.enable = true;
services.clightning.enable = true;
services.lnd.enable = true;
services.lnd.port = 9736;
services.tor.enable = true;
nix-bitcoin.onionServices.bitcoind.public = true;
test.shellcheckServices.enable = mkForce false;
}
(addProbe "bitcoind")
(addProbe "clightning")
(addProbe "lnd")
(addProbe "onion-addresses")
]; };
}Candidate fix
Add to defaultHardening:
ReadOnlyPaths = [ "/run" "/dev" ];ReadWritePaths, RuntimeDirectory and StateDirectory still nest inside it. Device nodes like /dev/null and /dev/urandom keep working, because a read-only mount does not stop writes to a device node.
Same probe with ReadOnlyPaths = [ "/run" "/dev" ] added to the four units: each one can then only write its own dirs (/var/lib/<unit>, /run/lnd for lnd) and its private /tmp//var/tmp. The bitcoind, clightning and lnd checks of the test framework passed in that run.
Not checked yet: the other units using defaultHardening (btcpayserver, charge-lnd, clightning-rest, electrs, fulcrum, lightning-loop, lightning-pool, liquid, mempool, rtl), and units that create sockets or pid files directly under /run instead of in a RuntimeDirectory. A fix PR would need the full scenario set.
🤖 Generated with Claude Code
Activity
lib: make /run and /dev read-only in defaultHardening
lib: make /run and /dev read-only in defaultHardening
lib: make /run and /dev read-only in defaultHardening
lib: make /run and /dev read-only in defaultHardening
lib: make /run and /dev read-only in defaultHardening (#51)
lib: make /run and /dev read-only in defaultHardening