Revert due to underflow error, leading to lock of the liquidated NFT
When a liquidator wins an isolated-lending NFT auction, the liquidation routine subtracts the accrued borrow amount from the pool's recorded total bid amount. Because interest keeps growing the borrow amount past the recorded bid, the subtraction underflows and the transaction reverts, so the winning liquidator cannot unlock the NFT.
Submitted by Ch_301, also found by bin2chen and oakcobalt
https://github.com/code-423n4/2024-07-benddao/blob/main/src/libraries/logic/IsolateLogic.sol#L464 https://github.com/code-423n4/2024-07-benddao/blob/main/src/libraries/logic/IsolateLogic.sol#L269
Description
The IsolateLogic.sol#executeIsolateAuction() is triggered by liquidators to launch a new auction or just participate in an existing one for isolated lending. Each NFT token has a predefined auction duration nftAssetData.auctionDuration, the MAX duration is 7 days. In the period of (e.g.) 7 days, the borrowAmount will keep increasing due to the interest:
File: GenericLogic.sol#calculateNftLoanLiquidatePrice()
279: vars.borrowAmount = nftLoanData.scaledAmount.rayMul(vars.normalizedIndex);
If the liquidator is the first bidder, the bid price must be greater than borrowed debt and the liquidate price. Otherwise, the liquidator will just try to outbid the last bidder, by at least 1%.
At the end of executeIsolateAuction(), it will transfer the underlying asset from the liquidator to escrow and increase the value of assetData.totalBidAmout by the bid amount from the liquidator (Note: remember the last point).
File: IsolateLogic.sol#executeIsolateAuction()
269: VaultLogic.erc20TransferInBidAmount(debtAssetData, params.msgSender, vars.totalBidAmount);
After 7 days, the liquidator who wins the auction will invoke IsolateLogic.sol#executeIsolateLiquidate() to implement the liquidation. It will calculate the borrowAmount and check for any remaining or extra amount.
File: IsolateLogic.sol#executeIsolateLiquidate()
427: vars.borrowAmount = loanData.scaledAmount.rayMul(vars.normalizedIndex);
Then, because the bid is already in the pool, it will sub-call to VaultLogic.sol#erc20TransferOutBidAmountToLiqudity() to increase liquidity in the pool assetData.availableLiquidity and decrease the assetData.totalBidAmout value.
The issue is here, is about how much totalBidAmout has decreased. In the below code snap is passing totalBorrowAmount to erc20TransferOutBidAmountToLiqudity():
File: IsolateLogic.sol#executeIsolateLiquidate()
464: VaultLogic.erc20TransferOutBidAmountToLiqudity(debtAssetData, vars.totalBorrowAmount);
The vars.totalBorrowAmount is just the value of vars.borrowAmount (in case of one NFT in the liquidation). As we mentioned above, the borrowed amount will keep increasing over time and it will exceed the saved value in assetData.totalBidAmout (check the above note). This will lead the transaction to revert:
File: VaultLogic.sol
503: function erc20TransferOutBidAmountToLiqudity(DataTypes.AssetData storage assetData, uint amount) internal {
504: assetData.totalBidAmout -= amount;
505: assetData.availableLiquidity += amount;
506: }
There are multiple scenarios to reach this point (revert):
- The auction ends with only one bidder (the first bidder wins it), so the rest of the time (until the auction ends) the borrowed amount will keep accumulating interest.
- The auction ends, but the borrowed amount exceeds the winner’s bid price (due to accumulated interest).
Impact
The liquidator wins the auction but he can’t withdraw/unlock the NFT, due to an Arithmetic over/underflow and fails when reducing the assetData.totalBidAmout value in erc20TransferOutBidAmountToLiqudity().
Proof of Concept
Foundry PoC:
- Please copy the following POC in
TestIntIsolateLiquidate.t.sol:
function test_POC_Should_LiquidateWETH() public {
TestCaseLocalVars memory testVars;
// deposit
prepareWETH(tsDepositor1);
uint256[] memory tokenIds = prepareIsolateBAYC(tsBorrower1);
// borrow
prepareBorrow(tsBorrower1, address(tsBAYC), tokenIds, address(tsWETH));
// make some interest
advanceTimes(365 days);
// drop down nft price
actionSetNftPrice(address(tsBAYC), 5000);
//?1- first bidder wins the auction
// auction
tsLiquidator1.approveERC20(address(tsWETH), type(uint256).max);
uint256[] memory bidAmounts = new uint256[](tokenIds.length);
for (uint256 i = 0; i < tokenIds.length; i++) {
bidAmounts[i] = 20561005318463975387;
}
tsLiquidator1.isolateAuction(tsCommonPoolId, address(tsBAYC), tokenIds, address(tsWETH), bidAmounts);
//?2- wait and accumulate interest until the borrowed amount exceeds the winner's bid price
// end the auction
advanceTimes(25 hours);
uint256[] memory liquidateAmounts = new uint256[](tokenIds.length);
//?3- call `isolateLiquidate()` should revert
// liquidate
vm.expectRevert(stdError.arithmeticError);
tsLiquidator1.isolateLiquidate(tsCommonPoolId, address(tsBAYC), tokenIds, address(tsWETH), liquidateAmounts, false);
}
- Go to
TestUser.soland update this part:
File: TestUser.sol
45: _uid = uid_;
46: _tokenIds = new uint256[](1); //<== from 3 to 1
47: for (uint i = 0; i < 1; i++) {//<== from 3 to 1
48: _tokenIds[i] = uid_ + i;
49: }
50: }
- Test result:
Running 1 test for test/integration/TestIntIsolateLiquidate.t.sol:TestIntIsolateLiquidate
[PASS] test_POC_Should_LiquidateWETH() (gas: 1144530)
Logs:
borrowAmount is: 20561005318463975387
=============
to reduce the totalBidAmout: 20561005318463975387
reduce by amount: 20567041544435426364
Test result: ok. 1 passed; 0 failed; finished in 30.08ms
Recommended Mitigation Steps
Just decrease assetData.totalBidAmout by vars.totalBidAmount:
File: IsolateLogic.sol#executeIsolateLiquidate()
- 464: VaultLogic.erc20TransferOutBidAmountToLiqudity(debtAssetData, vars.totalBorrowAmount);
+ 464: VaultLogic.erc20TransferOutBidAmountToLiqudity(debtAssetData, vars.totalBorrowAmount, vars.totalBidAmount);
File: VaultLogic.sol
- 503: function erc20TransferOutBidAmountToLiqudity(DataTypes.AssetData storage assetData, uint amount) internal {
+ 503: function erc20TransferOutBidAmountToLiqudity(DataTypes.AssetData storage assetData, uint amount, unit bidAmount) internal {
- 504: assetData.totalBidAmout -= amount;
+ 504: assetData.totalBidAmout -= bidAmount;
505: assetData.availableLiquidity += amount;
506: }
Assessed type
Math
thorseldon (BendDAO) confirmed and commented:
Fixed here.
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 path reduces the pool's total bid amount by the accrued borrow amount instead of by the actual bid amount, so accumulated interest can push the subtraction below zero.
Impact
The winning liquidator cannot withdraw or unlock the liquidated NFT, and the transaction reverts, leaving the auction outcome stuck.
What the attacker needed
An isolated NFT loan; a liquidator wins the auction; interest accrues past the bid
The fix
The pool's total bid amount should be reduced by the actual bid amount rather than the borrow amount; the project confirmed it was fixed.
How it was found
- Set up a deposit, an isolated NFT borrow and let a year of interest accrue, then lower the NFT price.
- Have a liquidator place the first bid on the auction.
- Wait until the auction ends while interest keeps growing the debt.
- Call the isolate liquidate function, which reverts with an arithmetic error.
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, people bid on a repossessed NFT and the winning bid is held by the pool. When the winner finalises the sale, the code subtracts a number from the pool's record of held bids. That number is the debt, which grows with interest, and it can become larger than the recorded bid, so the subtraction goes below zero and the whole action fails. The result is that the winner's money is stuck and the NFT cannot be released.
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.