{"id":"XE-13250","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"381","source_url":"https://github.com/xeprotocol/xe/issues/381","reporter_id":1143,"title":"archiveExpiredLease blind overwrite races concurrent settle/renew: Settled lease relabelled Expired, renewal payment destroyed","area":"leasing-ledger","claimed_tier":"medium","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":true,"internal_ref":"#1126","internal_url":"https://github.com/xeprotocol/core/issues/1126","status":"closed","closed_reason":"duplicate","github_state":"closed","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T20:42:03.497654+00:00","updated_at":"2026-11-20T13:32:53.982335+00:00","source_created_at":"2026-11-11T12:20:14+00:00","told_at":"2026-11-20T00:04:52+00:00","status_changed_at":"2026-11-20T00:04:29.476759+00:00","url":"https://bounty.xe.network/XE-13250","body":"# archiveExpiredLease blind overwrite races concurrent settle/renew: Settled lease relabelled Expired, renewal payment destroyed\n\n**Severity: Medium** (XE tier ceiling 2,500 XE). **CVSS 3.1: 4.8\n(AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:L)** — see §7 for the scoring and the\nrenew-leg variant (I:H → 5.9, still Medium).\n\n**Target:** `github.com/xeprotocol/xe` @\n`89a542ea51d38375299aee54c6c99102e9bfa666` (\"release: testnet-0005 quorum\nwithhold fix\", 2026-11-10). Go 1.25.0.\n\n## 1. Impact (read this first)\n\nA lease that has been **settled** — escrow paid out, provider emission\nminted — is silently relabelled **Expired** by the node's own archive\nsweep, because the archive writes a stale snapshot copy with a blind\n`PutLease` and never re-reads the lease. The settled lease then disappears\nfrom settled queries, and the node logs a false \"burned N µXUSD escrow\"\nfor escrow that was already paid. The supply auditor breaks (XUSD not\nconserved, discrepancy = the escrow amount).\n\nThe renew leg is worse: a **renew that commits after the archive's\nsnapshot is overwritten the same way, and the renewal payment is\ndestroyed with no record** — a live lease is killed mid-term, its renewal\nescrow (31,000 µXUSD in the tested fixture) is deleted while the archive\nlog claims only the base escrow (62,000) burned, and the provider's\nbase+renewal settlement emission never mints. The overwritten lease is\nterminally dead: a second renew and a settle-shaped commit are both\nCAS-rejected afterwards.\n\nFund loss is bounded per lease (one renewal payment + one lease's\nemission); there is no double-spend and no unbounded drain. The trigger\nis timing-dependent and is not attacker-controlled — see §5 for the exact\nprerequisites and §7 for why this is Medium, not High.\n\n## 2. Root cause\n\n`Node.archiveExpiredLease` (`node/node.go:1880-1895`), called from\n`tryArchiveExpiredLeases` (`:1863`) in the single `settleLoop` goroutine\n(`:1757`, 10 s ticks). Per lease it does:\n\n1. Read a snapshot: `GetLeasesByState(Accepted)`.\n2. Txn 1: `DeletePendingSend(leaseHash)`.\n3. Txn 2: blind `PutLease` of a mutated copy (`State=Expired,\n   Settled=true`).\n\nThere is **no lock, no CAS, and no re-read** anywhere in this path.\n`PutLease` (`store/badger_store.go:1562-1570`) is a blind `txn.Set` that\nnever reads the key in-transaction, so Badger's optimistic concurrency\ndetects nothing. No `AddBlock` is involved, so no account or lease lock is\nheld. The settle path takes the provider account lock + lease lock and\nits in-transaction CAS (`ExpectLeaseHash`/`ExpectLeaseState`) — but that\nCAS protects only the settle-against-prior-archive direction: if the\nsettle commits first and the archive writes second, nothing checks.\n\nThe renew leg has the same hole, and it is wider: renew is the one lease\ntransition **without the in-process lease lock**\n(`isLeaseStateTransition`, `core/ledger.go:492-498` — renew is not in the\nlist). Its in-transaction CAS is consulted on every ledger path, but the\narchive never consults any CAS at all. A sweep of all writers on\nlease/pending keys confirms the archive is the **only** lock-free/CAS-free\nwriter.\n\n## 3. Scenario (how it happens)\n\n1. A lease is accepted; its escrow-expiry time passes (expiry +\n   `LeaseEscrowExpiry` + `LeaseArchiveGap`; 365 days by default, shorter\n   on a network whose genesis sets a short `LeaseEscrowExpiryNs`).\n2. The lease's provider authors a `lease_settle` block (or the consumer\n   authors a `lease_renew`) — a valid signed block, authored inside the\n   settle grace window, with real timekeeper attestations.\n3. The victim node ingests that block via the **sync path** while its own\n   archive sweep is between its snapshot read and its `PutLease` — or the\n   block commits after the snapshot was taken.\n4. The archive overwrites the settled/renewed lease with the stale\n   `Expired` copy. The settle/renew block stays in the chain; the lease\n   row says `Expired`.\n\nThe gossip path cannot deliver the old block (1 h timestamp window + 10\nmin attestation skew are enforced there). The sync path skips both — that\nskip is the deciding gate (see §4).\n\n## 4. Proof of concept (local node/ledger only)\n\nPoC: `hunt-v3/stage2/e2e/h2_e2e_test.go` (worktree copy\n`stage2/worktree/node/h2_e2e_test.go`), run against a local node/ledger\nbuilt from the worktree copy at `89a542e`, isolated temp data dir, short\ngenesis escrow (660 s via real `ApplyGenesisLeaseTiming`, controlled clock\nvia `core.Now`). Fixture: cost 667 µXUSD, emission 1,334 XE, expiry\nT0+62s, settleTime expiry+30s, archive T0+783s.\n\nReproduction (deterministic, production Badger backend):\n\n| State | Lease | Settled | Escrow | Provider XE | `GetLeasesByState(Settled)` | Auditor XUSD |\n|---|---|---|---|---|---|---|\n| BEFORE | accepted | false | present (667) | 1,000,000 | 0 | conserved, disc 0 |\n| MID (after sync settle) | settled | true | nil | 1,001,334 | 1 | conserved, disc 0 |\n| AFTER (real archive from stale snapshot) | **expired** | true | nil | 1,001,334 | **0** | **NOT conserved, disc 667** |\n\nNode logs the false burn verbatim: `Archived expired lease\nb0c9b1fd59b6...: burned 667 µXUSD escrow (both parties offline)`.\n\nRenew leg (deterministic, both backends): renew commits (block in chain,\n31,000 µXUSD paid), archive runs both txns from the stale snapshot →\nfinal lease `Expired`/`Settled=true`, **0 renewal segments, effective\nduration back to 3,600, escrow nil**. The 93,000 escrow (62,000 base +\n31,000 renewal) is deleted while the log claims only 62,000 burned.\nBarrier (30 reps): Badger 1/30 corrupt, MemStore 15/30 (18/30 on rerun).\nAftermath: second renew and settle-shaped commit both CAS-rejected\n(`Expired != Accepted`).\n\nGate table (old settle — gossip rejects, sync accepts):\n\n- Hash binding, PoW, signature/credential, attestation signatures and\n  threshold, previous/frontier, per-account timestamp monotonicity,\n  `settleTime >= expiry`, `settleTime <= expiry+grace`, emission amount,\n  escrow presence, balance, lease CAS: **enforced on both paths**\n  (tampered settle rejected `verify: hash mismatch`; pre-expiry settle\n  rejected; past-grace settle rejected).\n- Block-timestamp window 1 h: enforced on gossip, **SKIPPED on sync**\n  (`addBlock`, `core/ledger.go:769-783`, `skipTimestamp=true` via\n  `AddSyncedBlock`).\n- Attestation skew 10 min: gossip **REJECTS** (`only 0 valid attestations\n  from trusted timekeepers, need 1`); sync **SKIPPED**\n  (`ValidateAttestations(..., skipSkew=true)`,\n  `core/attestation.go:86-98`).\n\nControl (archive-first): PASS — settle rejected `\"lease already\nsettled\"`. Full logs: `stage2/e2e/run1-gatetable.log` …\n`run5-full-e2e.log`; renew logs: `stage2/renew/*.log`. Race detector: 0\n`DATA RACE` in all runs (a logical race across transactions is invisible\nto the detector; the proof is final-state loss).\n\n## 5. Attacker prerequisites\n\n- A lease old enough to archive (expiry + escrow-expiry + archive gap\n  elapsed), or a network with a short genesis escrow-expiry.\n- A settle/renew block for that lease, validly signed by the lease's real\n  provider/consumer, authored inside the settle grace window with real\n  timekeeper attestations. **It cannot be forged after the fact.**\n- Ingestion via the sync path by a node whose archive sweep is in the\n  snapshot-to-`PutLease` window (a late-syncing / long-offline node, or\n  any node on a short-escrow network).\n\nNo privileges on the victim node are needed; the archive is the victim\nnode's own clock-driven sweep. The counterparty actions are legitimate\nhonest actions — this is a lost update between honest paths, not an\nattacker-crafted exploit. That is why the impact is Medium rather than\nHigh (see §7).\n\n## 6. Independent validation and false-positive assessment\n\n- **Store level (Stage 1):** deterministic rendezvous + stale-snapshot\n  order both end `Expired` instead of `Settled`, both backends; MemStore\n  barrier CORRUPT 19/30; archive-first order behaves as predicted (settle\n  CAS-rejected).\n- **Ledger level (Stage 2):** the gate table above — every gate checked\n  on both paths; the only skips are the two documented sync-path skips.\n- **Duplicate check (DB refreshed twice, 1,382 entries as of ~18:58\n  WIB):** closest match xe#310 / XE-1640 is the same function but the\n  **deterministic** burn (archive acting alone, no concurrent settle) —\n  different mechanism, different prerequisites, different observable,\n  different fix. xe#183 / XE-0105 covers block-vs-block pairs only; the\n  archive is not a block and appears in neither pair. No filing names\n  archive-vs-settle or archive-vs-renew. Classification: **NOVEL on\n  public data** (private Critical/Severe reports may exist; uniqueness is\n  not guaranteed).\n- **Disproof attempts that failed:** (a) \"the settle CAS protects it\" —\n  false: the CAS protects only settle-after-archive, proven by the\n  archive-first control; (b) \"Badger OCC detects it\" — false: the write\n  is a blind `Set` that never reads the key in-transaction; (c) \"the\n  gossip window blocks it\" — true for gossip, irrelevant for sync;\n  (d) \"the archive takes the lease lock\" — false: it takes no lock at\n  all.\n- **Not a false positive:** the corruption is measured final state\n  (lease row, settled queries, auditor discrepancy), not a pattern match.\n  The 7-Question Gate passes (Q1 conditional on the sync-path\n  reachability above); fp-check deep route passes.\n\n## 7. Severity justification (Medium, ceiling 2,500 XE)\n\nCVSS 3.1: **4.8 (AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:L)**. Attack vector is\nnetwork (sync ingestion from a peer); complexity is High (archivable\nlease + in-window interleave + valid in-grace block); no privileges or\nuser interaction on the victim; integrity impact is Low in CVSS terms\n(state corruption and false accounting on one lease) and availability\nLow (that lease is dead; the node keeps running). Scoring the renew\nfund-loss leg with Integrity High gives **5.9 — still Medium**.\n\nWhy not High: (a) the trigger is timing-dependent and not\nattacker-controlled (the archive clock is the victim node's own);\n(b) there is no remote unauthenticated trigger — the settle/renew must be\na valid signed block from the lease's real provider/consumer, authored\ninside grace; (c) the loss is bounded per lease (one renewal payment +\none lease's emission), with no double-spend and no cascade. If the\ncommittee weighs the renew fund loss more heavily, High is the honest\nalternative framing — the renew numbers are stated plainly above so the\ncommittee can judge.\n\n## 8. Fix suggestion\n\nRe-read + CAS the lease inside `archiveExpiredLease` (or take the lease\nlock): re-fetch the lease row immediately before the `PutLease` and abort\nwhen `curLease.Settled || State != Accepted`; or perform the\ndelete-pending + state change in a single transaction with an\n`ExpectLeaseState(Accepted)` CAS, so a post-snapshot settle/renew makes\nthe archive transaction fail instead of overwriting it. Either fix must\ncover the renew leg as well (same write path).\n\nNote on fix PR #311 (open/unmerged, \"gate lease archive on timekeeper\nattestations\"): its diff adds `blockCreateMu.Lock()` + a `GetLease`\nre-read bailing when settled/not-Accepted — but the lock covers only\nlocal block-creation paths (`settleLease`, `forceSettleLease`, etc.), NOT\ngossip (`handleIncoming`), sync (`net/sync.go` → `AddSyncedBlock`), API\n`SubmitBlock`, or phantom-pull ingestion. If #311 merges, the\nstale-snapshot and local-settle legs close but the gossip/sync rendezvous\nleg survives. A #311 merge alone would NOT fully fix this finding.\n\n## 9. Caveats (kept in the report)\n\n1. Reachability is conditional (see §5). Default 365-day escrow makes the\n   slow path slow; short genesis escrow-expiry makes it fast.\n2. The gossip path rejects the old settle; only sync (and local\n   block-creation paths) can deliver it.\n3. PR #311 narrows but does not fully fix this (see §8).\n4. Renew fund loss is bounded per lease; no double-spend, no unbounded\n   drain.\n5. NOVEL means no matching *public* finding as of the 2026-11-11 ~18:58\n   WIB refresh (DB 1,382 entries); private reports may exist.\n\n## 10. Reproduction (for the committee)\n\nLocal only, worktree copy at `89a542e`, isolated temp data dir:\n\n```bash\nexport PATH=~/workspace/go/go/bin:$PATH\nexport TMPDIR=~/workspace/xe-bounty/.tmp\ncd <worktree>/node\ngo test -race -count=1 -run TestH2_E2E -v ./...\n```\n\nPoC: `hunt-v3/stage2/e2e/h2_e2e_test.go`. Renew PoC:\n`hunt-v3/stage2/renew/stage2_renew_test.go`. Logs:\n`hunt-v3/stage2/e2e/run*.log`, `hunt-v3/stage2/renew/*.log`.\nFull analysis: `hunt-v3/stage2/STAGE2_VERDICT.md`; duplicate analyses:\n`duplicate-intel/reports/v3-h2-archive-overwrite-duplicate-analysis.md`\nand `v3-stage2-h2-duplicate-analysis-update.md`.","patch_refs":[{"id":731,"repo":"xeprotocol/xe","kind":"pr","ref":"880","title":"fix(xe): archiveexpiredlease-blind-over (fixes #381)","url":"https://github.com/xeprotocol/xe/pull/880","note":"","state":"open","author":"tipprb","added_by":"desk","created_at":"2026-11-19T18:35:49.110698+00:00"}],"thread":[{"id":12664,"source":"github","source_ref":"7025798661","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-13250 and says what differs; it is read like any other.","created_at":"2026-11-20T00:04:52+00:00"}]}