User can avoid bankrupting by calling PositionManager.moveLiquidity where to index is bankrupted index
The moveLiquidity function did not check whether the destination bucket was bankrupt, so a user could move liquidity into a bankrupt bucket and update its deposit time, making previously zeroed LP shares redeemable. This allowed an attacker to steal LP shares belonging to other users from the PositionManager contract.
rvierdiiev Follow with J4de, SpicyMeatball and 1 other
Originally disclosed on Code4rena
Submitted by rvierdiiev, also found by J4de, SpicyMeatball, and volodya
Bucket could become insolvent and in that case all LP within the bucket are zeroed out (lenders lose all their LP). Because of that, PositionManager.reedemPositions will not allow to redeem index that is bankrupted.
When user wants to move his LPs from one bucket to another he can call PositionManager.moveLiquidity where he will provide from and to indexes.
https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-core/src/PositionManager.sol#L262-L333
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
);
}
As you can see from bucket is checked to be not bankrupted before the moving.
And after the move, LPs of from and to buckets are updated.
Also depositTime of to bucket is updated to from.depositTime.
The problem here is that to bucket was never checked to be not bankrupted.
Because of that it’s possible that bankrupted to bucket now becomes not bankrupted as their depositTime is updated now.
This is how this can be used by attacker.
- Attacker has lp shares in the bucket, linked to token and this bucket became bankrupt.
- Then attacker mints small amount of LP in the Pool and then memorizes this index to the token.
- Attacker calls
moveLiquiditywithfrom: new bucket andto: bankrupt bucket. - Now attacker can redeem his lp shares from bankrupt bucket as depositedTime is updated now.
As result, attacker was able to steal LPs of another people from PositionManager contract.
Tools Used
VsCode
Recommended Mitigation Steps
In case if to bucket is bankrupt, then clear LP for it before adding moved lp shares.
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 moveLiquidity function failed to verify that the destination bucket was not bankrupt before updating its deposit time and adding LP shares.
Impact
An attacker could steal LP shares belonging to other users from the PositionManager contract.
What the attacker needed
A token position in PositionManager; a bankrupt bucket; ability to mint a small amount of LP
The fix
The destination bucket should be checked for bankruptcy, and if it is bankrupt its LP should be cleared before adding the moved shares.
How it was found
- Attacker has LP shares in a bucket linked to a token, and that bucket becomes bankrupt.
- Attacker mints a small amount of LP in the pool and memorises this index to the token.
- Attacker calls moveLiquidity with the new bucket as from and the bankrupt bucket as to.
- Attacker can now redeem their LP shares from the bankrupt bucket because the deposit time is updated.
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 this lending system, each 'bucket' holds lender funds, and if a bucket goes bankrupt its lenders' shares are wiped out. The function that moves shares between buckets only checked the source bucket for bankruptcy, not the destination. By moving a tiny amount into a bankrupt bucket, an attacker could reset its timestamp and then redeem shares that should have been worthless, taking other people's 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.