Open c4-bot-1 opened 10 months ago
alex-ppg marked the issue as primary issue
alex-ppg marked the issue as sufficient quality report
kupermind (sponsor) confirmed
kupermind marked the issue as disagree with severity
dmvt marked the issue as selected for report
@c4-judge 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
Lines of code
https://github.com/code-423n4/2023-12-autonolas/blob/main/lockbox-solana/solidity/liquidity_lockbox.sol#L377
Vulnerability details
Impact
The
getLiquidityAmountsAndPositions()
function in theliquidity_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: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:This discrepancy could lead to users withdrawing an incorrect amount if they use
getLiquidityAmountsAndPositions()
, as intended, to obtain positions and amounts to use when callingwithdraw()
. This can result in significant unintended transfer of funds for the user, especially in cases where the last position inpositionAmounts
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:
getLiquidityAmountsAndPositions()
to calculate the positions and amounts for the withdrawal and iterates through the returned arrays in a series of calls towithdraw()
.firstAvailablePositionAccountIndex
points to a very large position, which causesgetLiquidityAmountsAndPositions()
to return an amount much larger than Alice intended.withdraw
and as a result ends up withdrawing far more tokens than she intended, potentially depleting her liquidity in the contract.Tools Used
Manual review
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:
This change ensures that the last position amount is correctly calculated, preventing users from withdrawing an incorrect amount.
Assessed type
Other