Skip to content
Business logic Audit finding Advanced

Batched liquidations doesn’t distribute bad debt on next batches in the list

When a batch liquidation creates bad debt, the redistribution index is not updated after each liquidation, so subsequent liquidations in the same batch use an incorrect debt value. This means less debt is liquidated and more collateral goes to the CDP owner, while other users must cover the skipped bad debt.

rvierdiiev Follow with Stormy and Nyx

Originally disclosed on Code4rena

Submitted by rvierdiiev, also found by Stormy and Nyx

LiquidationLibrary.batchLiquidateCdps is called from CDPManager in order to liquidate list of cdps. Depending if system is currently in recovery mode liquidations will be done inside _getTotalFromBatchLiquidate_RecoveryMode or _getTotalsFromBatchLiquidate_NormalMode function.

After that _finalizeLiquidation function will be called, which will then do several important system updates, token transfer and what is important for this report bad debt redistribution. In case if new bad debt occurs in the system, then this debt amount is redistributed to all stakers that are still in the system.

Now, let’s check how liquidations occur by _getTotalsFromBatchLiquidate_NormalMode function. This function loops through all cpds one by one. It fetches ICR for them using getSyncedICR function. This function will calculate ICR not on stored debt and coll of cdp, but it will update this values, according to new fee rate(which means that coll will be reduced) and redistribute rate(which means that debt will increase, if rate is bigger than stored for cdp).

This is how it will be done for redistribution. https://github.com/code-423n4/2023-10-badger/blob/main/packages/contracts/contracts/CdpManagerStorage.sol#L864-L868

    function getSyncedICR(bytes32 _cdpId, uint256 _price) public view returns (uint256) {
        uint256 _debt = getSyncedCdpDebt(_cdpId);
        uint256 _collShare = getSyncedCdpCollShares(_cdpId);
        return _calculateCR(_collShare, _debt, _price);
    }

Then getSyncedCdpDebt is called, which calls _getSyncedCdpDebtAndRedistribution function. https://github.com/code-423n4/2023-10-badger/blob/main/packages/contracts/contracts/CdpManagerStorage.sol#L822-L833

    function _getSyncedCdpDebtAndRedistribution(
        bytes32 _cdpId
    ) internal view returns (uint256, uint256, uint256) {
        (uint256 pendingDebtRedistributed, uint256 _debtIndexDelta) = _getPendingRedistributedDebt(
            _cdpId
        );
        uint256 _newDebt = Cdps[_cdpId].debt;
        if (pendingDebtRedistributed > 0) {
            _newDebt = _newDebt + pendingDebtRedistributed;
        }
        return (_newDebt, pendingDebtRedistributed, _debtIndexDelta);
    }

So debt of cdp will be increased pendingDebtRedistributed, which depends on redistribution rate of position and current redistribution rate.

Later, for each cdp _getLiquidationValuesNormalMode function will be called, which will actually do the liquidation. It will call _liquidateIndividualCdpSetupCDPInNormalMode function. Inside of it _calculateFullLiquidationSurplusAndCap function is called and it’s purpose is to calculate shares that liquidator and cdp owner should receive and bad debt to redistribute.

So after liquidation has finished, then _addLiquidationValuesToTotals function is called, which will add values from previous liquidation(including redistribution debt) to the total accumulator.

So finally, I can explain the problem. In case if in batch there is cdp, that will create bad debt after liquidation, then this debt will not be redistributed to all next liquidations, as redistribution index is not updated after each liquidation. And as result, next liquidations will have incorrect debt value, which means that smaller amount of debt will be liquidated and bigger amount of coll will be received by cdp owner. And also all else users in the system will have to cover that delta debt that was not redistributed to next liquidations.

Impact

Bad debt is distributed incorrectly.

Tools Used

VsCode

Recommended Mitigation Steps

You need to redistribute bad debt after each liquidation in the batch (in case if bad debt occured).

Alex the Entreprenerd (Badger) acknowledged and commented:

This is a known finding from Cantina that unfortunately was not added to the report.

The finding specifically means that 3% of bad debt generated in a batch liquidation will be skipped, effectively making liquidations more favourable.

That said, it’s worth noting that since partial liquidations don’t cause a redistribution, the 3% bad debt could have been skipped under other circumstances.

https://github.com/GalloDaSballo/Cdp-Demo/blob/main/scripts/insolvency_cascade.py

MAX_BPS = 10_000

def cr(debt, coll):
    return coll / debt * 100

def getDebtByCr(coll, cr_bps):
   return coll / cr_bps * MAX_BPS

LICR = 103_00


def max(a, b):
   if(a > b):
      return a
   return b

def min(a, b):
   if(a > b):
      return b
   return a

def full_liq(COLL):
  ## burn all
  return [COLL, COLL / LICR * MAX_BPS]

def partial_liq(COLL, percent):
   return [COLL * percent / 100, COLL * percent / 100 / LICR * MAX_BPS]

def loop_full(cr_bps):
  COLL = 100
  DEBT = getDebtByCr(COLL, cr_bps)
  print("DEBT", DEBT)
  print("cr", cr(DEBT, COLL))

  ## Liquidate Partially based on premium
  while(COLL > 2):
     [sub_coll, sub_debt] = partial_liq(COLL, 10)
    #  [sub_coll, sub_debt] = full_liq(COLL)
     print("DEBT", DEBT)
     print("COLL", COLL)
     DEBT -= sub_debt
     COLL -= sub_coll

  print("DEBT", DEBT)




CRS = [
   100_00,
   90_00,
   80_00,
   70_00
]

def main():
   print("Simulate Total Liquidation with Bad debt")
   print("Coll is always 100")
   print("No price means 1 coll = 1 debt for price")
   for cr in CRS:
    print("")
    print("")
    print("")
    loop_full(cr)






main()

ronnyx2017 (Judge) commented:

I didn’t find a public record about the issue. So I’ll mark it as Medium severity.


The original report as published on Code4rena, by rvierdiiev. 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 redistribution index is not updated after each liquidation within a batch, so subsequent liquidations use a stale debt value.

Impact

Bad debt is distributed incorrectly, causing some users to cover debt that should have been redistributed to later liquidations in the batch.

What the attacker needed

Batch liquidation with bad debt

The fix

Redistribute bad debt after each liquidation in the batch when bad debt occurs.

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 this lending system, when a borrower's collateral is worth less than their debt, the shortfall (bad debt) is shared among all depositors. The bug is that during a batch of liquidations, the sharing calculation is not updated after each liquidation, so later liquidations in the batch use stale numbers. As a result, some bad debt is not shared correctly and the liquidator gets a better deal at the expense of other users.

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 rvierdiiev

View all

72 more disclosed reports, best first.

More from eBTC Zap Router

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.