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:
- 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 havinglender = pool_address. - In the Pool, assume
getLastLoanId[1][loan] = 100, indicating that queue index 1 points to the latest loanId in theloancontract. - An attacker calls
mergeTranches()to merge the two tranches ofloanId = 100with the same lender, which is the pool address. The newnewLoanId = 101is used in the new tranche. - Now, when the loan is repaid, the
_loanTermination()function is invoked with_loanId = 101. The loop returnsi == 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.
Only tranche lender can call
mergeTranchesso it assumes the responsibility.
Status: Mitigation confirmed. Full details in reports from minhquanym and bin2chen.
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
- 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.
- In the Pool, getLastLoanId[1][loan] is 100, so queue index 1 points to the latest loanId.
- An attacker calls mergeTranches() to merge the two tranches of loanId 100 with the same lender (the pool address), producing newLoanId 101.
- 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.