distribute() uses the wrong end time to break maxSeniorRepayment ’s expectations
The distribute() function calculates pending interest using the current block timestamp instead of the loan's end time, so interest accrues for longer than the loan duration. When proceeds are insufficient, this causes earlier lenders to receive more repayment than expected, breaking later lenders' maxSeniorRepayment protection.
Submitted by bin2chen
When the bid amount is not enough, the lender will be repaid in order of tranche[]. In order to minimize the risk, the user can specify maxSeniorRepayment to avoid the risk to some extent, and put himself in a position of higher repayment priority. At the same time emitLoan() checks maxSeniorRepayment for the emitLoan().
emitLoan() -> _processOffersFromExecutionData() -> _checkOffer()
function _processOffersFromExecutionData(
address _borrower,
address _principalReceiver,
address _principalAddress,
address _nftCollateralAddress,
uint256 _tokenId,
uint256 _duration,
OfferExecution[] calldata _offerExecution
) private returns (uint256, uint256[] memory, Loan memory, uint256) {
...
uint256 amount = thisOfferExecution.amount;
address lender = offer.lender;
/// @dev Please note that we can now have many tranches with same `loanId`.
tranche[i] = Tranche(loanId, totalAmount, amount, lender, 0, block.timestamp, offer.aprBps);
totalAmount += amount;
@> totalAmountWithMaxInterest += amount + amount.getInterest(offer.aprBps, _duration);
...
function _checkOffer(
LoanOffer calldata _offer,
address _principalAddress,
address _nftCollateralAddress,
uint256 _amountWithInterestAhead
) private pure {
if (_offer.principalAddress != _principalAddress || _offer.nftCollateralAddress != _nftCollateralAddress) {
revert InvalidAddressesError();
}
@> if (_amountWithInterestAhead > _offer.maxSeniorRepayment) {
revert InvalidTrancheError();
}
}
totalAmountWithMaxInterest is computed using loan._duration. But when the bidding ends and the distribution is done in LiquidationDistributor.distribute(), the current time is used to calculate Interest.
function distribute(uint256 _proceeds, IMultiSourceLoan.Loan calldata _loan) external {
uint256[] memory owedPerTranche = new uint256[](_loan.tranche.length);
uint256 totalPrincipalAndPaidInterestOwed = _loan.principalAmount;
uint256 totalPendingInterestOwed = 0;
for (uint256 i = 0; i < _loan.tranche.length;) {
IMultiSourceLoan.Tranche calldata thisTranche = _loan.tranche[i];
uint256 pendingInterest =
@> thisTranche.principalAmount.getInterest(thisTranche.aprBps, block.timestamp - thisTranche.startTime);
totalPrincipalAndPaidInterestOwed += thisTranche.accruedInterest;
totalPendingInterestOwed += pendingInterest;
owedPerTranche[i] += thisTranche.principalAmount + thisTranche.accruedInterest + pendingInterest;
unchecked {
++i;
}
}
Because bidding takes a certain amount of time (~3-7 days), using block.timestamp - thisTranche.startTime will be larger than expected! Correctly should use: (loan.startTime + loan.duration - thisTranche.startTime) to calculate the interest.
This leads to the problem that if there are not enough funds, the front lender will get a larger repayment than expected, breaking the back lender’s initial expectation of maxSeniorRepayment.
Impact
If there are not enough funds, the initial expectation of maxSeniorRepayment may be broken.
Recommended Mitigation
function distribute(uint256 _proceeds, IMultiSourceLoan.Loan calldata _loan) external {
uint256[] memory owedPerTranche = new uint256[](_loan.tranche.length);
uint256 totalPrincipalAndPaidInterestOwed = _loan.principalAmount;
uint256 totalPendingInterestOwed = 0;
+ uint256 loanExpireTime = _loan.startTime + _loan.duration;
for (uint256 i = 0; i < _loan.tranche.length;) {
IMultiSourceLoan.Tranche calldata thisTranche = _loan.tranche[i];
uint256 pendingInterest =
- thisTranche.principalAmount.getInterest(thisTranche.aprBps, block.timestamp - thisTranche.startTime);
+ thisTranche.principalAmount.getInterest(thisTranche.aprBps, loanExpireTime - thisTranche.startTime);
totalPrincipalAndPaidInterestOwed += thisTranche.accruedInterest;
totalPendingInterestOwed += pendingInterest;
owedPerTranche[i] += thisTranche.principalAmount + thisTranche.accruedInterest + pendingInterest;
unchecked {
++i;
}
}
Assessed type
Context
Changed to loan end time (instead of current timestamp).
Status: Mitigation confirmed. Full details in reports from oakcobalt, bin2chen and minhquanym.
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 distribute() function uses block.timestamp instead of the loan's end time when computing pending interest, causing interest to accrue beyond the loan duration.
Impact
If there are not enough funds, the initial expectation of maxSeniorRepayment may be broken, so later lenders could receive less repayment than they expected.
What the attacker needed
Insufficient funds at distribution; multiple tranches with different lenders
The fix
The project changed the interest calculation to use the loan end time instead of the current timestamp.
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.
In a lending system, lenders are repaid in order, and each lender can set a maximum amount they are willing to let be repaid to lenders ahead of them. This report shows that when the loan is liquidated, the code calculates interest up to the current time rather than the loan's end time, so the amount owed to earlier lenders is too high. That means later lenders can end up with less than they were promised.
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.