Trades in blocks where the bid or ask drops to zero will be priced using the previous block's price
The oracle contract treats a zero secondary price as 'empty' and falls back to the older primary price. An attacker could exit a position at the older price when the newer price is zero, causing a loss to the pool.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/155
Found by
IllIllI
Summary
The oracle prices used for traces allow multiple oracles and their last prices to be provided. The oldest block's price becomes the primary price, and the newer price becomes the secondary price. Trades in blocks where the primary price is non-zero, but the secondary price is zero, will be priced incorrectly
Vulnerability Detail
For position increase/decrease orders, the price used is either the primary or the secondary price, but a value of zero for the secondary price is considered to be a sentinel value indicating 'empty', or 'no price has been set'. In such cases, the secondary price is ignored, and the primary price is used instead.
Impact
Users exiting their positions in the first block where the price touches zero, are able to exit their positions at the primary (older) price rather than the secondary (newer) price of zero. This is pricing difference is at the expense of the pool and the other side of the trade.
Code Snippet
The secondary price is only used when it's non-zero:
// File: gmx-synthetics/contracts/oracle/Oracle.sol : Oracle.getLatestPrice() #1
341 function getLatestPrice(address token) external view returns (Price.Props memory) {
342 if (token == address(0)) { return Price.Props(0, 0); }
343
344 Price.Props memory secondaryPrice = secondaryPrices[token];
345
346 @> if (!secondaryPrice.isEmpty()) {
347 return secondaryPrice;
348 }
349
350 Price.Props memory primaryPrice = primaryPrices[token];
351 if (!primaryPrice.isEmpty()) {
352 return primaryPrice;
353 }
354
355 revert OracleUtils.EmptyLatestPrice(token);
356: }
Note that even if just the bid touches zero, that's enough to disqualify the secondary price.
Tool used
Manual Review
Recommendation
Use an actual sentinel flag rather than overloading the meaning of a 'zero' price.
Discussion
xvi10
oracles should report the minimum possible price instead of zero
IllIllI000
@xvi10 same question as for #156
xvi10
replied in 156
IllIllI000
the readme states that Oracle signers are expected to accurately report the price of tokens, so any misreporting would be a separate bug, and reporting a price of 100 seems much more dangerous than an unidentified bug. As is described above, using a sentinel value would be safer
xvi10
replied in https://github.com/sherlock-audit/2023-02-gmx-judging/issues/156
hack3r-0m
Escalate for 10 USDC
should be dup of https://github.com/sherlock-audit/2023-02-gmx-judging/issues/156 instead of seperate issue
sherlock-admin
Escalate for 10 USDC
should be dup of https://github.com/sherlock-audit/2023-02-gmx-judging/issues/156 instead of seperate issue
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
IllIllI000
looks like I missed this one. The fix for this one does not resolve the other. One is about using the wrong price, and the other is about an unhandled revert case
hrishibhat
Escalation rejected
Although related to the similar topic but Not a duplicate of #156
sherlock-admin
Escalation rejected
Although related to the similar topic but Not a duplicate of #156
This issue's escalations have been rejected!
Watsons who escalated this issue will have their escalation amount deducted from their next payout.
xvi10
no code changed, similar reason to https://github.com/sherlock-audit/2023-02-gmx-judging/issues/156
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
What the attacker needed
Price drops to zero; trade in same block
The fix
The code should use an explicit sentinel flag rather than overloading a zero price to mean 'empty'.
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.
A price oracle provides prices for tokens. This system keeps two prices: an older primary and a newer secondary. If the newer price is zero, the code thinks it is missing and uses the older price instead. That means someone can sell at the old, higher price even though the real price has dropped to zero, and the pool loses money.
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.