Hedging during liquidation is incorrect
During liquidation of a short position, the Exchange contract burns power perp tokens and reduces the short position, but also calls the LiquidityPool's liquidate function, which hedges the pool by the same amount. This causes the LiquidityPool to be over-hedged, leading to an incorrect position and potential loss of funds.
Originally disclosed on Code4rena
Submitted by KIntern_NA
Hedging will not work as expected, and LiquidityPool will lose funds without expectation.
Proof of concept
- When a short position is liquidated in contract Exchange, function
_liquidatewill be triggered. It will burn the power perp tokens and reduce the short position amount accordingly.
function _liquidate(uint256 positionId, uint256 debtRepaying) internal {
...
uint256 finalPosition = position.shortAmount - debtRepaying;
uint256 finalCollateralAmount = position.collateralAmount - totalCollateralReturned;
shortToken.adjustPosition(positionId, user, position.collateral, finalPosition, finalCollateralAmount);
pool.liquidate(debtRepaying);
powerPerp.burn(msg.sender, debtRepaying);
...
- As you can see, it will decrease the size of short position by
debtRepaying, and burndebtRepayingpower perp tokens. Because of the same amount, the skew ofLiquidityPoolwill not change. - Howerver,
pool.liquidatewill be called, andLiquidityPoolwill be hedged withdebtRepayingamount.
function liquidate(uint256 amount) external override onlyExchange nonReentrant {
(uint256 markPrice, bool isInvalid) = getMarkPrice();
require(!isInvalid);
uint256 hedgingFees = _hedge(int256(amount), true);
usedFunds += int256(hedgingFees);
emit Liquidate(markPrice, amount);
}
- Therefore, LiquidityPool will be hedged more than it needs, and the position of
LiquidityPoolin the Perp Market will be incorrect (compared with what it should be for hedging).
Recommended Mitigation Steps
Should not hedge the LiquidityPool during liquidation.
mubaris (Polynomial) confirmed
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 liquidation function in the Exchange contract calls the LiquidityPool's liquidate function, which hedges the pool, even though the short position is reduced by the same amount, so the pool's hedge becomes misaligned.
Impact
The LiquidityPool could lose funds because it is over-hedged, leading to an incorrect position in the Perp Market.
What the attacker needed
A short position exists; the short position becomes liquidatable
The fix
The LiquidityPool should not hedge during liquidation.
How it was found
- A short position is liquidated in the Exchange contract, triggering the _liquidate function.
- The _liquidate function reduces the short position by debtRepaying and burns debtRepaying power perp tokens.
- The _liquidate function calls pool.liquidate(debtRepaying), which hedges the LiquidityPool by debtRepaying amount.
- As a result, the LiquidityPool is hedged more than necessary, making its position in the Perp Market incorrect.
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 liquidity pool is supposed to hedge its risk by taking an opposite position in a market. When someone's short position is liquidated, the pool also adjusts its hedge, but it does so by the same amount as the liquidation, even though the short position was already reduced. This double-counting makes the pool's hedge too large, which can lose money for the pool.
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.