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.
Submitted by dimulski, also found by 0xPhantom, Centaur, codertjay, and hgrano
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
markPricefor 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.timestamp10, a malicious user can submit a SELL order with a price of 3_991e18 that expires atblock.timestamp11(the next block). - Now at
block.timestamp11 the protocol admin calls theMarket.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 thegetImpactPriceTwap()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.timestamp11 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.
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
- Note the current mark price and the best buy and sell orders.
- Submit a sell order with a price just below the best buy, expiring in the next block.
- In the next block, the protocol admin calls setMarkPrice, which uses the expired order in its calculations.
- 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.