Skip to content
DoS Audit finding Intermediate

Overflow can still happen when calculating priceX8 inside poolMatchesOracle operation

The price calculation in poolMatchesOracle can overflow because the scaling down of sqrtPriceX96 is insufficient. An attacker could cause the function to revert, preventing rebalancing.

said Follow with radev_sw, Satyam_Sharma and 4 others

Originally disclosed on Code4rena

Submitted by said, also found by radev_sw, Satyam_Sharma, 0xmuxyz, LokiThe5th, Team_FliBit, and T1MOH

poolMatchesOracle is used to compare price calculated from uniswap v3 pool and chainlink oracle and decide whether rebalance should happened or not. priceX8 will be holding price information calculated using sqrtPriceX96 and when operations is performed, it will try to scale down using 2 ** 12. However, the scale down is not enough and overflow can still happened.

Proof of Concept

Consider this scenario, The GeVault is using WBTC for token0 and WETH for token1.

These are information for the WBTC/WETH from uniswap v3 pool (0x4585FE77225b41b697C938B018E2Ac67Ac5a20c0):

slot0 data (at current time) :

sqrtPriceX96   uint160 :  31520141554881197083247204479961147

token0 (WBTC) decimals is 8 and token1 (WETH) decimals is 18.

Using these information, try to reproduce the priceX8 calculation :

    function testOraclePrice() public {
        uint160 sqrtPriceX96 = 31520141554881197083247204479961147;
        // decimals0 is 8
        uint priceX8 = 10 ** 8;
        // Overflow if dont scale down the sqrtPrice before div 2*192
        // @audit - the overflow still possible
        priceX8 =
            (priceX8 * uint(sqrtPriceX96 / 2 ** 12) ** 2 * 1e8) /
            2 ** 168;
        // decimals1 is 18
        priceX8 = priceX8 / 10 ** 18;
        assertEq(true, true);
    }

the test result in overflow :

[FAIL. Reason: Arithmetic over/underflow] testOraclePrice()

This will cause calculation still overflow, even using the widely used WBTC/WETH pair

Recommended Mitigation Steps

Consider to change the scale down using the recommended value from uniswap v3 library:

https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/OracleLibrary.sol#L49-L69

or change the scale down similar to the one used inside library

  function poolMatchesOracle() public view returns (bool matches){
    (uint160 sqrtPriceX96,,,,,,) = uniswapPool.slot0();

    uint decimals0 = token0.decimals();
    uint decimals1 = token1.decimals();
    uint priceX8 = 10**decimals0;
    // Overflow if dont scale down the sqrtPrice before div 2*192
-    priceX8 = priceX8 * uint(sqrtPriceX96 / 2 ** 12) ** 2 * 1e8 / 2**168;
+    priceX8 = priceX8 * (uint(sqrtPriceX96) ** 2 / 2 ** 64) * 1e8 / 2**128;
    priceX8 = priceX8 / 10**decimals1;
    uint oraclePrice = 1e8 * oracle.getAssetPrice(address(token0)) / oracle.getAssetPrice(address(token1));
    if (oraclePrice < priceX8 * 101 / 100 && oraclePrice > priceX8 * 99 / 100) matches = true;
  }

Keref (Good Entry) confirmed and commented:

See PR#3.

Good Entry Mitigated:

Scale down sqrtPriceX96 to prevent overflow. PR: https://github.com/GoodEntry-io/ge/pull/3

Status: Mitigation confirmed. Full details in reports from kutugu, xuwinnie and 3docSec.


The original report as published on Code4rena, by said. 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

Impact

The overflow causes poolMatchesOracle to revert, so the rebalance check cannot complete and rebalancing may be blocked.

What the attacker needed

Pool uses tokens with different decimals; large sqrtPriceX96 value

The fix

The scaling of sqrtPriceX96 was changed to divide by 2**64 before squaring and by 2**128 afterwards, and the mitigation was confirmed.

How it was found

  1. Use a pool with WBTC as token0 and WETH as token1.
  2. Read the current sqrtPriceX96 from the pool.
  3. Compute priceX8 using the same formula as poolMatchesOracle.
  4. Observe that the calculation reverts with an arithmetic overflow.

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 calculates a price from a pool and compares it to an oracle. The calculation multiplies large numbers and can exceed the maximum integer size, causing an error. This stops the rebalance check from working.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by said

View all

21 more disclosed reports, best first.

More from Good Entry

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.