Skip to content
DoS Audit finding Intermediate

Tokens can’t be removed as a collateral without breaking liquidations and other core functions

The protocol uses a token's collateral factor of zero to mean it is not accepted as collateral, but setting it to zero for an already-approved token breaks liquidation and health checks because calculations divide by that zero factor. An attacker could not directly exploit this, but the admin action of removing a collateral would cause liquidations to revert, locking funds.

iamandreiski Follow with 0xAlix2

Originally disclosed on Code4rena

Submitted by iamandreiski, also found by 0xAlix2

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Vault.sol#L856-L866

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Vault.sol#L1197-L1202

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Vault.sol#L1270-L1278

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Vault.sol#L702-L703

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Vault.sol#L1090-L1120

Impact

The core mechanism that Revert utilizes to prevent arbitrary tokens to be utilized as collateral is by the default setting of the collateralFactor. Since the collateralFactor for all tokens (besides the ones approved for usage in the system, and set by admins) is 0, that means that no one can borrow against a collateral which hasn’t been approved.

The problem arises when admins would want a collateral removed from the system. There would be multiple reasons as to why this might be the case:

  • The underlying collateral has become too volatile.
  • The DAO in charge of Revert protocol decides to remove it as a collateral.
  • There has been some kind of a change in the mechanics of how that token operates and Revert wants it removed (e.g. upgradeable tokens, most-popular examples include USDC/USDT).

The only way in which this could be performed is to set the collateralFactor back to 0, but this would break core mechanics such as liquidations.

Proof of Concept

All approved collateral which admins decided should be utilized inside of the protocol is introduced by increasing the collateralFactor through setTokenConfig():

 function setTokenConfig(address token, uint32 collateralFactorX32, uint32 collateralValueLimitFactorX32)
        external
        onlyOwner
    {
        if (collateralFactorX32 > MAX_COLLATERAL_FACTOR_X32) {
            revert CollateralFactorExceedsMax();
        }
        tokenConfigs[token].collateralFactorX32 = collateralFactorX32;
        tokenConfigs[token].collateralValueLimitFactorX32 = collateralValueLimitFactorX32;
        emit SetTokenConfig(token, collateralFactorX32, collateralValueLimitFactorX32);
    }

Once a token’s collateralFactor was set, and removed afterward due to extraordinary circumstances, all outstanding loans will never be able to be liquidated either due to panic reverts because of overflow/underflow or panic reverts due to division/modulo by 0.

The problem arises when _checkLoanIsHealthy() is called within liquidate() (the same can be tested by calling loanInfo() as well, since it also calls _checkLoanIsHealthy()).

  function _checkLoanIsHealthy(uint256 tokenId, uint256 debt)
        internal
        view
        returns (bool isHealthy, uint256 fullValue, uint256 collateralValue, uint256 feeValue)
    {
        (fullValue, feeValue,,) = oracle.getValue(tokenId, address(asset));
        uint256 collateralFactorX32 = _calculateTokenCollateralFactorX32(tokenId);
        collateralValue = fullValue.mulDiv(collateralFactorX32, Q32);
        isHealthy = collateralValue >= debt;
    }

This happens because when calculating the collateralValue through _checkLoanIsHealthy as it can be seen here:

function _checkLoanIsHealthy(uint256 tokenId, uint256 debt)
        internal
        view
        returns (bool isHealthy, uint256 fullValue, uint256 collateralValue, uint256 feeValue)
    {
        (fullValue, feeValue,,) = oracle.getValue(tokenId, address(asset));
        uint256 collateralFactorX32 = _calculateTokenCollateralFactorX32(tokenId);
        collateralValue = fullValue.mulDiv(collateralFactorX32, Q32);
        isHealthy = collateralValue >= debt;
    }

Since it’s needed to calculate the liquidation collateral value as it can be seen in the liquidate() function:

(state.isHealthy, state.fullValue, state.collateralValue, state.feeValue) =
            _checkLoanIsHealthy(params.tokenId, state.debt);

Since the collateral factor will be 0, the collateralValue will also be 0, this will lead to passing 0 as a value in the _calculateLiquidation() function:

 (state.liquidationValue, state.liquidatorCost, state.reserveCost) =
            _calculateLiquidation(state.debt, state.fullValue, state.collateralValue);

Which will always revert due to division with 0:

unction _calculateLiquidation(uint256 debt, uint256 fullValue, uint256 collateralValue)
        internal
        pure
        returns (uint256 liquidationValue, uint256 liquidatorCost, uint256 reserveCost)
    {
            ...
            uint256 startLiquidationValue = debt * fullValue / collateralValue;
            ...

This can be tested via PoC, by inserting the following line in the testLiquidation() test in V3Vault.t.sol:

vault.setTokenConfig(address(USDC), 0, type(uint32).max); vault.setTokenConfig(address(DAI), 0, type(uint32).max)

You can change USDC/DAI one or both with whatever collateral is part of the NFT in question and run testLiquidationTimeBased().

Recommended Mitigation Steps

Don’t use the collateralFactor as the common denominator whether a token is accepted as collateral or not; use a method such as whitelisting tokens by address and performing necessary checks to see if the token address matches the whitelist.

Assessed type

DoS

kalinbas (Revert) confirmed, but disagreed with severity

ronnyx2017 (judge) decreased severity to Medium and commented:

Valid DOS, but needs admin config upgrade. Since the modification of this config is reasonable and normal, it is classified as Medium.

Revert mitigated:

Fixed here.

Status: Mitigation Confirmed. Full details in reports from b0g0, thank_you and ktg.


The original report as published on Code4rena, by iamandreiski. 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 design uses the collateral factor as both a risk parameter and an acceptance flag, so setting it to zero for an existing collateral causes division by zero in health and liquidation calculations.

Impact

Liquidations and loan health checks for affected tokens would revert, potentially leaving bad debt uncollectable, but this requires an admin action to remove a collateral.

What the attacker needed

Admin sets collateral factor to zero

The fix

The protocol was fixed by no longer using the collateral factor as the sole indicator of whether a token is accepted as collateral, instead using a whitelist or similar mechanism.

How it was found

  1. Admin sets a token's collateral factor to zero via setTokenConfig to remove it as collateral.
  2. A liquidation is attempted on a loan using that token as collateral.
  3. The health check computes collateral value as zero because the factor is zero.
  4. The liquidation calculation divides by the zero collateral value and reverts.

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 this lending protocol, each token has a 'collateral factor' that says how much you can borrow against it. A factor of zero means 'not accepted as collateral'. The problem is that if the admin later sets an already-used token's factor to zero to remove it, the code that checks loan health and calculates liquidation amounts divides by that zero, causing errors. So removing a token this way breaks liquidations for all existing loans using that token.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by iamandreiski

View all

5 more disclosed reports, best first.

More from Revert Lend

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.