Skip to content
Business logic Audit finding Intermediate

User fund loss because function purchaseLiquidationAuctionNFT() takes extra liquidation penalty when user’s last collateral is liquidated, (set wrong value for maxDebtCached when isLastCollateral is true)

The function purchaseLiquidationAuctionNFT() incorrectly sets maxDebtCached to the full debt when the auctioned NFT is the user's last collateral, causing the entire payment to be treated as excess and a 10% liquidation penalty to be taken from the whole amount. This means the user loses 10% of the auction price instead of 10% of the excess over debt, resulting in fund loss.

unforgiven Follow with hansfriese

Originally disclosed on Code4rena

Submitted by unforgiven, also found by hansfriese

Function purchaseLiquidationAuctionNFT() purchases a liquidation auction with the controller’s papr token. The liquidator pays the papr amount which is equal to price of the auction and receives the auctioned NFT. Contract would transfer paid papr and pay borrower debt and if there is extra papr left, it would be transferred to the user.

For extra papr that is not required for brining user debt under max debt, contract gets liquidation penalty but in some cases (when the auctioned NFT is user’s last collateral) contract take penalty from all of the transferred papr and not just the extra. So users would lose funds in those situations because of this and the fund could be big because the penalty is 10% of the price of the auction and in most cases user would lose 10% of his debt (the value of the NFT).

Proof of Concept

This is purchaseLiquidationAuctionNFT() code:

    function purchaseLiquidationAuctionNFT(
        Auction calldata auction,
        uint256 maxPrice,
        address sendTo,
        ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
    ) external override {
        uint256 collateralValueCached = underwritePriceForCollateral(
            auction.auctionAssetContract, ReservoirOracleUnderwriter.PriceKind.TWAP, oracleInfo
        ) * _vaultInfo[auction.nftOwner][auction.auctionAssetContract].count;
        bool isLastCollateral = collateralValueCached == 0;

        uint256 debtCached = _vaultInfo[auction.nftOwner][auction.auctionAssetContract].debt;
        uint256 maxDebtCached = isLastCollateral ? debtCached : _maxDebt(collateralValueCached, updateTarget());
        /// anything above what is needed to bring this vault under maxDebt is considered excess
        uint256 neededToSaveVault = maxDebtCached > debtCached ? 0 : debtCached - maxDebtCached;
        uint256 price = _purchaseNFTAndUpdateVaultIfNeeded(auction, maxPrice, sendTo);
        uint256 excess = price > neededToSaveVault ? price - neededToSaveVault : 0;
        uint256 remaining;

        if (excess > 0) {
            remaining = _handleExcess(excess, neededToSaveVault, debtCached, auction);
        } else {
            _reduceDebt(auction.nftOwner, auction.auctionAssetContract, address(this), price);
            remaining = debtCached - price;
        }

        if (isLastCollateral && remaining != 0) {
            /// there will be debt left with no NFTs, set it to 0
            _reduceDebtWithoutBurn(auction.nftOwner, auction.auctionAssetContract, remaining);
        }
    }

As you can see when collateralValueCached is 0 and user has no more collaterals left then the value of isLastCollateral set as true. And when isLastCollateral is true the value of maxDebtCached set as debtCached (line maxDebtCached = isLastCollateral ? debtCached : _maxDebt(collateralValueCached, updateTarget());) and the value of the neededToSaveVault would be 0 (line neededToSaveVault = maxDebtCached > debtCached ? 0 : debtCached - maxDebtCached) and the excess would be equal to price (in the line excess = price > neededToSaveVault ? price - neededToSaveVault : 0) so all the papr paid by liquidator would be considered as excess and the contract would get liquidation penalty out of that. So in the current implementation in last collateral liquidation all of the paid papr by liquidator would be considered excess:

  1. uUer has no NFT left.
  2. debtCached is 100.
  3. collateralValueCached is 0 and isLastCollateral is true.
  4. maxDebtCached would be as debtCached which is 100.
  5. neededToSaveVault would be debtCached - maxDebtCached which is 0.
  6. excess would equal to price and code would take penalty out of all the price amount.

Code wants to take penalty from what borrower is going to receive (other than the required amount for extra debt), but in the current implementation when it is last NFT code took fee from all of the payment. These are the steps that show how the issue would harm the borrower and borrower would lose funds: (of course user debt would be set to 0 in the end, but if price was higher than user debt user won’t receive the extra amount).

  1. User debt is 900 and price of auction is 1000 and user has no NFT left.
  2. Some one pays 1000 Papr and buys the auctioned token, now user would receive 0 amount because the penalty would be 1000 * 10% = 100 and the debt is 900.
  3. But penalty should be (1000-900) * 10% = 10 and user should have received 90 token.

So users would receive less amount when their last NFT is liquidated and the price is higher than debt. Users would lose 10% of their entitled fund. Most users can use one token as collateral so the bug can happen most of the time.

Tools Used

VIM

Recommended Mitigation Steps

The code should be like this:

uint256 maxDebtCached = isLastCollateral ? 0: _maxDebt(collateralValueCached, updateTarget());

wilsoncusack (Backed) confirmed

trust1995 (judge) decreased severity to Medium


The original report as published on Code4rena, by unforgiven. 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 logic error is that maxDebtCached is set to debtCached when isLastCollateral is true, making neededToSaveVault zero and causing all of the price to be considered excess, so the penalty is applied to the full price.

Impact

Users could lose 10% of their entitled funds when their last NFT is liquidated and the auction price exceeds the debt.

What the attacker needed

User has only one collateral; auction price exceeds debt; liquidation occurs

The fix

The code should be fixed by setting maxDebtCached to 0 when isLastCollateral is true, so that only the excess over debt is subject to penalty.

How it was found

  1. User has no NFT left and debtCached is 100.
  2. collateralValueCached is 0 and isLastCollateral is true.
  3. maxDebtCached is set to debtCached (100).
  4. neededToSaveVault becomes debtCached - maxDebtCached = 0.
  5. excess equals price, so penalty is taken from the entire price.

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.

When a borrower's last collateral is sold at auction, the code mistakenly charges a 10% penalty on the entire sale price instead of only on the amount left after repaying the debt. This means the borrower gets less money back than they should.

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 unforgiven

View all

50 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.