Skip to content
Business logic Audit finding Intermediate

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.

KIntern_NA Follow

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 = 1000
  • newPosition is calculated incorrectly to be 1000 (since it missed queuedPerpSize)
  • It calls _placeDelayedOrder(1000, false), then queuedPerpSize increase to be 2000
  • After executing all delayed orders, position size of LiquidityPool = 2000 (incorrect hedging)
  • newPosition should be -1000 in this case

Recommended Mitigation Steps

currentPosition should be _getTotalPerpPosition() + queuedPerpSize in function hedgePositions.

mubaris (Polynomial) confirmed


The original report as published on Code4rena, by KIntern_NA. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Call hedgePositions when there is a queuedPerpSize.
  2. The function computes newPosition without including queuedPerpSize.
  3. It places a delayed order with the incorrect newPosition.
  4. 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.

More by KIntern_NA

View all

21 more disclosed reports, best first.

More from Polynomial Protocol contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.