Skip to content

wallet: bugfix, invalid crypted key "checksum_valid" set - #26532

Merged
achow101 merged 4 commits into
bitcoin:masterfrom
furszy:2022_wallet_fix_ckeys_checksum
Nov 29, 2022
Merged

achow101 merged 4 commits into
bitcoin:masterfrom
furszy:2022_wallet_fix_ckeys_checksum

Conversation

@furszy

@furszy furszy commented Nov 18, 2022 •

Copy link
Copy Markdown
Member

At wallet load time, the crypted key "checksum_valid" variable is always set to false. Which, on every wallet decryption call, forces the process to re-write all the ckeys to db when it's not needed.

Note:
The first commit fixes the issue, the two commits in the middle are cleanups so DuplicateMockDatabase
can be used without duplicating code. And, the last one is pure test coverage for the crypted keys loading
process.

Includes test coverage for the following scenarios:

  1. "All ckeys checksums valid" test:
    Loads an encrypted wallet with all the crypted keys with a valid checksum and
    verifies that 'CWallet::Unlock' doesn't force an entire crypted keys re-write.

    (we force a complete ckeys re-write if we find any missing crypted key checksum
    during the wallet loading process)

  2. "Missing checksum in one ckey" test:
    Verifies that loading up a wallet with, at least one, 'ckey' with no checksum
    triggers a complete re-write of the crypted keys.

  3. "Invalid ckey checksum error" test:
    Verifies that loading up a ckey with an invalid checksum stops the wallet loading
    process with a corruption error.

  4. "Invalid ckey pubkey error" test:
    Verifies that loading up a ckey with an invalid pubkey stops the wallet loading
    process with a corruption error.

At wallet load time, we set the crypted key "checksum_valid" variable always to false.
Which, on every wallet decryption call, forces the process to re-write the entire ckeys to db when
it's not needed.
@DrahtBot

DrahtBot commented Nov 18, 2022 •

Copy link
Copy Markdown
Contributor

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

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK aureleoules, achow101
Stale ACK luke-jr

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26153 (Reduce wasted pseudorandom bytes in ChaCha20 + various improvements by sipa)
  • #25909 (wallet: coverage for receiving txes with same id but different witness data by furszy)
  • #25766 (wallet: Include a signature with encrypted keys to mitigate a wallet scam by achow101)
  • #25361 (BIP324: Cipher suite by dhruv)
  • #25325 (Add pool based memory resource by martinus)
  • #24914 (wallet: Load database records in a particular order by achow101)
  • #23561 (BIP324: Handshake prerequisites by dhruv)
  • #23233 (BIP324: Add encrypted p2p transport {de}serializer by dhruv)

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.

@achow101

Copy link
Copy Markdown
Member

Good catch! Do you think it's possible to have a test for this?

@furszy

furszy commented Nov 19, 2022 •

Copy link
Copy Markdown
Member Author

Good catch! Do you think it's possible to have a test for this?

Yep :), added test coverage for it and more inside 4db80c90.

Covered the following scenarios

  1. "All ckeys checksums valid" test:
    Loads an encrypted wallet with all the crypted keys with a valid checksum and
    verifies that 'CWallet::Unlock' doesn't force an entire crypted keys re-write.

    (we force a complete ckeys re-write if we find any missing crypted key checksum
    during the wallet loading process)

  2. "Missing checksum in one ckey" test:
    Verifies that loading up a wallet with, at least one, 'ckey' with no checksum
    triggers a complete re-write of the crypted keys.

  3. "Invalid ckey checksum error" test:
    Verifies that loading up a ckey with an invalid checksum stops the wallet loading
    process with a corruption error.

  4. "Invalid ckey pubkey error" test:
    Verifies that loading up a ckey with an invalid pubkey stops the wallet loading
    process with a corruption error.

Extra note:
The two commits in the middle are cleanups so I can use the DuplicateMockDatabase
function without duplicating code.

Going to update the PR description with this too.

@achow101 achow101 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 4db80c90b02b70a71a3ac1d614d779d222561525

Comment thread src/Makefile.test_util.include Resolved
Comment thread src/wallet/test/util.cpp Resolved
@furszy
furszy force-pushed the 2022_wallet_fix_ckeys_checksum branch from 4db80c9 to bb71809 Compare November 21, 2022 20:29
files share the same purpose, and we shouldn't have wallet code
inside the test directory.

This later is needed to use wallet util functions in the bench
and test binaries without be forced to duplicate them.
Adds test coverage for the wallet's crypted key loading from db process.
The following scenarios are covered:

1) "All ckeys checksums valid" test:
   Loads an encrypted wallet with all the crypted keys with a valid checksum and
   verifies that 'CWallet::Unlock' doesn't force an entire crypted keys re-write.

   (we force a complete ckeys re-write if we find any missing crypted key checksum
    during the wallet loading process)

2) "Missing checksum in one ckey" test:
   Verifies that loading up a wallet with, at least one, 'ckey' with no checksum
   triggers a complete re-write of the crypted keys.

3) "Invalid ckey checksum error" test:
   Verifies that loading up a ckey with an invalid checksum stops the wallet loading
   process with a corruption error.

4) "Invalid ckey pubkey error" test:
   Verifies that loading up a ckey with an invalid pubkey stops the wallet loading
   process with a corruption error.
@furszy
furszy force-pushed the 2022_wallet_fix_ckeys_checksum branch from bb71809 to 13d9760 Compare November 21, 2022 20:30
@furszy

furszy commented Nov 21, 2022

Copy link
Copy Markdown
Member Author

Updated per feedback, thanks achow101.

Small Diff:

  1. Added ENABLE_WALLET guard for the wallet/test/util.h and util.cpp files at the test_util makefile level.
  2. Moved ADDRESS_BCRT1_UNSPENDABLE from wallet/test/util.h to wallet_balance.cpp (it's only used there).

@aureleoules aureleoules left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 13d9760

Cherry-picked the last three commits on master and verified the tests do not pass. I also verified that the tests correctly test the behavior.

Comment thread src/wallet/test/walletload_tests.cpp Resolved
Comment thread src/wallet/test/walletload_tests.cpp Resolved
@achow101

Copy link
Copy Markdown
Member

ACK 13d9760

@fanquake
fanquake requested a review from Sjors November 23, 2022 22:00

@luke-jr luke-jr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

utACK cc5a5e8 by itself (did not review refactors or new test)

@achow101
achow101 merged commit 5690848 into bitcoin:master Nov 29, 2022
@furszy
furszy deleted the 2022_wallet_fix_ckeys_checksum branch May 27, 2023 01:47
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Dec 10, 2023
…cksum_valid" set

backports cc5a5e8 only

```
wallet: bugfix, invalid crypted key "checksum_valid" set

At wallet load time, we set the crypted key "checksum_valid" variable always to false.
Which, on every wallet decryption call, forces the process to re-write the entire ckeys to db when
it's not needed.
```
PastaPastaPasta added a commit to dashpay/dash that referenced this pull request Dec 11, 2023
backport: partial merge bitcoin#26532: wallet: bugfix, invalid crypted key "checksum_valid" set
@bitcoin bitcoin locked and limited conversation to collaborators May 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants