Interest rates are incorrect on Liquidation
The liquidation function transfers the repaid debt asset to the xToken before calling updateInterestRates, so the interest rate calculation counts the same liquidity twice. This makes the computed currentLiquidityRate higher than it should be.
Anurag Jain Follow with unforgiven and cccz
Originally disclosed on Code4rena
Submitted by csanuragjain, also found by unforgiven and cccz
The debt tokens are being transferred before calculating the interest rates. But the interest rate calculation function assumes that debt token has not yet been sent thus the outcome currentLiquidityRate will be incorrect
Proof of Concept
- Liquidator L1 calls
executeLiquidateERC20for a position whose health factor <1
function executeLiquidateERC20(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(address => DataTypes.UserConfigurationMap) storage usersConfig,
DataTypes.ExecuteLiquidateParams memory params
) external returns (uint256) {
...
_burnDebtTokens(liquidationAssetReserve, params, vars);
...
}
- This internally calls
_burnDebtTokens
function _burnDebtTokens(
DataTypes.ReserveData storage liquidationAssetReserve,
DataTypes.ExecuteLiquidateParams memory params,
ExecuteLiquidateLocalVars memory vars
) internal {
...
// Transfers the debt asset being repaid to the xToken, where the liquidity is kept
IERC20(params.liquidationAsset).safeTransferFrom(
vars.payer,
vars.liquidationAssetReserveCache.xTokenAddress,
vars.actualLiquidationAmount
);
...
// Update borrow & supply rate
liquidationAssetReserve.updateInterestRates(
vars.liquidationAssetReserveCache,
params.liquidationAsset,
vars.actualLiquidationAmount,
0
);
}
- Basically first it transfers the debt asset to xToken using below. This increases the balance of xTokenAddress for liquidationAsset
IERC20(params.liquidationAsset).safeTransferFrom(
vars.payer,
vars.liquidationAssetReserveCache.xTokenAddress,
vars.actualLiquidationAmount
);
- Now
updateInterestRatesfunction is called on ReserveLogic.sol#L169
function updateInterestRates(
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache,
address reserveAddress,
uint256 liquidityAdded,
uint256 liquidityTaken
) internal {
...
(
vars.nextLiquidityRate,
vars.nextVariableRate
) = IReserveInterestRateStrategy(reserve.interestRateStrategyAddress)
.calculateInterestRates(
DataTypes.CalculateInterestRatesParams({
liquidityAdded: liquidityAdded,
liquidityTaken: liquidityTaken,
totalVariableDebt: vars.totalVariableDebt,
reserveFactor: reserveCache.reserveFactor,
reserve: reserveAddress,
xToken: reserveCache.xTokenAddress
})
);
...
}
- Finally call to
calculateInterestRatesfunction on DefaultReserveInterestRateStrategy#L127 contract is made which calculates the interest rate
function calculateInterestRates(
DataTypes.CalculateInterestRatesParams calldata params
) external view override returns (uint256, uint256) {
...
if (vars.totalDebt != 0) {
vars.availableLiquidity =
IToken(params.reserve).balanceOf(params.xToken) +
params.liquidityAdded -
params.liquidityTaken;
vars.availableLiquidityPlusDebt =
vars.availableLiquidity +
vars.totalDebt;
vars.borrowUsageRatio = vars.totalDebt.rayDiv(
vars.availableLiquidityPlusDebt
);
vars.supplyUsageRatio = vars.totalDebt.rayDiv(
vars.availableLiquidityPlusDebt
);
}
...
vars.currentLiquidityRate = vars
.currentVariableBorrowRate
.rayMul(vars.supplyUsageRatio)
.percentMul(
PercentageMath.PERCENTAGE_FACTOR - params.reserveFactor
);
return (vars.currentLiquidityRate, vars.currentVariableBorrowRate);
}
- As we can see in above code,
vars.availableLiquidityis calculated asIToken(params.reserve).balanceOf(params.xToken) +params.liquidityAdded - params.liquidityTaken - But the problem is that debt token is already transferred to
xTokenwhich meansxTokenalready consist ofparams.liquidityAdded. Hence the calculation ultimately becomes(xTokenBeforeBalance+params.liquidityAdded) +params.liquidityAdded - params.liquidityTaken - This is incorrect and would lead to higher
vars.availableLiquiditywhich ultimately impacts thecurrentLiquidityRate
Recommended Mitigation Steps
Transfer the debt asset post interest calculation
function _burnDebtTokens(
DataTypes.ReserveData storage liquidationAssetReserve,
DataTypes.ExecuteLiquidateParams memory params,
ExecuteLiquidateLocalVars memory vars
) internal {
IPToken(vars.liquidationAssetReserveCache.xTokenAddress)
.handleRepayment(params.liquidator, vars.actualLiquidationAmount);
// Burn borrower's debt token
vars
.liquidationAssetReserveCache
.nextScaledVariableDebt = IVariableDebtToken(
vars.liquidationAssetReserveCache.variableDebtTokenAddress
).burn(
params.borrower,
vars.actualLiquidationAmount,
vars.liquidationAssetReserveCache.nextVariableBorrowIndex
);
liquidationAssetReserve.updateInterestRates(
vars.liquidationAssetReserveCache,
params.liquidationAsset,
vars.actualLiquidationAmount,
0
);
IERC20(params.liquidationAsset).safeTransferFrom(
vars.payer,
vars.liquidationAssetReserveCache.xTokenAddress,
vars.actualLiquidationAmount
);
...
...
}
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 debt asset is transferred to the xToken before updateInterestRates is called, so calculateInterestRates adds liquidityAdded to a balance that already includes it.
Impact
The liquidity rate is calculated too high after a liquidation, which could distort interest rates for the pool.
What the attacker needed
A position with health factor below one; a liquidator calls executeLiquidateERC20
The fix
The debt asset should be transferred after the interest rate calculation, not before.
How it was found
- A liquidator calls executeLiquidateERC20 for a position whose health factor is below one.
- The function calls _burnDebtTokens, which transfers the repaid debt asset to the xToken address.
- _burnDebtTokens then calls updateInterestRates with the same amount as liquidityAdded.
- updateInterestRates calls calculateInterestRates, which reads the xToken balance that already includes the transferred amount.
- The available liquidity is therefore counted twice, producing an incorrect currentLiquidityRate.
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 protocol, interest rates depend on how much money is available to lend. This code moves the repaid money into the pool before asking the pool to recalculate rates, and the calculation then adds that same money again. The result is a wrong, inflated interest rate.
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.