Skip to content
Smart contracts Audit finding Intermediate

Not updating state before making custom external call can cause borrower's to loose assets due to re-entrancy

The contract updates repayment state only after calling an external listener, allowing a malicious lender to re-enter and close the loan, seizing collateral. A borrower who repays after default could lose both the repayment and the collateral.

hash Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-11-teller-finance-update-judging/issues/42

Found by

hash

Summary

Not updating state before making custom external call can cause borrower's to loose assets due to re-entrancy

Root Cause

The details of the repayment is updated only after the external call to the loanRepaymentListener is made

https://github.com/sherlock-audit/2024-11-teller-finance-update/blob/0c8535728f97d37a4052d2a25909d28db886a422/teller-protocol-v2-audit-2024/packages/contracts/contracts/TellerV2.sol#L865-L870

Solidity
    function _repayLoan(
        uint256 _bidId,
        Payment memory _payment,
        uint256 _owedAmount,
        bool _shouldWithdrawCollateral
    ) internal virtual {
        
        ....
        // @audit attacker can re-enter here. the repayment details are not yet updated
        _sendOrEscrowFunds(_bidId, _payment); //send or escrow the funds

        // update our mappings
        bid.loanDetails.totalRepaid.principal += _payment.principal;
        bid.loanDetails.totalRepaid.interest += _payment.interest;
        bid.loanDetails.lastRepaidTimestamp = uint32(block.timestamp);
Solidity
    function _sendOrEscrowFunds(uint256 _bidId, Payment memory _payment)
        internal virtual 
    {
        
        ....

        address loanRepaymentListener = repaymentListenerForBid[_bidId];

        // @audit re-enter in this call
        if (loanRepaymentListener != address(0)) {
            require(gasleft() >= 80000, "NR gas");  //fixes the 63/64 remaining issue
            try
                ILoanRepaymentListener(loanRepaymentListener).repayLoanCallback{
                    gas: 80000
                }( //limit gas costs to prevent lender preventing repayments
                    _bidId,
                    _msgSenderForMarket(bid.marketplaceId),
                    _payment.principal,
                    _payment.interest
                )
            {} catch {}

This allows a malicious lender to reenter the TellerV2 contract and invoke lenderCloseLoan seizing the collateral of the borrower as well if the loan is currently defaulted

Internal pre-conditions

  1. The repayment should be made after defaultTimestamp has passed

External pre-conditions

No response

Attack Path

  1. Defaulting timestmap of loan has passed
  2. Borrower does a repayment of 100 which is transferred to the lender. Following this .repayLoanCallback is called
  3. Lender reenters via the loanRepaymentListener and invokes the lenderCloseLoan function further seizing the collateral of the borrower
  4. Borrower looses both the repayment amount and the collateral

Impact

Borrower will loose repayment amount and also the collateral

PoC

No response

Mitigation

Update the state before the loanRepaymentListener call is made

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/teller-protocol/teller-protocol-v2-audit-2024/pull/81

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

The contract performs an external call to the repayment listener before updating the loan's repayment state, enabling re-entrancy.

Impact

The borrower could lose both the repayment amount and the collateral.

What the attacker needed

Loan is in default; Lender is malicious; Borrower makes a repayment

The fix

The state should be updated before the loanRepaymentListener call is made.

How it was found

  1. Wait until the loan's default timestamp has passed.
  2. Borrower repays 100, which is transferred to the lender, and then the repayLoanCallback is called.
  3. Lender re-enters via the loanRepaymentListener and invokes lenderCloseLoan, seizing the borrower's collateral.
  4. Borrower loses both the repayment amount and 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.

When a borrower repays a loan, the contract first sends the money to the lender and only then records that the repayment happened. A malicious lender can use that moment to call back into the contract and close the loan, taking the borrower's collateral even though the borrower just paid. This is a re-entrancy attack: the contract is tricked into running code again before it finishes updating its records.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by hash

View all

27 more disclosed reports, best first.

More from Teller Lender Groups Update Audit

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.