No slippage protection during adding liquidity to uniswap
The contract calls Uniswap's addLiquidity with zero minimum amounts for both tokens, so the transaction reverts if the pool price moves even slightly before execution. An attacker could manipulate the pool price just before the transaction to cause it to fail, denying the liquidity addition.
EPSec Follow with BestBugBusters, chupinexx and 7 others
Originally disclosed on Code4rena
Submitted by EPSec, also found by BestBugBusters, chupinexx, felconsec, gmh5225, Olugbenga, PotEater, pro_king, unique, and wahedtalash77
Finding description and impact
In the _executeApplication in AgentFactoryV4, during the process of adding liquidity using Uniswap’s IUniswapV2Router02.addLiquidity method, slippage tolerance is set to 0 for both token and assetToken. Specifically, in the following line:
IUniswapV2Router02(_uniswapRouter).addLiquidity(
token,
assetToken,
IERC20(token).balanceOf(address(this)),
initialAmount,
0, // slippage tolerance for token
0, // slippage tolerance for assetToken
address(this),
block.timestamp
);
Here, the 0 values for slippage tolerance mean that the liquidity addition will fail if the price of either token or assetToken fluctuates even slightly between when the transaction is initiated and when it’s executed. This is because Uniswap requires that the amount of tokens being swapped remains within a certain tolerance range to prevent slippage.
Recommended mitigation steps
To mitigate this issue, the following steps are recommended:
Introduce slippage protection: Modify the addLiquidity call to include a non-zero slippage tolerance. This ensures that the liquidity addition will proceed within a reasonable range of price variation.
uint256 slippageTolerance = 50; // 0.5% slippage tolerance (adjustable)
uint256 tokenAmountMin = (IERC20(token).balanceOf(address(this)) * (100 - slippageTolerance)) / 100;
uint256 assetAmountMin = (initialAmount * (100 - slippageTolerance)) / 100;
IUniswapV2Router02(_uniswapRouter).addLiquidity(
token,
assetToken,
IERC20(token).balanceOf(address(this)),
initialAmount,
tokenAmountMin,
assetAmountMin,
address(this),
block.timestamp
);
slippageTolerance: The percentage tolerance for slippage. This should be adjusted based on the application’s tolerance for price fluctuation (e.g., 0.5% or 1%).tokenAmountMinandassetAmountMin: These variables calculate the minimum acceptable amounts oftokenandassetTokenthat will be added to the liquidity pool, accounting for slippage.
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 addLiquidity call passes zero for both minimum-amount parameters, providing no slippage protection.
Impact
Liquidity additions could fail whenever the pool price moves, and an attacker could deliberately cause this failure by manipulating the price before the transaction.
What the attacker needed
Attacker can move pool price before tx; liquidity addition transaction is pending
The fix
The addLiquidity call should be changed to pass non-zero minimum amounts calculated from a chosen slippage tolerance.
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 trading pool, you tell the exchange the least you will accept back. Here the code says the least is zero, so any small price change makes the whole transaction fail. Someone who can move the price at the right moment can make it fail on purpose.
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.