Skip to content

lib: defaultHardening leaves /run and host /dev/{shm,mqueue,hugepages} writable #49

Closed

Description

@bobberb

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: with ProtectKernelTunables, ProtectControlGroups, ProtectProc = "invisible" or ProcSubset = "pid" (all in defaultHardening), /run and the mounts below it (/run/keys, /run/wrappers) are not remounted read-only.
  • /dev: PrivateDevices = true bind-mounts the host's /dev/shm, /dev/mqueue and /dev/hugepages into the private /dev read-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/shm and /dev/mqueue (both mode 1777).
  • onion-addresses runs as root with PrivateUsers = false and CAP_DAC_OVERRIDE CAP_CHOWN CAP_FOWNER (modules/onion-addresses.nix:53-60), so it can write anywhere in /run, /run/keys and /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

  1. added a commit that references this issue on Oct 15, 2026
    88eceb0
  2. added
    area: modules
    type: security
    on Oct 16, 2026
  3. added 2 commits that reference this issue on Oct 17, 2026
    dfd5f8f
    d072deb
  4. added a commit that references this issue on Oct 25, 2026
    fa68dd8
  5. added a commit that references this issue on Nov 8, 2026
    b4368d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: modulestype: security

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions