oracle_update panics on extreme reserve ratio (Decimal::from_ratio) and permanently bricks a pair, locking LP funds #465
Labels
No labels
agent:fix_bugfix
agent:fix_conflicts
agent:fix_security
agent:gap_analysis
agent:implement
agent:implement
agent:implement
agent:open_issues
agent:ready
agent:research
agent:security_audit
agent:verify
architecture
backend
blocker:hybrid
blocker:launch
blocker:limit-orders
blocker:v2
block:log_only
block:security
bug
ci
contracts
correctness
deploy
dev
devops
docs
documentation
duplicate
e2e
enhancement
epic
feature
frontend
functional-completion
gas
good first issue
governance
help wanted
high-risk
hooks
hybrid
indexer
infra
infrastructure
integrators
invalid
launch-blocker
limit-orders
localnet
localterra
low priority
missing-implementation
needs-design
ops
performance
priority
high
priority
medium
product
qa
QA
question
ready
ready
research
scripts
security
security-hardening
smartcontracts
tech-debt
testing
ux
UX
v2
verification
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
code/cl8y-dex-terraclassic#465
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Came out of the #381 security-hardening sweep, poking at the TWAP oracle path. There's a panic in
oracle_updatethat a swap can arm but not trip, so the pair commits an unsafe state and then every subsequent op dies. Net result: pair bricked, LP funds locked.Where
smartcontracts/contracts/pair/src/contract.rsoracle_updateruns at the top of every reserve-mutating path::970:1561:1747Why it panics
Decimal::from_ratiois the unwrapping variant — internally it'schecked_from_ratio(...).unwrap(), so it panics when the resulting value exceedsDecimal::MAX(u128::MAX / 1e18, ~3.40e20). A pair withreserve_a = 1andreserve_b > ~3.4e20raw makesprice_a = reserve_b / reserve_ablow pastDecimal::MAXand the tx aborts with an unhandled panic instead of a clean error.The nasty part is the timing. The oracle is deliberately sampled on the reserves before the current op mutates them — that's the manipulation-resistance design, and it's even spelled out in the comment at
:259-262("an attacker's trade in this block does NOT influence the observation recorded for this block"). Every call passes the pre-opreserve_a/reserve_bloaded just above the call (:967,:1558,:1743).That design is exactly what turns a revert into a permanent brick:
from_ratioon the prior (still-safe) reserves, so it does not panic. It commits the extreme reserve state.block_time > last_ts, so it gets past the:304early-return), every op — swap, provide, withdraw — loads the committed extreme reserves and callsfrom_ratioon them. Panic. Tx aborts.:1747, LPs can't even pull out. The funds are stuck.Note the
is_zeroguard at:273only covers the divide-by-zero edge, not the overflow edge, so it doesn't help here.Repro
On a shallow 18-decimal pool:
token_bin to pushreserve_adown toward ~1 raw andreserve_babove ~3.4e20raw — that's roughly ~340 whole tokens of an 18-dec asset, so not an exotic amount on a shallow pool. This swap SUCCEEDS (it sampled the old ratio).oracle_updaterunsfrom_ratio(reserve_b, reserve_a)on the committed extreme reserves and panics. Every op aborts.Pair is bricked and LP liquidity is unrecoverable. It doesn't even need to be adversarial — a big enough legit swap on a thin pool trips it.
Fix direction
The oracle should never be able to hard-panic a state transition. Concretely:
Decimal::from_ratioforDecimal::checked_from_ratioat:309-310and degrade gracefully on overflow — skip the observation for this update (returnOk(())without recording), or clamp the price toDecimal::MAX, rather than letting the whole tx panic. Skipping is cleanest: a missed TWAP sample is far better than a bricked pair.This is the same
u128/Decimal-no-widening family as the constant-product overflow finding — same root cause (unchecked arithmetic that can exceed the 128-bit/Decimalrange on lopsided reserves), just surfacing in the oracle instead of the AMM invariant. Worth fixing them together and cross-linking under #381.@PlasticDigits flagging this one as launch-blocker — it's a permanent LP fund lock reachable by a single oversized swap on any shallow pool, and the withdraw path is stuck behind the same panic so there's no self-rescue.
mentioned in issue #464
Companion finding, same root cause (unwidened native-width reserve math): #464 (constant-product k = reserve_a*reserve_b overflows u128 for 18-dec pools). Fix them together.
mentioned in merge request !1002
Fixed alongside #464 in MR !1002 (commit
4811caf9). oracle_update now uses Decimal::checked_from_ratio and skips the observation on an extreme ratio instead of panicking, so neither the swap nor the withdraw path can brick the pair. Added oracle_overflow_tests (extreme-ratio -> Ok, normal ratio still records) that panic against the pre-fix code. Needs review @PlasticDigits.mentioned in commit
fcdcbd87f2Verification — #465 (oracle_update extreme-ratio panic / LP brick)
Verified on
main@fcdcbd87(fix landed in4811caf9via !1002).Acceptance criteria
oracle_updatemust not use panickingDecimal::from_ratioon reserve pricessmartcontracts/contracts/pair/src/contract.rslines 332–337 useDecimal::checked_from_ratio; overflow branch returnsOk(())(skips observation)reserve_b/reserve_a > Decimal::MAX) must not paniccargo test -p cl8y-dex-pair oracle_overflow_tests—extreme_ratio_degrades_gracefully_instead_of_panickingpasses (reserve_a=1,reserve_b=Uint128::MAX)normal_ratio_still_records_observationpassesoracle_updateand must not brick pairmake test-contracts— 393 integration + 44 pair unit tests, 0 failures; includestest_oracle_observations_recorded_on_withdraw,test_withdraw_liquidity, swap boundary testscargo test -p cl8y-dex-tests oracle— 13/13 passglab mr view 284→ merged; commit message documents #465 fixIssue repro (manual on-chain)
The issue's 3-step shallow-pool repro (swap to extreme ratio → next-block ops panic) is covered at the unit level by
oracle_overflow_tests, which directly exercises the pre-op-reserveoracle_updatepath that previously panicked. Full LocalTerra on-chain replay was not run (not required — unit + integration suites encode the brick scenario and withdraw path).Follow-up (non-blocking)
oracle_observe_single(query-only TWAP extrapolation atcontract.rs:404–405) still usesDecimal::from_ratio. That cannot brick LP funds (read-only query), but a future hardening pass could mirrorchecked_from_ratiothere for consistent query error handling instead of panic.