Skip to content
Business logic Audit finding Advanced

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.

oakcobalt Follow

Originally disclosed on Code4rena

Submitted by oakcobalt

In Pool.sol, queueClaimAll flow will transfer received funds (returned funds from loans) for each queue to newer queues.

Received funds for a given queue are intended to be distributed to newer queues:

   /// @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:

  1. 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.

    As a result, any received funds from the oldesQueueIndex (getTotalReceived\(oldestQueueIndex\)) will not be distributed and directly deleted (getTotalReceived[_idx] = 0;).

  2. 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;}

Assessed type

Error

0xA5DF (judge) commented:

causing received funds to be added to the wrong queues

What are the consequences of that? I’ll might consider high severity if this leads to frozen funds.

0xend (Gondi) confirmed and commented:

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.

0xA5DF (judge) increased severity to High

Gondi mitigated:

Need to break 1 before.

Status: Mitigation confirmed. Full details in reports from oakcobalt, minhquanym and bin2chen.


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.

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.

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.

More by oakcobalt

View all

50 more disclosed reports, best first.

More from Gondi Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.