Skip to content
Business logic Audit finding Intermediate

`CoreRouter.sol`’s `repayBorrowInternal` incorrectly updates `same chain` borrow balances on `cross chain` repayments

The repayBorrowInternal function in CoreRouter.sol updates same-chain borrow balances even when repaying a cross-chain debt, which can erase or reduce a user's legitimate same-chain debt. An attacker could repay a cross-chain loan to wipe out a larger same-chain debt, causing lenders to lose funds.

0xbakeng Follow with Kirkeelee, Waydou and 5 others

Originally disclosed on Sherlock

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

Found by

0xbakeng, Kirkeelee, Waydou, dimah7, future, mahdifa, patitonar, t.aksoy

Summary

When a user repays a cross-chain debt (_isSameChain == false), CoreRouter.repayBorrowInternal still executes the block labeled // Update same-chain borrow balances. As a result, the function wipes out or reduces the user’s same-chain borrowBalance even though only a cross chain loan was repaid. This can erase legitimate same chain debt and cause direct loss to lenders on that chain.

Root Cause

In CoreRouter.sol:459 within repayBorrowInternal():

  1. If _isSameChain is true, borrowedAmount is derived from lendStorage.borrowWithInterestSame(), and updating borrowBalance is correct.

  2. If _isSameChain is false (repaying a cross-chain debt on Chain B), borrowedAmount is computed via lendStorage.borrowWithInterest() (from cross-chain records). The subsequent call to LErc20Interface(_lToken).repayBorrow() correctly reduces on chain debt.

  3. However, immediately afterwards the code does:

Solidity
// Update same-chain borrow balances
if (repayAmountFinal == borrowedAmount) {
    lendStorage.removeBorrowBalance(borrower, _lToken);
    lendStorage.removeUserBorrowedAsset(borrower, _lToken);
} else {
    lendStorage.updateBorrowBalance(
        borrower, _lToken,
        borrowedAmount - repayAmountFinal,
        LTokenInterface(_lToken).borrowIndex()
    );
}

This block always executes, regardless of _isSameChain. Therefore, a cross chain repayment can delete or reduce the same chain borrow record for _lToken, even if the user still owes a large same chain loan.

Worth noting: removing userBorrowedAsset() when repayAmountFinal == borrowedAmount is intended to clear any zero balance loan of that token. But because removeBorrowBalance already deleted the wrong same chain debt, the set cleanup compounds the error.

Internal Pre-conditions

  1. The user must have a nonzero same chain borrow of _lToken recorded in lendStorage.borrowBalance[borrower][_lToken].

  2. The user (or a liquidator) is repaying a cross-chain loan for the same _lToken on Chain B (so _isSameChain == false).

  3. lendStorage.borrowWithInterest() returns a positive borrowedAmount for the cross-chain entry.

External Pre-conditions

N/A

Vulnerability Path

  1. Alice opens a same chain loan on Chain B.
  • She calls CoreRouter.borrow() with _isSameChain = true.

  • Internally, lendStorage.borrowWithInterestSame() returns 0 initially (new borrow), so it passes the collateral check.

  • The borrow succeeds, and after:

  • Alice now owes tokens on Chain B.

  1. Alice previously opened a separate cross chain loan, collateralized by assets on Chain A.
  • On Chain A, Alice posted collateral and called CrossChainRouter.borrowCrossChain().

  • That message eventually delivered to Chain B’s router, which executed _handleBorrowCrossChainRequest and then CoreRouter.borrowForCrossChain().

  • Alice now owes tokens via cross chain, in addition to the tokens on the same chain loan. The two debts live in separate storage slots.

  1. Alice (or a liquidator) repays exactly the tokens toward her cross chain debt.
  • They call CoreRouter.repayBorrow() with _isSameChain = false.

  • That successfully repays the cross chain debt. At this point, the cross chain entry in LendStorage is removed. But the same-chain borrowBalance[Alice] is untouched so far.

  1. The bug: immediately after repaying cross chain, code still runs the //Update same-chain borrow balances block.

Right after the repayBorrow() call, the code does:

Solidity
// <<< BUG: This runs even though _isSameChain == false >>>
// Update same chain borrow balances
if (repayAmountFinal == borrowedAmount) {
    // repayAmountFinal == borrowedAmount
    lendStorage.removeBorrowBalance();
    lendStorage.removeUserBorrowedAsset();
} else {
    lendStorage.updateBorrowBalance();
}
  • Because repayAmountFinal == borrowedAmount, the removeBorrowBalance() call executes, deleting the same-chain debt.

  • Then removeUserBorrowedAsset() fires (removing lToken from userBorrowedAssets). At this point, Alice’s only remaining borrow record on Chain B is the (just-repaid) cross chain entry, which higher-level logic may also clear now or shortly after. The same chain borrowed tokens record is gone.

  1. Outcome: Alice’s same chain loan of has been wiped out by a repayment of the cross chain debt.

The protocol is left holding a “zero” borrow record, while in fact same chain token should still be owed.

Impact

A single repayment of a cross chain loan fully erases/reduces a larger same chain debt. Lenders who funded lose their money. This is a direct loss of user (lender) funds without external constraint.

PoC

See Vulnerability Path

Mitigation

Only update same-chain borrow records if _isSameChain == true

Diff
-   // Update same-chain borrow balances
-   if (repayAmountFinal == borrowedAmount) {
-       lendStorage.removeBorrowBalance(borrower, _lToken);
-       lendStorage.removeUserBorrowedAsset(borrower, _lToken);
-   } else {
-       lendStorage.updateBorrowBalance(
-           borrower, _lToken,
-           borrowedAmount - repayAmountFinal,
-           LTokenInterface(_lToken).borrowIndex()
-       );
-   }
-   // Clean up userBorrowedAssets unconditionally if all debt cleared
-   if (repayAmountFinal == borrowedAmount) {
-       lendStorage.removeUserBorrowedAsset(borrower, _lToken);
-   }
+   if (_isSameChain) {
+       // Update same-chain borrow balances only for same-chain loans
+       if (repayAmountFinal == borrowedAmount) {
+           lendStorage.removeBorrowBalance(borrower, _lToken);
+           lendStorage.removeUserBorrowedAsset(borrower, _lToken);
+       } else {
+           lendStorage.updateBorrowBalance(
+               borrower, _lToken,
+               borrowedAmount - repayAmountFinal,
+               LTokenInterface(_lToken).borrowIndex()
+           );
+       }
+   }
+   // If the user no longer owes any lToken on this chain (same-chain or cross-chain), 
+   // it is still valid to remove from userBorrowedAssets.
+   if (repayAmountFinal == borrowedAmount) {
+       lendStorage.removeUserBorrowedAsset(borrower, _lToken);
+   }

This ensures that repaying a cross chain debt never touches the same chain borrowBalance.

The original report as published on Sherlock, by 0xbakeng. 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 code unconditionally updates same-chain borrow balances after a repayment, without checking whether the repayment was for a same-chain or cross-chain loan.

Impact

A single repayment of a cross-chain loan can fully erase or reduce a larger same-chain debt, causing direct loss of lender funds.

What the attacker needed

User has a same-chain borrow; user has a cross-chain borrow for same token; repay cross-chain debt

The fix

The code should only update same-chain borrow records when _isSameChain is true.

How it was found

  1. Alice opens a same-chain loan on Chain B by calling CoreRouter.borrow() with _isSameChain = true.
  2. Alice previously opened a cross-chain loan collateralized on Chain A, which was recorded on Chain B via CoreRouter.borrowForCrossChain().
  3. Alice (or a liquidator) repays exactly the cross-chain debt by calling CoreRouter.repayBorrow() with _isSameChain = false.
  4. The cross-chain debt is repaid, but the code still runs the same-chain borrow balance update block.
  5. Because the repayment amount equals the cross-chain borrowed amount, the code calls removeBorrowBalance() and removeUserBorrowedAsset(), deleting the same-chain debt record.
  6. Alice's same-chain loan is wiped out, leaving the protocol with a zero borrow record while the debt still exists.

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 lending system, a person can borrow money on one blockchain (same-chain) or across blockchains (cross-chain). The code that handles repayments mistakenly treats a cross-chain repayment as if it also repays the same-chain loan, so it deletes the record of the same-chain debt. This means someone could repay a small cross-chain loan and have a larger same-chain loan disappear, leaving lenders unpaid.

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 0xbakeng

View all

1 more disclosed report, best first.

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.