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.
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
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.