Skip to content
Business logic Audit finding Advanced

Solvency tick divergence blind spot in RiskEngine.getSolvencyTicks

The getSolvencyTicks function uses a squared-distance gate that can be bypassed by symmetric tick configurations, causing solvency to be checked at only one tick even when the spot and current ticks diverge significantly. An attacker could exploit this to have a position pass solvency checks while actually being insolvent at the current price.

qed Follow

Originally disclosed on Code4rena

Submitted by qed

RiskEngine.sol L962-L967

The getSolvencyTicks() function uses a 3D squared-Euclidean-norm gate centered on the median tick to decide whether to check solvency at multiple ticks. However, symmetric tick configurations can bypass this gate even when |spotTick - currentTick| significantly exceeds MAX_TICKS_DELTA, potentially allowing positions to pass solvency checks at a single tick when they would be insolvent at the current tick.

Finding description

1. The squared-distance-from-median gate can be bypassed with symmetric tick arrangements:

// contracts/RiskEngine.sol:962-978
function getSolvencyTicks(
    int24 currentTick,
    OraclePack _oraclePack
) external view returns (int24[] memory, OraclePack) {
    (int24 spotTick, int24 medianTick, int24 latestTick, OraclePack oraclePack) = _oraclePack
        .getOracleTicks(currentTick, EMA_PERIODS, MAX_CLAMP_DELTA);

    int24[] memory atTicks;

    // Gate uses sum of squared distances from median
    if (
        int256(spotTick - medianTick) ** 2 +
            int256(latestTick - medianTick) ** 2 +
            int256(currentTick - medianTick) ** 2 >
        MAX_TICKS_DELTA ** 2
    ) {
        // Multi-tick check (4 ticks)
        atTicks = new int24[](4);
        // ...
    } else {
        // Single tick check (only spotTick)
        atTicks = new int24[](1);
        atTicks[0] = spotTick;
    }
    // ...
}

2. Mathematical blind spot with symmetric configurations:

If medianTick = M, latestTick = M, and:

  • spotTick = M + 0.70 * MAX_TICKS_DELTA
  • currentTick = M - 0.70 * MAX_TICKS_DELTA

Then the squared norm calculation:

(0.70 * D)^2 + 0^2 + (-0.70 * D)^2 = 0.98 * D^2 < D^2

The gate does NOT fire, but |spotTick - currentTick| = 1.40 * D > D is a significant divergence.

3. Discrepancy with isSafeMode logic:

Notably, isSafeMode() DOES include a check for |currentTick - spotTick| divergence:

// contracts/RiskEngine.sol:903
if (Math.abs(currentTick - spotTick) > MAX_TICKS_DELTA) {
    // External shock detected - safe mode ON
}

But getSolvencyTicks() does not use this same check. This creates an inconsistency where the protocol may recognize an external shock (safe mode ON) but still only check solvency at a single tick.

Attack scenario

Step 1: Market conditions create a symmetric tick divergence around the median:

  • medianTick = 0 (from TWAP)
  • latestTick = 0 (same as median)
  • spotTick = +667 (0.7 * 953)
  • currentTick = -667

Step 2: The squared norm calculation:

(667)^2 + 0 + (-667)^2 = 889,778 < 908,209 = 953^2

The gate doesn’t trigger - only spotTick (667) is returned for solvency checks.

Step 3: A position that is:

  • Solvent at spotTick = 667
  • Insolvent at currentTick = -667

…passes the solvency check because only spotTick is evaluated.

Step 4: The gap between spot and current tick is 1,334 ticks (667 - (-667)), which is 140% of MAX_TICKS_DELTA (953). This represents a significant price deviation that should trigger multi-tick evaluation.

Impact

  1. Solvency check bypass: Positions may pass solvency checks when evaluated only at spotTick while being insolvent at currentTick.
  2. Inconsistent risk assessment: The protocol uses different divergence logic in isSafeMode() vs getSolvencyTicks(), creating a gap where safe mode is active but solvency is only checked at one tick.
  3. Directional portfolio vulnerability: Portfolios with directional exposure (heavily long or short) are particularly sensitive to tick-specific solvency. Checking only at spotTick can miss insolvency at the actual current price.

Recommended mitigation steps

Add an explicit pairwise check for spot-current divergence to align with isSafeMode logic:

function getSolvencyTicks(
    int24 currentTick,
    OraclePack _oraclePack
) external view returns (int24[] memory, OraclePack) {
    (int24 spotTick, int24 medianTick, int24 latestTick, OraclePack oraclePack) = _oraclePack
        .getOracleTicks(currentTick, EMA_PERIODS, MAX_CLAMP_DELTA);

    int24[] memory atTicks;

    int256 d1 = int256(spotTick - medianTick);
    int256 d2 = int256(latestTick - medianTick);
    int256 d3 = int256(currentTick - medianTick);
    int256 d4 = int256(currentTick - spotTick);  // Pairwise check

    if (
        d1 ** 2 + d2 ** 2 + d3 ** 2 > MAX_TICKS_DELTA ** 2 ||
        Math.abs(d4) > MAX_TICKS_DELTA  // Align with isSafeMode logic
    ) {
        // Multi-tick check
        atTicks = new int24[](4);
        // ...
    } else {
        atTicks = new int24[](1);
        atTicks[0] = spotTick;
    }
    // ...
}

This ensures that any significant divergence between spot and current tick triggers multi-tick solvency evaluation, consistent with how isSafeMode detects external shocks.

Summary

RiskEngine.getSolvencyTicks() uses a 3D squared-Euclidean-norm gate that can be bypassed with symmetric tick configurations where spot and current ticks are on opposite sides of the median. This allows positions to be evaluated at only the spot tick even when |spotTick - currentTick| significantly exceeds MAXTICKSDELTA (953 ticks). The fix is to add an explicit pairwise spot-current check, aligning with the logic already present in isSafeMode().

Proof of Concept

File: test/foundry/core/PoC_SolvencyBlindSpot.t.sol

Run with: forge test --match-test test_poc_solvencyBlindSpot -vvv

View detailed Proof of Concept

Panoptic mitigated:

Check solvency at 4 ticks when safeMode > 0.

Status: Mitigation confirmed. Full details in reports from Valves, edoscoba, and Nyx.


The original report as published on Code4rena, by qed. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

The gate uses a sum of squared distances from the median tick, which can be small even when the spot and current ticks are far apart on opposite sides of the median.

Impact

Positions may pass solvency checks when evaluated only at spotTick while being insolvent at currentTick, and the protocol may have safe mode active but still only check solvency at one tick.

What the attacker needed

Market conditions with symmetric tick divergence; Position solvent at spotTick but insolvent at currentTick

The fix

The issue was mitigated by checking solvency at 4 ticks when safeMode > 0.

How it was found

  1. Set market conditions so that medianTick and latestTick are equal, and spotTick and currentTick are symmetric around the median at ±0.70 * MAX_TICKS_DELTA.
  2. Observe that the squared norm is 0.98 * MAX_TICKS_DELTA^2, which is below the threshold, so the gate does not trigger.
  3. Only spotTick is returned for solvency checks.
  4. A position that is solvent at spotTick but insolvent at currentTick passes the solvency check.

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.

This report is about a smart contract that checks whether a trading position is safe from liquidation. The check normally looks at several price points, but a mathematical shortcut can be tricked into looking at only one price point. If the real current price is very different from that one point, a risky position might be wrongly considered safe.

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.

More by qed

View all

13 more disclosed reports, best first.

More from Panoptic: Next Core

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.