Loan can be written off by anybody before overdue delay expires
A borrower's last repay block is not reset when their debt is written off, so a later loan can be written off immediately by any member before the overdue delay expires, causing the staker to lose their stake. An attacker could repeatedly borrow and have the loan written off instantly.
Source: https://github.com/sherlock-audit/2022-10-union-finance-judging/issues/115
Found by
Bahurum
Summary
When a borrower takes a second loan after a loan that has been written off, this second loan can be written off instantly by any other member due to missing update of last repay block, leaving the staker at a loss.
Vulnerability Detail
- A staker stakes and vouches a borrower
- the borrower borrows calling
UToken:borrow:accountBorrows[borrower].lastRepayis updated with the current block number - the staker writes off the entire debt of the borrower calling
UserManager:debtWriteOff. In the internal call toUToken:debtWriteOffthe principal is set to zero butaccountBorrows[borrower].lastRepayis not updated - 90 days pass and a staker vouches for the same borrower
- the borrower borrows calling
UToken:borrow:accountBorrows[borrower].lastRepayis not set to the current block since non zero and stays to the previous value. accountBorrows[borrower].lastRepayis now old enough to allow the check in
UserManager:debtWriteOff at line 738 to pass. The debt is written off by any other member immediatly after the loan is given. The staker looses the staked amount immediatly.
if (block.number <= lastRepay + overdueBlocks + maxOverdueBlocks) {
if (staker != msg.sender) revert AuthFailed();
}
- The last repay block is still stale and a new loan can be taken and written off immediatly many times as long as stakers are trusting the borrower
Note that this can be exploited maliciously by the borrower, who can continously ask for loans and then write them off immediatly.
Impact
The staker of the loan looses the staked amount well before the overdue delay is expired
Code Snippet
function debtWriteOff(address borrower, uint256 amount) external override whenNotPaused onlyUserManager {
uint256 oldPrincipal = getBorrowed(borrower);
uint256 repayAmount = amount > oldPrincipal ? oldPrincipal : amount;
accountBorrows[borrower].principal = oldPrincipal - repayAmount;
totalBorrows -= repayAmount;
}
Tool used
Manual Review
Recommendation
Reset lastRepay for the borrower to 0 when the debt is written off completely
function debtWriteOff(address borrower, uint256 amount) external override whenNotPaused onlyUserManager {
uint256 oldPrincipal = getBorrowed(borrower);
uint256 repayAmount = amount > oldPrincipal ? oldPrincipal : amount;
+ if (oldPrincipal == repayAmount) accountBorrows[borrower].lastRepay = 0;
accountBorrows[borrower].principal = oldPrincipal - repayAmount;
totalBorrows -= repayAmount;
}
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 debtWriteOff function does not reset the borrower's lastRepay timestamp when the principal is fully written off, so the overdue check uses a stale value.
Impact
The staker loses their staked amount well before the overdue delay expires, and the borrower can repeat this to cause repeated losses.
What the attacker needed
A staker vouches for the borrower; borrower takes a loan; debt is written off; 90 days pass
The fix
Reset the borrower's lastRepay to zero when the debt is fully written off.
How it was found
- A staker stakes and vouches for a borrower.
- The borrower takes a loan, which updates lastRepay to the current block.
- The staker writes off the entire debt, but lastRepay is not reset.
- After 90 days, a staker vouches for the same borrower again.
- The borrower takes a new loan, but lastRepay remains at the old value.
- Any member can now write off the new loan immediately because the overdue check passes, and the staker loses their stake.
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 this lending system, a staker puts up money to back a borrower. If the borrower does not repay within a set time, the staker can write off the debt and lose their stake. The bug is that the system forgets to reset the timer when a debt is written off, so a new loan can be written off straight away, before the borrower has had a chance to repay.
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.