Review report

bitcoin/bitcoin: PR #36796

1 major finding · 1 suggestion

Findings

Link

[major] · Encryption no longer scrubs plaintext key residue from before the upgrade

Without the encryption-time VACUUM, plaintext keys deleted before secure_delete was enabled can survive in an encrypted wallet.dat and its subsequent backups.

Location: src/wallet/wallet.cpp:900 (base).

Details and suggested fix

Consider an unencrypted legacy wallet migrated by a pre-PR depends-built release, then encrypted with this build. Migration commits the legacy records to SQLite before deleting KEY records, including the HD seed used to derive the migrated descriptor keys. With secure_delete disabled, those deletions can leave plaintext bytes in free space. Enabling the pragma on the next Open does not retroactively scrub that space, and Encrypt only processes live descriptor keys. Surviving remnants can therefore remain readable to someone obtaining wallet.dat, and the page-copy backup preserves them too, independently of the migration's plaintext .legacy.bak. Survival depends on later page reuse; wallets created or migrated with this build do not have this historical-residue trigger. The removed Rewrite attempted a whole-file VACUUM, although its ignored return value made cleanup best effort. The recorded synthetic-residue experiment retained private-key bytes after encryption and backup at head but removed them at base; no real migrated wallet was examined, so production exposure remains unconfirmed. Keep the encryption-time VACUUM in addition to secure_delete. Add a regression fixture that deletes plaintext key bytes with secure_delete off while the wallet is unloaded, confirms the residue exists, then checks that encryptwallet removes it.


Optional improvements

Link

[suggestion] · Python sqlite3 becomes a dependency of the entire encryption test

When Python's optional sqlite3 module is missing, the new setup check skips all wallet_encryption.py coverage even though only the mkey lookup needs that module.

Location: test/functional/wallet_encryption.py:34 (head).

Details and suggested fix

The setup-level skip prevents the existing passphrase, timeout, locking and encryption-error checks from running, along with the new raw-file private-key scans. Only the query that captures mkey_data requires sqlite3. Remove the whole-test module requirement and use self.inspect_sqlite_db(wallet_db, fn) for that lookup, keeping the row-count assertion inside the callback. The helper logs a warning and returns None on ImportError. Guard the later rotated-mkey byte scan on a returned value so it still runs whenever SQLite inspection is available, without dropping the other coverage. No current CI job lacking the module was identified.


Coverage details