Borrowers can not extend loans which has maximum duration less than 24 hours
The fee calculation for extending a loan incorrectly subtracts the initial fee from the maximum deadline fee, causing an arithmetic underflow when both durations are less than 24 hours. This prevents borrowers from extending such loans.
0x37 Follow with 0xc0ffEE, KaplanLabs and 5 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-10-debita-judging/issues/153
Found by
0x37, 0xc0ffEE, KaplanLabs, bbl4de, dhank, farismaulana, nikhil840096, ydlee
Summary
The logics to calculate the missing borrow fee is incorrect, which will cause the borrowers can not extend loans having maximum duration less than 24 hours because of arithmetic underflow
Root Cause
- In function
DebitaV3Loan::extendLoan(), the borrower has to pay the extra fee if he has not paid maximum fee yet. - The variable
feeOfMaxDeadlineis expected to be the fee to pay for lend offer's maximum duration, which is then adjusted to be within the range[feePerDay; maxFee]. This implies that the extra fee considers offer's min duration fee to be 1 day - The fee paid for the initial duration is bounded to be within the range
[minFEE; maxFee] - The fee configurations are set initially as. The fee implies that min fee for the loan initial duration is 0.2% , = 5 days of fee
uint public feePerDay = 4; // fee per day (0.04%)
uint public maxFEE = 80; // max fee 0.8%
uint public minFEE = 20; // min fee 0.2%
- The extra fee to be paid is calculated as
misingBorrowFee = feeOfMaxDeadline - PorcentageOfFeePaid, which will revert due to arithmetic underflow in case the loan's initial duration is less than 24 hours and the unpaid offers' maximum duration is also less than 24 hours. In this situation, the values will satisfyPorcentageOfFeePaid = minFEE = 0.2%,feeOfMaxDeadline = feePerDay = 0.04%, which will causemisingBorrowFee = feeOfMaxDeadline - PorcentageOfFeePaidto revert because of arithmetic underflow
uint feePerDay = Aggregator(AggregatorContract).feePerDay();
uint minFEE = Aggregator(AggregatorContract).minFEE();
uint maxFee = Aggregator(AggregatorContract).maxFEE();
@> uint PorcentageOfFeePaid = ((m_loan.initialDuration * feePerDay) /
86400);
// adjust fees
if (PorcentageOfFeePaid > maxFee) {
PorcentageOfFeePaid = maxFee;
} else if (PorcentageOfFeePaid < minFEE) {
@> PorcentageOfFeePaid = minFEE;
}
// calculate interest to pay to Debita and the subtract to the lenders
for (uint i; i < m_loan._acceptedOffers.length; i++) {
infoOfOffers memory offer = m_loan._acceptedOffers[i];
// if paid, skip
// if not paid, calculate interest to pay
if (!offer.paid) {
uint alreadyUsedTime = block.timestamp - m_loan.startedAt;
uint extendedTime = offer.maxDeadline -
alreadyUsedTime -
block.timestamp;
uint interestOfUsedTime = calculateInterestToPay(i);
uint interestToPayToDebita = (interestOfUsedTime * feeLender) /
10000;
uint misingBorrowFee;
// if user already paid the max fee, then we dont have to charge them again
if (PorcentageOfFeePaid != maxFee) {
// calculate difference from fee paid for the initialDuration vs the extra fee they should pay because of the extras days of extending the loan. MAXFEE shouldnt be higher than extra fee + PorcentageOfFeePaid
@> uint feeOfMaxDeadline = ((offer.maxDeadline * feePerDay) /
86400);
if (feeOfMaxDeadline > maxFee) {
feeOfMaxDeadline = maxFee;
} else if (feeOfMaxDeadline < feePerDay) {
@> feeOfMaxDeadline = feePerDay;
}
@> misingBorrowFee = feeOfMaxDeadline - PorcentageOfFeePaid;
}
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
- A borrow offer is created with duration = 5 hours
- A lend offer is created with min duration = 3 hours, max duration = 12 hours
- 2 offers matched
- After 4 hours, the borrower decides to extend loan by calling
extendLoan()and transaction gets reverted
Impact
- Borrowers can not extend loan for the loans having durations less than 24 hours (both initial duration and offers' max duration)
PoC
No response
Mitigation
Consider updating like below
- } else if (feeOfMaxDeadline < feePerDay) {
- feeOfMaxDeadline = feePerDay;
+ } else if (feeOfMaxDeadline < minFEE) {
+ feeOfMaxDeadline = minFEE;
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/2958108a7a7307830953dec9bf4f3178a6cff434
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
The extra fee is calculated as the difference between the fee for the maximum deadline and the fee already paid, but the maximum deadline fee is floored at the daily fee rate instead of the minimum fee, leading to a negative value when the initial duration is under 24 hours.
Impact
Borrowers cannot extend loans when both the initial duration and the offers' maximum duration are less than 24 hours.
What the attacker needed
Loan initial duration < 24 hours; offer max duration < 24 hours; borrower calls extendLoan()
The fix
The protocol team fixed this issue by changing the lower bound for the maximum deadline fee from the daily fee to the minimum fee.
How it was found
- A borrow offer is created with duration = 5 hours
- A lend offer is created with min duration = 3 hours, max duration = 12 hours
- 2 offers matched
- After 4 hours, the borrower decides to extend loan by calling `extendLoan()` and transaction gets reverted
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 you borrow money, you pay a fee. If you want to keep the loan longer, you pay an extra fee. The code that calculates this extra fee makes a mistake when the loan is very short (less than a day), so the transaction fails and you cannot extend the loan.
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.