_getUniswapTwapWei() will show incorrect price for negative ticks cause it doesn’t round up for negative ticks
The Uniswap TWAP price function truncates the tick toward zero instead of rounding down for negative ticks, so when the tick cumulative difference is negative and not divisible by the interval, the returned tick is too high. This can produce an incorrect price, enabling price manipulation and arbitrage, and may cause pricing logic to revert.
Bauchibred Follow with grearlake
Originally disclosed on Code4rena
Submitted by Bauchibred, also found by grearlake
Take a look at https://github.com/code-423n4/2024-01-salty/blob/f742b554e18ae1a07cb8d4617ec8aa50db037c1c/src/price_feed/CoreUniswapFeed.sol#L49-L75
// Returns amount of token0 * (10**18) given token1
function _getUniswapTwapWei( IUniswapV3Pool pool, uint256 twapInterval ) public view returns (uint256)
{
uint32[] memory secondsAgo = new uint32[](2);
secondsAgo[0] = uint32(twapInterval); // from (before)
secondsAgo[1] = 0; // to (now)
// Get the historical tick data using the observe() function
(int56[] memory tickCumulatives, ) = pool.observe(secondsAgo);
//@audit
int24 tick = int24((tickCumulatives[1] - tickCumulatives[0]) / int56(uint56(twapInterval)));
uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick( tick );
uint256 p = FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96 );
uint8 decimals0 = ( ERC20( pool.token0() ) ).decimals();
uint8 decimals1 = ( ERC20( pool.token1() ) ).decimals();
if ( decimals1 > decimals0 )
return FullMath.mulDiv( 10 ** ( 18 + decimals1 - decimals0 ), FixedPoint96.Q96, p );
if ( decimals0 > decimals1 )
return ( FixedPoint96.Q96 * ( 10 ** 18 ) ) / ( p * ( 10 ** ( decimals0 - decimals1 ) ) );
return ( FixedPoint96.Q96 * ( 10 ** 18 ) ) / p;
}
This function is used to get twap price tick using uniswap oracle. it uses pool.observe() to get tickCumulatives array which is then used to calculate int24 tick.
The problem is that in case if int24(tickCumulatives[1] - tickCumulatives[0]) is negative, then the tick should be rounded down as it’s done in theuniswap library.
As result, in case if int24(tickCumulatives[1] - tickCumulatives[0]) is negative and (tickCumulatives[1] - tickCumulatives[0]) % secondsAgo != 0, then returned tick will be bigger then it should be, which opens possibility for some price manipulations and arbitrage opportunities.
Impact
In case if int24(tickCumulatives[1] - tickCumulatives[0])is negative and ((tickCumulatives[1] - tickCumulatives[0]) % secondsAgo != 0, then returned tick will be bigger than it should be which places protocol wanting prices to be right not be able to achieve this goal, note that where as protocol still relies on multiple sources of price, they still come down and end on weighing the differences between the prices and reverting if a certain limit is passed, effectively causing the pricing logic to be unavailable and also reverting on important functions like CollateralAndLiquidity::liquidate() cause a call to underlyingTokenValueInUSD() is made which would not be available.
Recommended Mitigation Steps
Add this line: if (tickCumulatives[1] - tickCumulatives[0] < 0 && (tickCumulatives[1] - tickCumulatives[0]) % secondsAgo != 0) timeWeightedTick --;
othernet-global (Salty.IO) confirmed and commented:
Now rounds down for negative ticks as suggested.
https://github.com/othernet-global/salty-io/commit/4625393e9bd010778003a1424201513885068800
The stablecoin framework: /stablecoin, /price_feed, WBTC/WETH collateral, PriceAggregator, price feeds and USDS have been removed: https://github.com/othernet-global/salty-io/commit/88b7fd1f3f5e037a155424a85275efd79f3e9bf9
Status: Mitigation confirmed. Full details in reports from t0x1c, zzebra83, and 0xpiken.
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 code casts the tick cumulative difference divided by the interval to int24 without rounding down for negative values, unlike the Uniswap library.
Impact
The incorrect tick could enable price manipulation and arbitrage, and may cause pricing logic to be unavailable and functions like liquidation to revert.
What the attacker needed
Negative tick cumulative difference; difference not divisible by interval
The fix
The code was fixed to round down for negative ticks as suggested.
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 price feed calculates an average price from Uniswap and converts it to a tick, a small integer that represents the price. When the average is negative, the code cuts off the decimal part toward zero instead of rounding down, so the tick ends up slightly too high. That wrong price can be exploited for arbitrage or make the protocol's price checks fail.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.