Use of block.number leads to incorrect interest calculations
Interest owed on a borrowed position is calculated from the number of blocks elapsed rather than elapsed time, and on Optimism blocks are not produced at a constant rate. This means the interest a user repays can be higher than expected and the rate is manipulable.
Submitted by joestakey, also found by 0xkazim, nirlin, VAD37 and ladboy233.
The interests that need to be paid upon closing a position are computed as follows:
File: contracts/utilities/poolsUtility/Position.sol
322: function _calculateDebt(
323: address _bathToken,
324: uint256 _startBlock,
325: uint256 _borrowedAmount
326: ) internal view returns (uint256 _debt) {
327: uint256 _blockDelta = block.number - _startBlock;
328:
329: uint256 _interest = (
330: (_borrowedAmount).mul(borrowRate(_bathToken).mul(_blockDelta))
331: ).div(10 ** 18);
332: _debt = _borrowedAmount.add(_interest);
333: }
The interest rate is computed using the block delta, between block.number and the starting block.
The issue is that the block production on Optimism is currently not fixed:
each transaction on L2 is placed in a separate block and blocks are NOT produced at a constant rate
Proof of Concept
Run the following command several times in the repo, using an optimism rpc url:
cast block-number --rpc-url YOUR_OPTIMISM_RPC_URL
You will see that the blocks are not produced at a constant rate.
Impact
Interest rates are currently manipulable and will result in users having to repay more interest than expected upon closing positions.
Mitigation
Use block.timestamp instead of block.number for interest calculation. It will involve a lot of refactoring as the Compound contracts bathToken currently inherits from using a “per block” system.
daoio (Rubicon) acknowledged via duplicate issue #1212
HickupHH3 (judge) commented via duplicate issue #1212:
This issue is dependent on how the
borrowRatePerBlock(and supply rate) is configured.If there’s proof that the sponsor configures it with the assumption that each block is produced every 12s on mainnet, then it’s High severity. On the other hand, if it can be shown that they have configured the rates to be compatible with Optimism & Arbitrum, then it’s a low issue.
Considering it to be Medium severity because of this external requirement.
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 interest formula uses the block-number difference as a proxy for elapsed time, which is unreliable on Optimism where block production is not at a constant rate.
Impact
Users closing positions could be charged more interest than expected, and the interest rate is manipulable; the judge rated it Medium because the severity depends on how the rates were configured.
What the attacker needed
Borrowing on Optimism; position closed after borrowing
The fix
Interest should be calculated from block timestamps rather than block numbers, though this requires substantial refactoring of the inherited Compound contracts.
How it was found
- Query the current block number on an Optimism RPC endpoint repeatedly.
- Observe that blocks are not produced at a constant 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.
When you borrow money, interest grows over time. This code measures that time in 'blocks', the batches of transactions added to the blockchain, instead of seconds. On the Optimism network blocks do not arrive at a steady pace, so counting blocks can overstate how much time passed and charge too much interest.
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.