Skip to content
Business logic Audit finding Advanced

V3Oracle susceptible to price manipulation

The V3Oracle contract calculates the token amounts of a position using the current spot price from the Uniswap pool, which can be manipulated within a single transaction. An attacker could distort the spot price via a flash loan to inflate or deflate the position value, potentially borrowing or repaying at favourable conditions.

b0g0 Follow with 0x175, 14si2o_Flint and 13 others

Originally disclosed on Code4rena

Submitted by b0g0, also found by 0x175, 14si2o_Flint, kfx, Fitro, Giorgio, grearlake, 0xblackskull (1, 2), crypticdefense, Silvermist, 0xspryon, MohammedRizwan, y0ng0p3, 0xAlix2, MSaptarshi, boredpukar, and maxim371

V3Oracle::getValue() is used to calculate the value of a position. The value is the product of the oracle price * the amounts held in the position. Price manipulation is prevented by checking for differences between Chainlink oracle and Uniswap TWAP.

However, the amounts (amount0 and amount1) of the tokens in the position are calculated based on the current pool price (pool.spot0()), which means they can be manipulated. Since the value of the total position is calculated from amount0 and amount1 it can be manipulated as well.

Proof of Concept

Invoking V3Oracle::getValue() first calls the getPositionBreakdown() to calculate the amount0 and amount1 of the tokens in the position based on spot price:

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Oracle.sol#L102

 (address token0, address token1, uint24 fee,, uint256 amount0, uint256 amount1, uint256 fees0, uint256 fees1) =
            getPositionBreakdown(tokenId);

Under the hood this calls _initializeState() which gets the current price in the pool:

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Oracle.sol#L395

(state.sqrtPriceX96, state.tick,,,,,) = state.pool.slot0();

Based on this value the amount0 and amount1 (returned from getPositionBreakdown() are deduced:

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Oracle.sol#L426

function _getAmounts(PositionState memory state)
        internal
        view
        returns (uint256 amount0, uint256 amount1, uint128 fees0, uint128 fees1)
    {
        if (state.liquidity > 0) {
       ....
            (amount0, amount1) = LiquidityAmounts.getAmountsForLiquidity(
                state.sqrtPriceX96, state.sqrtPriceX96Lower, state.sqrtPriceX96Upper, state.liquidity
            );
        }
       ....
    }

After that, the prices are fetched from Uniswap & Chainlink and compared.

 (price0X96, cachedChainlinkReferencePriceX96) =
            _getReferenceTokenPriceX96(token0, cachedChainlinkReferencePriceX96);
 (price1X96, cachedChainlinkReferencePriceX96) =
            _getReferenceTokenPriceX96(token1, cachedChainlinkReferencePriceX96);

Finally, the value of the positions tokens and fees are calculated in the following formula:

value = (price0X96 * (amount0 + fees0) / Q96 + price1X96 * (amount1 + fees1) / Q96) * Q96 / priceTokenX96;
feeValue = (price0X96 * fees0 / Q96 + price1X96 * fees1 / Q96) * Q96 / priceTokenX96;
price0X96 = price0X96 * Q96 / priceTokenX96;
price1X96 = price1X96 * Q96 / priceTokenX96;

Basically the position value is a product of 2 parameters price0X96/price1X96 and amount0/amount1:

  • price0X96/price1X96 - are the prices derived from the oracles. They are validated and cannot be manipulated.
  • amount0/amount1 - are calculated based on the spot price and can be manipulated.

Since amount0 and amount1 can be increased/decreased if a malicious user decides to distort the pool price in the current block (through a flash loan for example), this will directly impact the calculated value, even though the price itself cannot be manipulated since it is protected against manipulation.

The check in the end _checkPoolPrice() only verifies that the price from the oracles is in the the acceptable ranges. However, this does not safeguard the value calculation, which as explained above also includes the amounts parameters.

It should be noted that _checkPoolPrice uses the uniswap TWAP price for comparison, which is the price over an extended period of time making it very hard to manipulate in a single block. And exactly this property of the TWAP price can allow an attacker to manipulate the spot price significantly, without affecting the TWAP much; which means the price difference won’t change much and _checkPoolPrice will pass.

A short example:

  • The V3Oracle has been configured to use a TWAP duration of 1 hour.
  • The TWAP price reported for the last hour is 4000 USDC for 1 WETH.
  • Bob takes a flash loan to distort the spot price heavily and call in the same transaction borrow | repay on a V3Vault (which call V3Oracle.getValue()).
  • Because of the price manipulation amount1 and amount0 are heavily inflated/deflated.
  • However this changes the TWAP value only a little (if at all), so the price validation passes.
  • The position value is calculated by multiplying the stable oracle price by the heavily manipulated amounts.
  • User repay/borrows at favorable conditions.

Recommended Mitigation Steps

Consider calculating amount0 & amount1 based on the oracle price and not on the spot price taken from slot0(). This way the above exploit will be mitigated.

Assessed type

Uniswap

kalinbas (Revert) commented:

The check in _checkPoolPrice() does limit the price manipulation. The current pool price (it is NOT the TWAP price as you mention) is compared to the derived pool price of the Chainlink oracle prices.

The amounts may be slightly manipulated for wide range positions, and more heavily manipulated for tight range positions. But the protection with _checkPoolPrice() should be enough to protect this from being a problem. Also, repay() does not need any oracles.

ronnyx2017 (judge) decreased severity to Medium and commented:

I think it is reasonable to classify this issue as Medium.

Firstly, I think that the possibility of exploitation exists only within the _checkPoolPrice method.

Secondly, although it would require very fringe (and even incorrect) configuration parameters to really cause un-dusty losses, given that similar attacks have occurred on the mainnet with substantial losses (refer to the Gamma gDai TWAP verification range configuration error, even though it’s unrelated to slot0), I believe it’s worth adding necessary safeguards, especially for tight range positions and tokens.

kalinbas (Revert) confirmed and commented:

We agree to change it to use the oracle price for position token calculation.

Revert mitigated:

Fixed here.

Status: Mitigation Confirmed. Full details in reports from thank_you, b0g0 and ktg.


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

An attacker could manipulate the position value to borrow or repay at favourable conditions, though the `_checkPoolPrice` check limits the manipulation and the judge noted that very fringe configuration parameters would be needed for real losses.

What the attacker needed

Ability to take a flash loan; a V3Vault position; specific oracle configuration (e.g., tight range)

The fix

The protocol was fixed by changing the calculation to use the oracle price for position token amounts instead of the spot price.

How it was found

  1. The V3Oracle is configured with a TWAP duration of one hour.
  2. The TWAP price for the last hour is 4000 USDC per 1 WETH.
  3. Bob takes a flash loan to heavily distort the spot price.
  4. In the same transaction, Bob calls borrow or repay on a V3Vault, which calls V3Oracle.getValue().
  5. The manipulated spot price inflates or deflates amount0 and amount1, but the TWAP changes little, so the price validation passes.
  6. The position value is calculated using the stable oracle price and the manipulated amounts, allowing Bob to borrow or repay at favourable conditions.

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 decentralised finance protocol uses an oracle to work out how much a user's collateral is worth. The oracle gets the token amounts from the current pool price, which can be temporarily moved by a large trade. An attacker could use a flash loan to move the price for a moment and trick the protocol into thinking their collateral is worth more or less than it really is.

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 b0g0

View all

2 more disclosed reports, best first.

More from Revert Lend

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.