Skip to content
Business logic Audit finding Intermediate

EMI last payment not handled perfectly could lead to borrower losing collaterals

The loan repayment calculation for the final payment cycle could under-calculate the amount owed, so a borrower paying early in the last cycle might not fully repay the loan. This could leave the loan open and allow the lender to claim the collateral after default.

RaymondFam Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-03-teller-judging/issues/315

Found by

RaymondFam

Summary

The ternary logic of calculateAmountOwed() could have the last EMI payment under calculated, leading to borrower not paying the owed principal and possibly losing the collaterals if care has not been given to.

Vulnerability Detail

Supposing Bob has a loan duration of 100 days such that the payment cycle is evenly spread out, i.e payment due every 10 days, here is a typical scenario:

  1. Bob has been making his payment due on time to avoid getting marked delinquent. For the last payment due, Bob decides to make it 5 minutes earlier just to make sure he will not miss it.
  2. However, duePrincipal_ ends up assigned the minimum of owedAmount - interest_ and owedPrincipal_, where the former is chosen since oweTime is less than _bid.terms.paymentCycle:
Solidity
        } else {
            // Default to PaymentType.EMI
            // Max payable amount in a cycle
            // NOTE: the last cycle could have less than the calculated payment amount
            uint256 maxCycleOwed = isLastPaymentCycle
                ? owedPrincipal_ + interest_
                : _bid.terms.paymentCycleAmount;

            // Calculate accrued amount due since last repayment
            uint256 owedAmount = (maxCycleOwed * owedTime) /
                _bid.terms.paymentCycle;
            duePrincipal_ = Math.min(owedAmount - interest_, owedPrincipal_);
        }
  1. Hence, in _repayLoan(), paymentAmount >= _owedAmount equals false failing to close the loan to have the collaterals returned to Bob:
Solidity
        if (paymentAmount >= _owedAmount) {
            paymentAmount = _owedAmount;
            bid.state = BidState.PAID;

            // Remove borrower's active bid
            _borrowerBidsActive[bid.borrower].remove(_bidId);

            // If loan is is being liquidated and backed by collateral, withdraw and send to borrower
            if (_shouldWithdrawCollateral) {
                collateralManager.withdraw(_bidId);
            }

            emit LoanRepaid(_bidId);
  1. While lingering and not paying too much attention to the collateral still in escrow, Bob presumes his loan is now settled.
  2. Next, Alex the lender has been waiting for this golden opportunity and proceeds to calling CollateralManager.withdraw() to claim all collaterals as soon as the loan turns defaulted.

Impact

Bob ended up losing all collaterals for the sake of the minute amount of loan unpaid whereas Alex receives almost all principal plus interests on top of the collaterals.

Code Snippet

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/libraries/V2Calculations.sol#L107-L119

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/TellerV2.sol#L727-L739

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/CollateralManager.sol#L250-L260

Tool used

Manual Review

Recommendation

Consider refactoring the affected ternary logic as follows:

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/libraries/V2Calculations.sol#L107-L119

Diff
        } else {
+            duePrincipal = isLastPaymentCycle
+                ? owedPrincipal
+               : (_bid.terms.paymentCycleAmount * owedTime) / _bid.terms.paymentCycle;

            // Default to PaymentType.EMI
            // Max payable amount in a cycle
            // NOTE: the last cycle could have less than the calculated payment amount
-            uint256 maxCycleOwed = isLastPaymentCycle
-                ? owedPrincipal_ + interest_
-                : _bid.terms.paymentCycleAmount;

            // Calculate accrued amount due since last repayment
-            uint256 owedAmount = (maxCycleOwed * owedTime) /
-                _bid.terms.paymentCycle;
-            duePrincipal_ = Math.min(owedAmount - interest_, owedPrincipal_);
        }

Discussion

iamjakethehuman

Escalate for 10 USDC Disagree with severity. Issue should be considered QA/ Low. Upon repaying, the user will see whether the final repayment has been successful. In fact, a corresponding event is emitted. If the final repayment was successful LoanRepaid(_bidId) is emitted. If the collateral isn't withdrawn LoanRepayment(_bidId) is emitted. Also, the said scenario requires the user to not see that they haven't received their collateral back for at least bidDefaultDuration[_bidId]) which makes the scenario even more unrealistic. https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/TellerV2.sol#L965-#L968

sherlock-admin

Escalate for 10 USDC Disagree with severity. Issue should be considered QA/ Low. Upon repaying, the user will see whether the final repayment has been successful. In fact, a corresponding event is emitted. If the final repayment was successful LoanRepaid(_bidId) is emitted. If the collateral isn't withdrawn LoanRepayment(_bidId) is emitted. Also, the said scenario requires the user to not see that they haven't received their collateral back for at least bidDefaultDuration[_bidId]) which makes the scenario even more unrealistic. https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/TellerV2.sol#L965-#L968

You've created a valid escalation for 10 USDC!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

raymondfam

Most borrowers would form a habit making the recurring payments in time by doing it slightly earlier just like anyone would be making a car, credit card or house payment just to make sure no late payments could tarnish their credit reputation. The protocol logic is intuitive in making adjustments to users forming this good habit. However, the current ternary logic could trap/trick many unsavvy borrowers when handling the last payment. The adept users might notice the event emitted but would run into the same issue again unless function repayLoanFull is used instead of function repayLoan. As a borrower, I would expect the system handle the transaction all clean come the last payment.

Additionally, function isLoanDefaulted does not care how much principal and interest has been paid in the past; all it cares is whether or not (uint32(block.timestamp) - _liquidationDelay - lastRepaidTimestamp(_bidId) > bidDefaultDuration[_bidId]) where _liquidationDelay == 0 in this case. Depending on the value of bidDefaultDuration[_bidId], the amount of seconds elapsed could transpire quickly. Imagine doing this before sleep, and then getting caught up with a full day of chores and work the next day, and before too long the position is already deemed delinquent without the borrower even aware of it.

ethereumdegen

Your updated logic does compile, however it makes 5 tests fail. Are you also implying that these 5 tests are incorrect? Or does that mean that there is an issue with your suggested logic? In any case, more deeply investigating.

[FAIL. Reason: Assertion failed.] test_01_calculateAmountOwed() (gas: 442923) [FAIL. Reason: Assertion failed.] test_02_calculateAmountOwed() (gas: 483615) [FAIL. Reason: Assertion failed.] test_03_calculateAmountOwed() (gas: 336240) [FAIL. Reason: Assertion failed.] test_04_calculateAmountOwed() (gas: 441542) [FAIL. Reason: Assertion failed.] test_05_calculateAmountOwed() (gas: 335700)

hrishibhat

Escalation rejected

Valid medium This is a valid issue where it gives an incorrect amount during the last payment cycle. Sponsor comment:

is most certainly a valid issue, there is a fix . The issue is if a user makes all their payments normally and on time, then in the last payment cycle, their amount owed fluctutes over time however the intent is that in the last payment cycle the amount owed should be 'the entire remaining balance' . Not fluctuating over time. (the time fluctuation is because of code that, for cycles that arent the last payment cycle, amount owed is (current cycle amount + amount unpaid from previous cycles) )

sherlock-admin

Escalation rejected

Valid medium This is a valid issue where it gives an incorrect amount during the last payment cycle. Sponsor comment:

is most certainly a valid issue, there is a fix . The issue is if a user makes all their payments normally and on time, then in the last payment cycle, their amount owed fluctutes over time however the intent is that in the last payment cycle the amount owed should be 'the entire remaining balance' . Not fluctuating over time. (the time fluctuation is because of code that, for cycles that arent the last payment cycle, amount owed is (current cycle amount + amount unpaid from previous cycles) )

This issue's escalations have been rejected!

Watsons who escalated this issue will have their escalation amount deducted from their next payout.

ethereumdegen

PR fix: https://github.com/teller-protocol/teller-protocol-v2/pull/99

IAm0x52

Fix looks good. owedAmount calculation has been slightly modified so that it will won't final payment amount by owedTime which will result in a complete repayment of the principal

The original report as published on Sherlock, by RaymondFam. 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.

How it worked

Root cause

Likely the ternary logic in `calculateAmountOwed()` uses a time-proportional formula for the last payment cycle instead of simply using the full remaining principal, causing the owed amount to fluctuate and be under-calculated when paid early.

Impact

The borrower could lose all collateral due to a small unpaid amount, while the lender receives almost all principal plus interest and the collateral.

What the attacker needed

Borrower has an active loan with collateral; borrower pays the last instalment early; borrower does not notice the loan is not closed

The fix

The ternary logic should be refactored so that in the last payment cycle the due principal is set to the full remaining principal, not a time-proportional amount; a fix was later confirmed.

How it was found

  1. Borrower makes all payments on time, including the last one, but pays a few minutes early.
  2. The repayment function calculates the amount owed using the time-proportional formula, resulting in a smaller `duePrincipal_`.
  3. The payment amount is less than the calculated `_owedAmount`, so the loan is not marked as paid and collateral is not withdrawn.
  4. Borrower assumes the loan is settled and does not notice the collateral is still locked.
  5. Lender waits until the loan becomes defaulted and then calls `CollateralManager.withdraw()` to claim the collateral.

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 loan, the last payment should clear the remaining balance. Here, if you pay a few minutes early in the final period, the code calculates a smaller amount than what is actually left, so the loan is not marked as fully paid. The borrower might think they are done, but the loan stays open and the lender can later take the collateral.

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.

More by RaymondFam

View all

18 more disclosed reports, best first.

More from Teller

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.