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.
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
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);
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
- The repayment should be made after defaultTimestamp has passed
External pre-conditions
No response
Attack Path
- Defaulting timestmap of loan has passed
- Borrower does a repayment of 100 which is transferred to the lender. Following this
.repayLoanCallbackis called - Lender reenters via the
loanRepaymentListenerand invokes thelenderCloseLoanfunction further seizing the collateral of the borrower - 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
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
- Wait until the loan's default timestamp has passed.
- Borrower repays 100, which is transferred to the lender, and then the repayLoanCallback is called.
- Lender re-enters via the loanRepaymentListener and invokes lenderCloseLoan, seizing the borrower's collateral.
- 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.