Skip to content
Business logic Audit finding Intermediate

Short positions with minimum collateral can be liquidated even though canLiquidate() returns false

The canLiquidate() function returns false for short positions with exactly the minimum collateral, but maxLiquidatableDebt() treats the same positions as liquidatable, allowing half their debt to be liquidated. An attacker could liquidate such positions even though the protocol's own check says they are safe, causing users to lose collateral.

MiloTruck Follow with joestakey and Nyx

Originally disclosed on Code4rena

Submitted by MiloTruck, also found by joestakey and Nyx

Frontends or contracts that rely on canLiquidate() to determine if a position is liquidatable could be incorrect. Users could think their positions are safe from liquidation even though they are liquidatable, leading to them losing their collateral.

Vulnerability Details

In the ShortCollateral contract, canLiquidate() determines if a short position can be liquidated using the following formula:

uint256 minCollateral = markPrice.mulDivUp(position.shortAmount, collateralPrice);
minCollateral = minCollateral.mulWadDown(collateral.liqRatio);

return position.collateralAmount < minCollateral;

Where:

  • position.collateralAmount - Amount of collateral in the short position.
  • minCollateral - Minimum amount of collateral required to avoid liquidation.

From the above, a short position can be liquidated if its collateral amount is less than minCollateral. This means a short position with the minimum collateral amount (ie. position.collateralAmount == minCollateral) cannot be liquidated.

However, this is not the case in maxLiquidatableDebt(), which is used to determine a position’s maximum liquidatable debt:

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;

Where:

  • safetyRatio - Equivalent to position.collateralAmount / minCollateral. Can be seen as a position’s collateral amount against the minimum collateral required.
  • maxDebt - The amount of debt liquidatable. Defined as 0 at the start of the function.

As seen from the safetyRatio > 1e18 check, a position is safe from liquidation (ie. maxDebt = 0) if its safetyRatio is greater than 1.

Therefore, as a position with the minimum collateral amount has a safetyRatio of 1, half its debt becomes liquidatable. This contradicts canLiquidate(), which returns false for such positions.

Proof of Concept

The following test demonstrates how a position with minimum collateral is liquidatable even though canLiquidate() returns false:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import {TestSystem, Exchange, ShortToken, ShortCollateral, PowerPerp, MockERC20Fail} from "./utils/TestSystem.sol";

contract CanLiquidateIsInaccurate is TestSystem {
    // Protocol contracts
    Exchange private exchange;
    ShortToken private shortToken;
    ShortCollateral private shortCollateral;

    // sUSD token contract
    MockERC20Fail private SUSD;

    function setUp() public {
        // Set liquidation ratio of sUSD to 125%
        susdLiqRatio = 1.24e18;

        // Deploy contracts
        deployTestSystem();
        initPool();
        initExchange();
        preparePool();

        exchange = getExchange();
        shortToken = getShortToken();
        shortCollateral = getShortCollateral();
        SUSD = getSUSD();

        // Mint sUSD for user_1
        SUSD.mint(user_1, 1e20);

        // Mint powerPerp for user_2
        vm.prank(address(exchange));
        getPowerPerp().mint(user_2, 1e20);
    }

    function testCanLiquidateMightBeWrong() public {
        // Initial price of base asset is 1e18
        uint256 initialPrice = 1e18;
        setAssetPrice(initialPrice);

        // Open short position with 1e15 sUSD as collateral
        Exchange.TradeParams memory tradeParams;
        tradeParams.amount = 1e18;
        tradeParams.collateral = address(SUSD);
        tradeParams.collateralAmount = 1e15;
        tradeParams.minCost = 0;

        vm.startPrank(user_1);
        SUSD.approve(address(exchange), tradeParams.collateralAmount);
        (uint256 positionId,) = exchange.openTrade(tradeParams);
        vm.stopPrank();

        // Initial price of base asset increases, such that minCollateral == collateralAmount
        setAssetPrice(1270001270001905664);

        // canLiquidate() returns false
        assertFalse(shortCollateral.canLiquidate(positionId));

        // However, maxLiquidatableDebt() returns half of original amount
        assertEq(shortCollateral.maxLiquidatableDebt(positionId), tradeParams.amount / 2);

        // Other users can liquidate the short position
        vm.prank(user_2);
        exchange.liquidate(positionId, tradeParams.amount);

        // Position's shortAmount and collateral is reduced
        (, uint256 remainingAmount, uint256 remainingCollateralAmount, ) = shortToken.shortPositions(positionId);
        assertEq(remainingAmount, tradeParams.amount / 2);
        assertLt(remainingCollateralAmount, tradeParams.collateralAmount);
    }
}

Recommended Mitigation

Consider making short positions safe from liquidation if their safetyRatio equals to 1:

ShortCollateral.sol#L235:

-        if (safetyRatio > 1e18) return maxDebt;
+        if (safetyRatio >= 1e18) return maxDebt;

mubaris (Polynomial) confirmed


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

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 an off-by-one error: canLiquidate() uses a strict less-than comparison while maxLiquidatableDebt() uses a strict greater-than check, so positions with safetyRatio exactly 1 are treated differently.

Impact

Users could lose collateral because positions that appear safe according to canLiquidate() can still be liquidated.

What the attacker needed

A short position with collateral exactly at minimum; asset price moves to make safetyRatio exactly 1

The fix

The check in maxLiquidatableDebt() should use >= instead of > so that positions with safetyRatio equal to 1 are considered safe.

How it was found

  1. Set the liquidation ratio for the collateral token.
  2. Open a short position with a specific collateral amount.
  3. Increase the asset price so that the position's collateral equals the minimum required.
  4. Observe that canLiquidate() returns false for the position.
  5. Observe that maxLiquidatableDebt() returns half of the original short amount.
  6. Liquidate the position and see that the short amount and collateral are reduced.

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.

A short position is a bet that an asset's price will fall, backed by collateral. The protocol has two functions that decide if a position can be liquidated: one says 'no' when collateral is exactly at the minimum, but the other says 'yes' and allows half the debt to be liquidated. This inconsistency means users can lose money even when the first check says they are safe.

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 MiloTruck

View all

35 more disclosed reports, best first.

More from Polynomial Protocol contest

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.