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
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.
Fixed here.
Status: Mitigation Confirmed. Full details in reports from b0g0, thank_you and ktg.
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
- Admin sets a token's collateral factor to zero via setTokenConfig to remove it as collateral.
- A liquidation is attempted on a loan using that token as collateral.
- The health check computes collateral value as zero because the factor is zero.
- 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.