Skip to content
Business logic Audit finding Advanced

Merging tranches could make _loanTermination() accounting incorrect

The permissionless mergeTranches function changes a loan's ID, which can cause the Pool contract's _loanTermination accounting to misattribute the loan to the wrong withdrawal queue. An attacker could merge tranches to make a loan appear newer than it is, leading to incorrect pool accounting.

minhquanym Follow with bin2chen

Originally disclosed on Code4rena

Submitted by minhquanym, also found by bin2chen

In the Pool contract, when a loan is repaid or liquidated, a call to the Pool is made for accounting. The _loanTermination() function is eventually invoked. This function uses the loanId to determine the withdrawal queue to which the loan belongs. If the loan was issued after the last queue, it belongs entirely to the pool, and _outstandingValues is updated. If not, it updates the queue accounting, queue outstanding values, getTotalReceived and getAvailableToWithdraw.

function _loanTermination(
    ...
) private {
    uint256 pendingIndex = _pendingQueueIndex;
    uint256 totalQueues = getMaxTotalWithdrawalQueues + 1;
    uint256 idx;
    /// @dev oldest queue is the one after pendingIndex
    uint256 i;
    for (i = 1; i < totalQueues;) {
        idx = (pendingIndex + i) % totalQueues;
        if (getLastLoanId[idx][_loanContract] >= _loanId) {
            break;
        }
        unchecked {
            ++i;
        }
    }
    /// @dev We iterated through all queues and never broke, meaning it was issued after the newest one.
    if (i == totalQueues) {
        _outstandingValues =
            _updateOutstandingValuesOnTermination(_outstandingValues, _principalAmount, _apr, _interestEarned);
        return;
    } else {
        uint256 pendingToQueue =
            _received.mulDivDown(PRINCIPAL_PRECISION - _queueAccounting[idx].netPoolFraction, PRINCIPAL_PRECISION);
        getTotalReceived[idx] += _received;
        getAvailableToWithdraw += pendingToQueue;
        _queueOutstandingValues[idx] = _updateOutstandingValuesOnTermination(
            _queueOutstandingValues[idx], _principalAmount, _apr, _interestEarned
        );
    }
}

However, the mergeTranches() function is permissionless and only requires the merged tranches to be contiguous. Once tranches are merged, the loanId of the new tranche changes, which can lead to incorrect accounting in the Pool.

Proof of Concept

Consider the following scenario:

  1. A borrower opens a loan and takes liquidity from multiple offers of the same Pool. The loan has the parameters loanId = 100, with two tranches, both having lender = pool_address.
  2. In the Pool, assume getLastLoanId[1][loan] = 100, indicating that queue index 1 points to the latest loanId in the loan contract.
  3. An attacker calls mergeTranches() to merge the two tranches of loanId = 100 with the same lender, which is the pool address. The new newLoanId = 101 is used in the new tranche.
  4. Now, when the loan is repaid, the _loanTermination() function is invoked with _loanId = 101. The loop returns i == totalQueues, making the loan belong entirely to the pool, while it should belong to withdrawal queue index 1.

MultiSourceLoan.sol#L1132-L1140

tranche[_minTranche] = IMultiSourceLoan.Tranche(
    _newLoanId, // @audit can be used to change loanId
    _loan.tranche[_minTranche].floor,
    principalAmount,
    lender,
    accruedInterest,
    startTime,
    cumAprBps / principalAmount
);

Recommended Mitigation Steps

Limit the ability to call mergeTranches() directly to lenders only.

0xend (Gondi) confirmed

Gondi mitigated:

Only tranche lender can call mergeTranches so it assumes the responsibility.

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


The original report as published on Code4rena, by minhquanym. 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 mergeTranches function is permissionless and assigns a new loanId, while _loanTermination relies on loanId ordering to determine queue membership.

Impact

The Pool's accounting could be incorrect, misattributing the loan to the pool instead of the correct withdrawal queue.

What the attacker needed

A loan with multiple tranches from the same pool; attacker can call mergeTranches()

The fix

The ability to call mergeTranches() should be limited to lenders only, and this was mitigated by Gondi.

How it was found

  1. A borrower opens a loan with loanId 100, taking liquidity from multiple offers of the same Pool, resulting in two tranches with the pool as lender.
  2. In the Pool, getLastLoanId[1][loan] is 100, so queue index 1 points to the latest loanId.
  3. An attacker calls mergeTranches() to merge the two tranches of loanId 100 with the same lender (the pool address), producing newLoanId 101.
  4. When the loan is repaid, _loanTermination() is invoked with _loanId 101, and the loop returns i == totalQueues, treating the loan as belonging entirely to the pool instead of queue index 1.

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 loan can be split into parts called tranches. The contract lets anyone merge these parts, which gives the loan a new ID number. The pool uses that ID to decide which queue the loan belongs to, so changing it can make the pool record the loan in the wrong place.

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 minhquanym

View all

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