Skip to content

fix(datadir): enforce strict permissions on datadir and config.toml - #334

Merged
tvpeter merged 1 commit into
bitcoindevkit:masterfrom
tvpeter:fix/harden-datadir-perms
Sep 25, 2026
Merged

tvpeter merged 1 commit into
bitcoindevkit:masterfrom
tvpeter:fix/harden-datadir-perms

Conversation

@tvpeter

@tvpeter tvpeter commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Description

wallet config persisted descriptors including xprv/tprv to config.toml via fs::write, and created the datadir / wallet dirs with umask defaults. Any other local user or process that could read the home tree could recover the account-level key and sweep the wallet. The existing warning only mentioned plaintext storage, not world-readability.
This PR creates new datadirs and wallet/payjoin subdirs with DirBuilder mode 0700 on Unix (create_restricted_dir), writes config.toml with OpenOptions mode 0600. On load/save, it hardens an existing config.toml (and the default ~/.bdk-bitcoin on startup) when group/other bits are set, and prints a warning when it does so.
Also, adds unit and integration tests that a private-descriptor config is 0600 and that a stale 0644 file is repaired on load and save.

Behaviour is unchanged for windows. wallet.sqlite / payjoin DB files may still be created 0644 by the DB libraries, they sit under a 0700 parent.

Fixes #331

Notes to the reviewers

  • limit_access only acts when mode & 0o077 != 0, so an already owner-only file (0400) is not widened to 0600.
  • Payjoin’s create_dir_all was the same umask defaults and is included so a session dir is not left 0755 beside the wallet.

Changelog notice

  • Fixed the data directory and config.toml permission being world-readable (0755/0644) to 0700/0600 on Unix.

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.67442% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.06%. Comparing base (682e414) to head (b2b99b4).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/utils/common.rs 98.00% 2 Missing ⚠️
src/config.rs 96.42% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #334      +/-   ##
==========================================
+ Coverage   57.78%   59.06%   +1.27%     
==========================================
  Files          22       22              
  Lines        3733     3857     +124     
==========================================
+ Hits         2157     2278     +121     
- Misses       1576     1579       +3     
Flag Coverage Δ
rust 59.06% <97.67%> (+1.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@notmandatory notmandatory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also wondering why "Custom --datadir is intentionally not auto-hardened. Passing --datadir ~/.bdk-bitcoin is Some(_), so that path is also not repaired as default." Is there some reason I'm missing not to also harden a non-default datadir path?

Comment thread src/utils/common.rs Outdated
Comment thread CHANGELOG.md Outdated
@tvpeter

tvpeter commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator Author

Also wondering why "Custom --datadir is intentionally not auto-hardened. Passing --datadir ~/.bdk-bitcoin is Some(_), so that path is also not repaired as default." Is there some reason I'm missing not to also harden a non-default datadir path?

I thought that a custom directory might likely be existing before and may contain other contents, and hardening it might prevent other users from accessing it. But looking at it again, that is not a sound decision. So I will update it.

@tvpeter
tvpeter force-pushed the fix/harden-datadir-perms branch from 46a8889 to 0808206 Compare September 23, 2026 20:12
@tvpeter

tvpeter commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator Author

Also wondering why "Custom --datadir is intentionally not auto-hardened. Passing --datadir ~/.bdk-bitcoin is Some(_), so that path is also not repaired as default." Is there some reason I'm missing not to also harden a non-default datadir path?

The PR has now been updated and the exception for passed custom data directories has been removed. The other minor changes have also been effected.
Thank you.

@notmandatory notmandatory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 0808206

Thanks for the fixes and cleanup with the DIR_MODE and FILE_MODE consts.

@notmandatory notmandatory moved this to Ready to Review in BDK-CLI Sep 24, 2026
- Replaces `fs::create_dir_all` with `DirBuilder` enforcing 0700 on Unix.
- Replaces `fs::write` with `OpenOptions` enforcing 0600 on Unix
when writing `config.toml`.
- Adds permission hardening on startup for existing configurations.
- Add tests for permissions
@tvpeter
tvpeter force-pushed the fix/harden-datadir-perms branch from 0808206 to b2b99b4 Compare September 25, 2026 03:10
@tvpeter

tvpeter commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator Author

Force-pushed a rebase on master.

@tvpeter
tvpeter merged commit f3a1eaf into bitcoindevkit:master Sep 25, 2026
9 checks passed
@tvpeter tvpeter added this to the CLI 4.1.0 milestone Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Wallet config file is saved with default permissions

2 participants