Skip to content

wallet, sqlite: Enable secure_delete by default - #36796

Open
achow101 wants to merge 6 commits into
bitcoin:masterfrom
achow101:raii-sqlite-secure-delete
Open

achow101 wants to merge 6 commits into
bitcoin:masterfrom
achow101:raii-sqlite-secure-delete

Conversation

@achow101

@achow101 achow101 commented Nov 7, 2026

Copy link
Copy Markdown
Member

Currently, when we make deletions or updates to the database that require records to be fully deleted and to not persist in free pages afterwards, we need to explicitly call WalletDatabase::Rewrite. This behavior was introduced in #635 to ensure that unencrypted private keys are erased from the database after encrypting a wallet, and initial encryption is the only consumer of this function.

However, initial encryption is not the only functionality that requires secure deletion. l0rinc's LLMs recently found changing the passphrase can leave the original that encryption key record in free pages. We should want to ensure that those records are also deleted, although I do not consider that the presence of such records in free space to actually be a problem. Note that this behavior appears to be present since wallet encryption was initially introduced in #352.

Fortunately, SQLite offers a secure_delete flag that can be enabled on each connection to a database. If set, all deletes or updates will ensure that any data that was deleted or overwritten actually are overwritten with 0's. It is one of the 2 methods in SQLite's documentation for ensuring that records do not persist in free pages, the other method being to call VACUUM after deletions or updates, which is what Rewrite does.

The downside is that secure_delete will increase CPU and disk I/O, which is why this was not set previously. However, it seems like several distros ship with secure_delete enabled by default: Debian, Ubuntu, Fedora, Arch, and NixOS all set SQLite's compile time option to default secure_delete to enabled, and these are just the ones I've looked at. This suggests to me that the performance penalty is not actually that bad. As I run Arch and often do not use depends builds, I had not noticed that secure_delete was always on and did not experience any particularly poor wallet performance that could be attributed to that behavior. In the grand scheme of things, erases and updates are fairly infrequent in the wallet.

This PR enables secure_delete when the database is opened, and also enables it by default in the sqlite depends package. There are also a couple tests to check that all records we want deleted actually are being deleted from the database. WalletDatabase::Rewrite is removed as it it is no longer used.

@DrahtBot

DrahtBot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK Zippo76, rkrux
Stale ACK jpk68, fametrano

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36257 (qa: assert_equal -> assert_true/assert_false by hodlinator)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/wallet/sqlite.cpp: Enbling → Enabling [misspelling]

2026-12-01 03:31:20

@DrahtBot

DrahtBot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/42328113449/job/128268068670
LLM reason (✨ experimental): CI failed because Python lint found six unnecessary semicolons in test/functional/wallet_encryption.py.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@achow101
achow101 force-pushed the raii-sqlite-secure-delete branch from 397e20a to 96352f5 Compare November 7, 2026 02:26
@DrahtBot DrahtBot removed the CI failed label Nov 7, 2026

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Concept ACK

For a wallet created at this head, no copy of the plaintext key stays in wallet.dat-journal after encryptwallet, while the wallet is loaded or after a kill -9.

I ran encryptwallet on a fresh descriptor wallet at 96352f5 and counted copies of its 32-byte xprv private key in its files. I linked bitcoind against Homebrew SQLite and against depends, both leaving secure_delete off by default, so only the pragma this PR adds can zero the pages. wallet.dat and the journal held 0 copies while loaded, after a kill -9, and after a restart and reload. The same run on the head of #36793, which still relies on VACUUM, left 6 copies in the journal, 1 after the restart.

The script, builds and output are in https://gist.github.com/fametrano/245e7d7aa2a4762d2ce7c102e097b92f/7cf068b0300062264dce40a1838c98d4958504b0.

Comment thread depends/packages/sqlite.mk Resolved
@achow101
achow101 force-pushed the raii-sqlite-secure-delete branch from 96352f5 to 38c2839 Compare November 7, 2026 09:02
@jpk68

jpk68 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

ACK 38c2839

@DrahtBot
DrahtBot requested a review from fametrano November 8, 2026 00:10

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tACK 38c2839

With the secure_delete pragma removed from SQLiteDatabase::Open, the depends default alone now keeps the key out of wallet.dat and its journal. Linked against the depends SQLite built at 38c2839, wallet.dat and its journal held no copy of the plaintext key after encryptwallet, after a kill -9, or after the restart. Linked against the one built at 96352f5, wallet.dat held 6 copies, and the journal 14 after encryption, 14 after the kill and 5 after the restart.

The PR as pushed left no copy with Homebrew SQLite or with either depends build. wallet_encryption.py and the wallet unit tests pass at this head. As before, this covers a freshly created wallet.

The builds, script and output are in https://gist.github.com/fametrano/ef1fb32f05c8163272c8be27aeb32cf2/c4fe15cebece1cc35956f636de8648a74ce8940c.

@Zippo76 Zippo76 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Concept ACK

Tested 38c28393cb against master a527a0077a on macOS 27.2.1 arm64.

With macOS system SQLite (secure_delete=2, FAST), master and the PR produced the same results, and the PR's wallet_encryption.py passed on both. With Homebrew SQLite 3.54.1 (secure_delete=0 by default), the test failed on master at line 118 because the old mkey was still present, and passed with the PR.

The Homebrew build confirmed both that the PR prevents new residues and that pre-upgrade residues can persist, as noted in the Ralph review:

Scenario Result
Fresh wallet: old mkey after walletpassphrasechange 1 copy on master, 0 with the PR
Wallet created on master, then encrypted with the PR 5 plaintext private-key copies remain in wallet.dat; an equivalent wallet encrypted on master has 0
Old mkey residue left by a passphrase change on master Both copies remain after opening with the PR, after another passphrase change with the PR, and in a backup made with the PR

Enabling secure_delete prevents new residues in these tests, but does not remove existing ones. Removing the encryption-time VACUUM therefore leaves plaintext private-key copies in an existing wallet that master would have removed when encrypting it.

Comment thread src/wallet/sqlite.cpp
Comment on lines +311 to +312
// Enable secure_delete
SetPragma(m_db, "secure_delete", "ON", "Failed to enable secure_delete");

@furszy furszy Nov 14, 2026 •

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.

As encryption only happens on specific paths, what about enabling secure deletion only for the DB connection that will erase the keys? Otherwise all wallet workflows pay the option overhead for no reason. As we never delete other records during any common path.

We currently keep a single DB connection per wallet for its entire lifetime, but as we lock cs_wallet during the entire operation, we can create a new connection only for this use case, with the secure deletion option enabled, then close it and resume/recreate the lifetime one.

@achow101 achow101 Nov 14, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My original plan was to do something like that, to enable and disable secure delete as needed. But as I was testing that, I realized that multiple distros ship with sqlite compiled with secure delete enabled by default, so I figured the performance cost of enabling secure delete always probably isn't actually that bad. Enabling it always makes this much easier to reason about as well.

@rkrux

rkrux commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@achow101
achow101 force-pushed the raii-sqlite-secure-delete branch from b565c5a to d95378e Compare November 25, 2026 18:45

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

While this solves the encryption key leak during passphrase change for new wallets, it introduces problems for the old ones, as the removal of Rewrite leaves private keys dangling in existing wallets after encryption.

Can cherry-pick the following test to reproduce the behavior here (see test_encrypt_wallet): https://github.com/furszy/bitcoin-core/blob/2026_wallet_where_is_my_key/test/functional/wallet_encryption_compatibility.py

Essentially, previous releases don't have secure_delete enabled, so when a page fills up, SQLite moves its records to a new page without erasing them from the old one. Creating a wallet always fills its first page, so that page keeps old copies of the first records. These leftovers are no longer part of any record, so SQLite doesn't track them, and secure_delete never sees them (it only zeroes data being touched at the moment it's deleted). So when such a prev-release wallet is encrypted with this PR, the key records get replaced and zeroed, but the old raw copies stay.

So, while I also agree on having secure_delete enabled by default, it doesn't let us drop the VACUUM cleanup. Just pushed a branch with everything, including a few more tests too: 2026_wallet_where_is_my_key. All yours if you like it.

To further avoid having additional data float around in the wallet file,
we can enable auto vacuum. Doing so requires vacuuming the database once
when enabling the option, and applies the option to all databases even
when loaded in old software. Databases will be incrementally vacuumed on
close to reduce disk usage.
Compatibility test ensuring that encryptwallet or walletpassphrasechange
leave no copy of unencrypted keys or the old mkey record.
@achow101

achow101 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Cherry picked the test.

As long as we vacuum old databases prior to encrypting them, we can still drop Rewrite. I was looking at enabling auto_vacuum for disk space saving, and if we were to enable it, we would need to perform a VACUUM, so I've added a commit that does that. This both enables additional disk space saving, and automatic vacuum for old databases (without running vacuum every time) which allows us to drop the vacuum after encrypting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants