Skip to content
Smart contracts Audit finding Intermediate

Users won’t liquidate positions because the logic used to calculate the liquidator’s profit is incorrect

The liquidator reward in the liquidation function was computed by multiplying a USDC-denominated future value by the reward percentage, mixing token decimals, so the reward came out roughly a trillion times too small. Liquidators therefore had little incentive to liquidate unhealthy positions, threatening the protocol's solvency.

ether_sky Follow with serial-coder, samuraii77 and 48 others

Originally disclosed on Code4rena

Submitted by ether_sky, also found by serial-coder, samuraii77, muellerberndt, 0xAlix2, 3n0ch, nfmelendez, BenRai, Zkillua, supersizer0x, ayden, gd, 0xBugSlayer, radin100, DanielArmstrong, max10afternoon, Bigsam, iamandreiski, asui, 0xanmol, Honour, 0xStalin, Jorgect, Nave765, zanderbyte, carlos__alegre, Nyx, Decipher100Eyes, inzinko (1, 2), dhank, Infect3d, adeolu, ParthMandale, 0xarno, 0xpiken, hezze, elhaj, trachev, BajagaSec, silver_eth, Nihavent, MidgarAudits, zarkk01, hyh, Tychai0s, alix40, 0xRstStn, Brenzee, KupiaSec, Kalogerone, 0xJoyBoy03, ilchovski, 0xRobocop, stakog, bin2chen, AlexCzm, AMOW, pkqs90, VAD37, Sentryx, and said

Liquidation is crucial, and all liquidatable positions must be liquidated to maintain the protocol’s solvency. However, because the liquidator’s profit is calculated incorrectly, users are not incentivized to liquidate unhealthy positions. This issue has a significant impact on the protocol.

Proof of Concept

When the collateral ratio of a user falls below the specified threshold, the user’s debt positions become liquidatable.

The liquidation process involves the following steps:

    function executeLiquidate(State storage state, LiquidateParams calldata params)
        external
        returns (uint256 liquidatorProfitCollateralToken)
    {
        DebtPosition storage debtPosition = state.getDebtPosition(params.debtPositionId);
        LoanStatus loanStatus = state.getLoanStatus(params.debtPositionId);
        uint256 collateralRatio = state.collateralRatio(debtPosition.borrower);

        uint256 collateralProtocolPercent = state.isUserUnderwater(debtPosition.borrower)
            ? state.feeConfig.collateralProtocolPercent
            : state.feeConfig.overdueCollateralProtocolPercent;

        uint256 assignedCollateral = state.getDebtPositionAssignedCollateral(debtPosition);  // @audit, step 1
        uint256 debtInCollateralToken = state.debtTokenAmountToCollateralTokenAmount(debtPosition.futureValue);  // @audit, step 2
        uint256 protocolProfitCollateralToken = 0;

        if (assignedCollateral > debtInCollateralToken) {
            uint256 liquidatorReward = Math.min(
                assignedCollateral - debtInCollateralToken,
                Math.mulDivUp(debtPosition.futureValue, state.feeConfig.liquidationRewardPercent, PERCENT)
            );  // @audit, step 3
            liquidatorProfitCollateralToken = debtInCollateralToken + liquidatorReward;

            ...
        } else {
            liquidatorProfitCollateralToken = assignedCollateral;
        }
    }

Step 1: Calculate the collateral assigned to the debt.

  • For example, if the collateral is 100 ETH and the total debt is 1000 USDC, and the liquidated position’s debt is 200 USDC, then 20 ETH is assigned to this debt.

Step 2: Convert debt tokens to collateral tokens.

  • For instance, if 1 ETH equals 1000 USDC and the liquidated position’s debt is 200 USDC, then this value converts to 0.2 ETH.

Step 3: Calculate the liquidator’s profit if the assigned collateral to this position is larger than the debt value.

  • It’s crucial to note that assignedCollateral and debtInCollateralToken values are denominated in ETH’s decimal (18), whereas debtPosition.futureValue is denominated in USDC’s decimal (6).
  • As a result, the second value becomes extremely small, which fails to fully incentivize liquidators. This discrepancy is because the calculated profit is equal to the exact profit divided by 1e12.

As you can see in the log below, the profit is calculated to be extremely small:

    future value in usdc        ==>  82814071
    future value in WETH        ==>  82814071000000000000
    assigned collateral in WETH ==>  100000000000000000000
    ********************
    liquidator profit in WETH   ==>  4140704
    exact profit in WETH        ==>  4140703550000000000

Please add below test to the test/local/actions/Liquidate.t.sol:

    function test_liquidate_profit_of_liquidator() public {
        uint256 rewardPercent = size.feeConfig().liquidationRewardPercent;

        // current reward percent is 5%
        assertEq(rewardPercent, 50000000000000000);

        _deposit(alice, weth, 100e18);
        _deposit(alice, usdc, 100e6);
        _deposit(bob, weth, 100e18);
        _deposit(liquidator, weth, 100e18);
        _deposit(liquidator, usdc, 100e6);

        _buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.03e18));
        uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, 80e6, 365 days, false);

        _setPrice(1e18);
        assertTrue(size.isDebtPositionLiquidatable(debtPositionId));

        uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;
        uint256 assignedCollateral = size.getDebtPositionAssignedCollateral(debtPositionId);
        uint256 debtInCollateralToken =
            size.debtTokenAmountToCollateralTokenAmount(futureValue);

        console2.log('future value in usdc        ==> ', futureValue);
        console2.log('future value in WETH        ==> ', debtInCollateralToken);
        console2.log('assigned collateral in WETH ==> ', assignedCollateral);

        console2.log('********************');

        uint256 liquidatorProfit = _liquidate(liquidator, debtPositionId, 0);
        console2.log('liquidator profit in WETH   ==> ', liquidatorProfit - debtInCollateralToken);

        uint256 exactProfit = Math.min(
            assignedCollateral - debtInCollateralToken,
            Math.mulDivUp(debtInCollateralToken, rewardPercent, PERCENT)
        );
        console2.log('exact profit in WETH        ==> ', exactProfit);
    }

Recommended Mitigation Steps

Use the converted value to ETH:

    function executeLiquidate(State storage state, LiquidateParams calldata params)
        external
        returns (uint256 liquidatorProfitCollateralToken)
    {
        DebtPosition storage debtPosition = state.getDebtPosition(params.debtPositionId);
        LoanStatus loanStatus = state.getLoanStatus(params.debtPositionId);
        uint256 collateralRatio = state.collateralRatio(debtPosition.borrower);

        uint256 collateralProtocolPercent = state.isUserUnderwater(debtPosition.borrower)
            ? state.feeConfig.collateralProtocolPercent
            : state.feeConfig.overdueCollateralProtocolPercent;

        uint256 assignedCollateral = state.getDebtPositionAssignedCollateral(debtPosition);
        uint256 debtInCollateralToken = state.debtTokenAmountToCollateralTokenAmount(debtPosition.futureValue);
        uint256 protocolProfitCollateralToken = 0;

        if (assignedCollateral > debtInCollateralToken) {
            uint256 liquidatorReward = Math.min(
                assignedCollateral - debtInCollateralToken,
    -            Math.mulDivUp(debtPosition.futureValue, state.feeConfig.liquidationRewardPercent, PERCENT)
    +            Math.mulDivUp(debtInCollateralToken, state.feeConfig.liquidationRewardPercent, PERCENT)
            );
            liquidatorProfitCollateralToken = debtInCollateralToken + liquidatorReward;

            ...
        } else {
            liquidatorProfitCollateralToken = assignedCollateral;
        }
    }

Assessed type

Math

aviggiano (Size) confirmed and commented:

Fixed in https://github.com/SizeCredit/size-solidity/pull/118

MotokoKusanagi-aka-Major (Size) commented:

We acknowledge the severity of these issues being high since they impact the users negatively and are not unlikely to happen, but we want to share a bit more context of how these bugs have been introduced into the code

H-03 and H-04 are two incorrect implementations of formulas correctly specified in the tech documentation, much like H-01. Note that this means that three of the four high-severity issues were simply incorrect implementations of formulas.

Incorrect implementations of this sort are usually easily caught by simple unit tests relying on pre-computed expected values so it is uncommon to find them in audit reports–and indeed numerous wardens also caught these bugs during the C4 competition–so we want to explain why this happened to avoid incorrect conclusions about our development process.

One finding of the Spearbit Audit (the one before the C4) required us to redesign and reimplement the fee mechanism entirely.

The changes required were quite significant and we had a tight deadline since we had already defined C4 start date and the launch date. This required us to be very judicious with our time and focus on the actual implementation and be light on tests to avoid having to reschedule the C4 competition.

However, since what ultimately matters the most for us is the security of our code, we worked on the tests in parallel with the competition and we were able to identify these bugs even before they were also discovered by the C4 competitors.

In conclusion, we were aware the strategy we had chosen would have increased the risk of having more bugs found during the competition–we did not optimize for receiving “high grades” from the competition (even though overall the “grades” we received seem pretty much aligned with the ones of other protocols). We chose the strategy to be able to meet the deadlines for the competition, and ultimately it did not come at the cost of less code security.

See also Issue #70.


The original report as published on Code4rena, by ether_sky. 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 the reward was calculated from the debt's future value in USDC decimals instead of the same value converted to collateral-token decimals, so the two operands of the multiplication had mismatched scales.

Impact

Liquidators receive a far smaller reward than intended, so they may not liquidate unhealthy positions, which could leave bad debt and threaten the protocol's solvency.

What the attacker needed

A liquidatable debt position; a liquidator with funds to repay the debt

The fix

The reward should be calculated from the debt value converted into collateral-token decimals rather than from the raw future value; the project states it was fixed in a pull request.

How it was found

  1. Deposit collateral and USDC for the borrower, the lender and the liquidator.
  2. Create a credit limit and sell credit so the borrower has a debt position.
  3. Set the price so the debt position becomes liquidatable.
  4. Read the future value, assigned collateral and converted debt value, then liquidate and compare the actual reward with the expected reward.

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, someone who repays an unhealthy borrower's debt gets a reward taken from the borrower's collateral. Here the reward was calculated using a number in one token's units (USDC, six decimals) where a number in another token's units (ETH, eighteen decimals) was needed. The result was a reward about a trillion times smaller than intended, so nobody would bother doing the job.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by ether_sky

View all

11 more disclosed reports, best first.

More from Size

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.