Malicious borrower can pay each payment and make its own loan default 1 month later
The loan due date calculation can produce a due date two payment cycles away instead of one when a borrower repays shortly after the accepted timestamp or after one payment cycle. A malicious borrower could exploit this to delay payments and reduce the effective interest rate.
There's an edge case in which NextDueDate calculation will yield a due date much longer than what it ought to.
Vulnerability Detail
If the user repays a portion of its loan exactly one day and one second after the accepted timestamp, the next due date will not be 1 month later, but two.
Take a look at the calculateNextDueDate function at the V2Calculations library, notice the following snippet:
Since one lastPaymentCycle unit will be summed to the due date as the equivalent of one month, lastRepaidTimestamp just has to be on the further day after the accepted timestamp to yield not 1 month but a 2 month due date. This can be used by malicious parties to avoid defaults and to repay loans with much smaller rates.
There's also a second case in which it fails:
Solidity
dueDate_ = _acceptedTimestamp + _paymentCycle;
// Calculate the cycle number the last repayment was made
uint32 delta = _lastRepaidTimestamp - _acceptedTimestamp;
if (delta > 0) {
uint32 repaymentCycle = uint32(
Math.ceilDiv(delta, _paymentCycle)
);
dueDate_ += (repaymentCycle * _paymentCycle);
}
The due date can be of two cycles if the borrower pays back a little later than 1 payment cycle duration.
The following POC utilizes the second case to exemplify
POC
Paste the following code snippet at the TellerV2_bids.sol contract:
Solidity
function test_repay_loan_minimum2_a() public {
uint256 bidId = 1;
setMockBid(bidId);
tellerV2.mock_setBidState(bidId, BidState.ACCEPTED);
vm.warp(2 days + block.timestamp);
//set the account that will be paying the loan off
tellerV2.setMockMsgSenderForMarket(address(this));
tellerV2.calculateNextDueDate(bidId);
//need to get some weth
lendingToken.approve(address(tellerV2), 1e20);
tellerV2.calculateAmountOwed(bidId, block.timestamp);
vm.warp( 28 days + block.timestamp );
tellerV2.repayLoan(bidId, 100);
tellerV2.calculateAmountOwed(bidId, block.timestamp);
assertEq(tellerV2.calculateNextDueDate(bidId), 5184100);
}
function test_repay_loan_minimum2_b() public {
uint256 bidId = 1;
setMockBid(bidId);
tellerV2.mock_setBidState(bidId, BidState.ACCEPTED);
vm.warp(2 days + block.timestamp);
//set the account that will be paying the loan off
tellerV2.setMockMsgSenderForMarket(address(this));
tellerV2.calculateNextDueDate(bidId);
//need to get some weth
lendingToken.approve(address(tellerV2), 1e20);
tellerV2.calculateAmountOwed(bidId, block.timestamp);
vm.warp( 28 days + block.timestamp + 1 hours);
tellerV2.repayLoan(bidId, 100);
tellerV2.calculateAmountOwed(bidId, block.timestamp);
assertEq(tellerV2.calculateNextDueDate(bidId), 7776100);
}
Before running the tests, make sure to alter the following snippets:
TellerV2_bids.sol setMockBid function:
forge test --match-test test_repay_loan_minimum2 -vvvvv
Take a look at the execution traces, the test b ends up with a calculateNextDueDate resulting in 7776100 and test a resulting in 5184100 while having the same amount owed. This effectively means a borrower can partially delay his/her payment to get much later dates for the next payment.
Impact
Borrowers can avoid defaults and repayments by arbitrarily paying on certain timestamps. In the worst case a borrower can make multiple monthly payments every two months, essentially halving the borrow APY.
This issue is IN-SCOPE as these calculations are utilized by TellerV2.sol.
As it is a very easy to setup attack vector, the likelihood is high. As it doesn't incur loss of funds, but decreases the earnings for lenders, the impact is medium.
For the second case, the next payment should always be after a single payment cycle, so the following dueDate should be as follows:
Solidity
if (delta > 0) {
uint32 repaymentCycle = uint32(
Math.ceilDiv(delta, _paymentCycle)
);
// no repayment cycle multiplication as that will round in favour of the borrower and make the next repayment happen on a much later time period
dueDate_ += _paymentCycle;
}
Discussion
ethereumdegen
In the vast majority of our cases, loans are 'seconds' instead of 'monthly' type. This does seem to be a valid, although minor, concern so thank you for bringing it to our attention, i think this is a wont-fix at this point .
nevillehuang
@ethereumdegen Since there is a non-zero chance of this occuring I believe medium is appropriate here given the unlikeliness of offering monthly loans.
pkqs90
Escalate
Though this is a valid issue, the error lies in V2Calculations.sol contract, which is not in scope of the contest. The original scope is the following.
Thus I think this issue should be OOS, specifically for this contest.
sherlock-admin3
Escalate
Though this is a valid issue, the error lies in V2Calculations.sol contract, which is not in scope of the contest. The original scope is the following.
The original report as published on Sherlock, by jovi. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the due date calculation incorrectly adds an extra payment cycle when the last repayment timestamp falls on a later day than the accepted timestamp, or when the repayment is made after one full payment cycle.
Impact
Borrowers can avoid defaults and repayments by paying on certain timestamps, potentially halving the borrow APY, though the protocol considers this a minor concern and unlikely for monthly loans.
What the attacker needed
Borrower has an active loan; borrower makes a repayment at a specific time
The fix
The due date calculation should always add exactly one payment cycle, without the conditional extra cycle or multiplication by repayment cycle.
How it was found
Set up a mock bid with accepted timestamp and payment cycle.
Warp time to 2 days after the accepted timestamp.
Approve lending token for repayment.
Warp time to 28 days after the accepted timestamp (or 28 days + 1 hour for the second case).
Repay a small amount of the loan.
Check the next due date, which is incorrectly two cycles away.
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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In a lending protocol, the due date for the next payment is calculated based on when the loan was accepted and when the last payment was made. A mistake in this calculation can make the next due date much later than it should be, allowing a borrower to pay less often and avoid penalties.
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.