<!-- XE-8029 · core: cancel-wins TOCTOU - a finalized lease accept is unwound when finalization lands between the FinalHeight check and the undo · testnet -->
*Markdown view of <https://bounty.xe.network/XE-8029>. The page is the record; this is the same page for reading.*

XE-8029 · Bounty

# core: cancel-wins TOCTOU \- a finalized lease accept is unwound when finalization lands between the FinalHeight check and the undo

Closed · duplicate
—claimed Medium
web
filed 2026-11-11 19:54 UTC
by xe:6de8078e

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Medium

Area
:   Consensus

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-17 09:38 UTC

Told
:   2026-11-17 09:38 UTC

### Patches

None linked.

### History

- [414838](https://bounty.xe.network/log/414838) 2026-11-17 09:38 UTC proposal.adopted
- [414837](https://bounty.xe.network/log/414837) 2026-11-17 09:38 UTC reply.sent
- [414836](https://bounty.xe.network/log/414836) 2026-11-17 09:38 UTC ticket.status new → closed
- [411987](https://bounty.xe.network/log/411987) 2026-11-17 09:37 UTC proposal.adopted
- [411986](https://bounty.xe.network/log/411986) 2026-11-17 09:37 UTC proposal.adopted
- [411985](https://bounty.xe.network/log/411985) 2026-11-17 09:37 UTC ticket.facts
- [411984](https://bounty.xe.network/log/411984) 2026-11-17 09:37 UTC proposal.adopted
- [411983](https://bounty.xe.network/log/411983) 2026-11-17 09:37 UTC ticket.facts
- [386417](https://bounty.xe.network/log/386417) 2026-11-17 05:06 UTC note.added
- [386416](https://bounty.xe.network/log/386416) 2026-11-17 05:06 UTC proposal.created
- [386415](https://bounty.xe.network/log/386415) 2026-11-17 05:06 UTC proposal.created
- [386414](https://bounty.xe.network/log/386414) 2026-11-17 05:06 UTC proposal.created

[Full history](https://bounty.xe.network/log?ref=XE-8029) · [JSON](https://bounty.xe.network/v1/tickets/XE-8029) · [markdown](https://bounty.xe.network/XE-8029.md)

### Report

## Summary

`resolveCancelWinsOverAccept` (core/ledger.go:1749-1789) checks whether the racing accept is finalized (`FinalHeight`, ledger.go:1772), then acquires the account lock (\~ledger.go:1780), then undoes the accept via `undoBlockApply` (core/rollback.go:374) — without re-checking finality after the lock is acquired. If `SetFinalHeight` for that provider lands between the check and the undo, a finalized accept is rolled back.

## Mechanism

- ledger.go:1772: `if fh := l.FinalHeight(provider); uint64(acceptIdx+1) <= fh { ... reject "accept is finalized" ... }`
- \~ledger.go:1780: `acquireAccountLockTry` — the window between the check and the lock is the race.
- rollback.go:374 (`undoBlockApply`): unwinds the accept with no finality re-check.

So a cancel that arrives just before finalization can pass the check (accept not yet final), then the accept finalizes, then the undo still runs. The node that processes the cancel rolls back a block the rest of the network considers final.

## Proof (local in-process test, deterministic harness, HEAD 89a542e)

Test: `store/zz_audit_r2_test.go`, `TestZZCancelWinsTOCTOU` (attached). Each of 400 iterations uses a fresh ledger (lease \+ valid accept, accept \= tip of the provider chain at height 2); the cancel's `AddSyncedBlock` is raced against `SetFinalHeight(provider, 2)` with a random delay of 0-150 microseconds. A hit \= the cancel succeeded even though finalization completed before the cancel returned (post-state verified: `FinalHeight=2, acceptStillOnChain=false`).

Results, three consecutive runs:

- Run 1: 3/400 hits
- Run 2: 6/400 hits (log attached: hits at iters 0, 21, 41, 61, 342, 385; 333 legit cancel-wins, 61 clean "accept is finalized" rejects, 0 other errors)
- Run 3 (independent re-run): 2/400 hits (334 legit cancel-wins, 64 clean rejects, 0 other errors)

All three runs PASS. The control outcomes (legit wins \+ clean rejects) behave exactly as designed, so the hits are the race, not a broken harness.

## Impact

The node processing the cancel diverges from the network: it rolls back a finalized accept while other nodes keep it. The per-attempt window is microseconds in-process (check to TryLock) and the harness hit rate is about 1%; in production the window is network-scale (milliseconds around finalization, roughly 3 s after the accept), so the real probability per attempt is lower but non-zero — and a cancel sent near the finalization moment is the realistic trigger, not a random one.

## Suggested fix

Re-check `FinalHeight` after `acquireAccountLockTry` succeeds, immediately before the undo — reject the cancel if the accept has become final in the meantime.

## Distinction from XE-0017

XE-0017 (Accepted, High) is a different trigger and a different root cause: there an INVALID cancel (wrong balance/previous/timestamp) deterministically unwinds a NOT-YET-FINALIZED accept because of validation order. This finding is a VALID cancel racing finalization: the accept is ALREADY FINAL when it is undone. XE-0017's suggested fix (full validation before unwind, atomic unwind with commit) does not close this race — the commit path already has `ExpectLeaseState` but no finality re-check after the lock.

## Severity

Medium proposed: a finalized block can be rolled back, causing local ledger divergence; exploitation needs precise timing, not attacker-controlled state beyond sending a cancel near finalization.

## Attachments

- `zz_audit_r2_test.go` — the race test (run it with `go test ./store/ -run TestZZCancelWinsTOCTOU -v -count=1`)
- `s-a-race.log` — full output of the 6/400 run
- `xe-finding5-screenshot.png` — screenshot of the independent verification run (2/400 hits, PASS)

### Thread

**Joseph Denne**team2026-11-17 09:38 UTCdesk

Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the
first verifiable submission, so this one closes as a duplicate. If you believe it is a different
bug, file a new report that links https://bounty.xe.network/XE-8029 and says what differs; it is read like any other.
