`getAdjustedLongAndShortTokenAmounts()` Incorrectly Calculates Amounts
The function getAdjustedLongAndShortTokenAmounts() uses the wrong variable in its if-statements, comparing diff to poolLongTokenAmount instead of longTokenAmount. This can cause an overflow that reverts the transaction or an unbalanced pool, especially on the first deposit where all funds are attributed to the short pool.
IllIllI Follow with kirk-baird and koxuan
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/72
Found by
IllIllI, kirk-baird, koxuan
Summary
The function getAdjustedLongAndShortTokenAmounts() attempts to balance the long and short pool amounts for a market which has the same long and short token. However, the calculations are incorrect and may result in an unbalanced pool or a reverted transaction.
Vulnerability Detail
There are two equivalent bugs which occur depending on which pool is larger (long vs short). The bug occurs due to incorrect values being used in the if-statements.
The first issue is occurs on line 395 where the diff is compared to poolLongTokenAmount. This is the incorrect value, instead longTokenAmount should be used.
For the case where diff < poolLongTokenAmount
- if
longTokenAmount < diffit will overflow on line 396 - if
longTokenAmount >= diffthis case behaves correctly
For the case where diff >= poolLongTokenAmount
- if
longTokenAmount >= diffthe result is an unbalanced pool since we are attributing too many tokens to the long amount. - if
longTokenAmount < diffthis case behaves correctly
The same issue occurs on line 404 for the case when there is a deficit in short tokens.
Impact
There are two different negative impacts stated in the above depending on the conditions. The first is an overflow that will cause the transaction to revert.
The second impact is more severe as it will cause an unbalanced pool to be created. The impact is the deposit will have a significant price impact. The most noticable case is the initial deposit. The first deposit will will have diff = 0 and poolShortTokenAMount = poolLongTokenAmount = 0. The impact is line 408-409 is executed which attributes all funds to the short pool and none to the long pool.
Code Snippet
Function getAdjustedLongAndShortTokenAmounts()
https://github.com/sherlock-audit/2023-02-gmx/blob/main/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol#L381-L414
First occurence of the bug. https://github.com/sherlock-audit/2023-02-gmx/blob/main/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol#L395
Second occurrence of the bug. https://github.com/sherlock-audit/2023-02-gmx/blob/main/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol#L404
Tool used
Manual Review
Recommendation
The aim of the function is to reduce the difference between the poolLongTokenAmount and poolShortTokenAmount. We have longTokenAmount as the amount of funds contributed by the user. Therefore the correct solution should be to use longTokenAmount in the mentioned if statements.
if (poolLongTokenAmount < poolShortTokenAmount) {
uint256 diff = poolLongTokenAmount - poolShortTokenAmount;
if (diff < longTokenAmount) { //@audit fix
adjustedLongTokenAmount = diff + (longTokenAmount - diff) / 2;
adjustedShortTokenAmount = longTokenAmount - adjustedLongTokenAmount;
} else {
adjustedLongTokenAmount = longTokenAmount;
}
} else {
uint256 diff = poolShortTokenAmount - poolLongTokenAmount;
if (diff < longTokenAmount) { //@audit fix
adjustedShortTokenAmount = diff + (longTokenAmount - diff) / 2;
adjustedLongTokenAmount - longTokenAmount - adjustedShortTokenAmount;
} else {
adjustedLongTokenAmount = 0;
adjustedShortTokenAmount = longTokenAmount;
}
}
Discussion
xvi10
Fix in https://github.com/gmx-io/gmx-synthetics/commit/d4066840f925c43116eed3c2c83ae2e44be39eed
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 if-statements compare the difference between pool amounts to the pool amount itself rather than to the user's deposit amount, leading to incorrect branching.
Impact
This could cause a transaction to revert due to overflow, or create an unbalanced pool with significant price impact, especially on the first deposit where all funds go to the short pool.
What the attacker needed
None
The fix
The fix is to use longTokenAmount instead of poolLongTokenAmount in the if-statements, and the project has fixed it in a commit.
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.
This is a smart contract bug in a function that tries to keep two pools (long and short) balanced when someone deposits tokens. The code checks the wrong number when deciding how to split the deposit, which can either crash the transaction or put all the money into one pool, making the deposit unfairly priced.
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.