Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of block.number leads to incorrect interest calculations #1141

Open
code423n4 opened this issue Apr 13, 2023 · 3 comments
Open

Use of block.number leads to incorrect interest calculations #1141

code423n4 opened this issue Apr 13, 2023 · 3 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working M-09 primary issue Highest quality submission among a set of duplicates satisfactory satisfies C4 submission criteria; eligible for awards selected for report This submission will be included/highlighted in the audit report

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2023-04-rubicon/blob/511636d889742296a54392875a35e4c0c4727bb7/contracts/utilities/poolsUtility/Position.sol#L327

Vulnerability details

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 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 block are not produced at a constant rate.

Impact

Interest rates are currently manipulable and will result in users having to repay more interests than expected upon closing positions.

Tools Used

Manual Analysis

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 use a "per block" system.

@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Apr 13, 2023
code423n4 added a commit that referenced this issue Apr 13, 2023
@c4-pre-sort
Copy link

0xSorryNotSorry marked the issue as duplicate of #1212

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jun 6, 2023
@c4-judge
Copy link
Contributor

c4-judge commented Jun 6, 2023

HickupHH3 marked the issue as satisfactory

@c4-judge
Copy link
Contributor

c4-judge commented Jun 8, 2023

HickupHH3 marked the issue as selected for report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working M-09 primary issue Highest quality submission among a set of duplicates satisfactory satisfies C4 submission criteria; eligible for awards selected for report This submission will be included/highlighted in the audit report
Projects
None yet
Development

No branches or pull requests

4 participants