`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.
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():
-
If
_isSameChainistrue,borrowedAmountis derived fromlendStorage.borrowWithInterestSame(), and updatingborrowBalanceis correct. -
If
_isSameChainisfalse(repaying a cross-chain debt on Chain B),borrowedAmountis computed vialendStorage.borrowWithInterest()(from cross-chain records). The subsequent call toLErc20Interface(_lToken).repayBorrow()correctly reduces on chain debt. -
However, immediately afterwards the code does:
// 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
-
The user must have a nonzero
same chainborrow of_lTokenrecorded inlendStorage.borrowBalance[borrower][_lToken]. -
The user (or a liquidator) is repaying a cross-chain loan for the same
_lTokenon Chain B (so_isSameChain == false). -
lendStorage.borrowWithInterest()returns a positive borrowedAmount for the cross-chain entry.
External Pre-conditions
N/A
Vulnerability Path
- 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.
- 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.
- 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
LendStorageis removed. But the same-chainborrowBalance[Alice]is untouched so far.
- The bug: immediately after repaying
cross chain, code still runs the//Update same-chain borrow balancesblock.
Right after the repayBorrow() call, the code does:
// <<< 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
lTokenfrom 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.
- 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
- // 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.
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
- Alice opens a same-chain loan on Chain B by calling CoreRouter.borrow() with _isSameChain = true.
- Alice previously opened a cross-chain loan collateralized on Chain A, which was recorded on Chain B via CoreRouter.borrowForCrossChain().
- Alice (or a liquidator) repays exactly the cross-chain debt by calling CoreRouter.repayBorrow() with _isSameChain = false.
- The cross-chain debt is repaid, but the code still runs the same-chain borrow balance update block.
- Because the repayment amount equals the cross-chain borrowed amount, the code calls removeBorrowBalance() and removeUserBorrowedAsset(), deleting the same-chain debt record.
- 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.