Repository navigation
Description
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_generationandset_gap_limitclampgap_limitto1..=MAX_GAP_LIMIT(:348,:764).insertrefuses hardened indices and gaps (:526–544). AddressPoolderivesSerialize/DeserializeandEncode/Decodefield by field (:299–317), so decoding setsgap_limit,addressesand the reverse lookup maps (address_index,script_pubkey_index) without any check. Nothing ties the maps toaddresseseither.maintain_gap_limit(:775–780) uses unchecked arithmetic:Withlet target = match self.highest_used() { None => self.gap_limit - 1, Some(highest) => highest + self.gap_limit, }; self.generate_up_to(target, key_source)
gap_limit == 0this underflows: a panic in debug,u32::MAXin release, sogenerate_up_toderives until memory runs out. A huge decodedgap_limithas the same effect.
Suggested fix
- Validating decode (roughly 120 lines including tests): decode into a private raw struct (
#[serde(try_from = "RawAddressPool")]and a manual bincodeDecode), then rebuild throughnew_without_generationandinsertfor 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. - Optional extra safeguard (roughly 25 lines):
checked_sub/checked_addinmaintain_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