High divergence check in isSafeMode is unreachable dead code providing false sense of security
The highDivergence check in the risk engine's safe mode logic compares the median tick against a slow moving average, but both values are derived from the same clamped input, so their divergence is mathematically bounded well below the trigger threshold. This makes the check dead code, so the intended third layer of oracle-manipulation protection never activates and minting may proceed when it should be blocked.
Originally disclosed on Code4rena
Submitted by anchabadze
RiskEngine.sol L929
Finding description
The highDivergence check in RiskEngine::isSafeMode() is designed to detect when the median tick deviates significantly from the slow EMA by comparing |medianTick - slowEMA| > 1906 ticks. This condition is intended to add +1 to safeMode, potentially pushing it to 3 (Lock Mode), which would cause minting operations to revert with StaleOracle.
However, this condition is mathematically and empirically proven to be unreachable dead code. Both medianTick and slowEMA are updated using the same clampedTick value, which is capped at MAX_CLAMP_DELTA = 149 ticks per 64-second epoch. Due to this shared input and capping mechanism, their maximum possible divergence is mathematically bounded.
In equilibrium with constant price movement at maximum speed, slowEMA lags behind the current tick by approximately 1397 ticks, while medianTick (median of 8 observations) lags by approximately 522 ticks. This results in a maximum achievable divergence of approximately 875 ticks in equilibrium, or up to 935 ticks when accounting for transient effects. The threshold of 1906 ticks is unreachable, representing only 42-49% of what would be needed to trigger the condition.
Comprehensive testing across multiple extreme scenarios (flash loan attacks, post-initialization manipulation, price oscillation) confirmed that the divergence never exceeds 815 ticks, making the 1906-tick threshold impossible to reach under any realistic conditions.
In the provided PoC you can find two tests that prove this claim: test_MathematicalProof_MaximumDivergence() which calculates the theoretical maximum divergence using EMA update formulas and equilibrium analysis, proving the threshold is unreachable by a factor of 2.04x, and test_HighDivergence_IsDeadCode() which empirically verifies the threshold is unreachable by testing extreme scenarios and comparing results with theoretical predictions, confirming maximum observed divergence of ~815 ticks.
Scenario
- The protocol assumes the
highDivergencecheck provides protection against oracle staleness by comparingmedianTickandslowEMAto detect significant divergence. - Both metrics are updated in
OraclePack::insertObservation()using the sameclampedTickvalue, which is limited byclampTick()to a maximum change of 149 ticks per epoch. - The
clampedTickis calculated from the current tick but clamped to be within 149 ticks of the last observed tick, ensuring bothmedianTickandslowEMAalways move in the same direction with bounded difference. - Even under extreme market conditions or deliberate manipulation attempts, the divergence between
medianTickandslowEMAstabilizes at approximately 726-815 ticks, well below the 1906-tick threshold. - The
highDivergencethreshold of 1906 ticks (2 ×MAX_TICKS_DELTA) remains permanently unreachable, making this check dead code that never executes. - The protocol documentation and code comments suggest this check provides protection, creating a false sense of security while offering no actual protection.
Impact
The protocol documentation and code suggest that highDivergence provides protection against oracle manipulation, but this protection is completely non-functional.
The safeMode mechanism was designed with three additive conditions (externalShock, internalDisagreement, highDivergence). With highDivergence being dead code, safeMode can never reach 3 through oracle-based detection
Users may be able to mint positions during oracle manipulation scenarios that should be blocked, as the intended third layer of protection is non-functional.
Recommended mitigation steps
Recalculate the threshold based on actual achievable divergence. A threshold of approximately 600-700 ticks would be reachable under extreme conditions while still providing meaningful protection or use different metrics
Proof of Concept
- Create a test file
HighDivergenceTest.t.solintest/foundry/core/. - Paste the PoC code provided below into the file.
- Save the file.
- Run the test:
ETH_RPC_URL="<YOUR_MAINNET_RPC_URL>" forge test --match-path test/foundry/core/HighDivergenceTest.t.sol -vv.
View detailed Proof of Concept
Panoptic marked as informative.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely both medianTick and slowEMA are updated from the same clampedTick value capped at 149 ticks per epoch, bounding their divergence below the 1906-tick threshold.
Impact
Users may be able to mint positions during oracle manipulation scenarios that should be blocked, because the intended third layer of protection never fires.
What the attacker needed
Ability to manipulate the oracle price; minting enabled on the protocol
The fix
The threshold should be recalculated to around 600-700 ticks, which is reachable under extreme conditions while still providing meaningful protection, or different metrics should be used.
How it was found
- Create a test file in the foundry core test directory.
- Paste the provided proof-of-concept code into the file.
- Save the file.
- Run the test with forge using a mainnet RPC URL and the matching path.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract tracks the market price in two ways: a median of recent readings and a slow average. It was meant to raise an alarm if these two drifted far apart, which would suggest price manipulation. But both are fed the same capped input, so they can never drift far enough to trip the alarm, leaving that safeguard permanently silent.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.