Skip to content
Business logic Audit finding Intermediate

The protocol doesn’t check whether the orders have expired when the mid and impact price are calculated

The protocol calculates the mark price using orders that may have expired, allowing a malicious user to submit short-lived orders that manipulate the mid and impact prices. This can distort the mark price, affecting funding rates and potentially causing unfair liquidations.

dimulski Follow with 0xPhantom, Centaur and 2 others

Originally disclosed on Code4rena

Submitted by dimulski, also found by 0xPhantom, Centaur, codertjay, and hgrano

  • perps/types/Market.sol #L437
  • perps/types/Market.sol #L421

When the markPrice is calculated, the protocol doesn’t check whether the orders from which it takes the price for the mid price and the impact price have expired. This allows a malicious user to submit orders that expire in the next block and manipulate the markPrice. The utilization of twap prices and basis spread EMA will mitigate part of the price discrepancy. However, a malicious user may execute the below described attack every block and thus successfully manipulate the markPrice despite the utilization of twapPrices and the EMA indicator. The PriceHistory::snapshot() function checks whether the last snapshot was taken in the same block.timestamp and if so, only updates the price of the last PriceSnapshot struct, otherwise a new PriceSnapshot struct is created and pushed into the snapshots array. Whether the Market.setMarkPrice() function is called every second or every 5 - 10 seconds doesn’t make much of a difference for this attack path, as a malicious user will have to submit orders that expire in the next block and cancel them in the next block.

  • Consider the current markPrice for the ETH market is 4000e18, the best BUY order has a price of 3990e18 and the best SELL order has a price of 4_010e18, both have an amount of 1e18, and are not expired.
  • If we are at block.timestamp 10, a malicious user can submit a SELL order with a price of 3_991e18 that expires at block.timestamp 11(the next block).
  • Now at block.timestamp 11 the protocol admin calls the Market.setMarkPrice() function, and the indexPrice is 4_004e18, the price increased by 0.1%.
  • The calculated mid price will be (3990e18 + 3991e18) / 2 = 3990.5e18, the basis spread stored will be 3990.5e18 - 4004e18 = -13.5e18. When it should have been 4000e18 - 4_004e18 = - 4e18.
  • Given both the best ask and best bid orders have an amount of 1e18, the calculated impactBid in the Market::getImpactPrice() function will be ~3991e18 and the calculated impactAsk will be ~3990e18, the impact price will be (3990e18 + 3991e18) / 2 ~ 3990.5e18. When instead it should have been 4000e18.
  • Now if only those values are pushed, and the getBasisSpreadEMA() returns -13.5e18 instead of -4e18 and the getImpactPriceTwap() function returns 3990.5e18 instead of 4000e18, and the provided index price is 4004e18 the mark price will be set to 3990.5e18, as this will be the median price, we can disregard the funding rate for this scenario. Instead the set markPrice should have been much closer to 4_000e18.
  • If the markPrice is already updated, and at block.timestamp 11 the protocol tries to match an incoming bid order with the best sell order with a price of 3_991e18, the best sell order will be removed, and the bid order will be matched with the next best sell order, if the incoming bid order price is bigger or equal that the not expired best ask order price.

If the order from which we took the price will be removed when the protocol tries to match an incoming order with it, shouldn’t be taken into the calculations of the mid price and the impact price, as that price doesn’t actually exist in the CLOB. Keep in mind that orders can just be expired when the Market.setMarkPrice() function is called, it is not necessary that a malicious actor submits such orders every block, manipulating even 10% of the twap entries, and the entries for the EMA indicator may have a detrimental impact on the newly set markPrice. This represents a big problem because when the markPrice is manipulated the funding rate that users should pay or receive will be completely different, additionally users that shouldn’t be liquidated may get liquidated.

Recommended mitigation steps

When calculating the mid price and the impact price, consider checking whether the orders have expired.


The original report as published on Code4rena, by dimulski. 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 mark price calculation includes expired orders because it does not filter them out when determining the best bid and ask.

Impact

The mark price can be manipulated, leading to incorrect funding rates and potentially unfair liquidations, although TWAP and EMA may mitigate some discrepancy.

What the attacker needed

Ability to submit orders; orders expire in next block; mark price update occurs

How it was found

  1. Note the current mark price and the best buy and sell orders.
  2. Submit a sell order with a price just below the best buy, expiring in the next block.
  3. In the next block, the protocol admin calls setMarkPrice, which uses the expired order in its calculations.
  4. The mid price and impact price are skewed, leading to a manipulated mark price.

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.

The mark price is a reference price used by the exchange. The protocol works it out from the best buy and sell orders, but it does not check if those orders are still valid. An attacker can place an order that expires almost immediately to skew the price, then cancel it, causing the mark price to be wrong.

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 dimulski

View all

7 more disclosed reports, best first.

More from GTE Perps and Launchpad

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.