Skip to content
Business logic Audit finding Intermediate

fillOrder executor can be front-run by the order creator by changing order’s limitPrice_e36 , the executor’s assets can be stolen

The order creator could change the order's limit price after the executor had committed to filling it, because the fill calculation trusted the current stored limit price. By front-running the fill with an update, the creator could inflate the amount owed to them and take tokens from the executor.

said Follow with rvierdiiev and ladboy233

Originally disclosed on Code4rena

Submitted by said, also found by rvierdiiev and ladboy233

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

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

limitPrice_e36 acts as slippage protection for the order creator, depending on the trade/order position (whether long or short). A higher or lower limit price impacts the tokenOut amount that needs to be transferred to the order’s creator. However, when the executor executes fillOrder, it can be front-run by the order creator to update limitPrice_e36 and steal tokens from the executor.

Proof of Concept

When fillOrder is executed, it will calculate the amtOut that needs to be transferred to order.recipient by calling _calculateFillOrderInfo.

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

    function _calculateFillOrderInfo(Order memory _order, MarginPos memory _marginPos, address _collToken)
        internal
        returns (uint amtOut, uint repayShares, uint repayAmt)
    {
        (repayShares, repayAmt) = _calculateRepaySize(_order, _marginPos);
        uint collTokenAmt = ILendingPool(_marginPos.collPool).toAmtCurrent(_order.collAmt);
        // NOTE: all roundings favor the order owner (amtOut)
        if (_collToken == _order.tokenOut) {
            if (_marginPos.isLongBaseAsset) {
                // long eth hold eth
                // (2 * 1500 - 1500) = 1500 / 1500 = 1 eth
                // ((c * limit - borrow) / limit
>>>             amtOut = collTokenAmt - repayAmt * ONE_E36 / _order.limitPrice_e36;
            } else {
                // short eth hold usdc
                // 2000 - 1 * 1500 = 500 usdc
                // (c - borrow * limit)
>>>             amtOut = collTokenAmt - (repayAmt * _order.limitPrice_e36 / ONE_E36);
            }
        } else {
            if (_marginPos.isLongBaseAsset) {
                // long eth hold usdc
                // (2 * 1500 - 1500) = 1500 usdc
                // ((c * limit - borrow)
>>>             amtOut = (collTokenAmt * _order.limitPrice_e36).ceilDiv(ONE_E36) - repayAmt;
            } else {
                // short eth hold eth
                // (3000 - 1 * 1500) / 1500 = 1 eth
                // (c - borrow * limit) / limit
>>>             amtOut = (collTokenAmt * ONE_E36).ceilDiv(_order.limitPrice_e36) - repayAmt;
            }
        }
    }

As can be observed, it heavily relies on limitPrice_e36 to calculate amtOut. A malicious order creator can front-run the execution of fillOrder and steal assets from the executor by changing limitPrice_e36, resulting in a high value of amtOut depending on the order’s position.

Recommended Mitigation Steps

Consider to add limitPrice_e36 check inside the fillOrder if it bigger than min/max provided limit price, revert the operation.

fez-init (INIT) confirmed and commented:

We will change the updateOrder logic to cancel and create a new order instead of modifying the existing order instead.


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 fill logic reads the order's current limit price at execution time instead of a value fixed when the executor agreed to fill it, so the creator can change it in between.

What the attacker needed

Attacker is the order creator; attacker can front-run the fill transaction

The fix

The project said it would change the update logic so an order is cancelled and recreated rather than modified in place.

How it was found

  1. The order creator sets up an order with a limit price.
  2. An executor decides to fill the order.
  3. The creator front-runs the fill by updating the order's limit price.
  4. The fill executes using the new limit price, producing a larger payout to the creator.

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.

An order lets someone trade using borrowed money, and a limit price decides how much the trader gets back. The person filling the order (the executor) trusts that price when working out the payout. The order's owner could quietly change that price just before the fill happened, so the executor paid out more 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.