`PerpDepository._rebalanceNegativePnlWithSwap()` shouldn't use a `sqrtPriceLimitX96` twice
The function `_rebalanceNegativePnlWithSwap()` uses the same `sqrtPriceLimitX96` parameter for two different Uniswap V3 pools, which may cause the transaction to revert even when it should succeed.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-uxd-judging/issues/425
Found by
hansfriese
Summary
PerpDepository._rebalanceNegativePnlWithSwap() shouldn't use a sqrtPriceLimitX96 twice.
Vulnerability Detail
Currently, _rebalanceNegativePnlWithSwap() uses a sqrtPriceLimitX96 param twice for placing a perp order and swapping.
function _rebalanceNegativePnlWithSwap(
uint256 amount,
uint256 amountOutMinimum,
uint160 sqrtPriceLimitX96,
uint24 swapPoolFee,
address account
) private returns (uint256, uint256) {
uint256 normalizedAmount = amount.fromDecimalToDecimal(
ERC20(quoteToken).decimals(),
18
);
_checkNegativePnl(normalizedAmount);
bool isShort = false;
bool amountIsInput = true;
(uint256 baseAmount, uint256 quoteAmount) = _placePerpOrder(
normalizedAmount,
isShort,
amountIsInput,
sqrtPriceLimitX96
);
vault.withdraw(assetToken, baseAmount);
SwapParams memory params = SwapParams({
tokenIn: assetToken,
tokenOut: quoteToken,
amountIn: baseAmount,
amountOutMinimum: amountOutMinimum,
sqrtPriceLimitX96: sqrtPriceLimitX96, //@audit
poolFee: swapPoolFee
});
uint256 quoteAmountOut = spotSwapper.swapExactInput(params);
In _placePerpOrder(), it uses the uniswap pool inside the perp protocol and uses a spotSwapper for the second swap which is for the uniswap as well.
But as we can see here, Uniswap V3 introduces multiple pools for each token pair and 2 pools might be different and I think it's not good to use the same sqrtPriceLimitX96 for different pools.
Also, I think it's not mandatory to check a sqrtPriceLimitX96 as it checks amountOutMinimum already. (It checks amountOutMinimum only in _openLong() and _openShort().)
Impact
PerpDepository._rebalanceNegativePnlWithSwap() might revert when it should work as it uses the same sqrtPriceLimitX96 for different pools.
Code Snippet
Tool used
Manual Review
Recommendation
I think we can use the sqrtPriceLimitX96 param for one pool only and it would be enough as there is an amountOutMinimum condition.
Discussion
WarTech9
Fix: https://github.com/UXDProtocol/uxd-evm/pull/24
IAm0x52
Fix looks good. sqrtPriceLimitX96 is now only applied to the second swap
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 same `sqrtPriceLimitX96` value is passed to two separate Uniswap V3 pools that may have different price ranges.
Impact
The function might revert when it should work, potentially blocking legitimate rebalancing operations.
What the attacker needed
None
The fix
The fix was to apply `sqrtPriceLimitX96` only to the second swap, as confirmed in the discussion.
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.
This is a smart contract bug where a price limit meant for one trading pool is reused for another pool. Since different pools can have different prices, the limit might be invalid for the second pool, causing the transaction to fail. The fix is to apply the limit only where it makes sense.
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.