Skip to content
Business logic Audit finding Intermediate

Incorrect Debt Tracking in `_updateRepaymentState`

The repayment function checks the length of the collateral array after removing an entry, so a full repayment of a single debt leaves the token in the borrowed-assets list, while repaying one of two debts removes it prematurely. This incorrect debt tracking could let a user withdraw collateral without proper validation or cause liquidation errors.

37H3RN17Y2 Follow with Brene, DharkArtz and 11 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-05-lend-audit-contest-judging/issues/672

Found by

37H3RN17Y2, Brene, DharkArtz, PNS, Tigerfrake, Waydou, future, h2134, hgrano, jokr, mahdifa, patitonar, rudhra1749, t.aksoy

Summary

In CrossChainRouter.sol, the _updateRepaymentState function incorrectly checks userCrossChainCollaterals.length == 1 after calling removeCrossChainCollateral. This leads to two issues: (1) if crossChainCollaterals has one element, the token is not removed from userBorrowedAssets despite no remaining debt, and (2) if it has two elements, the token is erroneously removed while a debt remains. This disrupts debt tracking, potentially enabling unauthorized collateral withdrawal or liquidation errors.

Root Cause

In _updateRepaymentState:

Solidity
if (repayAmountFinal == borrowedAmount) {
    lendStorage.removeCrossChainCollateral(borrower, _token, index);
    if (userCrossChainCollaterals.length == 1) {
        lendStorage.removeUserBorrowedAsset(borrower, _lToken);
    }
}

The userCrossChainCollaterals.length is checked after removeCrossChainCollateral, which reduces the array length. If length was 1 before removal, it becomes 0, so removeUserBorrowedAsset is not called, leaving the token in userBorrowedAssets. If length was 2, it becomes 1, triggering removeUserBorrowedAsset incorrectly while a debt remains.

Internal Pre-conditions

  1. The protocol tracks cross-chain debts in lendStorage.crossChainCollaterals.
  2. _updateRepaymentState manages debt updates during cross-chain repayments.
  3. userBorrowedAssets tracks tokens with active debts.

External Pre-conditions

  1. A user has a cross-chain borrow (e.g., 100 USDC) on chain B with collateral in chain A.
  2. The user or a liquidator calls repayCrossChainBorrow with _amount = type(uint256).max to fully repay the borrow.

Attack Path

  1. Single Debt Case:
    • User has one cross-chain borrow; repayment removes it but leaves _lToken in userBorrowedAssets.
    • User may withdraw collateral without proper debt validation.
  2. Multiple Debt Case:
    • User has two cross-chain borrows; repaying one removes _lToken from userBorrowedAssets despite a remaining debt.
    • This may prevent liquidation or allow incorrect collateral calculations.

Impact

  • Broken Functionality: Incorrect debt tracking disrupts collateral and liquidation calculations, breaking core protocol functionality (Sherlock Section V).
  • Relevant Loss: Potential unauthorized collateral withdrawal or liquidation failures could cause losses (>0.01% and >$10) under specific conditions (Section V).
  • Sherlock Criteria: The issue requires specific debt scenarios (one or two cross-chain borrows), aligning with Medium severity.

Mitigation

Check userCrossChainCollaterals.length before calling removeCrossChainCollateral. Update the code as follows:

Solidity
if (repayAmountFinal == borrowedAmount) {
    bool isLastCollateral = userCrossChainCollaterals.length == 1;
    lendStorage.removeCrossChainCollateral(borrower, _token, index);
    if (isLastCollateral) {
        lendStorage.removeUserBorrowedAsset(borrower, _lToken);
    }
}

This ensures _lToken is removed from userBorrowedAssets only when the last debt is repaid.

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

Impact

Incorrect debt tracking could allow a user to withdraw collateral without proper debt validation or cause liquidation failures, though the report notes this requires specific debt scenarios.

What the attacker needed

A cross-chain borrow exists; full repayment is attempted

The fix

The length should be checked before removing the collateral entry, so the borrowed asset is removed only when the last debt is repaid.

How it was found

  1. A user has a cross-chain borrow tracked in the collateral array.
  2. The user or a liquidator calls the repayment function with the maximum amount to fully repay.
  3. The function removes the collateral entry and then checks the array length.
  4. Because the length was reduced, the borrowed-asset removal is skipped or wrongly triggered.

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.

A smart contract keeps a list of what a user has borrowed. When the user pays back everything, the contract should remove that item from the list. Here it checks the list size after already deleting an item, so the check gives the wrong answer and the list ends up wrong. That wrong list can later be used to decide if the user is allowed to take back collateral, which is unsafe.

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 from LEND

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.