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:
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:
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.
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
- User deposits 1mm DAI and borrows $950k of ETH and $50k worth of ICHI.
- Both assets are deposited into the ETH-ICHI pool, yielding the same collateral token.
- Both prices crash by 25%, making the position liquidatable.
- A liquidator pays back the full ICHI position.
- 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.