Skip to content

key-wallet: decoding AddressPool skips the gap_limit clamp and insert checks #1443

In progress

Description

@Claudius-Maginificent

TL;DR: An address pool loaded from saved bytes skips the checks that a newly created pool gets. Bad saved data can then crash the wallet or exhaust memory when it tops up its addresses.

Priority: low. No known consumer decodes AddressPool today: Dash Platform builds its pools through ManagedWalletInfo::from_wallet only. The defect sits in public API, though.

User story

As a developer persisting key-wallet state, I want a decoded address pool to be either valid or refused, so that a corrupt or hostile store cannot panic or exhaust memory in my wallet.

Scenario

Base flow

Serialize an AddressPool (feature serde or bincode), store it, decode it later, then let the wallet maintain its gap limit.

Actual behavior

Decoding accepts any field values, for example a gap limit of 0 or 4 billion, gaps in the address indices, or hardened indices. The next gap-limit top-up then panics in debug builds, or in release builds tries to derive about 2^31 addresses.

Expected behavior

Decoding enforces the same invariants as constructing and inserting do, and returns an error for data that breaks them.

Detailed discussion

At 399f47f9, key-wallet/src/managed_account/address_pool.rs:

  • The invariants are enforced only on construction and insertion. new_without_generation and set_gap_limit clamp gap_limit to 1..=MAX_GAP_LIMIT (:348, :764). insert refuses hardened indices and gaps (:526–544).
  • AddressPool derives Serialize/Deserialize and Encode/Decode field by field (:299–317), so decoding sets gap_limit, addresses and the reverse lookup maps (address_index, script_pubkey_index) without any check. Nothing ties the maps to addresses either.
  • maintain_gap_limit (:775–780) uses unchecked arithmetic:
    let target = match self.highest_used() {
        None => self.gap_limit - 1,
        Some(highest) => highest + self.gap_limit,
    };
    self.generate_up_to(target, key_source)
    With gap_limit == 0 this underflows: a panic in debug, u32::MAX in release, so generate_up_to derives until memory runs out. A huge decoded gap_limit has the same effect.

Suggested fix

  1. Validating decode (roughly 120 lines including tests): decode into a private raw struct (#[serde(try_from = "RawAddressPool")] and a manual bincode Decode), then rebuild through new_without_generation and insert for each address in index order. This reuses the existing checks and rebuilds the reverse maps. Data that would not have passed those checks is then refused.
  2. Optional extra safeguard (roughly 25 lines): checked_sub/checked_add in maintain_gap_limit, returning an error instead of wrapping.

Found while reviewing dashpay/platform#5981, which removes a Platform-side guard that this decode path made reachable only through Platform's public API.

🤖 Co-authored by Claudius the Magnificent AI Agent

Activity

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