Incorrect Update of `calculateCumulativeRate` in `_withdraw` Results in Erroneous Interest Calculation
In the `_withdraw` function, `lastEventTime` is updated to the current block timestamp before calling `calculateCumulativeRate`, causing the time interval to be zero and interest not to accrue for that period. This results in missed interest for all users on each withdrawal.
Source: https://github.com/sherlock-audit/2024-11-autonomint-judging/issues/1003
Found by
0x37, 0xAadi, Abhan1041, Cybrid, EgisSecurity, LordAlive, ParthMandale, RampageAudit, Silvermist, futureHack, newspacexyz, nuthan2x, onthehunt, smbv-1923, super_jack, vatsal, volodya
Summary
In the Borrowing::_withdraw function, the calculateCumulativeRate method is invoked after the lastEventTime variable is updated to the current block.timestamp. Here's the relevant portion of the function:
function _withdraw(
address toAddress,
uint64 index,
bytes memory odosAssembledData,
uint64 ethPrice,
uint128 exchangeRate,
uint64 withdrawTime
) internal {
...
lastEventTime = uint128(block.timestamp);
}
// Call calculateCumulativeRate function to get the interest
calculateCumulativeRate();
...
}
The calculateCumulativeRate function internally calls a method from BorrowLib and passes the lastEventTime—which was updated just before this call—as a parameter. Here's the code for calculateCumulativeRate:
function calculateCumulativeRate() public returns (uint256) {
uint128 noOfBorrowers = treasury.noOfBorrowers();
uint256 currentCumulativeRate = BorrowLib.calculateCumulativeRate(
noOfBorrowers,
ratePerSec,
lastEventTime,
lastCumulativeRate
);
lastCumulativeRate = currentCumulativeRate;
return currentCumulativeRate;
}
In BorrowLib.calculateCumulativeRate, the currentCumulativeRate is calculated using the difference between the current block.timestamp and the lastEventTime. This difference is referred to as timeInterval. However, since lastEventTime is updated to block.timestamp before the call to calculateCumulativeRate, the timeInterval becomes 0. This causes the interest to remain unchanged, as no meaningful time difference is accounted for in the calculations. This issue prevents the proper update of the cumulative rate and leaves it at its previous value.
Root Cause
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
The interest calculation relies on the timeInterval, which is the difference between the current time and lastEventTime. In the _withdraw function, lastEventTime is updated to block.timestamp before the interest is calculated, resulting in no actual update to the interest.
As a result, each time the _withdraw function is called, the interest for the period (current time - lastEventTime before the function call) is not properly accounted for.
Impact
The interest is not being updated correctly in the _withdraw function, causing the interest for that time interval to be missed for all users.
PoC
No response
Mitigation
The protocol should ensure that calculateCumulativeRate is updated before lastEventTime is set to block.timestamp, allowing the interest to be calculated accurately.
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 order of operations is wrong: `lastEventTime` is set to the current timestamp before `calculateCumulativeRate` uses it to compute the elapsed time.
Impact
Interest for the period since the last update is not accounted for, causing all users to miss interest on each withdrawal.
What the attacker needed
None
The fix
The protocol should call `calculateCumulativeRate` before updating `lastEventTime` to `block.timestamp`.
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.
This is a smart contract bug where interest is calculated based on how much time has passed. The code updates the 'last event time' to now before calculating interest, so the time difference becomes zero and no interest is added. As a result, users lose out on interest they should have earned.
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.