Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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:
2026-12-01 03:31:20 |
921dbd0 to
397e20a
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
397e20a to
96352f5
Compare
fametrano
left a comment
There was a problem hiding this comment.
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.
96352f5 to
38c2839
Compare
|
ACK 38c2839 |
fametrano
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
| // Enable secure_delete | ||
| SetPragma(m_db, "secure_delete", "ON", "Failed to enable secure_delete"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
38c2839 to
b565c5a
Compare
|
Concept ACK |
b565c5a to
d95378e
Compare
There was a problem hiding this comment.
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.
|
Cherry picked the test. As long as we vacuum old databases prior to encrypting them, we can still drop |

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
VACUUMafter deletions or updates, which is whatRewritedoes.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::Rewriteis removed as it it is no longer used.