# User are forced to borrow again in order to unlock their NFTs from IsolateLending.sol

When redeemThreshold is 10000, isolateRedeem zeroes scaledAmount but leaves loan active and NFT locker set, locking the NFT until the user borrows again.

- Payout: Audit contest
- Severity: medium
- Program: BendDAO
- Found by: Ch\_301
- Disclosed: 2024-09-03
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-benddao-findings/issues/31
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-benddao%3AM-09
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** IsolateLending/IsolateLiquidation redeem flow in BendDAO lending contracts
**Root cause:** executeIsolateRedeem subtracts the full scaledAmount to zero but does not reset loanStatus or clear erc721TokenData.LockerAddr when redeemThreshold equals MAX_REDEEM_THRESHOLD (10000).
**Steps:**
1. Set asset auction params with redeemThreshold=10000 via `setAssetAuctionParams(poolId, BAYC, 10000, 500, 2000, 1 days)`
2. Borrower deposits BAYC and borrows USDT via isolate borrow
3. Advance time and drop NFT price to trigger auction via `prepareAuction`
4. Borrower calls `isolateRedeem(poolId, BAYC, tokenIds, USDT, redeemAmounts)` paying full redeemAmount
5. Observe loanData.scaledAmount==0 but loanStatus still ACTIVE and LockerAddr unchanged
6. Call `withdrawERC721(...)` reverts with `ASSET_ALREADY_LOCKED_IN_USE`
7. Only workaround: borrow again to make scaledAmount>0 then isolateRepay to clear LockerAddr
**How found:** Code reading of executeIsolateRedeem: noticed scaledAmount subtraction can reach zero while loanStatus and LockerAddr updates are skipped, then confirmed with Foundry PoC that withdrawERC721 reverts.
**Exploitation:** Borrower fully repays via isolateRedeem at max threshold; contract zeroes debt but leaves NFT locked, forcing a new borrow (re-exposing to liquidation) before withdrawal is possible.
**Insight:** When a repay path zeroes a debt counter, check whether all state transitions (status flags, locker addresses) are gated on that counter being nonzero — a max-threshold config can skip cleanup branches.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test for withdraw after full redeem**

```
function test_Withdraw_After_Full_Redeem_Should_Revert() public {
    TestCaseLocalVars memory testVars;

    tsHEVM.startPrank(tsPoolAdmin);
    tsConfigurator.setAssetAuctionParams(tsCommonPoolId, address(tsBAYC), 10000, 500, 2000, 1 days);
    tsHEVM.stopPrank();
    // deposit
    prepareUSDT(tsDepositor1);
    uint256[] memory tokenIds = prepareIsolateBAYC(tsBorrower1);

    // borrow
    prepareBorrow(tsBorrower1, address(tsBAYC), tokenIds, address(tsUSDT));

    // make some interest
    advanceTimes(365 days);

    // drop down nft price
    actionSetNftPrice(address(tsBAYC), 5000);

    // auction
    prepareAuction(tsLiquidator1, address(tsBAYC), tokenIds, address(tsUSDT));

    uint256[] memory redeemAmounts = new uint256[](tokenIds.length);
    testVars.loanDataBefore = getIsolateLoanData(tsCommonPoolId, address(tsBAYC), tokenIds);
    for (uint256 i = 0; i < tokenIds.length; i++) {
      testVars.totalBidAmount += testVars.loanDataBefore[i].bidAmount;
      testVars.totalBidFine += testVars.loanDataBefore[i].bidFine;
      testVars.totalRedeemAmount += testVars.loanDataBefore[i].redeemAmount;
      redeemAmounts[i] = testVars.loanDataBefore[i].redeemAmount;
    }

    testVars.poolBalanceBefore = tsUSDT.balanceOf(address(tsPoolManager));
    testVars.walletBalanceBefore1 = tsUSDT.balanceOf(address(tsLiquidator1));
    testVars.walletBalanceBefore2 = tsUSDT.balanceOf(address(tsBorrower1));

    // redeem
    tsBorrower1.approveERC20(address(tsUSDT), type(uint256).max);
    tsBorrower1.isolateRedeem(tsCommonPoolId, address(tsBAYC), tokenIds, address(tsUSDT), redeemAmounts);
    testVars.txAuctionTimestamp = block.timestamp;

    // check results
    testVars.loanDataAfter = getIsolateLoanData(tsCommonPoolId, address(tsBAYC), tokenIds);
    for (uint256 i = 0; i < tokenIds.length; i++) {
      assertEq(testVars.loanDataAfter[i].bidStartTimestamp, 0, 'bidStartTimestamp');
      assertEq(testVars.loanDataAfter[i].firstBidder, address(0), 'firstBidder');
      assertEq(testVars.loanDataAfter[i].lastBidder, address(0), 'lastBidder');
      assertEq(testVars.loanDataAfter[i].bidAmount, 0, 'bidAmount');
      assertEq(testVars.loanDataAfter[i].scaledAmount, 0, 'scaledAmount');
    }
    tsHEVM.expectRevert(bytes(Errors.ASSET_ALREADY_LOCKED_IN_USE));
    tsBorrower1.withdrawERC721(
      tsCommonPoolId,
      address(tsBAYC),
      tokenIds,
      Constants.SUPPLY_MODE_ISOLATE,
      address(tsBorrower1),
      address(tsBorrower1)
    );
  }
```

