Skip to content

Review finding: re-importing a descriptor resets the PQC counter of a key that failed validation #279

Open

Description

@kiwidream

Summary

If a plaintext PQC key record fails background validation, re-importing its descriptor rewrites the record with signature counter 0. After the next load the key passes validation and signs from 0.

Observed

On regtest at 1.x.x e374ba24cf, unencrypted P2MR wallet:

  • The key has signed 3 times (counter 3). Its record is then edited, with the wallet unloaded, so that it still parses and matches its hash and pubkey tail but fails slh_dsa_secret_key_validate.
  • After reload, validation reports failed and signing is refused with "Error: plaintext PQC wallet key validation failed. Wallet signing is disabled until the wallet is restored or repaired."
  • importdescriptors with the same xprv succeeds with no warning. In memory the counter is still 3 and the key stays blocked, but on disk the record now holds the repaired secret and counter 0.
  • After unloadwallet/loadwallet, validation is complete, the counter is 0, and the next signature takes it to 1.

Cause: AddDescriptorPQCKeyWithDB treats a key as already present only if it is in m_map_pqc_keys, m_map_crypted_pqc_keys or m_pending_plaintext_pqc_keys (src/wallet/scriptpubkeyman.cpp:1788). A key that failed validation is moved to m_failed_plaintext_pqc_keys (:3038), so it counts as absent. Re-import goes through UpdateWalletDescriptor, which sets m_max_cached_index = -1 (:3171); the top-up then re-derives every index, and the key is written with sig_counter=0 (:1814) over the existing record (src/wallet/walletdb.cpp:259).

Impact

Low. That key's counter goes back to 0, so the 2^30 cap and the usage reporting undercount for it. It needs a record that fails validation (corruption, hand edits or an older buggy writer) and then the user following the repair hint. There is no remote trigger, and reaching 2^30 at about 210 ms per signature takes years of continuous signing. v1.0.0 has the same code. v0.1.1 and v0.1.2 have the same check (read, not run). v0.1.0 has no background validation.

Suggested fix

Treat m_failed_plaintext_pqc_keys as present in AddDescriptorPQCKeyWithDB. If the re-derived secret is meant to repair the record, rewrite it with the counter from m_map_pqc_sig_counters and clear the failed state. As defence in depth, no key-record writer should write a literal 0 over an existing record: read the stored counter in the same transaction and write the maximum. Regression test: fail a key, re-import, reload, and check that the counter is unchanged.

Evidence

src/wallet/scriptpubkeyman.cpp:1783-1820,3014-3040,3161-3176; src/wallet/wallet.cpp:5146-5206; src/wallet/walletdb.cpp:244-273,1054; src/wallet/rpc/spend.cpp:72.

Activity

  1. added 6 commits that reference this issue on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions