{"id":"XE-0135","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"226","source_url":"https://github.com/xeprotocol/xe/issues/226","reporter_id":43,"title":"[Track 1 Bug Bounty] Multi-Step Cascade Unwind Stale Store Reads, Missing SwapBlock Credential Verification, Renewed Lease Reputation Drop, and BadgerStore Pending/CAS Desync","area":"consensus","claimed_tier":null,"assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":true,"internal_ref":"","internal_url":null,"status":"accepted","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-10-31T01:10:30.767588+00:00","updated_at":"2026-11-14T20:59:30.458174+00:00","source_created_at":"2026-10-31T01:08:25+00:00","told_at":"2026-11-14T20:58:12+00:00","status_changed_at":"2026-11-14T20:42:22.835074+00:00","url":"https://bounty.xe.network/XE-0135","body":"## Summary\n\nThis report details **5 distinct, non-overlapping consensus, ledger rollback, and storage engine vulnerabilities** in `core/` and `store/` (`master` branch), accompanied by deterministic unit tests and a complete surgical fix PR:\n\n1. **[Critical / Severe] Multi-Step Cascade Unwind Reads Stale Pre-Cascade Store State Instead of `cascadeBuild` Overlay (`core/rollback.go:87-144, 163-184`, `core/ledger.go:2976-2998`)**\n2. **[Severe] `SwapBlock` Bypasses `verifyBlockCredential` on Replacement Winner Blocks & Reads Stale Keyset/AccountKey Caches During Cascade Validation (`core/ledger.go:1426, 1447, 1456, 2876, 3004`, `core/cascade_commit.go:21, 66-68`)**\n3. **[High] Renewed Lease Duration Dropped in Reputation Settlement/Undo & Fallback Timestamp Corruption on Rebuild (`core/ledger.go:657, 688, 695, 700, 708, 2244`, `core/rollback.go:262`)**\n4. **[High] `BadgerStore.CommitCascade` Omission of Pre-Undo Lease CAS (`ExpectLeaseHash` / `ExpectLeaseState` / `ExpectLeaseEffectiveDurationSecs`) & Unbounded Duplicate `prefixPendingDst` Append (`store/badger_store.go:778, 1063, 1110-1127`)**\n5. **[Medium] In-Memory Map Poisoning Across `rebuildKeysets`, `rebuildAccountKeys`, and `rebuildDelegation` (`core/ledger.go:1402, 1489, 2572`)**\n\n---\n\n## Vulnerability 1: Multi-Step Cascade Unwind Reads Stale Pre-Cascade Store State Instead of `cascadeBuild` Overlay\n\n### Root Cause\nDuring conflict promotion (`SwapBlock` -> `swapBlockLocked` at `core/ledger.go:2976-2998`), `buildBlockUndo` is invoked sequentially for each step in the cascade plan before `activateCascade(cascade)` is called, and `getAssetBalanceOverlay(account, \"XUSD\")` calls `l.xusdBalanceOnChain(ch)` during winner validation.\n\nHowever, four helper functions in `core/rollback.go` read directly from `l.store.(LeaseStore).GetLease(leaseHash)` instead of consulting the in-progress `cascadeBuild` lease overlay (`cb.restoreLease` / `cb.deleteLease`):\n- `priorLease` (`core/rollback.go:87-107`)\n- `escrowPendingOf` (`core/rollback.go:109-130`)\n- `priorLeasePopRenewal` (`core/rollback.go:132-144`)\n- `xusdBalanceOnChain` (`core/rollback.go:163-184`)\n\n### Impact\n1. **Multi-step lease unwind corruption**: When a cascade unwinds multiple lease-related blocks (e.g., unwinding `BlockLeaseSettle` followed by `BlockLeaseRenew` and/or `BlockLeaseAccept`), `priorLease`, `priorLeasePopRenewal`, and `escrowPendingOf` read the pre-cascade `Lease` from `l.store` rather than the intermediate `RestoreLease` already recorded in `cascade.restoreLease`. Consequently, popped renewals re-appear in subsequent `RestoreLease` / `AddPending` records, or `Stake` / `State` reverts to stale values.\n2. **XUSD balance miscalculation during cascade**: If `BlockLeaseAccept` (or `BlockLease`) was unwound in `cascade.restoreLease` (setting `Stake = 0` or deleting the lease), `xusdBalanceOnChain` still queries `ls.GetLease(b.Source)` from the underlying store and adds the stale `lease.Stake`, corrupting `parentBal` and `getAssetBalanceOverlay(account, \"XUSD\")`.\n\n---\n\n## Vulnerability 2: `SwapBlock` Bypasses `verifyBlockCredential` on Replacement Winner Blocks & Reads Stale Keyset/AccountKey Caches During Cascade Validation\n\n### Root Cause\n1. In `SwapBlock` (`core/ledger.go:2876-2885`) and `swapBlockLocked` (`core/ledger.go:3004`), the replacement block `newBlock` is checked only with `VerifyBlock(newBlock)` (`core/crypto.go:145-167`) before calling `dispatchValidateAndAdd(newBlock, true)`.\n   - Crucially, `VerifyBlock(b)` **returns `nil` without verifying any signature whenever `b.Previous != \"0\"` or `len(b.Signatures) > 0`** (`core/crypto.go:158-164`), deferring signature and multisig verification to `l.verifyBlockCredential(b)`.\n   - Because `dispatchValidateAndAdd` does not call `verifyBlockCredential(b)`, any direct call to `SwapBlock` with a non-open replacement block or multisig block skips signature verification entirely!\n2. Even when `verifyBlockCredential(b)` is called (`core/ledger.go:1416-1465`), it reads `l.GetKeyset(b.Account)` (`lines 1426, 1447`), `l.GetAccountKey(b.Account)` (`line 1456`), and `l.store.GetAccountChain(account)` in `resolveMissingKeyset` (`line 1468`) instead of `l.getKeysetOverlay(b.Account)`, `l.getAccountKeyOverlay(b.Account)`, and `l.getAccountChainOverlay(account)`.\n3. Furthermore, `cascadeBuild` (`core/cascade_commit.go:21, 66-68`) tracks `deleteKeyset` (`undo.DeleteKeysetAccount`) but omits `deleteAccountKey` (`undo.DeleteAccountKeyAccount`). If an account's open block is unwound during a cascade and replaced by a new open block or multisig open block, stale `accountKeys` or `keysets` remain visible during cascade validation.\n\n### Impact\n- Replacement blocks in `SwapBlock` bypass `verifyBlockCredential` unless validated externally beforehand, and cascade validation of replacement blocks on accounts whose open/keyset block was unwound fails or validates against the unwound loser's public key / keyset.\n\n---\n\n## Vulnerability 3: Renewed Lease Duration Dropped in Reputation Settlement/Undo & Fallback Timestamp Corruption on Rebuild\n\n### Root Cause\n1. When a lease with one or more `BlockLeaseRenew` segments is settled or rebuilt:\n   - `validateAndAddLeaseSettle` (`core/ledger.go:2239-2245`) passes `DurationSecs: lease.Duration` instead of `lease.EffectiveDurationSecs()`.\n   - `rebuildReputation` (`core/ledger.go:652-658`) passes `DurationSecs: lease.Duration` instead of `lease.EffectiveDurationSecs()`.\n   - `buildBlockUndo` (`core/rollback.go:262`) reverts `DurationSecs: restoreLease.Duration` instead of `restoreLease.EffectiveDurationSecs()`.\n2. In `lookupLeaseSettleTimestamp` (`core/ledger.go:688, 695`) and `lookupLeaseForceSettleTimestamp` (`core/ledger.go:701, 708`), the fallback timestamp is computed as `lease.StartTime + int64(lease.Duration)*1e9` instead of `lease.EffectiveExpiry()`, ignoring all renewal durations.\n\n### Impact\n- Every renewed lease permanently loses 100% of its renewal duration seconds in `LeaseHoursSettledAsProvider` and `LeaseHoursSettledAsConsumer` across live settlement, undo, and startup reputation rebuilds, and fallback timestamps on renewed leases regress prior to actual expiry.\n\n---\n\n## Vulnerability 4: `BadgerStore.CommitCascade` Omission of Pre-Undo Lease CAS & Unbounded Duplicate `prefixPendingDst` Append\n\n### Root Cause\n1. Compare `MemStore.CommitCascade` (`store/mem_store.go:467-513`) with `BadgerStore.CommitCascade` (`store/badger_store.go:1110-1127`):\n   - `MemStore.CommitCascade` evaluates the post-undo lease state before mutating state and enforces the winner commit's `ExpectLeaseHash`, `ExpectLeaseState`, and `ExpectLeaseEffectiveDurationSecs` CAS invariants.\n   - `BadgerStore.CommitCascade` completely omitted this pre-undo check and relied on `applyCommitTxn` running *after* `applyUndoTxn`, or failed to account for lease mutations cleanly before applying undo steps.\n2. In `applyCommitTxn` (`store/badger_store.go:778`) and `applyUndoTxn` (`store/badger_store.go:1063`), adding a `PendingSend` unconditionally appends `SendHash` to `destHashes` in `prefixPendingDst` without checking if `SendHash` is already present.\n   - When an undo re-adds a `PendingSend` whose hash was still present in `prefixPendingDst`, `GetPendingByDest` returns duplicate `PendingSend` entries for the same `SendHash` (diverging from `MemStore`, which keys `pending` by `SendHash` in a map).\n\n### Impact\n- Storage backend divergence between `MemStore` and `BadgerStore` during cascade commits and duplicate pending-send entries returned by `BadgerStore.GetPendingByDest`.\n\n---\n\n## Vulnerability 5: In-Memory Map Poisoning Across `rebuildKeysets`, `rebuildAccountKeys`, and `rebuildDelegation`\n\n### Root Cause\n- `rebuildKeysets()` (`core/ledger.go:1489`), `rebuildAccountKeys()` (`core/ledger.go:1402`), and `rebuildDelegation()` (`core/ledger.go:2572`) iterate over the store and populate `l.keysets`, `l.accountKeys`, `l.delegation`, and `l.weights` **without clearing existing entries first** (unlike `rebuildReputation()`, which calls `l.reputation.Reset()`).\n- In addition, `commitBlockWrite` (`core/ledger.go:2292-2346`) did not update `l.setKeyset(c.Account, c.PutKeyset)` inside `commitBlockWrite` itself (relying on callers after `commitBlockWrite`), whereas `PutAccountKey` was applied inside `commitBlockWrite`.\n\n---\n\n## Proof of Concept / Reproduction Tests\n\nAdded in `core/cascade_reputation_test.go` and `store/badger_cascade_test.go`:\n```bash\ngo test -v ./core -run \"TestCascadeOverlayStaleStoreReadsInUndoAndXUSDBalance|TestRenewedLeaseReputationDurationAndFallbackTimestamps|TestSwapBlockCredentialVerificationAndOverlayKeyset\"\ngo test -v ./store -run \"TestBadgerStoreCommitCascadeEnforcesLeaseCASAndDeduplicatesPendingByDest\"\n```\n\n---\n\n## Payout Address\n- **XE Wallet Address**: `23bd7ef9c17e79ee76e9f656d618cf0f7c2cda4a700b58d065917d14e97a304a`\n","patch_refs":[{"id":230,"repo":"xeprotocol/xe","kind":"pr","ref":"227","title":"fix(core,store): cascade overlay lease/credential reads, renewed lease reputation duration, and BadgerStore CAS/pending dedup (Fixes #226)","url":"https://github.com/xeprotocol/xe/pull/227","note":"","state":"open","author":"ket4min-susan","added_by":"desk","created_at":"2026-10-31T01:10:31.712551+00:00"},{"id":233,"repo":"xeprotocol/xe","kind":"pr","ref":"232","title":"fix(core,store): cascade overlay lease/credential reads, renewed lease reputation duration, and BadgerStore CAS/pending dedup (Fixes #226)","url":"https://github.com/xeprotocol/xe/pull/232","note":"","state":"open","author":"tipprb","added_by":"desk","created_at":"2026-10-31T10:33:10.650310+00:00"}],"thread":[{"id":90,"source":"github","source_ref":"6536969976","author":"ket4min-susan","team":false,"body":"Submitted complete fix and regression unit tests in PR #227: https://github.com/xeprotocol/xe/pull/227\n\n**XE Payout Wallet**: `23bd7ef9c17e79ee76e9f656d618cf0f7c2cda4a700b58d065917d14e97a304a`","created_at":"2026-10-31T01:09:40+00:00"},{"id":1164,"source":"github","source_ref":"6726596922","author":"Joseph Denne","team":true,"body":"Thank you for the report and the accompanying pull request. We have reviewed the findings; part of what you describe is being handled and part did not reproduce against the current tree. We will update this ticket as the work progresses.","created_at":"2026-11-08T20:18:54+00:00"},{"id":3157,"source":"github","source_ref":"6884101277","author":"XE desk","team":true,"body":"Read and accepted. It is tracked internally and the fix will land in a public PR; this issue is\nupdated when it does, and the tier and award follow the fix. The desk entry is https://bounty.xe.network/XE-0135.","created_at":"2026-11-14T20:58:12+00:00"}]}