Skip to content
Business logic Audit finding Intermediate

Wrong trading pricing calculations

An agent could manipulate trading prices by creating many tiny orders at arbitrary prices and matching them with another account, which skewed the hourly average price used for funding rates and fair price. This allowed a malicious agent to manipulate the market.

0xsanson Follow

Originally disclosed on Code4rena

Submitted by 0xsanson, also found by shw

In the Pricing contract, an agent can manipulate the trading prices by spamming a high amount of trades.

Indeed an agent can create a high amount of orders at an arbitrary price and with a near-zero amount (so the agent doesn’t even need large funds); next he/she pairs the orders with another account and calls Trader.executeTrade; now every order calls a Pricing.recordTrade using the arbitrary price set by the agent.

Since the trades are all made in the same hour, by the way hourlyTracerPrices[currentHour] is calculated, it skews the average price towards the price set by the agent. This arbitrary value is used to calculate the fundingRates and the fairPrice, allowing a malicious agent the ability to manipulate the market.

Recommend passing the fillAmount parameter to recordTrade(...), and calculate hourlyTracerPrices[currentHour].trades summing fillAmount instead of 1 every trade.

raymogg (Tracer) confirmed:

Issue is valid, and there appear to be a few other issues that reference similar problems.

The Trader contract will have a whitelist allowing only select relayers to push orders on chain. As long as off chain order books have sufficient liquidity, this issue is then mitigated as users can’t just arbitrarily match orders and send them in, they must be matched on a book with liquidity. To alter the price you would then need to eat through significant liquidity (increasing the cost of this attack).

The original report as published on Code4rena, by 0xsanson. 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 hourly average price calculation counted each trade equally regardless of its size, so many tiny trades could skew the average.

Impact

The manipulated average price is used to calculate funding rates and fair price, allowing a malicious agent to manipulate the market; however, a whitelist of relayers and sufficient off-chain liquidity could mitigate the attack by increasing its cost.

What the attacker needed

Ability to create orders; a second account to pair orders; trades within the same hour

The fix

The recommendation is to pass the fill amount to recordTrade and sum fill amounts instead of counting each trade as one.

How it was found

  1. Create many orders at an arbitrary price with a near-zero amount.
  2. Pair the orders with another account and call `Trader.executeTrade`.
  3. Each order calls `Pricing.recordTrade` using the arbitrary price.
  4. Because all trades occur in the same hour, the hourly average price is skewed towards the attacker's 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.

In this trading system, prices are partly set by averaging recent trades. An attacker could make lots of tiny trades at a price they choose, which pulled the average towards their price. That fake average then affected funding rates and the fair price, letting them manipulate the market.

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 0xsanson

View all

8 more disclosed reports, best first.

More from Tracer 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.