Skip to content
Business logic Audit finding Intermediate

isolateRepay() lack of check onBehalf == nftOwner

The isolateRepay function allows specifying an onBehalf address that is not the NFT owner, so an attacker can reduce another user's userScaledIsolateBorrow balance. This can cause an underflow when the victim tries to repay or is liquidated, preventing repayment or liquidation.

bin2chen Follow

Originally disclosed on Code4rena

Submitted by bin2chen

When calling isolateRepay() we can specify nftTokenIds and onBehalf. The current implementation just restricts onBehalf!=address(0) and does not restrict onBehalf == nftOwner.

  function validateIsolateRepayBasic(
    InputTypes.ExecuteIsolateRepayParams memory inputParams,
    DataTypes.PoolData storage poolData,
    DataTypes.AssetData storage debtAssetData,
    DataTypes.AssetData storage nftAssetData
  ) internal view {
    validatePoolBasic(poolData);

    validateAssetBasic(debtAssetData);
    require(debtAssetData.assetType == Constants.ASSET_TYPE_ERC20, Errors.ASSET_TYPE_NOT_ERC20);

    validateAssetBasic(nftAssetData);
    require(nftAssetData.assetType == Constants.ASSET_TYPE_ERC721, Errors.ASSET_TYPE_NOT_ERC721);

@>  require(inputParams.onBehalf != address(0), Errors.INVALID_ONBEHALF_ADDRESS);

    require(inputParams.nftTokenIds.length > 0, Errors.INVALID_ID_LIST);
    require(inputParams.nftTokenIds.length == inputParams.amounts.length, Errors.INCONSISTENT_PARAMS_LENGTH);
    validateArrayDuplicateUInt256(inputParams.nftTokenIds);

    for (uint256 i = 0; i < inputParams.amounts.length; i++) {
      require(inputParams.amounts[i] > 0, Errors.INVALID_AMOUNT);
    }
  }

This way, we can maliciously specify onBehalf!=nftOwner and when the method is executed userScaledIsolateBorrow[onBehalf] will be maliciously reduced. When the victim makes a real repayment or is liquidated, it will not be repaid or liquidated due to insufficient userScaledIsolateBorrow[onBehalf] resulting in an underflow.

Example:

  1. Alice depositERC721(NFT_1).

  2. Bob depositERC721(NFT_2).

  3. Alice isolateBorrow(NFT_1, 100).

    • userScaledIsolateBorrow[alice] = 100
    • loanData[NFT_1].scaledAmount = 100
  4. Bob isolateBorrow(NFT_2, 100)

    • userScaledIsolateBorrow[bob] = 100
    • loanData[NFT_2].scaledAmount = 100
  5. Alice malicious repay, isolateRepay(NFT_1,amounts = 1, onBehalf = bob)

    • userScaledIsolateBorrow[bob] = 99
  6. When NFT_2 be liquidated, isolateLiquidate(NFT_2)

    • userScaledIsolateBorrow[bob] = loanData[NFT_2].scaledAmount = 99 - 100 =====> underflow.

Note: isolateRedeem() is similar.

Impact

Maliciously prevent being liquidated or repaid.

Recommended Mitigation

limit onBehalf==owner:

  function validateIsolateRepayBasic(
    InputTypes.ExecuteIsolateRepayParams memory inputParams,
    DataTypes.PoolData storage poolData,
    DataTypes.AssetData storage debtAssetData,
    DataTypes.AssetData storage nftAssetData
  ) internal view {
    validatePoolBasic(poolData);

    validateAssetBasic(debtAssetData);
    require(debtAssetData.assetType == Constants.ASSET_TYPE_ERC20, Errors.ASSET_TYPE_NOT_ERC20);

    validateAssetBasic(nftAssetData);
    require(nftAssetData.assetType == Constants.ASSET_TYPE_ERC721, Errors.ASSET_TYPE_NOT_ERC721);

    require(inputParams.onBehalf != address(0), Errors.INVALID_ONBEHALF_ADDRESS);

    require(inputParams.nftTokenIds.length > 0, Errors.INVALID_ID_LIST);
    require(inputParams.nftTokenIds.length == inputParams.amounts.length, Errors.INCONSISTENT_PARAMS_LENGTH);
    validateArrayDuplicateUInt256(inputParams.nftTokenIds);

    for (uint256 i = 0; i < inputParams.amounts.length; i++) {
      require(inputParams.amounts[i] > 0, Errors.INVALID_AMOUNT);

+     DataTypes.ERC721TokenData storage tokenData = VaultLogic.erc721GetTokenData(
+       nftAssetData,
+       inputParams.nftTokenIds[i]
+     );
+     require(tokenData.owner == inputParams.onBehalf, Errors.ISOLATE_LOAN_OWNER_NOT_MATCH);
    }
  }

Assessed type

Context

thorseldon (BendDAO) confirmed and commented:

Fixed here.


The original report as published on Code4rena, by bin2chen. 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 validateIsolateRepayBasic function only checks that onBehalf is not the zero address, but does not verify that onBehalf is the owner of the NFT being repaid.

Impact

An attacker could prevent a victim from being liquidated or from repaying their loan, potentially causing a denial of service for those operations.

What the attacker needed

Attacker has an NFT and a loan; victim has an NFT and a loan; attacker can call isolateRepay

The fix

The fix was to add a check that the onBehalf address matches the owner of each NFT being repaid.

How it was found

  1. Alice deposits NFT_1 and Bob deposits NFT_2.
  2. Alice borrows 100 against NFT_1, setting her scaled borrow to 100.
  3. Bob borrows 100 against NFT_2, setting his scaled borrow to 100.
  4. Alice calls isolateRepay for NFT_1 with a small amount and onBehalf set to Bob, reducing Bob's scaled borrow to 99.
  5. When NFT_2 is liquidated, the contract tries to subtract 100 from Bob's scaled borrow of 99, causing an underflow.

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, each user has a record of how much they owe. The repay function lets you say who you are repaying for, but it does not check that you actually own the NFT you are repaying for. So an attacker can repay a tiny amount on behalf of someone else, reducing that person's debt record incorrectly. Later, when the victim tries to repay or gets liquidated, the numbers do not match and the transaction fails.

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 bin2chen

View all

139 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.