The unused tokens from the user’s initialization of UniswapV4‘s pool will be locked in the UniswapImplementation contract
When initialising a Uniswap V4 pool, the contract calculates liquidity from the provided token amounts, but one token is always left over. The leftover tokens remain locked in the UniswapImplementation contract because no refund is made to the user.
Source: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/208
Found by
0x37, 0xHappy, 0xNilesh, 0xc0ffEE, 0xlucky, BADROBINX, BugPull, ComposableSecurity, Ironsidesec, KingNFT, Ollam, ZeroTrust, blockchain555, h2134, merlinboii, novaman33, shaflow01, wickie, zarkk01, zzykxx
Summary
The unused tokens from the user’s initialization of UniswapV4‘s pool will be locked in the UniswapImplementation contract.
Vulnerability Detail
function initializeCollection(address _collection, uint _amount0, uint _amount1, uint _amount1Slippage, uint160 _sqrtPriceX96) public override {
// Ensure that only our {Locker} can call initialize
if (msg.sender != address(locker)) revert CallerIsNotLocker();
// Ensure that the PoolKey is not empty
PoolKey memory poolKey = _poolKeys[_collection];
if (poolKey.tickSpacing == 0) revert UnknownCollection();
// Initialise our pool
poolManager.initialize(poolKey, _sqrtPriceX96, '');
// After our contract is initialized, we mark our pool as initialized and emit
// our first state update to notify the UX of current prices, etc.
PoolId id = poolKey.toId();
_emitPoolStateUpdate(id);
// Load our pool parameters and update the initialized flag
PoolParams storage poolParams = _poolParams[id];
poolParams.initialized = true;
// Obtain the UV4 lock for the pool to pull in liquidity
@>> poolManager.unlock(
abi.encode(CallbackData({
poolKey: poolKey,
liquidityDelta: LiquidityAmounts.getLiquidityForAmounts({
sqrtPriceX96: _sqrtPriceX96,
sqrtPriceAX96: TICK_SQRT_PRICEAX96,
sqrtPriceBX96: TICK_SQRT_PRICEBX96,
amount0: poolParams.currencyFlipped ? _amount1 : _amount0,
amount1: poolParams.currencyFlipped ? _amount0 : _amount1
}),
liquidityTokens: _amount1,
liquidityTokenSlippage: _amount1Slippage
})
));
}
This function calls UniswapV4’s poolManager.unlock().
function unlock(bytes calldata data) external override returns (bytes memory result) {
if (Lock.isUnlocked()) AlreadyUnlocked.selector.revertWith();
Lock.unlock();
// the caller does everything in this callback, including paying what they owe via calls to settle
@>> result = IUnlockCallback(msg.sender).unlockCallback(data);
if (NonzeroDeltaCount.read() != 0) CurrencyNotSettled.selector.revertWith();
Lock.lock();
}
And in poolManager.unlock(), the unlockCallback() function of UniswapImplementation is called.
function _unlockCallback(bytes calldata _data) internal override returns (bytes memory) {
// Unpack our passed data
CallbackData memory params = abi.decode(_data, (CallbackData));
// As this call should only come in when we are initializing our pool, we
// don't need to worry about `take` calls, but only `settle` calls.
(BalanceDelta delta,) = poolManager.modifyLiquidity({
key: params.poolKey,
params: IPoolManager.ModifyLiquidityParams({
tickLower: MIN_USABLE_TICK,
tickUpper: MAX_USABLE_TICK,
liquidityDelta: int(uint(params.liquidityDelta)),
salt: ''
}),
hookData: ''
});
// Check the native delta amounts that we need to transfer from the contract
@>> if (delta.amount0() < 0) {
@>> _pushTokens(params.poolKey.currency0, uint128(-delta.amount0()));
}
// Check our ERC20 donation
@>> if (delta.amount1() < 0) {
@>> _pushTokens(params.poolKey.currency1, uint128(-delta.amount1()));
}
// If we have an expected amount of tokens being provided as liquidity, then we
// need to ensure that this exact amount is sent. There may be some dust that is
// lost during rounding and for this reason we need to set a small slippage
// tolerance on the checked amount.
if (params.liquidityTokens != 0) {
uint128 deltaAbs = _poolParams[params.poolKey.toId()].currencyFlipped ? uint128(-delta.amount0()) : uint128(-delta.amount1());
if (params.liquidityTokenSlippage < params.liquidityTokens - deltaAbs) {
revert IncorrectTokenLiquidity(
deltaAbs,
params.liquidityTokenSlippage,
params.liquidityTokens
);
}
}
// We return our `BalanceDelta` response from the donate call
return abi.encode(delta);
}
In the above code, _pushTokens() transfers the required amounts of currency0 and currency1 (i.e., nativeToken and collectionToken) from the UniswapImplementation contract to the poolManager contract in UniswapV4.
function getLiquidityForAmounts(
uint160 sqrtPriceX96,
uint160 sqrtPriceAX96,
uint160 sqrtPriceBX96,
uint256 amount0,
uint256 amount1
) internal pure returns (uint128 liquidity) {
if (sqrtPriceAX96 > sqrtPriceBX96) (sqrtPriceAX96, sqrtPriceBX96) = (sqrtPriceBX96, sqrtPriceAX96);
if (sqrtPriceX96 <= sqrtPriceAX96) {
liquidity = getLiquidityForAmount0(sqrtPriceAX96, sqrtPriceBX96, amount0);
} else if (sqrtPriceX96 < sqrtPriceBX96) {
uint128 liquidity0 = getLiquidityForAmount0(sqrtPriceX96, sqrtPriceBX96, amount0);
uint128 liquidity1 = getLiquidityForAmount1(sqrtPriceAX96, sqrtPriceX96, amount1);
@>> liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1;
} else {
liquidity = getLiquidityForAmount1(sqrtPriceAX96, sqrtPriceBX96, amount1);
}
}
From LiquidityAmounts.getLiquidityForAmounts() in UniswapV4, we can see that the amounts of currency0 and currency1 might not be fully utilized, and one of the tokens will always have a leftover amount. In the UniswapImplementation::_unlockCallback() function, there is no operation to return the leftover tokens to the msg.sender (i.e., the Locker). It only compares the leftover collectionToken with the liquidityTokenSlippage, and if the leftover collectionToken exceeds the liquidityTokenSlippage, the entire operation will revert.
This only ensures that the remaining amount of collectionToken is within the user’s control. However, the leftover tokens (either nativeToken or collectionToken) will remain permanently locked in the UniswapImplementation contract.
Impact
The user loses the leftover tokens.
Code Snippet
Tool used
Manual Review
Recommendation
Add handling for refunding the leftover tokens.
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 initialisation callback does not refund the excess token amount after liquidity is added, leaving the surplus in the contract.
Impact
The user permanently loses the leftover tokens that were not used for liquidity.
What the attacker needed
User initialises a Uniswap V4 pool
The fix
The contract should be updated to refund any leftover tokens to the user after liquidity is added.
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, the system may not use all of it because the two coins must be supplied in a specific ratio. Here, the unused portion is not returned to the user but stays stuck in the contract, so the user loses it.
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.