You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The UniswapPricingLibrary contains a critical vulnerability in its price fetching mechanism where it allows direct access to the current pool price through slot0 when twapInterval is set to zero. The core issue lies in the getSqrtTwapX96 function which bypasses Uniswap V3's time-weighted average price (TWAP) protection when twapInterval equals zero, making it susceptible to flash loan attacks and price manipulation within a single block.
This vulnerability stems from the assumption that instantaneous price readings from slot0 are acceptable alternatives to TWAP. However, in the context of Uniswap V3's architecture, slot0 prices can be manipulated through large swaps within a single transaction, especially using flash loans. The impact is severe because any protocol relying on this library for price feeds could be exploited through price manipulation, potentially leading to significant financial losses through oracle attacks on lending, derivatives, or other DeFi protocols.
The vulnerability manifests in the following code path:
An attacker can exploit this by executing the following attack flow in a single transaction:
Obtain a flash loan
Execute a large swap to manipulate the pool's current price
Take advantage of protocols using the vulnerable price reading
Profit from the manipulation
Repay the flash loan
Recommended mitigation steps
The vulnerability should be addressed through a comprehensive overhaul of the price reading mechanism. Instead of allowing direct slot0 access, the library should implement a robust oracle system that ensures price reliability.
The fix involves modifying the getSqrtTwapX96 function to enforce TWAP usage in all scenarios. This can be achieved by removing the twapInterval=0 case entirely and implementing a minimum TWAP interval requirement:
For cases requiring more recent price data, implement a separate function that combines TWAP with additional safety checks:
function getRecentPrice(addressuniswapV3Pool)
internalviewreturns (uint160sqrtPriceX96)
{
// Get current price
(uint160currentPrice, , , , , , ) =IUniswapV3Pool(uniswapV3Pool).slot0();
// Get short TWAP for comparisonuint160 twapPrice =getSqrtTwapX96(uniswapV3Pool, SHORT_TWAP_PERIOD);
// Ensure current price hasn't deviated significantly from TWAPrequire(
calculateDeviation(currentPrice, twapPrice) <= MAX_DEVIATION,
"Price deviation too high"
);
return currentPrice;
}
This solution provides strong protection against price manipulation while maintaining the ability to access recent price data when needed. The implementation should be accompanied by thorough testing across various market conditions and proper documentation of the security assumptions and limitations.
The text was updated successfully, but these errors were encountered:
Clever Mocha Pheasant
Medium
Unprotected Price Oracle Reading Enables Single-Block Price Manipulation
Summary
The UniswapPricingLibrary contains a critical vulnerability in its price fetching mechanism where it allows direct access to the current pool price through slot0 when twapInterval is set to zero. The core issue lies in the getSqrtTwapX96 function which bypasses Uniswap V3's time-weighted average price (TWAP) protection when twapInterval equals zero, making it susceptible to flash loan attacks and price manipulation within a single block.
This vulnerability stems from the assumption that instantaneous price readings from slot0 are acceptable alternatives to TWAP. However, in the context of Uniswap V3's architecture, slot0 prices can be manipulated through large swaps within a single transaction, especially using flash loans. The impact is severe because any protocol relying on this library for price feeds could be exploited through price manipulation, potentially leading to significant financial losses through oracle attacks on lending, derivatives, or other DeFi protocols.
The vulnerability manifests in the following code path:
https://github.com/sherlock-audit/2024-11-teller-finance-update/blob/main/teller-protocol-v2-audit-2024/packages/contracts/contracts/libraries/UniswapPricingLibrary.sol#L105
An attacker can exploit this by executing the following attack flow in a single transaction:
Recommended mitigation steps
The vulnerability should be addressed through a comprehensive overhaul of the price reading mechanism. Instead of allowing direct slot0 access, the library should implement a robust oracle system that ensures price reliability.
The fix involves modifying the getSqrtTwapX96 function to enforce TWAP usage in all scenarios. This can be achieved by removing the twapInterval=0 case entirely and implementing a minimum TWAP interval requirement:
For cases requiring more recent price data, implement a separate function that combines TWAP with additional safety checks:
This solution provides strong protection against price manipulation while maintaining the ability to access recent price data when needed. The implementation should be accompanied by thorough testing across various market conditions and proper documentation of the security assumptions and limitations.
The text was updated successfully, but these errors were encountered: