Skip to content
Smart contracts Audit finding Advanced

At the second time the nft is used as collateral to take a loan, the debt repayment via auction fund can be failed when liquidation

When an NFT is used as collateral a second time, a leftover token allowance from the first liquidation causes the approval call in the clearing house to revert, so the debt cannot be repaid from auction funds.

KIntern_NA Follow

Originally disclosed on Code4rena

Submitted by KIntern_NA

https://github.com/code-423n4/2023-01-astaria/blob/1bfc58b42109b839528ab1c21dc9803d663df898/src/ClearingHouse.sol#L143-L146

When a user transfer an NFT to CollateralToken contract, it will toggle the function CollateralToken.onERC721Received(). In this function if there didn’t exist any clearingHouse for the collateralId, it will create a new one for that collateral.

if (s.clearingHouse[collateralId] == address(0)) {
    address clearingHouse = ClonesWithImmutableArgs.clone(
      s.ASTARIA_ROUTER.BEACON_PROXY_IMPLEMENTATION(),
      abi.encodePacked(
        address(s.ASTARIA_ROUTER),
        uint8(IAstariaRouter.ImplementationType.ClearingHouse),
        collateralId
      )
    );

    s.clearingHouse[collateralId] = clearingHouse;
  }

The interesting thing of this technique is: there will be just one clearingHouse be used for each collateral no matter how many times the collateral is transferred to the contract. Even when the lien is liquidated / fully repayed, the s.clearingHouse[collateralId] remain unchanged.

The question here is any stale datas in clearingHouse from the previous time that the nft was used as collateral can affect the behavior of protocol when the nft was transfered to CollateralToken again?

Let take a look at the function ClearingHouse._execute(). In this function, the implementation uses safeApprove() to approve payment - liquidatorPayment amount for the TRANSFER_PROXY.

ERC20(paymentToken).safeApprove(
  address(ASTARIA_ROUTER.TRANSFER_PROXY()),
  payment - liquidatorPayment
);

the safeApprove function will revert if the allowance was set from non-zero value to non-zero value. This will incur some potential risk for the function like example below:

  1. NFT x is transferred to CollateralToken to take loans and then it is liquidated.
  2. At time 10, function ClearingHouse._execute() was called and the payment - liquidatorPayment > totalDebt. This will the paymentToken.allowance[clearingHouse][TRANSFER_PROXY] > 0 after the function ended.
  3. NFT x is transferred to CollateralToken for the second time to take a loans and then it is liquidated again.
  4. At time 15 (> 10), function ClearingHouse._execute() was called, but at this time, the safeApprove will revert since the previous allowance is different from 0

Impact

The debt can be repayed by auction funds when liquidation.

Recommended Mitigation Steps

Consider to use approve instead of safeApprove.

androolloyd (Astaria) commented:

We use the solmate library which doesn’t seem to have a check for approvals being set to 0.

SantiagoGregory (Astaria) confirmed


The original report as published on Code4rena, by KIntern_NA. 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 clearing house uses a token approval function that reverts when changing a non-zero allowance to another non-zero value, and the allowance is not reset after the first liquidation.

Impact

The debt repayment via auction funds can fail when liquidation occurs for a collateral that was previously used.

What the attacker needed

NFT used as collateral at least once before; Previous liquidation left a non-zero allowance; NFT used as collateral a second time; Second liquidation triggers _execute

The fix

The report recommends using approve instead of safeApprove, though the project noted its library does not check for setting approvals to zero.

How it was found

  1. Transfer NFT x to the CollateralToken contract to take a loan, then let it be liquidated.
  2. At time 10, ClearingHouse._execute() is called and the payment minus liquidator payment is greater than totalDebt, leaving a non-zero allowance for the transfer proxy.
  3. Transfer NFT x to the CollateralToken contract a second time to take another loan, then let it be liquidated again.
  4. At time 15, ClearingHouse._execute() is called again, but safeApprove reverts because the previous allowance is not zero.

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.

A smart contract lets people borrow money using a digital collectible as security. When the loan is not repaid, the collectible is sold and the money is used to pay the debt. The bug means that if the same collectible is used for a second loan, a leftover permission from the first sale stops the payment step, so the debt stays unpaid.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by KIntern_NA

View all

21 more disclosed reports, best first.

More from Astaria contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.