Skip to content

clightning: make the data dir not writable by its group - #272

Merged
prusnak merged 2 commits into
bitcoinix:masterfrom
ostermayer:clightning-datadir-not-group-writable
Nov 18, 2026
Merged

prusnak merged 2 commits into
bitcoinix:masterfrom
ostermayer:clightning-datadir-not-group-writable

Conversation

@ostermayer

@ostermayer ostermayer commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #123.

This is hardening of the data dir route. The RPC socket already lets every member of the clightning group run code as the clightning user, for example with plugin start. Taking BTCPay Server off the socket is #87. The bitcoind and lnd data dirs stay 0770, because only the operator joins those groups.

The Core Lightning data dir had mode 0770, so every member of the clightning group could create, rename and delete entries in it. The group holds the operator user when nix-bitcoin.operator.enable is set, the btcpayserver user with services.btcpayserver.lightningBackend = "clightning", and any user you add. A member could rename its own file over config after preStart wrote it and before lightningd read it, and make lightningd load a plugin as the clightning user. Or it could move the network dir aside and plant one with its own hsm_secret.

Group members only need to pass through the data dir to reach the RPC socket and to read config, so the dir is now 0750. The tmpfiles rule sets this mode on existing nodes at the next deployment. lightningd creates the network dir with mode 0700, and postStart adds only g+x. So the RPC socket is the only entry under the data dir that the group can write. I checked each user of the group: BTCPay Server, lightning-cli, lnconnect-clnrest, nodeinfo, RTL's root scripts, the clnrest rune, the plugins and replication. None of them writes to the data dir as a group member.

A script that writes to the data dir as a group member now fails. So config version 0.2.0 gets a notice for configs with clightning. docs/upgrading.md explains the change and how to list entries that a group member created. The release notes get a 0.2.0 entry.

Tests:

  • The clightning test checks the data dir's owner, group and mode. It checks that the RPC socket is the only entry the group or others can write, and that every entry belongs to the clightning user. For each member of the clightning group, it checks that lightning-cli getinfo works and that creating a file in the data dir fails.
  • The new scenario btcpayserverClightning runs BTCPay Server on the clightning backend. It's in the CI matrix. The btcpayserver test checks lnd's macaroon only with the lnd backend. The Greenfield request in the regtest test reaches clightning through the socket.
  • config-replace plants its symlink as the clightning user, because the operator can't write the data dir anymore.
  • module-config checks which configs get the notice.

Testing

On an x86_64-linux KVM host:

  • ./test/run-tests.sh -s btcpayserverClightning: pass on the final head
  • ./test/run-tests.sh -s regtest, -s default and -s lnconnectNoOperatorRole: pass on the previous head. Since then only the test's quoting and member loop and the docs changed.
  • nix eval .#ciTestInfo: pass, and it lists btcpayserverClightning
  • nix build .#checks.x86_64-linux.{module-config,test-runner,ci-workflows,lnd-macaroon-filters}: pass
  • ./test/shellcheck.sh: pass
  • systemd-tmpfiles --create with a d <dir> 0750 rule changes an existing 0770 dir to 0750, as on an upgraded node.

I didn't run netns and netnsRegtest. CI runs them.

  • After the rebase onto master c3d955c (Keep unreleased migration notices visible on master #283), module-config, test-runner, ci-workflows, shellcheck and btcpayserverClightning pass on the KVM host. This PR doesn't change master's plugin migration checks. On master, the notice is a warning until 0.2.0 is released, and the tests pass with version.nix at 0.1.6 and at 0.2.0.

Models

Claude Opus 5.5 wrote the change. Codex GPT-5.6 Sol, Codex GPT-6 Astra (two passes plus a recheck), Kimi K3.1 Pro (two passes), DeepSeek V4.1 Flash 1017 and GLM 5.3 reviewed it. Claude Opus 5.5 rebased it onto #224 and #283.

@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 type: feature New capability or service labels Nov 16, 2026

@viberaven viberaven left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 7592e18e5377.

No actionable code findings. The tmpfiles change removes group write access while preserving traversal to the RPC socket; the existing config replacement still handles files left by older deployments. The new BTCPay/Core Lightning scenario is wired into the existing CI matrix, and the migration notice explains the affected scripts. Local module-config, repository ShellCheck, and mdBook builds pass. I did not rerun the VMs locally.

CI follow-up: the failing ShellCheck job never reached ShellCheck. Nix installation failed downloading the tarball with curl: (35) ... unexpected eof while reading. Rerun that failed job before merging; this is not a reported lint defect.

Review performed with Codex (GPT-6 Astra).

@github-actions github-actions Bot added the status: rebase needed Merge conflicts need to be resolved label Nov 16, 2026
@ostermayer
ostermayer force-pushed the clightning-datadir-not-group-writable branch from 7592e18 to 7c15500 Compare November 18, 2026 00:19
The data dir had mode 0770, so every member of group clightning could
create, rename and delete entries in it. The operator is in this group,
and so is btcpayserver with lightningBackend = "clightning". A member
could rename its own file over `config` after preStart wrote it and
before lightningd read it. Then lightningd loads the member's plugin as
the clightning user, which can read hsm_secret. A member could also
move the network dir aside and plant one with its own hsm_secret.

Group members only need to pass through the data dir to reach the RPC
socket and to read `config`, so make it 0750. lightningd creates the
network dir with mode 0700 and postStart adds g+x, so the RPC socket is
the only group-writable entry left under the data dir. The VM test now
checks this, and that each group member can use lightning-cli but can't
create files in the data dir. A new scenario in the CI matrix runs
BTCPay Server on the clightning backend. The config-replace test plants
its symlink as the clightning user, because the operator can't write
the data dir anymore.

The RPC socket still gives every group member full control over the
node, so this takes no power from a member that uses it. It removes
write access that no member needs.

Add a 0.2.0 notice, because a script that writes to the data dir as a
group member now fails.

Fixes bitcoinix#123.

Assisted-by: Claude Code (Claude Opus 5.5)
@ostermayer
ostermayer force-pushed the clightning-datadir-not-group-writable branch from 7c15500 to 6b1896f Compare November 18, 2026 01:01
@github-actions github-actions Bot added status: rebase needed Merge conflicts need to be resolved and removed status: rebase needed Merge conflicts need to be resolved labels Nov 18, 2026
@prusnak prusnak added this to the v0.2.0 milestone Nov 18, 2026
@github-actions github-actions Bot removed the status: rebase needed Merge conflicts need to be resolved label Nov 18, 2026
@prusnak
prusnak merged commit f9d239e into bitcoinix:master Nov 18, 2026
26 checks passed
prusnak added a commit that referenced this pull request Nov 23, 2026
…oup (#305)

Fixes #55.

Bitcoin Core calls `umask(077)` when it starts, and Core 25.0 removed
the `sysperms` setting. So with `dataDirReadableByGroup`, every file
that bitcoind created while it ran got mode 0600. A group member that
reads blocks from disk lost access to new ones until the next restart.
On regtest the option did nothing, because preStart looked for `blocks`
in the data dir and not in `regtest/`.

A default ACL on a dir takes the place of the umask for new files below
it. Core creates block files with `fopen` (mode 0666) and LevelDB uses
0644, so with the ACL new files get 0640. I checked this with Core 32.1
in a VM. A file that's created with an explicit 0600 stays 0600, but
Core doesn't do that in the blocks dir.

This PR:
- adds the oneshot unit `bitcoind-group-read`, which runs before each
start of bitcoind, only with `dataDirReadableByGroup` on. It's a unit of
its own because bitcoind runs with `PrivateUsers`, and there setfacl
can't write back an ACL that names other users or groups. It waits for a
mount at `blocks/index`, at `blocks` or at any dir above them, so a disk
that's mounted on demand gets the ACL and not the empty mount point.
- gives the owning group `r-x` and a default ACL with `r-x` on the
blocks dir and each dir in it, and `r--` on each file. To reach the
blocks dir, the group gets `--x` on the data dir and, on regtest, on the
network dir.
- adds the group's rights to an ACL that's already there, with getfacl,
a short awk script and `setfacl --restore`. All other users and groups
keep their effective rights. Before, `chmod g+rX` changed only the mask
of such an ACL, so the group could stay locked out while named entries
gained rights. An entry of another group is left alone.
- creates the blocks dir before bitcoind does, so the ACL also covers
the files of the first start. On regtest it also creates
`regtest/wallets`, because Core makes that dir only in a network dir
that it creates itself.
- uses the network dir, so the option also works on regtest.
- logs a warning and starts bitcoind anyway when a step fails, for
example on ZFS without `acltype=posixacl`. The group then reads only the
files that exist at start, like before. That's safer than a failed
start. The option only gives another program read access, and a stopped
bitcoind takes the Lightning nodes down with it.
- enters each dir with `cd` only after it checks that it's a dir of the
bitcoin user, and then works only on `.` and the names in it. The data
dir is group-writable, so a group member could put a symlink at `blocks`
or `regtest`, also between two commands. A symlink there stops the setup
with the warning. Master's `chmod -R` followed such a symlink on
mainnet, so this gets a 0.2.0 notice and an upgrade note. The note says
how to move to a bind mount.
- removes `services.bitcoind.sysperms` with `mkRemovedOptionModule`, a
0.2.0 notice and an upgrade note.
- removes `UMask = "0027"`, which Core overrides.
- says in the option description that turning the option off doesn't
revoke the access, also for new files, and how to revoke it.
- adds the regtest VM scenario `bitcoindGroupRead` to CI. It runs only
bitcoind and its tests. The test checks that the operator can read every
file in the blocks dir after the first start, after a restart and after
a crash, but not `chainstate`. It replaces `regtest` and `blocks` with
symlinks and checks that the setup leaves the targets alone. It also
sets up an ACL with named groups and narrow masks and checks that their
effective rights don't change.
- adds module-config tests for the removed option and the two notices,
and a release note draft.

A group member can still rename dirs in the mainnet data dir before a
restart, for example `wallets` to `blocks`. Master's chmod has the same
problem, and no path check can stop it. A data dir that the group can't
write would, like #272 did for Core Lightning. That's for a separate PR.

## Testing

- `bitcoindGroupRead` passes. On master it fails, because the operator
can't enter `/var/lib/bitcoind/regtest/blocks`.
- The same test on mainnet passes. On master it fails, because the
operator can't enter `/var/lib/bitcoind/blocks` after the first start.
After a restart, the new `CURRENT`, `MANIFEST-000004`, `000005.ldb` and
`000006.log` in `blocks/index` have mode 0600 too.
- Without the `blocks` or the `regtest` symlink check, without the
change to the named entries of the default ACL, or without the check of
the owning group, `bitcoindGroupRead` fails.
- With the ACL set and `fastprune`, 400 mined blocks gave the new
`blk00001.dat` and `rev00001.dat` mode 0640.
- On btrfs mounted with `noacl`, setfacl fails with "Operation not
supported". bitcoind starts and logs the warning.
- With a `noauto` bind mount at the blocks dir that mounts late, the
operator can read every file on the first start and after a restart, on
mainnet and on regtest. Without the mount dependency, the setup runs
before the mount and the operator can't enter the blocks dir. The same
holds for a late mount at `blocks/index`.
- `default` passes.
- `module-config`, `test-runner`, `nix eval --raw .#ciTestInfo`,
`test/shellcheck.sh` and `helper/check-release-notes.sh` pass.

Written with Claude Opus 5.5 and reviewed with Claude Fable 5.2, GPT-6
Astra, Kimi K3.1 Pro, DeepSeek V4.1 Flash, Gemini 4 Carbon, GLM 5.3 and
Qwen4-Max.
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 type: feature New capability or service

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clightning: the group-writable data directory lets a group member swap the config and run code as the clightning user

3 participants