Inconsistent Use of `lastCumulativeRate` in `depositTokens()` and `withdraw()` Functions in `Borrowings` Contract
The depositTokens() and withdraw() functions in the Borrowings contract use the stored lastCumulativeRate before updating it via calculateCumulativeRate(), so the first transaction after a time gap uses an outdated interest rate. This can cause incorrect interest calculations and discrepancies in normalized deposit amounts and borrower debt.
0x37 Follow with 0x73696d616f, 0xAadi and 13 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-11-autonomint-judging/issues/961
Found by
0x37, 0x73696d616f, 0xAadi, Cybrid, EgisSecurity, John44, LZ_security, OrangeSantra, Pablo, RampageAudit, Ruhum, almurhasan, gss1, pashap9990, super_jack, t.aksoy
Summary
The depositTokens() and withdraw() functions in the Borrowing contract exhibit inconsistent use of the lastCumulativeRate. This inconsistency arises because the lastCumulativeRate is used in calculations before it is updated, leading to potential inaccuracies in interest calculations among multiple transactions.
Root Cause
In both depositTokens() and withdraw() functions, lastCumulativeRate is passed to BorrowLib.deposit() and BorrowLib.withdraw() respectively, before calling calculateCumulativeRate() to update it.
totalNormalizedAmount = BorrowLib.deposit(
BorrowLibDeposit_Params(
LTV,
APR,
@> lastCumulativeRate,
totalNormalizedAmount,
exchangeRate,
ethPrice,
lastEthprice
),
depositParam,
Interfaces(treasury, globalVariables, usda, abond, cds, options),
assetAddress
);
//Call calculateCumulativeRate() to get currentCumulativeRate
@> calculateCumulativeRate();
lastEventTime = uint128(block.timestamp);
BorrowWithdraw_Result memory result = BorrowLib.withdraw(
depositDetail,
BorrowWithdraw_Params(
toAddress,
index,
ethPrice,
exchangeRate,
withdrawTime,
lastCumulativeRate,
totalNormalizedAmount,
bondRatio,
collateralRemainingInWithdraw,
collateralValueRemainingInWithdraw
),
Interfaces(treasury, globalVariables, usda, abond, cds, options)
);
.
.
lastEventTime = uint128(block.timestamp);}
// Call calculateCumulativeRate function to get the interest
@> calculateCumulativeRate();
And the lastCumulativeRate is used to calculate normalizedAmount and borrowerDebt
// Calculate normalizedAmount
uint256 normalizedAmount = calculateNormAmount(tokensToMint, libParams.lastCumulativeRate);
// Calculate th borrower's debt
uint256 borrowerDebt = calculateDebtAmount(depositDetail.normalizedAmount, params.lastCumulativeRate);
This results in using an outdated cumulative rate for the first transaction, while subsequent transactions use the updated rate, causing discrepancies.
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
- Allow a significant time gap since the last transaction to accumulate interest.
- Execute two simultaneous transactions (e.g., deposits or withdrawals).
- Observe that the first transaction uses an outdated
lastCumulativeRate, while the second uses the updated rate.
Impact
The first transaction uses an outdated cumulative rate, while subsequent transactions use the updated cumulative rate, leading to incorrect interest calculations. This discrepancy causes inconsistencies in the normalized deposit amount and borrower debt, potentially affecting the protocol's financial accuracy .
PoC
No response
Mitigation
Ensure that calculateCumulativeRate() is called at the beginning of both the depositTokens() and withdraw() functions to update lastCumulativeRate before it is used in any calculations. This ensures that all transactions use the most current cumulative rate.
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
Likely the functions call BorrowLib.deposit() and BorrowLib.withdraw() with the stored lastCumulativeRate before calling calculateCumulativeRate() to refresh it.
Impact
The first transaction uses an outdated cumulative rate, potentially causing incorrect interest calculations and inconsistencies in normalized deposit amounts and borrower debt.
What the attacker needed
A significant time gap since last transaction; two simultaneous transactions
The fix
calculateCumulativeRate() should be called at the beginning of both depositTokens() and withdraw() to update lastCumulativeRate before it is used.
How it was found
- Allow a significant time gap since the last transaction to accumulate interest.
- Execute two simultaneous transactions (e.g., deposits or withdrawals).
- Observe that the first transaction uses an outdated lastCumulativeRate, while the second uses the updated rate.
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 a lending protocol, interest builds up over time and is tracked by a cumulative rate. The contract updates this rate only after using it, so the first action after a pause uses old interest data, which can make the numbers slightly wrong.
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.