Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A market's hourly average price can be biased by a large number of trades #144

Closed
code423n4 opened this issue Jul 2, 2021 · 1 comment
Closed
Labels
3 (High Risk) bug Something isn't working duplicate This issue or pull request already exists

Comments

@code423n4
Copy link
Contributor

Handle

shw

Vulnerability details

Impact

An attacker can artificially move a market's hourly average price (i.e., the result of getHourlyAvgTracerPrice) by executing a large number of trades on the market with only paying gas fees.

Proof of Concept

The hourly average price is calculated by the cumulativePrice divided by the number of trades in the given hour (i.e., the average of all trade prices). Therefore, an attacker can bias this average price by executing a large number of trades (i.e., calling executeTrade on Trader with many matched orders), and all of them have an extremely high (or low) trade price, as long as the maker and taker's positions are valid after the trade.

The attacker only pays the gas fees without losing the assets since the makers and takers are all his accounts. Besides, the attacker can avoid paying the trade fees if the fillAmount of trade is 0.

Referenced code:
Trader.sol#L121-L126
TracerPerpetualSwaps.sol#L280
Pricing.sol#L100
Pricing.sol#L126-L129
Pricing.sol#L254-L256
LibPrices.sol#L41-L49

Recommended Mitigation Steps

This attack is generally difficult to prevent since anyone can execute trades and match orders generated by him. A possible mitigation is to modify the hourly average price formula: increase the cumulativePrice by the trade price multiply the fill amount of each trade. As a result, the attacker has to increase the trade volume to move the average price effectively, and thus the charged trade fees are increased for launching such attacks.

@code423n4 code423n4 added 2 (Med Risk) bug Something isn't working labels Jul 2, 2021
code423n4 added a commit that referenced this issue Jul 2, 2021
@raymogg
Copy link
Collaborator

raymogg commented Jul 5, 2021

Duplicate of issue #119. Worded slightly differently but it comes down to the fact that

  • volume isn't taken into account when computing average price
  • arbitrary execution of orders is possible through the trader

@raymogg raymogg closed this as completed Jul 5, 2021
@raymogg raymogg added the duplicate This issue or pull request already exists label Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 (High Risk) bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants