Liquidations will revert incorrectly due to an out-of-sync leftover collateral value
The liquidation function computes a leftover collateral value using an optimistic fee, then recomputes the liquidator's collateral with a lower dirty fee but never updates the leftover value. As a result, valid liquidations can revert with a bad dirty liquidation error even when the real leftover collateral is above the minimum.
Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/74
Found by
X77
Summary
Liquidations will revert incorrectly due to an out-of-sync value
Root Cause
Upon liquidations, we have this code:
_leftoverCollateral = (_userCollateralBalance.toInt256() - _optimisticCollateralForLiquidator.toInt256());
_collateralForLiquidator = _leftoverCollateral <= 0 ? _userCollateralBalance : (_liquidationAmountInCollateralUnits * (LIQ_PRECISION + dirtyLiquidationFee)) / LIQ_PRECISION;
We compute an optimistic collateral for the liquidator which is based on cleanLiquidationFee. If the leftover collateral is not below 0, we recompute the collateral for liquidator based on the dirtyLiquidationFee which is a value lower than the clean fee (if we take a look at Fraxlend, it is 9000 vs 10000 for the clean fee).
Then, we have this code:
if (_leftoverCollateral <= 0) {
...
} else if (_leftoverCollateral < minCollateralRequiredOnDirtyLiquidation.toInt256()) {
revert BadDirtyLiquidation();
}
If the leftover collateral is <=, we end up in the first block where we compute shares to adjust. If it is above 0 however, we will revert if we are under minCollateralRequiredOnDirtyLiquidation. The issue is that the leftover collateral is still based on the optimistic calculation which results in a much lower leftover collateral, resulting in reverts when the actual leftover collateral is not even close to minCollateralRequiredOnDirtyLiquidation.
Internal Pre-conditions
No response
External Pre-conditions
No response
Attack Path
- Let's imagine the following state:
- userCollateralBalance = 2.3e18
- dirtyLiquidationFee = 9_000 (based on Fraxlend)
- cleanLiquidationFee = 10_000 (based on Fraxlend)
_liquidationAmountInCollateralUnitsis equal to 2e18 based on the shares to liquidate provided by the userminCollateralRequiredOnDirtyLiquidationis 1.1e17
_optimisticCollateralForLiquidatorwill equal2e18 * (1e5 + 1e4) / 1e5 = 2.2e18_leftoverCollateralequals2.3e18 - 2.2e18 = 1e17- As it is above 0, we recompute the collateral for liquidator which will equal
2e18 * (1e5 + 9e3) / 1e5 = 2.18e18 - As the leftover collateral is 1e17 and the minimum required collateral is 1.1e17, we will revert
- In reality, the actual leftover collateral is
2.3e18 - 2.18e18 = 1.2e17as the collateral we will take out from the user is 2.18e18, not 2.2e18
Impact
Liquidations will revert incorrectly which is an extremely time-sensitive operation, this can lead to bad debt for the protocol
PoC
No response
Mitigation
Update the leftover collateral based on the new collateral for the liquidator
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 leftover collateral variable is calculated with the optimistic clean fee and is not recalculated after the liquidator's collateral is recomputed with the dirty fee.
Impact
Liquidations can revert incorrectly, and since liquidation is time-sensitive this could lead to bad debt for the protocol.
What the attacker needed
A liquidation with leftover collateral above zero; dirty fee lower than clean fee
The fix
The leftover collateral should be recalculated using the new collateral for the liquidator before the minimum check.
How it was found
- Set up a user collateral balance, a dirty fee lower than the clean fee, a liquidation amount and a minimum required collateral.
- Compute the optimistic collateral for the liquidator using the clean fee.
- Compute the leftover collateral as the user balance minus the optimistic collateral.
- Since the leftover is above zero, recompute the liquidator collateral using the dirty fee.
- Compare the stale leftover against the minimum and observe the revert.
- Note that the real leftover, using the recomputed collateral, would be above the minimum.
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 someone repays a bad loan, the contract works out how much of the borrower's collateral is left over. It first guesses using one fee, then recalculates using a smaller fee, but keeps checking the old guess. That makes the safety check fail and the repayment is rejected even though it should be allowed.
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.