Inexpedient liquidatable logic that could have half liquidable position turns fully liquidable instantly
The liquidation logic in ShortCollateral.sol halves a position's debt when its safety ratio is between 0.95 and 1, but the liquidation bonus reduces the collateral ratio so much that the position immediately becomes fully liquidatable. An attacker could exploit this to liquidate a barely unhealthy position completely in two transactions.
RaymondFam Follow with Josiah
Originally disclosed on Code4rena
Submitted by RaymondFam, also found by Josiah
In ShortCollateral.sol, the slash logic of maxLiquidatableDebt() is specifically too harsh to the barely unhealthy positions because maxDebt will be half of the position to be liquidated if 0.95e18 <= safetyRatio <= 1e18.
Additionally, once a position turns liquidatable, the position is deemed fully liquidatable atomically in two repeated transactions.
Proof of Concept
Supposing we resort to the following setup as denoted in ShortCollateral.t.sol (#L21-L23):
collRatio = 1.5e18 liqRatio = 1.3e18 liqBonus = 1e17
Collateral ratio of a position, x = (position.collateralAmount * collateralPrice) / (position.shortAmount * markPrice)
File: ShortCollateral.sol#L230-L239
uint256 safetyRatioNumerator = position.collateralAmount.mulWadDown(collateralPrice);
uint256 safetyRatioDenominator = position.shortAmount.mulWadDown(markPrice);
safetyRatioDenominator = safetyRatioDenominator.mulWadDown(collateral.liqRatio);
uint256 safetyRatio = safetyRatioNumerator.divWadDown(safetyRatioDenominator);
if (safetyRatio > 1e18) return maxDebt;
maxDebt = position.shortAmount / 2;
if (safetyRatio < WIPEOUT_CUTOFF) maxDebt = position.shortAmount;
According to the code block above with liqRatio factored in:
In order to avoid being liquidated, a position will need to have a collateral ratio, x > 1.3e18 so that safetyRatio > (1.3 / 1.3)e18 which is safetyRatio > 1e18.
The position will be half liquidated if its associated collateral ratio falls in the range of 1.235e18 <= x <= 1.3e18. To avoid full liquidation, the condition at the lower end will need to be safetyRatio >= (1.235 / 1.3)e18 which is safetyRatio >= 0.95e18.
The position will be fully liquidated if x < 1.235e18.
Here is the unforgiving scenario:
- Bob has a short position whose collateral ratio happens to be 1.3e18.
- Bob’s position gets half liquidated the first round ending up with a collateral ratio, x (Note: The numerator is multiplied by 0.45 because of the additional 10%
liqBonusadded to the one half collateral slashed:(1.3 * 0.45 / 0.5)e18 = 1.17e18 - Immediately, Bob’s position becomes fully liquidatable because
x < 1.235e18.
Recommended Mitigation Steps
Consider restructuring the slashing logic such that the position turns healthy after being partially liquidated, instead of making it spiral down to the drain.
Not a duplicate of https://github.com/code-423n4/2023-03-polynomial-findings/issues/146
mubaris (Polynomial) confirmed
rivalq (Polynomial) confirmed and commented:
Depending upon the collateral and its collateral ratio etc, that spiral of liquidation may happen.
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 partial liquidation logic does not account for the liquidation bonus reducing the collateral ratio below the full-liquidation threshold, causing a spiral.
Impact
A position that is only slightly unhealthy could be fully liquidated in two transactions, causing a larger loss to the user than intended.
What the attacker needed
A short position with collateral ratio near 1.3e18; the position becomes liquidatable
How it was found
- Bob has a short position with collateral ratio exactly 1.3e18.
- Bob's position gets half liquidated, and the 10% liquidation bonus reduces the collateral ratio to 1.17e18.
- Because 1.17e18 is below 1.235e18, the position becomes fully liquidatable immediately.
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 system, a position becomes unsafe when its collateral is worth too little compared to its debt. The code here tries to be gentle by only liquidating half of a barely unsafe position, but the penalty taken during that half-liquidation makes the position even less safe, so it can be fully liquidated straight away. This means a small price drop can wipe out a user's whole position instead of just part of it.
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.