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.
Originally disclosed on Code4rena
Submitted by KIntern_NA
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:
- NFT x is transferred to
CollateralTokento take loans and then it is liquidated. - At time 10, function
ClearingHouse._execute()was called and thepayment - liquidatorPayment > totalDebt. This will thepaymentToken.allowance[clearingHouse][TRANSFER_PROXY] > 0after the function ended. - NFT x is transferred to
CollateralTokenfor the second time to take a loans and then it is liquidated again. - At time 15 (> 10), function
ClearingHouse._execute()was called, but at this time, thesafeApprovewill 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
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
- Transfer NFT x to the CollateralToken contract to take a loan, then let it be liquidated.
- 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.
- Transfer NFT x to the CollateralToken contract a second time to take another loan, then let it be liquidated again.
- 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.