core: cancel-wins TOCTOU - a finalized lease accept is unwound when finalization lands between the FinalHeight check and the undo
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 withgo test ./store/ -run TestZZCancelWinsTOCTOU -v -count=1)s-a-race.log— full output of the 6/400 runxe-finding5-screenshot.png— screenshot of the independent verification run (2/400 hits, PASS)
Thread
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.