Skip to content
Business logic Audit finding Intermediate

borrow must accrueInterest first

The borrow function checks the user's borrowed balance and credit limit before accruing interest, so the checks use stale values. In low-activity markets, a borrower could borrow more than the maximum borrow amount and more than their credit limit.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

The UToken.borrow function first checks the borrowed balance and the old credit limit before accruing the actual interest on the market:

// @audit this uses the old value
require(borrowBalanceView(msg.sender) + amount + fee <= maxBorrow, "UToken: amount large than borrow size max");

require(
    // @audit this calls uToken.calculateInterest(account) which returns old value
    uint256(_getCreditLimit(msg.sender)) >= amount + fee,
    "UToken: The loan amount plus fee is greater than credit limit"
);

// @audit accrual only happens here
require(accrueInterest(), "UToken: accrue interest failed");

Thus the borrowed balance of the user does not include the latest interest as it uses the old global borrowIndex but the new borrowIndex is only set in accrueInterest.

Impact

In low-activity markets, it could be that the borrowIndex accruals (accrueInterest calls) happen infrequently and a long time is between them. A borrower could borrow tokens, and borrow more tokens later at a different time without first having their latest debt accrued. This will lead to borrowers being able to borrow more than maxBorrow and more than their credit limit as these checks are performed before updating accruing interest.

Recommended Mitigation Steps

The require(accrueInterest(), "UToken: accrue interest failed"); call should happen at the beginning of the function.

GeraldHost (Union Finance) confirmed

GalloDaSballo (judge) commented:

Agree with the finding, this fundamentally breaks the accounting of the protocol

In protocols that calculate interest, and that have to recalculate state after something changed, it is vital that you accrue all changes up to this point before proceeding with any other state-changing logic

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

How it worked

Root cause

The borrow function performs balance and credit limit checks before calling accrueInterest, so it uses an outdated borrowIndex.

Impact

In low-activity markets, a borrower could borrow more than maxBorrow and more than their credit limit.

What the attacker needed

Low-activity market; borrower with existing debt

The fix

The accrueInterest call should be moved to the beginning of the borrow function.

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.

When you borrow money, interest adds to what you owe over time. This code checks how much you owe before adding the latest interest, so it thinks you owe less than you really do. That lets someone borrow more than they should be allowed.

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 cmichel

View all

196 more disclosed reports, best first.

More from Union Finance contest

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.