Skip to content
Business logic Audit finding Advanced

Incorrect formula used in function Market.computeClosingFactor()

The liquidation function uses an incorrect formula to compute the amount of debt to close, leading to wrong updates of the user's position. An attacker could exploit this to liquidate an incorrect amount of assets.

KIntern_NA Follow with carrotsmuggler and 0xRobocop

Originally disclosed on Code4rena

Submitted by KIntern_NA, also found by carrotsmuggler and 0xRobocop

Incorrect amount of assets that will be liquidated

Proof of Concept

Function BigBang._liquidateUser() is used to liquidate an under-collateralization position in the market. This function calls BigBang._updateBorrowAndCollateralShare() to calculate the amount of borrowPart and collateralShare that will be removed from the user’s position and update the storage.

The amount of borrowPart to be removed can be calculated using the function Market.computeClosingFactor(). This amount will then be converted to borrowAmount, which is the corresponding elastic amount, and be used to determine the amount of collateralShare that needs to be removed.

However, the returned value from Market.computeClosingFactor() is incorrect, which leads to the wrong update for the user’s position.

To prove the statement above, let’s denote:

  • x: The elastic amount that will be removed to execute the liquidation.

  • userElastic and userElastic': The elastic amount corresponding to userBorrowPart[user] before and after the liquidation.

  • collateralShare and collateralShare': The value of userCollateralShare[user] before and after the liquidation.

  • Following the implementation of yieldBox.toAmount() and yieldBox.toShare(), in one transaction we can denote that:

    • yieldBox.toAmount(): A multiplication expression with a constant C.
    • yieldBox.toShare(): A division expression with constant C.

Following the update of these variables depicted in the function BigBang._updateBorrowAndCollateralShare(), we have:

  • $userElastic' = userElastic - x$
  • $collateralShare' = collateralShare - \frac{x \times (1+liquidationMultiplier)*\frac{exchangeRate}{10^{18}}}{C}$

After the liquidation, the function Market._isSolvent(user) must return true. In other words, at least the following equation should hold:

  • $C \times (collateralShare' \times \frac{collateralRate}{10^5} \times \frac{10^{18}}{exchangeRate}) = userElastic'$

Solving the equation, we get:

  1. $C \times (collateralShare' \times \frac{collateralRate}{10^5} \times \frac{10^{18}}{exchangeRate}) = userElastic'$
  2. $C \times collateralShare \times \frac{collateralRate}{10^5} \times \frac{10^{18}}{exchangeRate} - x \times (1 + \frac{liquidationMultiplier}{10^5}) \times \frac{collateralizationRate}{10^5} = userElastic - x$
  3. $x = \frac{userElastic - C \times collateralShare \times \frac{collateralRate}{10^5} \times \frac{10^{18}}{exchangeRate}}{1 - (1 + \frac{liquidationMultiplier}{10^5}) * \frac{collateralizationRate}{10^5}}$

So, the returned value of the function Market.computeClosingFactor() should be the corresponding base amount of x (totalBorrow.toBase(x, false)).

Comparing it to the current implementation of computeClosingFactor(), we can see the issues are:

  • The implementation uses the borrowPart in the numerator instead of the corresponding elastic amount of borrowPart.
  • The multiplication with borrowPartDecimals and collateralPartDecimals doesn’t make sense since these decimals can be different and may cause the numerator to underflow.

Recommended Mitigation Steps

Correct the formula of function computeClosingFactor() following the section “Proof of Concept”.

cryptotechmaker (Tapioca) confirmed


The original report as published on Code4rena, by KIntern_NA. 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

Likely the formula uses borrowPart instead of the corresponding elastic amount and multiplies by decimals that may differ, causing incorrect or underflowing results.

Impact

The incorrect formula leads to wrong updates of the user's position during liquidation, potentially liquidating an incorrect amount of assets.

What the attacker needed

A position eligible for liquidation

The fix

The formula in computeClosingFactor() should be corrected as derived in the proof of concept.

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.

In a lending market, if a borrower's collateral falls below a required level, anyone can liquidate (close) part of their debt. The code that calculates how much debt to close used a flawed formula, so it could close the wrong amount. This could let someone liquidate more or less than intended, harming the borrower or the system.

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 KIntern_NA

View all

21 more disclosed reports, best first.

More from Tapioca Invitational

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.