Skip to content
Business logic Audit finding Intermediate

deployWithdrawalQueue() need to clear _queueAccounting[lastQueueIndex]

The deployWithdrawalQueue function fails to clear the _queueAccounting mapping for the last queue index, leaving stale data. When queueClaimAll is later called, it uses this stale accounting to distribute received funds incorrectly.

bin2chen Follow

Originally disclosed on Code4rena

Submitted by bin2chen

In deployWithdrawalQueue(), only clears _queueOutstandingValues[lastQueueIndex] and _outstandingValues, but doesn’t clear _queueAccounting[lastQueueIndex].

    function deployWithdrawalQueue() external nonReentrant {
...

        /// @dev We move outstanding values from the pool to the queue that was just deployed.
        _queueOutstandingValues[pendingQueueIndex] = _outstandingValues;
        /// @dev We clear values of the new pending queue.
        delete _queueOutstandingValues[lastQueueIndex];
        delete _outstandingValues;
@>      //@audit miss delete _queueAccounting[lastQueueIndex]

        _updateLoanLastIds();

@>      _pendingQueueIndex = lastQueueIndex;

        // Cannot underflow because the sum of all withdrawals is never larger than totalSupply.
        unchecked {
            totalSupply -= sharesPendingWithdrawal;
        }
    }

After this method, anyone calling queueClaimAll() will use this stale data _queueAccounting[lastQueueIndex].

queueClaimAll() -> _queueClaimAll(_pendingQueueIndex)-> _updatePendingWithdrawalWithQueue(_pendingQueueIndex)

    function _updatePendingWithdrawalWithQueue(
        uint256 _idx,
        uint256 _cachedPendingQueueIndex,
        uint256[] memory _pendingWithdrawal
    ) private returns (uint256[] memory) {
        uint256 totalReceived = getTotalReceived[_idx];
        uint256 totalQueues = getMaxTotalWithdrawalQueues + 1;
        /// @dev Nothing to be returned
        if (totalReceived == 0) {
            return _pendingWithdrawal;
        }
        getTotalReceived[_idx] = 0;

        /// @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.
        for (uint256 i; i < totalQueues;) {
            uint256 secondIdx = (_idx + i) % totalQueues;
@>          QueueAccounting memory queueAccounting = _queueAccounting[secondIdx];
            if (queueAccounting.thisQueueFraction == 0) {
                unchecked {
                    ++i;
                }
                continue;
            }
            /// @dev We looped around.
@>          if (secondIdx == _cachedPendingQueueIndex + 1) {
                break;
            }
            uint256 pendingForQueue = totalReceived.mulDivDown(queueAccounting.thisQueueFraction, PRINCIPAL_PRECISION);
            totalReceived -= pendingForQueue;

            _pendingWithdrawal[secondIdx] = pendingForQueue;
            unchecked {
                ++i;
            }
        }
        return _pendingWithdrawal;
    }

Impact

Not clearing _queueAccounting[lastQueueIndex] when executing queueClaimAll() will use this stale data to distribute totalReceived.

Recommended Mitigation

    function deployWithdrawalQueue() external nonReentrant {
...

        /// @dev We move outstaning values from the pool to the queue that was just deployed.
        _queueOutstandingValues[pendingQueueIndex] = _outstandingValues;
        /// @dev We clear values of the new pending queue.
        delete _queueOutstandingValues[lastQueueIndex];
+       delete _queueAccounting[lastQueueIndex]
        delete _outstandingValues;

        _updateLoanLastIds();

        _pendingQueueIndex = lastQueueIndex;

        // Cannot underflow because the sum of all withdrawals is never larger than totalSupply.
        unchecked {
            totalSupply -= sharesPendingWithdrawal;
        }
    }

Assessed type

Context

0xend (Gondi) confirmed

Gondi mitigated:

Clear state vars.

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


The original report as published on Code4rena, by bin2chen. 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 deployWithdrawalQueue function omits a delete statement for _queueAccounting[lastQueueIndex], leaving stale data that is later used in _updatePendingWithdrawalWithQueue.

What the attacker needed

None

The fix

The deployWithdrawalQueue function should delete _queueAccounting[lastQueueIndex] to clear stale data.

How it was found

  1. Call deployWithdrawalQueue, which sets _pendingQueueIndex to lastQueueIndex but does not clear _queueAccounting[lastQueueIndex].
  2. Later call queueClaimAll, which invokes _queueClaimAll(_pendingQueueIndex) and then _updatePendingWithdrawalWithQueue(_pendingQueueIndex).
  3. In _updatePendingWithdrawalWithQueue, the loop reads _queueAccounting[secondIdx] and uses the stale thisQueueFraction to compute pendingForQueue.

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.

When a new withdrawal queue is created, the old queue's accounting record is not deleted. Later, when someone claims funds, the system reads the old record and splits the money using outdated numbers, so the amounts are 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 bin2chen

View all

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