Orders with single-sided deposits that are auto-adjusted, always revert
The deposit execution code compares the pool's long and short token amounts with the wrong inequality, so the subtraction of the two unsigned values underflows and reverts. As a result, single-sided deposits that trigger an automatic adjustment to reduce price impact always fail.
IllIllI Follow with berndartmueller, drdr and 5 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/165
Found by
IllIllI, berndartmueller, drdr, float-audits, hack3r-0m, joestakey, kirk-baird, koxuan
Summary
Deposits of only the long collateral token, when doing so results in an auto-adjustment of the order to minimize price impact, results in the order always reverting.
Vulnerability Detail
The code that determines which order to subtract the two amounts in order to get a positive difference value, has the wrong equality condition, which means the difference operation reverts due to underflow.
Impact
Single sided deposits as a feature are completely broken when the deposit doesn't solely push the swap impact towards a lower value.
Code Snippet
Since the variables are uint256s, if poolLongTokenAmount is less than poolShortTokenAmount, subtracting the latter from the former will always revert. The else condition has the same issue:
// File: gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol : ExecuteDepositUtils.getAdjustedLongAndShortTokenAmounts() #1
392 @> if (poolLongTokenAmount < poolShortTokenAmount) {
393 @> uint256 diff = poolLongTokenAmount - poolShortTokenAmount;
394
395 if (diff < poolLongTokenAmount) {
396 adjustedLongTokenAmount = diff + (longTokenAmount - diff) / 2;
397 adjustedShortTokenAmount = longTokenAmount - adjustedLongTokenAmount;
398 } else {
399 adjustedLongTokenAmount = longTokenAmount;
400 }
401 } else {
402:@> uint256 diff = poolShortTokenAmount - poolLongTokenAmount;
Tool used
Manual Review
Recommendation
diff --git a/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol b/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol
index 03467e4..915c7fe 100644
--- a/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol
+++ b/gmx-synthetics/contracts/deposit/ExecuteDepositUtils.sol
@@ -389,7 +389,7 @@ library ExecuteDepositUtils {
uint256 adjustedLongTokenAmount;
uint256 adjustedShortTokenAmount;
- if (poolLongTokenAmount < poolShortTokenAmount) {
+ if (poolLongTokenAmount > poolShortTokenAmount) {
uint256 diff = poolLongTokenAmount - poolShortTokenAmount;
if (diff < poolLongTokenAmount) {
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
The branch condition compares the pool long and short token amounts with the wrong inequality, so the unsigned subtraction underflows and reverts.
Impact
Single-sided deposits are completely broken whenever the deposit does not only push the swap impact towards a lower value.
What the attacker needed
Single-sided deposit; deposit triggers auto-adjustment
The fix
The comparison was fixed by reversing the inequality so the larger amount is subtracted from the smaller one.
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 smart contract stores token amounts as unsigned numbers, which cannot go below zero. The code picks which amount to subtract from which by comparing the two, but it uses the wrong comparison, so it sometimes subtracts a bigger number from a smaller one. That makes the transaction fail, so users cannot complete this kind of deposit.
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.