Skip to content
Business logic Audit finding Intermediate

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.

X77 Follow

Originally disclosed on Sherlock

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:

Solidity
_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:

Solidity
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

  1. Let's imagine the following state:
  • userCollateralBalance = 2.3e18
  • dirtyLiquidationFee = 9_000 (based on Fraxlend)
  • cleanLiquidationFee = 10_000 (based on Fraxlend)
  • _liquidationAmountInCollateralUnits is equal to 2e18 based on the shares to liquidate provided by the user
  • minCollateralRequiredOnDirtyLiquidation is 1.1e17
  1. _optimisticCollateralForLiquidator will equal 2e18 * (1e5 + 1e4) / 1e5 = 2.2e18
  2. _leftoverCollateral equals 2.3e18 - 2.2e18 = 1e17
  3. As it is above 0, we recompute the collateral for liquidator which will equal 2e18 * (1e5 + 9e3) / 1e5 = 2.18e18
  4. As the leftover collateral is 1e17 and the minimum required collateral is 1.1e17, we will revert
  5. In reality, the actual leftover collateral is 2.3e18 - 2.18e18 = 1.2e17 as 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

The original report as published on Sherlock, by X77. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. Set up a user collateral balance, a dirty fee lower than the clean fee, a liquidation amount and a minimum required collateral.
  2. Compute the optimistic collateral for the liquidator using the clean fee.
  3. Compute the leftover collateral as the user balance minus the optimistic collateral.
  4. Since the leftover is above zero, recompute the liquidator collateral using the dirty fee.
  5. Compare the stale leftover against the minimum and observe the revert.
  6. 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.

More by X77

View all

6 more disclosed reports, best first.

More from Peapods

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.