wallet, sqlite: Enable secure_delete by default

bitcoin/bitcoin: PR #36796

1 major finding · 2 suggestions

Findings

Link

[major] · Removing VACUUM leaves pre-existing plaintext remnants outside encryption cleanup

For wallets containing plaintext freed before secure_delete was enabled, removing VACUUM can leave keys recoverable after encryption; the recorded experiment used synthetic remnants, not an actual migrated wallet.

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

Details and suggested fix

Earlier migration code commits all legacy records to SQLite, then erases legacy key records. When migration ran with secure_delete off, those deletions could leave plaintext in free space. Enabling the pragma later does not provide the removed retrospective cleanup: the recorded head experiment inserted and deleted markers with secure_delete=OFF, then loaded and encrypted the wallet through encryptwallet with -deprecatedrpc=encryptwallet. After encryption, 46 of 50 markers remained in the file. Whether a particular migrated wallet retains private-key bytes depends on how much free space subsequent writes reused.

Keep the one-time VACUUM at the end of EncryptWallet alongside secure_delete. The former rebuilds the database, whereas the latter protects subsequent deletions and updates. Add a regression fixture that creates remnants with the pragma explicitly off, verifies their presence before encryption, and checks their absence afterward. The new functional test creates its wallet with secure_delete already enabled, so it does not exercise this upgrade case.


Optional improvements

Link

[suggestion] · Missing Python sqlite3 now skips unrelated encryption coverage

The new skip_if_no_py_sqlite3() call skips all encryption checks when Python lacks sqlite3, although only the new mkey lookup and its dependent assertion need that module.

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

Details and suggested fix

skip_if_no_py_sqlite3() raises SkipTest before test setup, so this also skips the existing encryption, locking, timeout, and passphrase-change RPC checks. The raw-file private-key scans only use open() and do not need the module either. Guard the sqlite3.connect() block and the later mkey_data assertion based on import availability, logging that those checks were skipped, while allowing the remaining encryption coverage to run.


Link

[suggestion] · Compiled defaults mask missing runtime secure_delete coverage

With SQLITE_SECURE_DELETE enabled in depends builds, the new functional scans would still pass if the connection-level pragma were removed, leaving the runtime override untested.

Location: src/wallet/sqlite.cpp:312 (head).

Details and suggested fix

The runtime pragma matters for system SQLite builds that default secure_delete to off, but the depends default hides that requirement from the functional scans. Add a file-backed unit test that executes PRAGMA secure_delete=OFF through SQLiteDatabase::m_db, calls Open() again, and asserts that querying PRAGMA secure_delete returns 1. Both members are public, and Open() reapplies the pragmas to an existing connection. This distinguishes a missing runtime override regardless of the compiled default; keep the functional scans as end-to-end checks.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

The original encryption fix addressed residual plaintext, not just the logical replacement of live private-key records. PR #635 stated that unencrypted private keys could remain in wallet.dat and other Berkeley DB files after encryption, and prescribed a complete wallet.dat rewrite.

The historical objective was to prevent recovery of plaintext remnants after encryption. That objective remains relevant when removing Rewrite, although the original source does not establish SQLite-specific free-page semantics or show that connection-level secure_delete is insufficient.

Source 1

The original fix treated auxiliary-file cleanup and cached plaintext as additional problems: it cleaned temporary database files at shutdown and shut down after successful encryption so cached unencrypted keys would not be written into the rewritten wallet.

Historically, the rewrite was one part of a broader sanitization procedure, not a documented guarantee that rewriting alone erased every artifact. This supports examining auxiliary artifacts separately, but does not establish that SQLite VACUUM sanitizes journals in a way secure_delete does not.

Source 1

Ralph's inferences

The pinned change replaces a post-encryption whole-database operation with an opening-time deletion policy. At the base, EncryptWallet calls Rewrite after encryption and wallet generation, and SQLite implements Rewrite as VACUUM. At the head, Open enables secure_delete and EncryptWallet no longer performs that final operation.

This distinguishes the historical retrospective cleanup step from a policy applied to subsequent database operations. Whether remnants left by earlier writes receive equivalent treatment is therefore material to the assignment, but cannot be resolved from these call sites alone.

The inspected history supports retaining the residual-plaintext threat model, but not a definitive claim of stronger SQLite journal sanitization by Rewrite/VACUUM. The original auxiliary-file and cache measures were explicitly Berkeley DB-era measures.

The bounded answer is that the original requirement targeted plaintext remnants beyond live records, while its exact application to pre-existing SQLite free pages and journal artifacts remains unverified. Historical intent alone does not establish either equivalence or non-equivalence when removing Rewrite.

Source 1