Skip to content
Business logic Audit finding Intermediate

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.

Bahurum Follow

Originally disclosed on Sherlock

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

  1. A staker stakes and vouches a borrower
  2. the borrower borrows calling UToken:borrow: accountBorrows[borrower].lastRepay is updated with the current block number
  3. the staker writes off the entire debt of the borrower calling UserManager:debtWriteOff. In the internal call to UToken:debtWriteOff the principal is set to zero but accountBorrows[borrower].lastRepay is not updated
  4. 90 days pass and a staker vouches for the same borrower
  5. the borrower borrows calling UToken:borrow: accountBorrows[borrower].lastRepay is not set to the current block since non zero and stays to the previous value.
  6. accountBorrows[borrower].lastRepay is 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.

Solidity
        if (block.number <= lastRepay + overdueBlocks + maxOverdueBlocks) {
            if (staker != msg.sender) revert AuthFailed();
        }
  1. 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

https://github.com/sherlock-audit/2022-10-union-finance/blob/main/union-v2-contracts/contracts/market/UToken.sol#L666-L672

Solidity
    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

Diff
    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;
    }

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

  1. A staker stakes and vouches for a borrower.
  2. The borrower takes a loan, which updates lastRepay to the current block.
  3. The staker writes off the entire debt, but lastRepay is not reset.
  4. After 90 days, a staker vouches for the same borrower again.
  5. The borrower takes a new loan, but lastRepay remains at the old value.
  6. 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.

More by Bahurum

View all

10 more disclosed reports, best first.

More from Union Finance

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.