PerpDespository#reblance and rebalanceLite can be called to drain funds from anyone who has approved PerpDepository
The rebalance and rebalanceLite functions allow any caller to specify which account pays the shortfall, so an attacker can force a user who has approved the contract to pay for a manipulated swap. This lets the attacker steal the entire allowance of any user who has approved the contract.
kankodu Follow with berndartmueller, unforgiven and 17 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-uxd-judging/issues/288
Found by
kankodu, berndartmueller, unforgiven, yixxas, clems4ever, ck, HollaDieWaldfee, carrot, Zarf, chiranz, dipp, 0xNazgul, 0x52, GimelSec, ctf_sec, Ruhum, DecorativePineapple, hl_, koxuan, Bahurum
Summary
PerpDespository#reblance and rebalanceLite allows anyone to specify the account that pays the quote token. These functions allow a malicious user to abuse any allowance provided to PerpDirectory. rebalance is the worst of the two because the malicious user could sandwich attack the rebalance to steal all the funds and force the unsuspecting user to pay the shortfall.
Vulnerability Detail
function rebalance(
uint256 amount,
uint256 amountOutMinimum,
uint160 sqrtPriceLimitX96,
uint24 swapPoolFee,
int8 polarity,
address account // @audit user specified payer
) external nonReentrant returns (uint256, uint256) {
if (polarity == -1) {
return
_rebalanceNegativePnlWithSwap(
amount,
amountOutMinimum,
sqrtPriceLimitX96,
swapPoolFee,
account // @audit user address passed directly
);
} else if (polarity == 1) {
// disable rebalancing positive PnL
revert PositivePnlRebalanceDisabled(msg.sender);
// return _rebalancePositivePnlWithSwap(amount, amountOutMinimum, sqrtPriceLimitX96, swapPoolFee, account);
} else {
revert InvalidRebalance(polarity);
}
}
rebalance is an unpermissioned function that allows anyone to call and rebalance the PNL of the depository. It allows the caller to specify the an account that passes directly through to _rebalanceNegativePnlWithSwap
function _rebalanceNegativePnlWithSwap(
uint256 amount,
uint256 amountOutMinimum,
uint160 sqrtPriceLimitX96,
uint24 swapPoolFee,
address account
) private returns (uint256, uint256) {
...
// @audit this uses user supplied swap parameters which can be malicious
SwapParams memory params = SwapParams({
tokenIn: assetToken,
tokenOut: quoteToken,
amountIn: baseAmount,
amountOutMinimum: amountOutMinimum,
sqrtPriceLimitX96: sqrtPriceLimitX96,
poolFee: swapPoolFee
});
uint256 quoteAmountOut = spotSwapper.swapExactInput(params);
int256 shortFall = int256(
quoteAmount.fromDecimalToDecimal(18, ERC20(quoteToken).decimals())
) - int256(quoteAmountOut);
if (shortFall > 0) {
// @audit shortfall is taken from account specified by user
IERC20(quoteToken).transferFrom(
account,
address(this),
uint256(shortFall)
);
} else if (shortFall < 0) {
...
}
vault.deposit(quoteToken, quoteAmount);
emit Rebalanced(baseAmount, quoteAmount, shortFall);
return (baseAmount, quoteAmount);
}
_rebalanceNegativePnlWithSwap uses both user specified swap parameters and takes the shortfall from the account specified by the user. This is where the function can be abused to steal funds from any user that sets an allowance for this contract. A malicious user can sandwich attack the swap and specify malicious swap parameters to allow them to steal the entire rebalance. This creates a large shortfall which will be taken from the account that they specify, effectively stealing the funds from the user.
Example:
Any account that gives the depository allowance can be stolen from. Imagine the following scenario. The multisig is going to rebalance the contract for 15000 USDC worth of ETH and based on current market conditions they are estimating that there will be a 1000 USDC shortfall because of the difference between the perpetual and spot prices (divergences between spot and perpetual price are common in trending markets). They first approve the depository for 1000 USDC. A malicious user sees this approval and immediately submits a transaction of their own. They request to rebalance only 1000 USDC worth of ETH and sandwich attack the swap to steal the rebalance. They specify the multisig as account and force it to pay the 1000 USDC shortfall and burn their entire allowance, stealing the USDC.
Impact
Anyone that gives the depository allowance can easily have their entire allowance stolen
Code Snippet
Tool used
Manual Review
Recommendation
PerpDespository#reblance and rebalanceLite should use msg.sender instead of account:
function rebalance(
uint256 amount,
uint256 amountOutMinimum,
uint160 sqrtPriceLimitX96,
uint24 swapPoolFee,
int8 polarity,
- address account
) external nonReentrant returns (uint256, uint256) {
if (polarity == -1) {
return
_rebalanceNegativePnlWithSwap(
amount,
amountOutMinimum,
sqrtPriceLimitX96,
swapPoolFee,
- account
+ msg.sender
);
} else if (polarity == 1) {
// disable rebalancing positive PnL
revert PositivePnlRebalanceDisabled(msg.sender);
// return _rebalancePositivePnlWithSwap(amount, amountOutMinimum, sqrtPriceLimitX96, swapPoolFee, account);
} else {
revert InvalidRebalance(polarity);
}
}
Discussion
hrishibhat
Fix: https://github.com/UXDProtocol/uxd-evm/pull/20
IAm0x52
Fix looks good. Shortfall is now transferred from msg.sender instead of account
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 rebalance function accepts an arbitrary account parameter and uses it as the payer for the shortfall, instead of using msg.sender.
What the attacker needed
Victim has approved the PerpDepository contract
The fix
The fix was to use msg.sender instead of the account parameter, so the caller pays the shortfall.
How it was found
- A user approves the PerpDepository contract to spend their quote tokens.
- An attacker sees the approval and calls rebalance with a small amount and malicious swap parameters.
- The attacker sets the victim's address as the account parameter.
- The attacker sandwiches the swap to create a large shortfall.
- The contract transfers the shortfall from the victim's account, draining their allowance.
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.
When you approve a contract to spend your tokens, you trust it to only take what you owe. Here, the contract let anyone say 'take the money from this other person' and also let them set up a bad trade that made that person owe a lot. So an attacker could make you pay for their trade and take all the tokens you had approved.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.