Skip to content
Audit finding Advanced

The system protects from any rounding issues wh

In recovery mode, liquidating a CDP with ICR below MCR can produce a non-zero collateral surplus for the borrower due to rounding, whereas normal mode forces any surplus back to the liquidator. This breaks the invariant that no surplus should be issued when liquidating undercollateralised CDPs.

Stormy Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

28791 - [SC - Low] The system protects from any rounding issues wh...

Submitted on Feb 27th 2024 at 14:19:55 UTC by @Stormy for Boost | eBTC

Report ID: #28791

Report type: Smart Contract

Report severity: Low

Target: https://github.com/ebtc-protocol/ebtc/blob/release-0.7/packages/contracts/contracts/LiquidationLibrary.sol

Impacts:

  • Contract fails to deliver promised returns, but doesn't lose value

Description

Brief/Intro

The system may issue surplus in recovery mode when liquidating cdps with ICR < MCR duo to rounding error.

Vulnerability Details

On short explanation the liquidating system in eBTC works on the following bases.

  • The system is in normal mode as a result any cdps with ICR < MCR can be liquidated.
  • The system is in recovery mode duo to that cdps with ICR < CCR can be liquidated.

Normal mode liquidation

If we look at the function _liquidateIndividualCdpSetupCDPInNormalMode which liquidates a cdp position with collateral ratio below the minimum one, we can see that the system doesn't allow any spare collateral to be send to the cdp owner when accounting the surplus of the liquidation.

  • This is made in case any rounding errors occur when calculating the incentive collateral, as based on the system rules when liquidation happens with a cdp's ICR < MCR the whole cdp collateral should be send to the liquidator.
Solidity
            if (_collSurplus > 0) {
                // due to division precision loss, should be zero surplus in normal mode
                _cappedColPortion = _cappedColPortion + _collSurplus;
                _collSurplus = 0;
            }

Recovery mode liquidation

The system enters recovery mode as a defensive mode to increase the total collateral ratio of the system, as a result liquidators can further liquidate positions with ICR above the MCR and below the CCR one.

  • Lets say we liquidate cdp position with ICR == 120%, the liquidator gets a maximum incentive of 110% and the rest is returned to the cdp owner via surplus.

However in recovery mode we are still free to liquidate cdps below the minimum collateral ratio which can still lead to the rounding error when calculating the surplus. In this case the liquidator will get less incentive collateral while the cdp owner will earn extra surplus which shouldn't be possible when liquidating cdps with ICR < MCR.

Solidity
            if (_collSurplus > 0) {
                collSurplusPool.increaseSurplusCollShares(_borrower, _collSurplus);
                _recoveryState.totalSurplusCollShares =
                    _recoveryState.totalSurplusCollShares +
                    _collSurplus;
            }

Impact Details

l would say the loss here is not significant but rather broken invariant, the system enforces a rule that there should not be any surplus in normal mode which is true as when liquidating cdps with ICR < MCR the whole incentive collateral is supposed to be send to the liquidator. However this invariant doesn't hold in recovery mode, but theoretically it should be the same as the system is allowed to liquidate cdps below the minimum collateral ratio which shouldn't issue any surplus.

References

https://github.com/ebtc-protocol/ebtc/blob/release-0.7/packages/contracts/contracts/LiquidationLibrary.sol#L336

Proof of concept

Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

import "forge-std/Test.sol";
import {eBTCBaseInvariants} from "./BaseInvariants.sol";

contract stormy is eBTCBaseInvariants {

address wallet = address(0xbad455);
uint256 shareEth = 1158379174506084879;
bytes32 underwater;


function setUp() public override {
    super.setUp();

    connectCoreContracts();
    connectLQTYContractsToCore();

}

function testSurplusInRMWhenICRBelowMCR() public {

    // set eth per stETH share
    collateral.setEthPerShare(shareEth);

    // fetch price before open
    uint256 oldprice = priceFeedMock.fetchPrice();

    // open five cdps
    _openTestCDP(wallet, 2e18 + 2e17, ((2e18 * oldprice) / 240e16));
    _openTestCDP(wallet, 2e18 + 2e17, ((2e18 * oldprice) / 240e16));
    _openTestCDP(wallet, 2e18 + 2e17, ((2e18 * oldprice) / 240e16));
    _openTestCDP(wallet, 2e18 + 2e17, ((2e18 * oldprice) / 240e16));
    underwater = _openTestCDP(wallet, 2e18 + 2e17, ((2e18 * oldprice) / 210e16));

    // reduce the price by half to make underwater cdp
    priceFeedMock.setPrice(oldprice / 2);

    // fetch new price after reduce
    uint256 newPrice = priceFeedMock.fetchPrice();

    // ensure the system is in recovery mode
    assert(cdpManager.getSyncedTCR(newPrice) < CCR);

    // liquidate underwater cdp with ICR < MCR
    vm.startPrank(wallet);
    cdpManager.liquidate(underwater);
    vm.stopPrank();

    // make sure the cdp is no longer in the sorted list
    assert(!sortedCdps.contains(underwater));

    // fetch the surplus after the liquidation
    uint256 surplus = collSurplusPool.getSurplusCollShares(wallet);

    // ensure that the surplus is non-zero
    assert(surplus != 0);

    // console log the surplus coll
    console.log("Surplus:", surplus);
    
}
}

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

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 recovery-mode liquidation path does not zero out the surplus when the CDP's ICR is below MCR, so rounding dust is credited to the borrower.

Impact

The loss is small, but the system's rule that no surplus is issued for ICR < MCR liquidations is broken in recovery mode.

What the attacker needed

System in recovery mode; CDP with ICR below MCR; rounding occurs in incentive calculation

How it was found

  1. Set the stETH share price and open several CDPs, including one with a lower collateral ratio.
  2. Halve the price so the system enters recovery mode and the target CDP has ICR below MCR.
  3. Liquidate the underwater CDP.
  4. Check that the CDP is removed and that the borrower's surplus is non-zero.

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 CDP is a loan backed by collateral; if the collateral value falls too low, the loan can be liquidated. The liquidator is meant to receive the whole collateral when the loan is below the minimum ratio, but a rounding error in recovery mode gives a tiny extra amount back to the borrower instead. This is a small accounting mistake, not a way to steal funds.

More by Stormy

View all

4 more disclosed reports, best first.

More from eBTC

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.