Skip to content
Business logic Audit finding Intermediate

Order’s creator can update tokenOut to arbitrary token

The updateOrder function lets an order's creator change tokenOut to any token, unlike order creation which restricts it to the margin position's base or quote asset. An attacker could front-run the executor's fillOrder by swapping in a high-value token, causing the executor to transfer that token instead.

said Follow with ladboy233

Originally disclosed on Code4rena

Submitted by said, also found by ladboy233

When an order is created, it checks whether the order.tokenOut is equal to marginPos.baseAsset or _tokenOut is equal to marginPos.quoteAsset. However, when tokenOut is updated via updateOrder, it can be changed to an arbitrary token. This has the potential to be exploited by changing tokenOut to a high-value token right before the executor executes the fillOrder.

Proof of Concept

It can be observed that order’s creator can update order.tokenOut to arbitrary token.

https://github.com/code-423n4/2024-01-init-capital-invitational/blob/main/contracts/hook/MarginTradingHook.sol#L504-L526

    function updateOrder(
        uint _posId,
        uint _orderId,
        uint _triggerPrice_e36,
        address _tokenOut,
        uint _limitPrice_e36,
        uint _collAmt
    ) external {
        _require(_collAmt != 0, Errors.ZERO_VALUE);
        Order storage order = __orders[_orderId];
        _require(order.status == OrderStatus.Active, Errors.INVALID_INPUT);
        uint initPosId = initPosIds[msg.sender][_posId];
        _require(initPosId != 0, Errors.POSITION_NOT_FOUND);
        MarginPos memory marginPos = __marginPositions[initPosId];
        uint collAmt = IPosManager(POS_MANAGER).getCollAmt(initPosId, marginPos.collPool);
        _require(_collAmt <= collAmt, Errors.INPUT_TOO_HIGH);

        order.triggerPrice_e36 = _triggerPrice_e36;
        order.limitPrice_e36 = _limitPrice_e36;
        order.collAmt = _collAmt;
        order.tokenOut = _tokenOut;
        emit UpdateOrder(initPosId, _orderId, _tokenOut, _triggerPrice_e36, _limitPrice_e36, _collAmt);
    }

This can be exploited by the order’s creator. When the executor is about to execute the fillOrder, the order creator can front-run the operation by changing the order.tokenOut to a token with a high value. Consequently, the executor will transfer the amount that needs to be transferred but using this new high-value token. But this require hook already have approval from the executor to manage the new high value token.

Recommended Mitigation Steps

Validate the new token when updateOrder is called :

    function updateOrder(
        uint _posId,
        uint _orderId,
        uint _triggerPrice_e36,
        address _tokenOut,
        uint _limitPrice_e36,
        uint _collAmt
    ) external {
        _require(_collAmt != 0, Errors.ZERO_VALUE);
        Order storage order = __orders[_orderId];
        _require(order.status == OrderStatus.Active, Errors.INVALID_INPUT);
        uint initPosId = initPosIds[msg.sender][_posId];
        _require(initPosId != 0, Errors.POSITION_NOT_FOUND);
        MarginPos memory marginPos = __marginPositions[initPosId];
        uint collAmt = IPosManager(POS_MANAGER).getCollAmt(initPosId, marginPos.collPool);
        _require(_collAmt <= collAmt, Errors.INPUT_TOO_HIGH);
+       _require(_tokenOut == marginPos.baseAsset || _tokenOut == marginPos.quoteAsset, Errors.INVALID_INPUT);

        order.triggerPrice_e36 = _triggerPrice_e36;
        order.limitPrice_e36 = _limitPrice_e36;
        order.collAmt = _collAmt;
        order.tokenOut = _tokenOut;
        emit UpdateOrder(initPosId, _orderId, _tokenOut, _triggerPrice_e36, _limitPrice_e36, _collAmt);
    }

fez-init (INIT) confirmed

Ladboy233 (Warden) commented:

I think the severity should be high because it is very common the executor tries to fill a lot of order that have different token.

It is common that executor approves the hook to spend multiple tokens.

Order creator frontrun to update order to different token clearly violate order executor’s expectation if order creator update a low value token with high value token before the order is filled.

hansfriese (Judge) increased severity to High and commented:

I left a comment here.


The original report as published on Code4rena, by said. 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

Likely the updateOrder function omits the validation present at order creation that tokenOut must equal the margin position's baseAsset or quoteAsset.

Impact

The executor could transfer a high-value token instead of the intended one, but this requires the hook to already have approval to manage that token.

What the attacker needed

Attacker is the order creator; executor has approved the hook for the target token; attacker can front-run the fillOrder transaction

The fix

The updateOrder function should validate that the new tokenOut equals the margin position's baseAsset or quoteAsset, as the report recommends.

How it was found

  1. Create an order with a valid tokenOut.
  2. Wait until the executor is about to call fillOrder.
  3. Front-run by calling updateOrder to set tokenOut to a high-value token.
  4. The executor's fillOrder then transfers the new high-value token.

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 lets someone who made an order change which token they will receive, and the check that normally limits this to two specific tokens is missing. Because the change can happen just before the order is filled, the person filling the order might pay out a much more valuable token than expected.

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 said

View all

21 more disclosed reports, best first.

More from INIT Capital Invitational

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.