Skip to content
Business logic Audit finding Intermediate

Liquidator can take all collateral and underlying tokens for a fraction of the correct price

The liquidation calculation used the proportion of a single debt token repaid to determine the share of all collateral and underlying tokens to send to the liquidator. When a position had multiple debt types, a liquidator could repay the smallest debt and receive the entire collateral and underlying assets, far exceeding the correct amount.

cducrest-brainbot Follow with rvierdiiev, obront and 4 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-blueberry-judging/issues/127

Found by

cducrest-brainbot, rvierdiiev, obront, evan, berndartmueller, 0x52, XKET

Summary

When performing liquidation calculations, we use the proportion of the individual token's debt they pay off to calculate the proportion of the liquidated user's collateral and underlying tokens to send to them. In the event that the user has multiple types of debt, the liquidator will be dramatically overpaid.

Vulnerability Detail

When a position's risk rating falls below the underlying token's liquidation threshold, the position becomes liquidatable. At this point, anyone can call liquidate() and pay back a share of their debt, and receive a propotionate share of their underlying assets.

This is calculated as follows:

Solidity
uint256 oldShare = pos.debtShareOf[debtToken];
(uint256 amountPaid, uint256 share) = repayInternal(
    positionId,
    debtToken,
    amountCall
);

uint256 liqSize = (pos.collateralSize * share) / oldShare;
uint256 uTokenSize = (pos.underlyingAmount * share) / oldShare;
uint256 uVaultShare = (pos.underlyingVaultShare * share) / oldShare;

pos.collateralSize -= liqSize;
pos.underlyingAmount -= uTokenSize;
pos.underlyingVaultShare -= uVaultShare;

// ...transfer liqSize wrapped LP Tokens and uVaultShare underlying vault shares to the liquidator
}

To summarize:

  • The liquidator inputs a debtToken to pay off and an amount to pay
  • We check the amount of debt shares the position has on that debtToken
  • We call repayInternal(), which pays off the position and returns the amount paid and number of shares paid off
  • We then calculate the proportion of collateral and underlying tokens to give the liquidator
  • We adjust the liquidated position's balances, and send the funds to the liquidator

The problem comes in the calculations. The amount paid to the liquidator is calculated as:

Solidity
uint256 liqSize = (pos.collateralSize * share) / oldShare
uint256 uTokenSize = (pos.underlyingAmount * share) / oldShare;
uint256 uVaultShare = (pos.underlyingVaultShare * share) / oldShare;

These calculations are taking the total size of the collateral or underlying token. They are then multiplying it by share / oldShare. But share / oldShare is just the proportion of that one type of debt that was paid off, not of the user's entire debt pool.

Let's walk through a specific scenario of how this might be exploited:

  • User deposits 1mm DAI (underlying) and uses it to borrow $950k of ETH and $50k worth of ICHI (11.8k ICHI)
  • Both assets are deposited into the ETH-ICHI pool, yielding the same collateral token
  • Both prices crash down by 25% so the position is now liquidatable (worth $750k)
  • A liquidator pays back the full ICHI position, and the calculations above yield pos.collateralSize * 11.8k / 11.8k (same calculation for the other two formulas)
  • The result is that for 11.8k ICHI (worth $37.5k after the price crash), the liquidator got all the DAI (value $1mm) and LP tokens (value $750k)

Impact

If a position with multiple borrows goes into liquidation, the liquidator can pay off the smallest token (guaranteed to be less than half the total value) to take the full position, stealing funds from innocent users.

Code Snippet

https://github.com/sherlock-audit/2023-02-blueberry/blob/main/contracts/BlueBerryBank.sol#L511-L572

https://github.com/sherlock-audit/2023-02-blueberry/blob/main/contracts/BlueBerryBank.sol#L760-L787

Tool used

Manual Review

Recommendation

Adjust these calculations to use amountPaid / getDebtValue(positionId), which is accurately calculate the proportion of the total debt paid off.

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

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 liquidation payout was calculated using the proportion of a single debt token repaid (share / oldShare) instead of the proportion of the total debt value repaid.

What the attacker needed

A position with multiple debt types; position becomes liquidatable; liquidator repays the smallest debt

The fix

The calculations should use the proportion of the total debt value repaid (amountPaid / getDebtValue(positionId)) instead of the proportion of a single debt token.

How it was found

  1. User deposits 1mm DAI and borrows $950k of ETH and $50k worth of ICHI.
  2. Both assets are deposited into the ETH-ICHI pool, yielding the same collateral token.
  3. Both prices crash by 25%, making the position liquidatable.
  4. A liquidator pays back the full ICHI position.
  5. The calculation gives the liquidator all the DAI and LP tokens for repaying only the ICHI debt.

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.

When a borrower's collateral falls below a required level, anyone can repay part of their debt and receive some of their collateral as a reward. The bug meant that if the borrower owed several different tokens, repaying just one small debt gave the liquidator all the collateral, not just a fair share. This let attackers take much more value than they paid.

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 cducrest-brainbot

View all

11 more disclosed reports, best first.

More from Blueberry Update #3

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.