The attackers front-running repayloans so that the debt cannot be repaid
The repayLoan function checks the loan ID against a stored hash, but functions like mergeTranches can change that stored hash, causing repayLoan to revert. An attacker (e.g., the lender) can front-run a repayment with mergeTranches to make the loan impossible to repay, leading to liquidation and loss of collateral.
An attacker can use the front-running attack method. When repayLoan is called, execute the mergeTranches function in advance, and make the id in _loans updated. In this case, the repayLoan execution will fail due to inconsistent _loanId.
If the attacker keeps using this attack, the borrower’s debt will not be repaid; eventually causing the collateral to be liquidated.
In addition to the mergeTranches function, the attacker can also call addNewTranche, and the borrower can also call the refinance-related function, again causing _loanId to be updated.
An attacker can also use the same method to attack refinance related functions, making refinance unable to execute. An attacker can also use the same method to attack the liquidateLoan function, making it impossible for debts to be cleared.
I have some doubts about severity, since this requires too many resources from the attacker (see here), and the addNewTranche() requires the lender’s signature (and when using mergeTranches() alone the attacker would eventually run out of tranches to merge).
I think there are too many limitations on this one, and the motivation for the attacker isn’t very high - they’re not going to get the entire principal from this.
I think it’s a high risk, because anyone can be an attacker, so Lender can be an attacker.
If the lender does not want the borrower to repay the debt, the lender can use addNewTranche/mergeTranches and to attack repayLoans and make the borrower’s loan impossible to repay, especially when the loan is about to expire. This causes the borrower’s NFT to be loss, so it would have a high impact.
When _liquidateLoan, if _canClaim == true, the borrower can get the NFT directly:
You’re right that the lender has a high motivation to execute this attack. You’re also right that when the borrower attempts to repay close to the expiry time this attack becomes feasible.
While some conditions are required in order for this to work, it still seems pretty likely to happen. Due to those reasons I’m reinstating high severity.
Side note: I think that a better mitigation would be to not allow functions that change the loanID to run near the expiry time.
No specific PR here since it’s addressed when limiting addNewTranche to only be able to be called by the borrower and checking in refinancePartial that there’s at least one tranche being refinanced. This ends up limiting the number of times a loan can be locked by the lender (tranches are locked for some time after a refinance for future ones).
The original report as published on Code4rena, by zhaojie. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the loan ID is mutable and functions like mergeTranches update the stored loan hash, so repayLoan's consistency check fails after such updates.
Impact
If the attacker keeps using this attack, the borrower’s debt will not be repaid; eventually causing the collateral to be liquidated.
What the attacker needed
Attacker can front-run transactions; loan has multiple tranches to merge; repayment attempted near expiry
The fix
The report recommends not deleting the loan ID, but the judge suggested not allowing functions that change the loan ID to run near expiry, and the protocol addressed it by limiting addNewTranche to the borrower and checking refinancePartial.
How it was found
Attacker monitors for a repayLoan transaction.
Attacker front-runs it by calling mergeTranches to update the loan ID.
The repayLoan transaction then reverts due to inconsistent loan ID.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When you repay a loan, the system checks that the loan details match what it has stored. But some actions can change the stored details, so the check fails and the repayment is rejected. An attacker can trigger such a change just before you repay, making it impossible for you to pay back and causing your collateral to be taken.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.