leverageStrategy will revert due users interest rate accrual
The leverageStrategy function checks that a user's principal borrow amount increases by exactly the new borrow amount, but the principal also grows from accrued interest, so the check fails. As a result, the leverageStrategy function reverts almost always, blocking users from using it.
Source: https://github.com/sherlock-audit/2024-12-numa-audit-judging/issues/120
Found by
KupiaSec, jokr
Summary
In the CNumaToken.leverageStrategy() function, after borrowing from the market using the borrowInternalNoTransfer function, a check is performed to ensure that the user's borrow amount changes only by borrowAmount using a require statement. However, this check will fail because the user's principal borrow amount will increase by more than borrowAmount due to the interest accrued on the user's borrow position.
Root Cause
uint accountBorrowBefore = accountBorrows[msg.sender].principal;
// Borrow but do not transfer borrowed tokens
borrowInternalNoTransfer(borrowAmount, msg.sender);
// uint accountBorrowAfter = accountBorrows[msg.sender].principal;
require(
(accountBorrows[msg.sender].principal - accountBorrowBefore) ==
borrowAmount,
"borrow ko"
);
The require statement above will always fail since the user's previous borrow amount accrues interest, causing the principal borrow amount to increase beyond borrowAmount.
Even if the global interest rate index is updated, the user's borrow position will only accrue interest when their borrow position is touched as below.
function borrowBalanceStoredInternal(
address account
) internal view returns (uint) {
/* Get borrowBalance and borrowIndex */
BorrowSnapshot storage borrowSnapshot = accountBorrows[account];
/* If borrowBalance = 0 then borrowIndex is likely also 0.
* Rather than failing the calculation with a division by 0, we immediately return 0 in this case.
*/
if (borrowSnapshot.principal == 0) {
return 0;
}
/* Calculate new borrow balance using the interest index:
* recentBorrowBalance = borrower.borrowBalance * market.borrowIndex / borrower.borrowIndex
*/
uint principalTimesIndex = borrowSnapshot.principal * borrowIndex;
return principalTimesIndex / borrowSnapshot.interestIndex;
}
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
- Suppose the user has an existing borrow amount of 100. Hence,
accountBorrows[msg.sender].principal = 100. - The user calls the
leverageStrategyfunction to borrow an additional 50 from the market. - During the borrowing process, interest will accrue on the existing borrow amount. For example, the principal borrow amount will increase to 150.2 (existing borrow = 100, new borrow = 50, accrued interest = 0.2).
- The
requirestatement(accountBorrows[msg.sender].principal - accountBorrowBefore) == borrowAmountwill then fail because the principal borrow amount includes the accrued interest, making the difference greater thanborrowAmount.
Impact
leverageStrategy function will fail almost always.
PoC
No response
Mitigation
Instead of directly fetching the user's previous borrow amount from the state using accountBorrows[msg.sender].principal, use the borrowBalanceStored() function. This function accounts for accrued interest and provides the correct previous borrow balance, ensuring that the require statement works as intended.
- uint accountBorrowBefore = accountBorrows[msg.sender].principal;
+ uint accountBorrowBefore = borrowBalanceStored(msg.sender);
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 code compares the raw stored principal, which does not include accrued interest, against the new borrow amount, so any accrued interest makes the difference larger than expected.
Impact
The leverageStrategy function will fail almost always, preventing users from using it.
What the attacker needed
User has an existing borrow position; interest has accrued on that position
The fix
The previous borrow amount should be read using borrowBalanceStored, which includes accrued interest, instead of the raw principal.
How it was found
- The user has an existing borrow amount of 100, so the stored principal is 100.
- The user calls leverageStrategy to borrow an additional 50.
- Interest accrues on the existing borrow, making the principal increase to more than 150.
- The require statement checks that the principal difference equals the new borrow amount, but it is larger, so the transaction reverts.
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 someone borrows money in this lending system, the amount they owe slowly grows because of interest. The code tries to confirm that a new borrow only adds the new amount, but it forgets that interest has already made the old amount bigger. So the check fails and the borrowing feature stops working.
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.