Skip to content
Business logic Audit finding Advanced

Last collateral check is not safe

The liquidation purchase function decided whether an NFT was a vault's last collateral by checking whether the cached collateral value was zero, rather than checking the NFT count. If the external price oracle returned zero while the vault still held NFTs, the code would treat it as the last collateral and wipe the remaining debt, letting the owner withdraw the other NFTs for free.

hansfriese Follow

Originally disclosed on Code4rena

Submitted by hansfriese

Liquidation might work incorrectly.

Proof of Concept

There is a function purchaseLiquidationAuctionNFT() to allow liquidators to purchase NFTs on auction.

In the line 273, the protocol checks if the current NFT is the last collateral using the collateralValueCached.

But it might be possible for Reservoir Oracle to return zero (for whatever reason) and in that case collateralValueCached will be zero even when the _vaultInfo[auction.nftOwner][auction.auctionAssetContract].count!=0.

One might argue that it is impossible for the Reservoir oracle to return zero output but I think it is safe not to rely on it.

PaprController.sol
264:     function purchaseLiquidationAuctionNFT(
265:         Auction calldata auction,
266:         uint256 maxPrice,
267:         address sendTo,
268:         ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
269:     ) external override {
270:         uint256 collateralValueCached = underwritePriceForCollateral(
271:             auction.auctionAssetContract, ReservoirOracleUnderwriter.PriceKind.TWAP, oracleInfo
272:         ) * _vaultInfo[auction.nftOwner][auction.auctionAssetContract].count;
273:         bool isLastCollateral = collateralValueCached == 0;//@audit not safe
274:
275:         uint256 debtCached = _vaultInfo[auction.nftOwner][auction.auctionAssetContract].debt;
276:         uint256 maxDebtCached = isLastCollateral ? debtCached : _maxDebt(collateralValueCached, updateTarget());
277:         /// anything above what is needed to bring this vault under maxDebt is considered excess
278:         uint256 neededToSaveVault = maxDebtCached > debtCached ? 0 : debtCached - maxDebtCached;
279:         uint256 price = _purchaseNFTAndUpdateVaultIfNeeded(auction, maxPrice, sendTo);time
280:         uint256 excess = price > neededToSaveVault ? price - neededToSaveVault : 0;
281:         uint256 remaining;
282:
283:         if (excess > 0) {
284:             remaining = _handleExcess(excess, neededToSaveVault, debtCached, auction);
285:         } else {
286:             _reduceDebt(auction.nftOwner, auction.auctionAssetContract, address(this), price);
287:             remaining = debtCached - price;
288:         }
289:
290:         if (isLastCollateral && remaining != 0) {
291:             /// there will be debt left with no NFTs, set it to 0
292:             _reduceDebtWithoutBurn(auction.nftOwner, auction.auctionAssetContract, remaining);
293:         }
294:     }
295:

Recommended Mitigation Steps

Change the line 273 as below.

bool isLastCollateral = _vaultInfo[auction.nftOwner][auction.auctionAssetContract].count == 0;

wilsoncusack (Backed) confirmed and commented:

Not sure if this was flagged in other issues but the outcome of this is significant: if we incorrectly think that it is a user’s last NFT, then we will set their debt to 0. If they did in fact have other NFTs in, then they can withdraw these for free!

trust1995 (judge) commented:

The impact of underwritePriceForCollateral() returning 0 when count != 0 is clear. However, user has not specified a single plausible reason as to how the oracle could return 0. From my knowledge, it should not be possible with standard oracles, and therefore the finding can at most be treated as a Low level find. Medium severity should clearly define hypotheticals, which are missing in the above report.

hansfriese (warden) commented:

@trust1995 Please note that it is possible for the Reservoir oracle (that is used in this protocol) to return zero price.

I tried their test suite using a collection 0x495f947276749Ce646f68AC8c248420045cb7b5e.

From the protocol’s viewpoint, Reservoir is still an external dependency and I think no assumptions should be made about it.

I reached out to the Reservoir protocol dev team regarding this and got a reply as below.

After all, the Reservoir team also warns that it is not safe to assume their return price can not be zero.

trust1995 (judge) commented:

After deliberating on the decision with another judge, believe it is best to give warden the benefit of the doubt regarding hypotheticals surrounding zero return value. Will award Medium.


The original report as published on Code4rena, by hansfriese. 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 last-collateral check relied on a cached collateral value being zero instead of directly checking the stored NFT count, so a zero oracle price was mistaken for an empty vault.

What the attacker needed

Oracle returns a zero price; vault still holds other NFTs; liquidation auction purchase occurs

The fix

The check should compare the stored NFT count for the vault against zero instead of relying on the cached collateral value.

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 lending protocol lets people borrow against NFTs they deposit. When a loan is liquidated, the code tries to work out if the NFT being sold is the borrower's last one, and if so it clears any leftover debt. The bug is that it guesses this from a price value being zero instead of counting the NFTs, so a zero price from an outside price service could wrongly clear the debt.

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 hansfriese

View all

65 more disclosed reports, best first.

More from Papr contest

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.