LiquidityProviders.sol The share price of the LP can be manipulated and making future liquidityProviders unable to removeLiquidity()
The share price of the liquidity pool can be manipulated to an extremely low value, causing an overflow in the calculation of token amounts when removing liquidity. This prevents future liquidity providers from withdrawing their funds.
Submitted by WatchPug
LiquidityProviders.sol#L345-L362
function removeLiquidity(uint256 _nftId, uint256 _amount)
external
nonReentrant
onlyValidLpToken(_nftId, _msgSender())
whenNotPaused
{
(address _tokenAddress, uint256 nftSuppliedLiquidity, uint256 totalNFTShares) = lpToken.tokenMetadata(_nftId);
require(_isSupportedToken(_tokenAddress), "ERR__TOKEN_NOT_SUPPORTED");
require(_amount != 0, "ERR__INVALID_AMOUNT");
require(nftSuppliedLiquidity >= _amount, "ERR__INSUFFICIENT_LIQUIDITY");
whiteListPeriodManager.beforeLiquidityRemoval(_msgSender(), _tokenAddress, _amount);
// Claculate how much shares represent input amount
uint256 lpSharesForInputAmount = _amount * getTokenPriceInLPShares(_tokenAddress);
// Calculate rewards accumulated
uint256 eligibleLiquidity = sharesToTokenAmount(totalNFTShares, _tokenAddress);
LiquidityProviders.sol#L192-L194
function sharesToTokenAmount(uint256 _shares, address _tokenAddress) public view returns (uint256) {
return (_shares * totalReserve[_tokenAddress]) / totalSharesMinted[_tokenAddress];
}
The share price of the liquidity can be manipulated to an extremely low value (1 underlying token worth a huge amount of shares), making it possible for sharesToTokenAmount(totalNFTShares, _tokenAddress) to overflow in removeLiquidity() and therefore freeze users’ funds.
Proof of Concept
- Alice
addTokenLiquidity()with1e8 * 1e18XYZ on B-Chain, totalSharesMinted ==1e44; - Alice
sendFundsToUser()and bridge1e8 * 1e18XYZ from B-Chain to A-Chain; - Alice
depositErc20()and bridge1e8 * 1e18XYZ from A-Chain to B-Chain; - Alice
removeLiquidity()and withdraw1e8 * 1e18 - 1XYZ, then:totalReserve==1 weiXYZ, andtotalSharesMinted==1e26; - Bob
addTokenLiquidity()with3.4e7 * 1e18XYZ; - Bob tries to
removeLiquidity().
Expected Results: Bob to get back the deposits;
Actual Results: The tx reverted due to overflow at sharesToTokenAmount().
Recommended Mitigation Steps
LiquidityProviders.sol#L280-L292
function _increaseLiquidity(uint256 _nftId, uint256 _amount) internal onlyValidLpToken(_nftId, _msgSender()) {
(address token, uint256 totalSuppliedLiquidity, uint256 totalShares) = lpToken.tokenMetadata(_nftId);
require(_amount > 0, "ERR__AMOUNT_IS_0");
whiteListPeriodManager.beforeLiquidityAddition(_msgSender(), token, _amount);
uint256 mintedSharesAmount;
// Adding liquidity in the pool for the first time
if (totalReserve[token] == 0) {
mintedSharesAmount = BASE_DIVISOR * _amount;
} else {
mintedSharesAmount = (_amount * totalSharesMinted[token]) / totalReserve[token];
}
...
Consider locking part of the first mint’s liquidity to maintain a minimum amount of totalReserve[token], so that the share price can not be easily manipulated.
ankurdubey521 (Biconomy) confirmed
Great find, with a PoC, deserves a severity of high because it is a valid attack path that does not have hand-wavy hypotheticals.
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 share price can be manipulated because the first mint does not lock a minimum amount of liquidity, allowing an attacker to make totalReserve very small relative to totalSharesMinted, so that later share-to-token calculations overflow.
Impact
Future liquidity providers could be unable to remove their liquidity, effectively freezing their funds.
What the attacker needed
Attacker has enough tokens to manipulate pool; victim adds liquidity after manipulation
How it was found
- Alice adds a large amount of token liquidity on B-Chain, setting totalSharesMinted to a huge number.
- Alice bridges tokens from B-Chain to A-Chain and back, manipulating the pool's reserves.
- Alice removes almost all her liquidity, leaving totalReserve at 1 wei and totalSharesMinted still very large.
- Bob adds liquidity with a large amount of tokens, receiving an enormous number of shares due to the low share price.
- Bob tries to remove his liquidity, but the transaction reverts due to an overflow in the share-to-token calculation.
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, users deposit tokens and receive shares representing their portion. The value of one share depends on the total tokens divided by total shares. An attacker can make each share worth very little by adding a tiny amount of tokens after almost emptying the pool. Then, when a new user deposits, they get a huge number of shares. Later, when they try to withdraw, a calculation multiplies that huge number by the token amount and overflows, causing the transaction to fail and locking their funds.
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.