Rebalancing a negative Perp PnL via a Uniswap V3 token swap is broken due to the lack of token spending allowance
The PerpDepository contract calls a Uniswap V3 swapper to rebalance a negative Perp PnL but never approves the swapper to spend the asset token. As a result, the swap reverts due to insufficient allowance, so the rebalancing path cannot complete.
berndartmueller Follow with cccz, Jeiwan and 7 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-uxd-judging/issues/339
Found by
berndartmueller, cccz, Jeiwan, rvierdiiev, 0x52, GimelSec, koxuan, jprod15, CRYP70, Bahurum
Summary
The ISwapper spotSwapper (i.e., Uniswapper) helper contract, used by the PerpDepository._rebalanceNegativePnlWithSwap function to perform the actual Uniswap V3 token swap, is missing the required assetToken spending allowance due to a lack of calling the assetToken.approve function.
Vulnerability Detail
Rebalancing a negative Perp PnL with the PerpDepository.rebalance function calls the _rebalanceNegativePnlWithSwap function, which performs a Uniswap swap. However, the required assetToken spending allowance for the ISwapper spotSwapper (i.e. Uniswapper) helper contract is missing. This leads to a revert due to insufficient allowance.
Impact
Rebalancing a negative Perp PnL via a Uniswap swap is missing the token approval and leads to a revert.
Code Snippet
integrations/perp/PerpDepository.sol#L507
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,
poolFee: swapPoolFee
});
uint256 quoteAmountOut = spotSwapper.swapExactInput(params); // @audit-info missing token approval
// [...]
}
Tool used
Manual Review
Recommendation
Consider adding the appropriate token approval before the swap in L507.
Discussion
hrishibhat
Fix: https://github.com/UXDProtocol/uxd-evm/pull/22
IAm0x52
Fix looks good. SpotSwapper is now approved for AssetToken
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 calls the swapper without first calling approve on the asset token, so the swapper has no allowance to pull the tokens.
Impact
Rebalancing a negative Perp PnL through a Uniswap swap reverts because the token approval is absent, so that operation cannot succeed.
What the attacker needed
Negative Perp PnL to rebalance
The fix
The issue was fixed by approving the SpotSwapper for the AssetToken before the swap.
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 smart contract can only move someone else's tokens if that owner has first given it permission, called an allowance. Here the code asks a helper contract to swap tokens but never grants that permission, so the swap fails and the whole rebalancing transaction is undone.
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.