Skip to content
Business logic Audit finding Intermediate

Major insolvency risk in LiquidationLogic::executeCrossLiquidateERC721() due to not setting a maximum liquidation price

The liquidation function for ERC721 collateral calculates the price per NFT by dividing the debt by the number of collateral tokens, without an upper limit. If the NFT price drops significantly or the debt asset increases in price, the implied price per NFT becomes inflated, making liquidations unprofitable and potentially leading to bad debt.

0x73696d616f Follow

Originally disclosed on Code4rena

Submitted by 0x73696d616f

Whenever the price of a nft suffers a significant drop or the debt asset increases in price, there is a threshold after which liquidations are not profitable at all for the liquidator. Moreover, it does not offer the possibility of partial liquidation to at least partially mitigate this issue.

Proof of Concept

In LiquidationLogic::executeCrossLiquidateERC721(), the actual debt to liquidate is calculated in LiquidationLogic::_calculateDebtAmountFromERC721Collateral(). Here, it calculates the price of the liquidated nft by dividing the debt by the number of collateral tokens the user has. The problem with this approach is that it places no upper limit on the price, such that if the debt increases to much or the nft price suffers a sudden loss; the price of each nft would be too high. The amount of debt to repay is calculated based on this price, so it would make no sense for liquidators to pay an inflated amount for each nft.

Additionally, it’s possible to liquidate only a selected amount of nfts at a time by setting params.collateralTokenIds.length to less than the nft balance of the borrower, but it always sells them to the liquidator at the same inflated price. Thus, it’s impossible to use this mechanism to repay some amount of debt at a profitable price for the liquidator.

Place the following test in TestIntCrossLiquidateERC721.t.sol, confirming that the liquidator takes a big loss if the price of the nft collateral drops significantly.

function test_POC_RepayUSDT_HasBAYC_InflatedPrice() public {
  prepareUSDT(tsDepositor1);

  uint256[] memory depTokenIds = prepareCrossBAYC(tsBorrower1);

  TestUserAccountData memory accountDataBeforeBorrow = getUserAccountData(address(tsBorrower1), tsCommonPoolId);

  // borrow some eth
  uint8[] memory borrowGroups = new uint8[](1);
  borrowGroups[0] = tsLowRateGroupId;

  uint256[] memory borrowAmounts = new uint256[](1);
  borrowAmounts[0] =
    (accountDataBeforeBorrow.availableBorrowInBase * (10 ** tsUSDT.decimals())) /
    tsPriceOracle.getAssetPrice(address(tsUSDT));

  actionCrossBorrowERC20(
    address(tsBorrower1),
    tsCommonPoolId,
    address(tsUSDT),
    borrowGroups,
    borrowAmounts,
    new bytes(0)
  );

  // make some interest
  advanceTimes(365 days);

  // drop down price and lower health factor
  uint256 baycCurPrice = tsBendNFTOracle.getAssetPrice(address(tsBAYC));
  uint256 baycNewPrice = (baycCurPrice * 60) / 100;
  tsBendNFTOracle.setAssetPrice(address(tsBAYC), baycNewPrice);

  TestUserAccountData memory accountDataAfterBorrow = getUserAccountData(address(tsBorrower1), tsCommonPoolId);
  assertLt(accountDataAfterBorrow.healthFactor, 1e18, 'ACC:healthFactor');

  (uint256 actualCollateralToLiquidate, uint256 actualDebtToLiquidate) = tsPoolLens.getUserCrossLiquidateData(
    tsCommonPoolId,
    address(tsBorrower1),
    address(tsBAYC),
    1,
    address(tsUSDT),
    0
  );

  uint256 tsBAYCprice = tsPriceOracle.getAssetPrice(address(tsBAYC));
  uint256 tsUSDTprice = tsPriceOracle.getAssetPrice(address(tsUSDT));
  assertEq(tsBAYCprice, 3822840829686);
  assertEq(tsUSDTprice, 100053000);

  uint256 collateralValue = tsBAYCprice;
  uint256 debtValue = actualDebtToLiquidate * tsUSDTprice / 1e6;
  assertEq(debtValue, 4300008062440, 'actualDebtToLiquidate');
  assertEq(debtValue * 100 / collateralValue, 112); //@audit debt is worth 112% of collateral, not worth it

  assertGt(actualDebtToLiquidate, 0, 'actualDebtToLiquidate');
  assertGt(actualCollateralToLiquidate, 0, 'actualCollateralToLiquidate');

  // liquidate some eth
  tsLiquidator1.approveERC20(address(tsUSDT), type(uint256).max);

  uint256[] memory liqTokenIds = new uint256[](1);
  liqTokenIds[0] = depTokenIds[0];

  actionCrossLiquidateERC721(
    address(tsLiquidator1),
    tsCommonPoolId,
    address(tsBorrower1),
    address(tsBAYC),
    liqTokenIds,
    address(tsUSDT),
    false,
    new bytes(0)
  );
}

Tools Used

Vscode, Foundry

Recommended Mitigation Steps

Cap the price per nft of the liquidation to a reasonable value so liquidators can at least repay some of the token ids of the position. This is how it works for ERC20 liquidations, for example, in which the maximum debt to repay is calculated based on the maximum collateral balance sold at a discount.

thorseldon (BendDAO) acknowledged and commented:

In our current service design: We want NFT assets to bear corresponding debts according to their collateral value, and try to avoid the bad debt.

The final bad debt will be covered the DAO treasury which comes from protocol income.

We suggest adjust the severity level to informative, because this belong to service design which can be optimised in the future.

0xTheC0der (judge) commented:

Having the DAO treasury cover bad debt from protocol income is a protocol internal design decisions which doesn’t immediately put users at risk or allows direct theft of funds. Therefore, I agree with the sponsor.

Seems to fall below Questioning the protocol’s business model is unreasonable.

0x73696d616f (warden) commented:

@0xTheC0der - the protocol means they will take unfavourable liquidations to cover for their users. This was not documented so the issue should be valid, as it is a serious risk for BendDao.

As far as the docs and code were concerned (not mentioning treasury intervention to clear bad debt), this is a high severity issue.

0xTheC0der (judge) decreased severity to Medium and commented:

Similar reasoning as in Issue #23. But ERC721 instead of ERC20 instance.

However, Medium severity seems more justified in this case given the discussed impacts in contrast to #23 which discusses impacts more thoroughly (e.g., withdrawal DoS).


The original report as published on Code4rena, by 0x73696d616f. 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 liquidation price per NFT is derived from the debt amount without a maximum cap, so it can exceed the market price when collateral value drops.

Impact

Liquidators would incur a loss when liquidating, so they may avoid liquidations, potentially leading to bad debt that the DAO treasury must cover.

What the attacker needed

NFT collateral price drops significantly; or debt asset price increases significantly

The fix

Cap the price per NFT used in liquidation to a reasonable value so that liquidators can profitably repay part of the debt.

How it was found

  1. Borrow USDT against BAYC NFT collateral.
  2. Advance time to accrue interest.
  3. Drop the BAYC price by 40%.
  4. Check that the health factor is below 1.
  5. Query liquidation data and observe that the debt value is 112% of the collateral value.
  6. Attempt liquidation and confirm the liquidator takes a loss.

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, if a borrower's collateral loses value, liquidators can repay the debt and take the collateral at a discount. Here, the code sets the price of each NFT based on the total debt divided by the number of NFTs, with no cap. If the NFT price crashes, that calculated price becomes much higher than the market price, so liquidators would lose money and refuse to liquidate, leaving the protocol with bad debt.

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 0x73696d616f

View all

172 more disclosed reports, best first.

More from BendDAO Invitational

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.