Incorrect circular array check in _updatePendingWithdrawalWithQueue flow, causing received funds to be added to the wrong queues
The circular array index check in the withdrawal queue distribution loop was incorrect, so received funds could be skipped for the oldest queue or distributed to both newer and older queues. This could cause funds to be allocated to the wrong queues, potentially freezing them.
/// @dev We go from idx to newer queues. Each getTotalReceived is the total
/// returned from loans for that queue. All future queues/pool also have a piece of it.
/// X_i: Total received for queue `i`
/// X_1 = Received * shares_1 / totalShares_1
/// X_2 = (Received - (X_1)) * shares_2 / totalShares_2 ...
/// Remainder goes to the pool.
This logic is implemented in _updatePendingWithdrawalWithQueue(). Due to queue arrays are circular, the array index never exceeds getMaxTotalWithdrawalQueues and will restart from 0.
% totalQueues should be used when checking array indexes in most cases. However, in the queue index for-loop, if (secondIdx == _cachedPendingQueueIndex + 1) {break;} is used to break the loop instead of secondIdx == (_cachedPendingQueueIndex + 1)%totalQueues.
This is problematic in some cases:
When _cachedPendingQueueIndex<getMaxTotalWithdrawalQueues. _updatePendingWithdrawalWithQueue() will always skip the oldest queue when distributing getTotalReceived[_idx] funds.
In _queueClaimAll(), the first for-loop start with the oldestQueueIndex (_cachedPendingQueueIndex + 1) % totalQueues + 0)%totalQueues). When (_cachedPendingQueueIndex + 1) % totalQueues== _cachedPendingQueueIndex + 1, this first iteration will always result in a break in the second for-loop, where secondIdx == oldestQueueIndex == _cachedPendingQueueIndex + 1.
When _cachedPendingQueueIndex==getMaxTotalWithdrawalQueues The second for-loop will never break, because secondIdx < _cachedPendingQueueIndex + 1. for (uint256 i; i < totalQueues;) will always run getMaxTotalWithdrawalQueues+1 times. This will result in received funds from any queues being distributed to both newer queues and older queues.
Recommended Mitigation Steps
Based on my understanding, this should be if (i≠0 && secondIdx == (_cachedPendingQueueIndex + 1)%totalQueues) { break;}
Conversation continued over discord. There’s a bug here I believe (a high severity one), the condition for breaking the loop should be secondIdx == _cachedPendingQueueIndex.
The original report as published on Code4rena, by oakcobalt. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the loop break condition compared the second index to the cached pending queue index plus one without applying the modulo operation needed for a circular array.
Impact
Received funds could be added to the wrong queues, and the judge noted this could lead to frozen funds.
What the attacker needed
Funds returned from loans; withdrawal queues in use
The fix
The break condition should be corrected to account for the circular array, and the project mitigated the issue.
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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A smart contract keeps a list of withdrawal requests in a circular array, meaning after the last slot it wraps back to the first. When loan money comes back, the code is supposed to pass it along to newer requests only. A wrong check on where the list wraps around made it skip the oldest request or spread money to older requests too, so the accounting goes wrong.
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.