MinShares Slippage Parameters Are Ineffective For Initial Deposit
The initial liquidity share calculation used x + y instead of the geometric mean, making the minShares slippage parameter ineffective for the first deposit. An attacker could front-run the first depositor to inflate shares and manipulate the exchange rate, causing the victim to lose most of their tokens.
Banditx0x Follow with 0xMango and jasonxiale
Originally disclosed on Code4rena
Submitted by Banditx0x, also found by 0xMango and jasonxiale
The first depositor in the AMM can lose the majority of their tokens to a frontrunning attack even if they set the correct minShares slippage parameters.
Proof of Concept
Summary:
By using $x + y$ instead of $sqrt(x * y)$ as the formula for determining the initial liquidity shares, minshares is no longer a effective slippage parameter for the first deposit as front running can change the shares minted even when the token ratio is the same.
Explanation:
Salty is a Uniswap style pool which maintains an $x * y = k$ invaraint. However, the equation determining the initial liquidity amount is different from Uniswap v2
Here is Uniswap’s:
$$ L = sqrt(x * y) $$
Here is Salty’s (implemented in the _addLiquidity function):
$$ L = (x + y) $$
Here is a quote from Uniswap’s whitepaper:
“Uniswap v2 initially mints shares equal to the geometric mean of the amounts, liquidity = sqrt(xy). This formula ensures that the value of a liquidity pool share at any time is essentially independent of the ratio at which liquidity was initially deposited… The above formula ensures that a liquidity pool share will never be worth less than the geometric mean of the reserves in that pool.”
Uniswap’s liquidity formula is path-independent. As long as we know the x and y values in a deposit, we know that the liquidity will conform to the $L = sqrt(x * y)$ formula.
Salty’s formula is not path independent. The number of shares minted for a token deposit is affected by the initial liquidity deposit.
This is the basis for this attack.
The only slippage parameter used for depositing into the AMM is minShares. However, during the first deposit, an attacker can exploit the difference in the initial deposit liquidity equation $x + y$ and the invariant $x * y$ to inflate the number of shares minted per token, and then use the inflated shares to set the AMM at an incorrect reserve ratio such that it can be arbitraged.
First let’s examine a base case, where a user makes an initial deposit with correct minShares parameters:
User deposits 10000 DAI and 10000 USDT at a 1-1 reserve ratio. This is correct as they are both tokens with 18 decimals and value of $1. Let’s calculate the minShares slippage parameters they should set. During the first deposit they are depositing 10000 * 1e18 wei of each token, so 1e22 wei. Liquidity minted is the sum of the 2 amounts (1e22 + 1e22). So liquidity minted = 2e22
Now let’s examine how a frontrunner can inflate the shares so that the slippage parameter is no longer effective.
Frontrunning Case, Step by step:
- Attacker deposits at very imbalanced token ratios, eg 1e2 USDT and 1e18 DAI. This is to make the (x + y) result, or the liquidity shares, inflated relative to the correct deposit ratio.
- This sets
x * y = 1e20 - So now we manipulate the x * y back to be equal
- x = 1e10 and y = 1e10
- 1e18 shares represents only 1e10 and 1e10 tokens
- If the user deposits now, they get far more shares. So the user gets 1e8 (10 million) times more shares per wei of token deposited.
- Depositing 1e22 of each token now yields
1e30shares! In the example without frontrunning, they only minted1e22shares - The example so far involves manipulating the AMM back to the “correct” reserve ratio to mint >1 million times more shares. However, this won’t make any profit to the attacker yet. The attacker can use the “extra shares” to make the victims transaction go through even if they set “correct” minShares parameters based on the non-frontrunned case. So therefore, they manipulate the exchange-rate where 1 DAI = 5 USDT, and then even though less shares are minted, the extra shares minted from the attacker’s share inflation ensures the shares minted does not drop below the
minSharesparameter.
Recommended Mitigation Steps
Consider using minAmount0 and minAmount1 deposited into the AMM. This is how Uniswap V3 implements their slippage parameters.
Alternatively, using the same liquidity formula as Uniswap v2 - $L = sqrt(x * y)$ will prevent this attack.
othernet-global (Salty.IO) confirmed and commented:
minAddedAmountA and minAddedAmountB are now used.
Fixed in: https://github.com/othernet-global/salty-io/commit/0bb763cc67e6a30a97d8b157f7e5954692b3dd68
Picodes (Judge) decreased severity to Medium
Note: For full discussion, see here.
Status: Mitigation confirmed. Full details in reports from 0xpiken, zzebra83, and t0x1c.
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 use of a path-dependent liquidity formula (x + y) instead of the path-independent geometric mean (sqrt(x*y)), which allows an attacker to manipulate the share price before the first deposit.
What the attacker needed
Attacker can front-run the first deposit; victim makes the first deposit
The fix
The project fixed it by using minAddedAmountA and minAddedAmountB as slippage parameters, and the fix was confirmed.
How it was found
- Attacker deposits at very imbalanced token ratios to inflate the (x + y) result.
- Attacker manipulates the reserves back to an equal ratio, making each share represent fewer tokens.
- Victim deposits, receiving far more shares per token than normal.
- Attacker manipulates the exchange rate (e.g., 1 DAI = 5 USDT) so that even with fewer shares minted, the victim's minShares is still met.
- Attacker profits by arbitraging the manipulated pool.
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.
When you add money to a liquidity pool, you get shares representing your portion. The pool should give you shares based on a formula that doesn't depend on the order of deposits. Here, the formula was different, so an attacker could deposit first in a weird way, then manipulate the pool so that when you deposit, you get far more shares than you should, but the attacker can then trade to take your 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.