Price limit is used as the price range in internal swaps, causing swap TXs to revert
The contract incorrectly used the user-supplied price limit as the price target in internal swap calculations, leading to wrong token amounts and reverting swap transactions. This could cause a denial of service for swaps.
Source: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/402
Found by
0xAlix2
Summary
When initializing a swap on Uniswap V4, the user inputs a price limit that represents the sqrt price at which, if reached, the swap will stop executing, from Uni V4 code.
struct SwapParams {
/// Whether to swap token0 for token1 or vice versa
bool zeroForOne;
/// The desired input amount if negative (exactIn), or the desired output amount if positive (exactOut)
int256 amountSpecified;
/// The sqrt price at which, if reached, the swap will stop executing
uint160 sqrtPriceLimitX96;
}
When a swap happens in a Uniswap pool, in Pool::swap the swap calculation happens by calling SwapMath.computeSwapStep, where the input price limit is translated to a price target using SwapMath.getSqrtPriceTarget. Knowing that the price target represents the "The price target for the next swap step"
https://github.com/Uniswap/v4-core/blob/main/src/libraries/SwapMath.sol#L19
On the other hand, when the internal swap is done in the Uniswap implementation, SwapMath.computeSwapStep is called while passing the price limit as the price target.
(, ethIn, tokenOut, ) = SwapMath.computeSwapStep({
sqrtPriceCurrentX96: sqrtPriceX96,
sqrtPriceTargetX96: params.sqrtPriceLimitX96,
liquidity: poolManager.getLiquidity(poolId),
amountRemaining: int(amountSpecified),
feePips: 0
});
This affects the in/out token calculation, as it will calculate those values based on a wrong target, forcing swap TXs to unexpectedly revert.
Root Cause
When calculating internal swaps, the input price limit is used as the price range for the swap calculation, here and here.
Impact
Swap transactions will revert in most cases; DOSing swaps.
PoC
Add the following test in flayer/test/UniswapImplementation.t.sol:
function test_WrongPriceTargetUsed() public withLiquidity withTokens {
bool flipped = false;
PoolKey memory poolKey = _poolKey(flipped);
CollectionToken token = flipped ? flippedToken : unflippedToken;
ERC721Mock nft = flipped ? flippedErc : unflippedErc;
uint256 fees = 10 ether;
deal(address(token), address(this), fees);
token.approve(address(uniswapImplementation), type(uint).max);
uniswapImplementation.depositFees(address(nft), 0, fees);
// token0 = WETH, token1 = token
assertEq(address(Currency.unwrap(poolKey.currency0)), address(WETH));
assertEq(address(Currency.unwrap(poolKey.currency1)), address(token));
// amount of token out to receive
uint amountSpecified = 15 ether;
// Uniswap implementation + pool manager have enough tokens to fulfill the swap
assertGt(
token.balanceOf(address(uniswapImplementation)) +
token.balanceOf(address(uniswapImplementation.poolManager())),
amountSpecified
);
// This contract has more than enough WETH to fulfill the swap
assertEq(WETH.balanceOf(address(this)), 1000 ether);
// Swap WETH -> TOKEN
vm.expectRevert();
poolSwap.swap(
poolKey,
IPoolManager.SwapParams({
zeroForOne: true,
amountSpecified: int(amountSpecified),
sqrtPriceLimitX96: TickMath.MIN_SQRT_PRICE + 1
}),
PoolSwapTest.TestSettings({
takeClaims: false,
settleUsingBurn: false
}),
""
);
}
Mitigation
In UniswapImplementation::beforeSwap, whenever the internal swap is being computed, i.e. by calling SwapMath.computeSwapStep, translate the passed price limit to price range, using SwapMath.getSqrtPriceTarget, by doing something similar to:
(, ethIn, tokenOut, ) = SwapMath.computeSwapStep({
sqrtPriceCurrentX96: sqrtPriceX96,
sqrtPriceTargetX96: SwapMath.getSqrtPriceTarget(zeroForOne, step.sqrtPriceNextX96, params.sqrtPriceLimitX96),
liquidity: poolManager.getLiquidity(poolId),
amountRemaining: int(amountSpecified),
feePips: 0
});
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 internal swap calculation passed the price limit directly as the price target instead of converting it using the proper function.
Impact
Swap transactions will revert in most cases, causing a denial of service for swaps.
What the attacker needed
A pool with liquidity; a user initiates a swap
The fix
The internal swap calculation should convert the price limit to a price target using SwapMath.getSqrtPriceTarget before calling computeSwapStep.
How it was found
- Set up a pool with liquidity and tokens.
- Deposit fees to the Uniswap implementation.
- Call the swap function with a price limit.
- Observe that the transaction reverts.
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.
In a swap, a price limit tells the system when to stop trading. The code mistakenly treated that limit as the target price for each step, so it calculated the wrong amounts and the swap failed. This meant users often couldn't swap at all.
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.