Function hedgePositions is incorrect, because it missed the queuedPerpSize in the calculation
The hedgePositions function calculates the new position without including the queuedPerpSize, which is the total pending size delta. This causes the hedging to be incorrect and the LiquidityPool could lose funds unexpectedly.
Originally disclosed on Code4rena
Submitted by KIntern_NA
Function hedgePositions is incorrect, leads to the hedging will not work as expected, and LiquidityPool can lose funds without expectation.
Proof of concept
Let’s see function hedgePositions in LiquidtyPool contract:
function hedgePositions() external override requiresAuth nonReentrant {
int256 currentPosition = _getTotalPerpPosition();
int256 skew = _getSkew();
uint256 delta = _getDelta();
int256 requiredPosition = wadMul(skew, int256(delta));
int256 newPosition = requiredPosition - currentPosition;
int256 marginDelta = int256(_calculateMargin(newPosition));
if (requiredPosition.abs() < currentPosition.abs()) {
marginDelta = -marginDelta;
}
usedFunds += marginDelta;
perpMarket.transferMargin(marginDelta);
_placeDelayedOrder(newPosition, false);
emit HedgePositions(currentPosition, requiredPosition, marginDelta);
}
currentPosition is the sum of: the current position size of LiquidityPool in Synthetix and the delta size of the current delayed order which was submitted into Synthetix perp market.
function _getTotalPerpPosition() internal view returns (int256 positionSize) {
IPerpsV2MarketBaseTypes.Position memory position = perpMarket.positions(address(this));
IPerpsV2MarketBaseTypes.DelayedOrder memory delayedOrder = perpMarket.delayedOrders(address(this));
positionSize = position.size + delayedOrder.sizeDelta;
}
However, currentPosition missed the variable queuedPerpSize, is the total amount of pending size delta (waiting to be submitted).
Then _placeDelayedOrder will be called with the wrong newPosition, leads to the position size of pool can get a large deviation. The hedging will not be safe anymore.
Scenario:
_getTotalPerpPosition= 0,requiredPosition= 1000,queuedPerpSize= 1000newPositionis calculated incorrectly to be 1000 (since it missedqueuedPerpSize)- It calls
_placeDelayedOrder(1000, false), thenqueuedPerpSizeincrease to be 2000 - After executing all delayed orders, position size of LiquidityPool = 2000 (incorrect hedging)
newPositionshould be -1000 in this case
Recommended Mitigation Steps
currentPosition should be _getTotalPerpPosition() + queuedPerpSize in function hedgePositions.
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 calculation of currentPosition omits the queuedPerpSize, which represents pending size delta not yet submitted.
Impact
The hedging will not work as expected, and the LiquidityPool can lose funds without expectation.
What the attacker needed
QueuedPerpSize > 0
The fix
The currentPosition should be calculated as _getTotalPerpPosition() plus queuedPerpSize in the hedgePositions function.
How it was found
- Call hedgePositions when there is a queuedPerpSize.
- The function computes newPosition without including queuedPerpSize.
- It places a delayed order with the incorrect newPosition.
- The queuedPerpSize increases further, leading to an incorrect final position.
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 tries to keep its overall market exposure balanced by placing orders. The function that calculates how much to adjust ignores orders that are already waiting to be processed. As a result, it places too large an order and the pool ends up with an unintended position.
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.