Lender may loose part of the interest he has accrued if he makes his lend offer perpetual after a loan has been extended by the borrower
A lender who makes their lend offer perpetual after a loan has been extended loses the interest accrued before the extension, because the repayment function marks the debt as claimed and prevents later claiming. The lost interest remains locked in the contract.
Source: https://github.com/sherlock-audit/2024-10-debita-judging/issues/345
Found by
dimulski
Summary
The DebitaV3Loan.sol contract allows borrowers to extend their loan against certain fees by calling the extendLoan() function:
function extendLoan() public {
...
/*
CHECK IF CURRENT LENDER IS THE OWNER OF THE OFFER & IF IT'S PERPETUAL FOR INTEREST
*/
DLOImplementation lendOffer = DLOImplementation(
offer.lendOffer
);
DLOImplementation.LendInfo memory lendInfo = lendOffer
.getLendInfo();
address currentOwnerOfOffer;
try ownershipContract.ownerOf(offer.lenderID) returns (
address _lenderOwner
) {
currentOwnerOfOffer = _lenderOwner;
} catch {}
if (
lendInfo.perpetual && lendInfo.owner == currentOwnerOfOffer
) {
IERC20(offer.principle).approve(
address(lendOffer),
interestOfUsedTime - interestToPayToDebita
);
lendOffer.addFunds(
interestOfUsedTime - interestToPayToDebita
);
} else {
loanData._acceptedOffers[i].interestToClaim +=
interestOfUsedTime -
interestToPayToDebita;
}
loanData._acceptedOffers[i].interestPaid += interestOfUsedTime;
}
}
Aggregator(AggregatorContract).emitLoanUpdated(address(this));
}
The extendLoan() function, also calculates the interest that is owed to the lenders up to the point the function is called. As can be seen from the above code snippet if the lend order is not perpetual the accrued interest will be added to the interestToClaim field. Now if a loan has been extended by the borrower, and a lender decides he wants to make his lend order perpetual(meaning that any generated interest, which may come from other loans as well, will be directly deposited to his lend order contract), but doesn't first claim his interest he will loose the interest that has been accrued up to the point the loan was extended. When the borrower repays his loan via the payDebt() function:
function payDebt(uint[] memory indexes) public nonReentrant {
...
DLOImplementation lendOffer = DLOImplementation(offer.lendOffer);
DLOImplementation.LendInfo memory lendInfo = lendOffer
.getLendInfo();
SafeERC20.safeTransferFrom(
IERC20(offer.principle),
msg.sender,
address(this),
total
);
// if the lender is the owner of the offer and the offer is perpetual, then add the funds to the offer
if (lendInfo.perpetual && lendInfo.owner == currentOwnerOfOffer) {
loanData._acceptedOffers[index].debtClaimed = true;
IERC20(offer.principle).approve(address(lendOffer), total);
lendOffer.addFunds(total);
} else {
loanData._acceptedOffers[index].interestToClaim =
interest -
feeOnInterest;
}
SafeERC20.safeTransferFrom(
IERC20(offer.principle),
msg.sender,
feeAddress,
feeOnInterest
);
loanData._acceptedOffers[index].interestPaid += interest;
}
// update total count paid
loanData.totalCountPaid += indexes.length;
Aggregator(AggregatorContract).emitLoanUpdated(address(this));
// check owner
}
As can be seen from the above code snippet if the lend order is perpetual the intrest generated after the loan has been extended will be directly send to the lend offer contract alongside with the principal of the loan, and the debtClaimed will be set to true. This prohibits the user from calling the claimDebt() function later on in order to receive the interest he accrued before the loan was extended. This results in the lender loosing the interest he has generated before the loan was extended, which based on the amount of the loan, the duration and the APR may be a significant amount. Keep in mind that most users of the protocol are not experienced web3 developers or auditors and most probably won't be tracking if and when a loan has been extended. They will expect that after certain time has passed, they will be able to claim their interest, or if they have set their lend order to be a perpetual one, they will expect just to sit back, and generate interest.
Root Cause
The payDebt() function sets the debtClaimed to true, if a lend order is perpetual. The lender can't call the claimDebt() function in order to get his accrued interest, if he had any before the payDebt() function was called by the borrower to repay his debt.
Internal pre-conditions
- Borrow and Lend Orders are matched, the Lend orders are not perpetual
- Several days after the loan has been created pass, the borrower decides to extend the loan
- Some of the lenders decide to make their lend orders perpetual, without first claiming the interest they have generated before the loan was extended.
External pre-conditions
No response
Attack Path
No response
Impact
In a scenario where a borrower extends a loan, and later on a lender makes his lend order a perpetual one, the lender will loose the interest he accrued before the loan was extended. Based on factors such as loan duration, APR and amount those losses may be significant. Those funds will be locked in the contract forever.
PoC
No response
Mitigation
Consider implementing a separate function just for claiming interest.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/8008bb515d7f6eafc5c98a444d985cd6f9416c37
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
Impact
The lender loses the interest accrued before the loan extension, and those funds are locked in the contract forever; the loss could be significant depending on loan duration, APR, and amount.
What the attacker needed
Borrow and lend orders matched; lend orders initially not perpetual; borrower extends the loan; lender makes offer perpetual without claiming
The fix
The protocol team fixed this issue in a pull request.
How it was found
- Borrow and lend orders are matched, with lend orders not perpetual.
- After several days, the borrower extends the loan.
- Some lenders make their lend orders perpetual without first claiming accrued interest.
- The borrower repays the loan, and the system marks the debt as claimed, preventing the lender from claiming pre-extension interest.
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, a lender earns interest over time. If the borrower extends the loan and the lender later changes their offer to automatically reinvest interest, the system forgets to pay the interest earned before the extension. That money gets stuck in the contract and the lender cannot retrieve it.
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.