Skip to content
Business logic Audit finding Intermediate

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:

  1. Bob uses the Position contract to open a leveraged position using the buyAllAmountWithLeverage(..) function. This results in an increased borrow interest rate.
  2. Later, Alice also uses the Position contract to open a leveraged position using the buyAllAmountWithLeverage(..) function. This again results in an increased borrow interest rate.
  3. Bob calls closePosition(..), where the call to _repay(..) references the current borrowRatePerBlock(), 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.

daoio (Rubicon) confirmed

HickupHH3 (judge) increased severity to High and commented:

Incorrect interest calculation, consider this to be high severity because it affects repay amount.


The original report as published on Code4rena, by Toshii. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Bob opens a leveraged position using buyAllAmountWithLeverage, increasing the borrow interest rate.
  2. Alice also opens a leveraged position using buyAllAmountWithLeverage, further increasing the borrow interest rate.
  3. 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.

More by Toshii

View all

4 more disclosed reports, best first.

More from Rubicon v2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.