PositionManager.moveLiquidity could revert due to underflow
The moveLiquidity function could revert due to an underflow when subtracting the moved LP amount from the stored position LP, because the moved amount can exceed the tracked LP. An attacker could trigger this to cause a denial of service for the position owner.
0xnev Follow with mrpathfindr and 0xnev
Originally disclosed on Code4rena
Submitted by 0xnev, also found by mrpathfindr and 0xnev
function moveLiquidity(
MoveLiquidityParams calldata params_
) external override mayInteract(params_.pool, params_.tokenId) nonReentrant {
Position storage fromPosition = positions[params_.tokenId][params_.fromIndex];
MoveLiquidityLocalVars memory vars;
vars.depositTime = fromPosition.depositTime;
// handle the case where owner attempts to move liquidity after they've already done so
if (vars.depositTime == 0) revert RemovePositionFailed();
// ensure bucketDeposit accounts for accrued interest
IPool(params_.pool).updateInterest();
// retrieve info of bucket from which liquidity is moved
(
vars.bucketLP,
vars.bucketCollateral,
vars.bankruptcyTime,
vars.bucketDeposit,
) = IPool(params_.pool).bucketInfo(params_.fromIndex);
// check that bucket hasn't gone bankrupt since memorialization
if (vars.depositTime <= vars.bankruptcyTime) revert BucketBankrupt();
// calculate the max amount of quote tokens that can be moved, given the tracked LP
vars.maxQuote = _lpToQuoteToken(
vars.bucketLP,
vars.bucketCollateral,
vars.bucketDeposit,
fromPosition.lps,
vars.bucketDeposit,
_priceAt(params_.fromIndex)
);
EnumerableSet.UintSet storage positionIndex = positionIndexes[params_.tokenId];
// remove bucket index from which liquidity is moved from tracked positions
if (!positionIndex.remove(params_.fromIndex)) revert RemovePositionFailed();
// update bucket set at which a position has liquidity
// slither-disable-next-line unused-return
positionIndex.add(params_.toIndex);
// move quote tokens in pool
(
vars.lpbAmountFrom,
vars.lpbAmountTo,
) = IPool(params_.pool).moveQuoteToken(
vars.maxQuote,
params_.fromIndex,
params_.toIndex,
params_.expiry
);
Position storage toPosition = positions[params_.tokenId][params_.toIndex];
// update position LP state
fromPosition.lps -= vars.lpbAmountFrom;
toPosition.lps += vars.lpbAmountTo;
// update position deposit time to the from bucket deposit time
toPosition.depositTime = vars.depositTime;
emit MoveLiquidity(
ownerOf(params_.tokenId),
params_.tokenId,
params_.fromIndex,
params_.toIndex,
vars.lpbAmountFrom,
vars.lpbAmountTo
);
}
If lp position is worth more than when at deposit time based on amount of quote token moved which could likely be the case due to interest accrued or simply from exchange rates favoring quote token, moveLiquidity could revert due to underflow as vars.lpbAmountFrom could be greater than fromPosition.lps (i.e. vars.lpbAmountFrom > fromPosition.lps)
Since call to Position.moveLiquidity() is supposed to move all lp positions attached to associated NFT within a bucket, we can simply delete the fromPosition mapping to remove bucket index at which a position has moved liquidity to prevent potential cases where Position.moveLiquidity() could revert due to underflow in this line:
fromPosition.lps -= vars.lpbAmountFrom;
Recommendation
LP tracked by position manager at bucket index should be deleted similar to redeemPositions, if not Position.moveLiquidity can likely underflow.
function moveLiquidity(
MoveLiquidityParams calldata params_
) external override mayInteract(params_.pool, params_.tokenId) nonReentrant {
...
// update position LP state
fromPosition.lps -= vars.lpbAmountFrom;
toPosition.lps += vars.lpbAmountTo;
// update position deposit time to the from bucket deposit time
toPosition.depositTime = vars.depositTime;
++ delete fromPosition
emit MoveLiquidity(
ownerOf(params_.tokenId),
params_.tokenId,
params_.fromIndex,
params_.toIndex,
vars.lpbAmountFrom,
vars.lpbAmountTo
);
}
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 code subtracts the moved LP amount from the stored position LP without ensuring the stored amount is at least as large, and does not delete the position after moving all liquidity.
Impact
The moveLiquidity function could revert, preventing the owner from moving liquidity and potentially locking funds.
What the attacker needed
Position with accrued interest or favourable exchange rate
The fix
The position's LP should be deleted after moving liquidity, similar to redeemPositions, to prevent the underflow.
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.
This is a smart contract bug where a function that moves liquidity between buckets can fail with an error called an underflow. An underflow happens when a program tries to subtract a larger number from a smaller one, which is not allowed for unsigned integers. The result is that the function reverts, meaning the transaction fails and the user cannot move their liquidity.
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.