Withdraw amount returned by getLiquidityAmountsAndPositions may be incorrect
The getLiquidityAmountsAndPositions function incorrectly assigns the leftover liquidity to the last position amount instead of subtracting it, causing users to withdraw more tokens than intended when using the function's output.
Submitted by EV_om, also found by hash, erebus, and lsaudit
The getLiquidityAmountsAndPositions() function in the liquidity_lockbox contract is used to calculate the liquidity amounts and positions to be withdrawn for a given total withdrawal amount. It iterates through each deposited position following a FIFO order as shown below:
uint64 liquiditySum = 0;
uint32 numPositions = 0;
uint64 amountLeft = 0;
// Get the number of allocated positions
for (uint32 i = firstAvailablePositionAccountIndex; i < numPositionAccounts; ++i) {
address positionAddress = positionAccounts[i];
uint64 positionLiquidity = mapPositionAccountLiquidity[positionAddress];
// Increase a total calculated liquidity and a number of positions to return
liquiditySum += positionLiquidity;
numPositions++;
// Check if the accumulated liquidity is enough to cover the requested amount
if (liquiditySum >= amount) {
amountLeft = liquiditySum - amount;
break;
}
}
However, there is an error in the calculation of the last position amount when it entails a partial withdrawal. The amountLeft variable represents the leftover liquidity in the last position after the user’s withdrawals, but it is assigned to the returned amounts as if it represented the amount the user should withdraw:
// Adjust the last position, if it was not fully allocated
if (numPositions > 0 && amountLeft > 0) {
positionAmounts[numPositions - 1] = amountLeft;
}
This discrepancy could lead to users withdrawing an incorrect amount if they use getLiquidityAmountsAndPositions(), as intended, to obtain positions and amounts to use when calling withdraw(). This can result in significant unintended transfer of funds for the user, especially in cases where the last position in positionAmounts is of significant size. Given the fact that this issue can occur under normal usage of the contract, this issue is assessed as medium severity.
Proof of Concept
Consider the following step-by-step scenario:
- Alice has a large amount of bridged tokens.
- Alice decides to withdraw a small portion of her liquidity. She has written a script that calls
getLiquidityAmountsAndPositions()to calculate the positions and amounts for the withdrawal and iterates through the returned arrays in a series of calls towithdraw(). - The
firstAvailablePositionAccountIndexpoints to a very large position, which causesgetLiquidityAmountsAndPositions()to return an amount much larger than Alice intended. - Alice unknowingly passes the returned amount to
withdrawand as a result ends up withdrawing far more tokens than she intended, potentially depleting her liquidity in the contract.
Recommended Mitigation Steps
To fix this issue, the last position amount should be reduced by the remaining amount when the accumulated liquidity is not fully allocated. This can be achieved by changing the assignment operation to a subtraction operation:
@@ -374,7 +374,7 @@ contract liquidity_lockbox {
// Adjust the last position, if it was not fully allocated
if (numPositions > 0 && amountLeft > 0) {
- positionAmounts[numPositions - 1] = amountLeft;
+ positionAmounts[numPositions - 1] -= amountLeft;
}
}
This change ensures that the last position amount is correctly calculated, preventing users from withdrawing an incorrect amount.
kupermind (Olas) confirmed, but disagreed with severity and commented:
@dmvt The bug is related to the view function, and this can’t be considered as Medium risk. It’s more like informative, and thus we are disputing this.
Not relevant anymore as the code has been completely re-written: https://github.com/valory-xyz/lockbox-solana
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
The code sets the last position's withdrawal amount to the leftover liquidity instead of subtracting the leftover from the original amount.
Impact
Users could withdraw an incorrect, larger amount of tokens, potentially depleting their liquidity in the contract.
What the attacker needed
User calls getLiquidityAmountsAndPositions; first position is large relative to withdrawal
The fix
The last position amount should be reduced by the remaining amount by changing the assignment to a subtraction.
How it was found
- Alice has a large amount of bridged tokens.
- Alice decides to withdraw a small portion of her liquidity and uses a script that calls getLiquidityAmountsAndPositions() to calculate positions and amounts.
- The firstAvailablePositionAccountIndex points to a very large position, causing the function to return an amount much larger than intended.
- Alice unknowingly passes the returned amount to withdraw and ends up withdrawing far more tokens than she intended.
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 helper function that calculates how much to withdraw gives a wrong number. If someone uses that number to withdraw, they might take out more than they wanted, losing extra 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.