Skip to content

bitcoind: let the group read new block files with dataDirReadableByGroup - #305

Merged
prusnak merged 1 commit into
bitcoinix:masterfrom
ostermayer:bitcoind-group-read
Nov 23, 2026
Merged

prusnak merged 1 commit into
bitcoinix:masterfrom
ostermayer:bitcoind-group-read

Conversation

@ostermayer

@ostermayer ostermayer commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

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.

@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 20, 2026
@ostermayer ostermayer added type: bug Behaviour is wrong vs. documented or intended type: removal Module, package or option dropped (implies breaking) breaking Changes existing user config; needs a versioning.nix entry labels Nov 20, 2026
@seberm seberm added this to the v0.2.0 milestone Nov 20, 2026

@sebsgent sebsgent 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.

No must/should items.

The ACL is limited to the blocks dir, and the group still can't read chainstate. The remaining race between the -L check and chmod -R (which follows a symlink given as its argument) can't reach anything beyond the data dir and /run/bitcoind, because of ProtectSystem=strict. That's the same rename exposure the PR body already names, so it fits the follow-up for a data dir the group can't write.

Tested: CI green, including bitcoindGroupRead. Checked locally that with the default ACL and umask 077, new files get 0640 and new subdirs 0750 and inherit the ACL, and that setfacl -P refuses a symlink argument.

Assisted-by: Claude Code (Claude Opus 5.5)

@sebsgent sebsgent 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.

Approving per the review above.

Assisted-by: Claude Code (Claude Opus 5.5)

@github-actions github-actions Bot added the status: rebase needed Merge conflicts need to be resolved label Nov 21, 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.

The standalone bitcoindGroupRead VM and the module-config and test-runner checks passed locally on x86_64-linux. I’m requesting changes because I also reproduced a group-read failure with an existing ACL, an LND start-limit failure in a combined scenario, and an evaluation error in the documented migration. The inline comments include the fixes and the permission-handling details.

The branch currently has merge conflicts with master in test/run-tests.sh and test/tests.nix.

Assisted-by: Codex (gpt-6.2-astra)

Comment thread modules/bitcoind.nix Resolved
Comment thread modules/bitcoind.nix Resolved
Comment thread modules/bitcoind.nix Resolved
Comment thread test/tests.nix Resolved
@github-actions github-actions Bot removed the status: rebase needed Merge conflicts need to be resolved label Nov 22, 2026
@ostermayer

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. All in ce87711, rebased onto master.

  • The setup now moves through the tree with cd and works only on . and bare names. enterDir takes the type and dev:ino of a name with one lstat, enters it, and checks that . is a dir of the bitcoin user with the same dev:ino. A rename after that can't move it out of the dir, and setfacl -P doesn't follow a symlink at a bare name. A symlink at blocks or regtest still stops the setup with the warning. There's no VM test for the race itself, so the commit message explains the construction.
  • Existing ACLs are now read with getfacl, changed by a short awk script (modules/bitcoind/grant-group.awk) and applied with setfacl -P --restore. The script ORs the group's rights into group:: and into the mask, so the owning group gets r-x on each dir of the blocks tree and r-- on each file, also with group::---. The data dir and, on regtest, the network dir get --x the same way. I reproduced your case first. The new test fails on db4618d with Permission denied for the operator on the blocks dir.
  • Where the mask gains a right, the script takes that right from every named entry, in the access and the default ACL. So each named user and group keeps its effective rights, and --restore doesn't recalculate the mask. The test covers this with blockbackup and blockwriter entries and narrow masks on the blocks dir, its index dir and their files. A variant that leaves the default ACL's named entries alone fails it.
  • bitcoind runs with PrivateUsers, so in preStart getfacl shows the named groups as 4294967295 and setfacl can't write them back. The setup is now its own oneshot unit, bitcoind-group-read, with the same hardening but without PrivateUsers. bitcoind wants it and starts after it, so it runs before each start. The warning is in that unit's journal now.
  • bitcoind-group-read runs only in the bitcoindGroupRead scenario, in the disruptive phase. That scenario runs only the bitcoind tests and enables nothing that depends on bitcoind. In your case, with lnd on, lnd still hits its start limit, but no test runs on lnd there, so the scenario passes.
  • The rights go to the owning group of each entry, so the script now leaves an entry of another group alone and logs the warning. The documented revoke commands now use setfacl -P and run as the bitcoind user. -P only protects the last path component, so as root they followed a symlink that a group member put at blocks or regtest. As the bitcoind user, such a symlink can only lead them to that user's own files. The review gate found both.
  • The bind mount example has fsType = "none";. The fstab entry is /mnt/disk2/blocks /var/lib/bitcoind/blocks none bind 0 0.

Tests on a KVM host. bitcoindGroupRead on regtest and on mainnet, the btrfs noacl case, module-config, test-runner and the flake evaluation pass. The test now also kills bitcoind and checks that the setup runs again before systemd's automatic restart. The last edit changes only the option's description, so on this head I ran only module-config and the docs build. The scenario with lnd, default and regtest passed before the last fixes, which change only the setup, its docs and its test. The new test fails on db4618d, without the trimming of default ACL entries, and without the group check.

@ostermayer
ostermayer requested a review from viberaven November 22, 2026 11:50

@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.

The revision addresses all five findings from my previous review. The dedicated regtest VM passed with the existing ACL and symlink cases, a separate group-read VM with LND passed, and the upgrade example generated the expected /etc/fstab entry. I also verified the fallback on btrfs mounted with noacl and ran the module-config and test-runner checks.

One startup regression remains for nodes with an on-demand blocks mount. I reproduced it on first start and verified the mount dependency suggested inline.

Assisted-by: Codex (gpt-6.2-astra)

Comment thread modules/bitcoind.nix Resolved

@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.

Apply these two suggestions together to make the ACL helper wait for the blocks mount on mainnet and regtest. The path quoting follows the Core Lightning replication module.

Assisted-by: Codex (gpt-6.2-astra)

Comment thread modules/bitcoind.nix Resolved
Comment thread modules/bitcoind.nix Resolved
@sebsgent sebsgent mentioned this pull request Nov 23, 2026
4 tasks done
Bitcoin Core sets umask 077 at startup and has had no sysperms setting
since 25.0. So with dataDirReadableByGroup, every file that bitcoind
created while it ran had mode 0600, and the group lost access to new
blocks until the next restart. On regtest, the option did nothing,
because preStart looked for the blocks dir in the wrong place.

Set a default ACL on the blocks dir and the dirs in it. Below such a
dir, the ACL takes the place of the umask, so new files are readable by
the group. Create the dir first, so this also covers the files of the
first start, and create regtest/wallets with it like Core would. If a
step fails, for example on a filesystem without ACL support, log a
warning and start bitcoind anyway, so a node doesn't go down over read
access for another program.

The admin may have set ACLs there already, for example for a backup
group. chmod g+rX changes only the mask of such an ACL, so the owning
group can stay locked out while named entries gain rights, and
setfacl -m recalculates the mask from all entries. So read each ACL with
getfacl, add the group's rights with a short awk script, and apply the
result with setfacl --restore. Where the mask gains a right, the script
takes it from the named entries, so their effective rights stay the
same. The rights go to the owning group of each entry, so an entry of
another group is left alone with the warning. Without ACL support,
setfacl falls back to chmod for these minimal ACLs, as before.

bitcoind runs with PrivateUsers, which shows the users and groups that
an ACL names as unmapped ids, and setfacl can't write them back. So do
the setup in its own oneshot unit, bitcoind-group-read, with the same
hardening but without PrivateUsers. bitcoind wants it and starts after
it, so it runs before each start of bitcoind. It also waits for a
mount at blocks/index, at blocks or at a dir above them, so with a disk
that's mounted on demand, it sets the ACL on that disk and not on the
empty mount point.

The data dir is group-writable, so a group member can replace blocks
(or regtest on regtest) with a symlink to other files of the bitcoin
user, also between two commands. So enter each dir with cd, only after
checking that it's a dir of the bitcoin user, and then check that the
dir we're in is the one we checked. A rename after that can't move us
out of it. Change only the current dir and the names in it: setfacl -P
doesn't follow a symlink at a name, and for a path with a dir part it
would need openat2(), which systemd's RestrictSUIDSGID blocks. A
symlink at blocks or regtest stops the setup with a warning. Master's
chmod followed such a symlink on mainnet, so add a notice and an
upgrade note.

The VM test restarts bitcoind many times, which also stops the services
that depend on it. So run it only in its own scenario, with bitcoind
alone, and in the disruptive phase.

Remove the sysperms option and the UMask setting, which had no effect.

Fixes bitcoinix#55

Assisted-by: Claude Code (Claude Opus 5.5)
@ostermayer

Copy link
Copy Markdown
Collaborator Author

Thanks for catching this. It's fixed in e1191d0. Master didn't move, so there's no rebase.

  • I reproduced your case with a VM test first. A noauto bind mount at the blocks dir waits 5 seconds for a slow unit, and bitcoind has RequiresMountsFor for it. On ce87711 the setup finished before the mount, and the operator got Permission denied on the blocks dir, on mainnet and on regtest. With the fix the setup starts after the mount, and the operator can read every file. That's also true after a restart onto a disk with 0600 files and no ACL.
  • I took unitPath as you wrote it. For RequiresMountsFor I used ${networkDir}/blocks/index and not blocks. systemd adds the mount units of the path and of each dir above it, so this also covers a separate mount at blocks/index, where Core keeps the block index. A late mount there failed the same way, and it passes now. A late mount of the blocks dir, the network dir or the data dir is covered too.
  • The pinned systemd 262 unquotes the value and then expands specifiers, so the quoting holds. In the VM "/mnt/a b/%%n\\x\"y" gave the one path /mnt/a b/%n\x"y.
  • The upgrade note and the warning now say that the blocks dir itself must belong to the bitcoind group, not only the entries in it. The setup already skipped a dir of another group.

On this head bitcoindGroupRead on regtest and on mainnet, module-config, test-runner, test/shellcheck.sh and helper/check-release-notes.sh pass. The mount test isn't in the repo.

@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.

Approved at e1191d04. All six findings from my earlier reviews are addressed.

I tested first startup with an on-demand bind mount at blocks on mainnet and at regtest/blocks/index on regtest. In both VMs, systemd mounted the storage before starting bitcoind-group-read.service, and operator could read a pre-existing file immediately. Using the network-specific blocks/index path for RequiresMountsFor covers both layouts.

The dedicated VM passed its symlink and existing-ACL checks, including preservation of named groups’ effective permissions. A separate VM with LND and group read enabled also passed. I verified the documented bind mount generates the expected /etc/fstab entry, and the module-config and test-runner checks passed locally.

Assisted-by: Codex (gpt-6.2-astra)

@prusnak
prusnak merged commit d030c91 into bitcoinix:master Nov 23, 2026
31 checks passed
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 breaking Changes existing user config; needs a versioning.nix entry type: bug Behaviour is wrong vs. documented or intended type: feature New capability or service type: removal Module, package or option dropped (implies breaking)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bitcoind: dataDirReadableByGroup relies on sysperms, which Bitcoin Core ignores

5 participants