Wrong calculation of repayment amount in Position contract
The Position contract calculates the repayment amount using the current borrow rate per block, which can change over time, rather than the rate that applied when the position was opened. This can cause the calculated repayment to be higher or lower than the actual amount owed, leading to excess asset being swapped or insufficient repayment.
Toshii Follow with immeas and 0xDING99YA
Originally disclosed on Code4rena
Submitted by Toshii, also found by immeas and 0xDING99YA.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L282 https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L82 https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L329-L331 https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L87
When a user calls closePosition(..) -> _repay(..) on the Position contract, the function borrowBalanceOfPos(..) is used to calculate the amount that the user needs to repay. This repayment amount is equivalent to the amount the user borrowed (_borrowedAmount) plus their interest (_interest). To calculate this interest portion, the function multiplies the current borrowRate by the difference in number of blocks between when the user created the position and the current block timestamp. The issue arises because the borrowRate over the entire duration of the borrow is defined as: rate = CTokenInterface(bathToken).borrowRatePerBlock();. However, borrowRatePerBlock() is a function of total borrows and total supply for the cToken; thus, is not required to be the same over the entire duration of when the user takes it out of their position. This means the calculated repayment amount can potentially overshoot/undershoot the actual amount.
Proof of Concept
Steps:
- Bob uses the
Positioncontract to open a leveraged position using thebuyAllAmountWithLeverage(..)function. This results in an increased borrow interest rate. - Later, Alice also uses the
Positioncontract to open a leveraged position using thebuyAllAmountWithLeverage(..)function. This again results in an increased borrow interest rate. - Bob calls
closePosition(..), where the call to_repay(..)references the currentborrowRatePerBlock(), which prices Bob’s interest at the rate which includes Bob and Alice’s borrows.
This results in excess asset being swapped to quote, rather than what is needed for repayment.
Recommended Mitigation Steps
Potentially keep track of changes in the supply/borrow state. However, this might not be worth the tradeoff in increased gas.
HickupHH3 (judge) increased severity to High and commented:
Incorrect interest calculation, consider this to be high severity because it affects repay amount.
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 interest is calculated using the current borrow rate per block for the entire duration, but that rate is not constant and depends on total borrows and supply.
Impact
The calculated repayment amount can be higher or lower than the actual amount, causing excess asset to be swapped or insufficient repayment.
What the attacker needed
A leveraged position in the Position contract; another user opens a position affecting the rate
The fix
The contract should track changes in the supply/borrow state to calculate interest accurately, though this may increase gas costs.
How it was found
- Bob opens a leveraged position using buyAllAmountWithLeverage, increasing the borrow interest rate.
- Alice also opens a leveraged position using buyAllAmountWithLeverage, further increasing the borrow interest rate.
- Bob calls closePosition, which calls _repay and uses the current borrowRatePerBlock, which now includes both Bob's and Alice's borrows, to calculate Bob's interest.
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, the interest rate can change over time. This contract calculates how much you owe using today's interest rate for the whole borrowing period, instead of the rates that actually applied each day. That means you might pay too much or too little when closing your position.
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.