Skip to content
Business logic Audit finding Intermediate

_liquidateUser() should not re-use the same minimum swap amount out for multiple liquidation

The liquidation function reuses a single minimum swap output amount for multiple borrowers, so slippage protection is inaccurate. This can cause the liquidator to receive less than expected or the entire liquidation to revert.

peakbolt Follow with carrotsmuggler, Nyx and 3 others

Originally disclosed on Code4rena

Submitted by peakbolt, also found by carrotsmuggler, Nyx, n1punp, Ack, and rvierdiiev

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/master/contracts/markets/singularity/SGLLiquidation.sol#L337-L340

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/bigBang/BigBang.sol#L603-L606

Vulnerability details

In Singularity and BigBang, the minAssetAmount in _liquidateUser() is provided by the liquidator as a slippage protection to ensure that the swap provides the specified amountOut. However, the same value is utilized even when liquidate() is used to liquidate multiple borrowers.

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/master/contracts/markets/singularity/SGLLiquidation.sol#L337-L351

    function _liquidateUser(
        ...
        uint256 minAssetAmount = 0;
        if (dexData.length > 0) {
            //@audit the same minAssetAmount is incorrectly applied to all liquidations
            minAssetAmount = abi.decode(dexData, (uint256));
        }

        ISwapper.SwapData memory swapData = swapper.buildSwapData(
            collateralId,
            assetId,
            0,
            collateralShare,
            true,
            true
        );
        swapper.swap(swapData, minAssetAmount, address(this), "");

Impact

Using the same minAssetAmount (minimum amountOut for swap) for the liquidation of multiple borrowers will result in inaccurate slippage protection and transaction failure.

If minAssetAmount is too low, there will be insufficient slippage protection and the the liquidator and protocol could be short changed with a worse than expected swap.

If minAssetAmount is too high, the liquidation will fail as the swap will not be successful.

Proof of Concept

First scenario

  1. Liquidator liquidates two loans X & Y using liquidate(), and set the minAssetAmount to be 1000 USDO.
  2. Loan X liquidated collateral is worth 1000 USDO and the swap is completely successful with zero slippage.
  3. However, Loan Y liquidated collateral is worth 5000 USDO, but due to low liquidity in the swap pool, it was swapped at 1000 USDO (minAssetAmount).

The result is that the liquidator will receive a fraction of the expected reward and the protocol gets repaid at 1/5 of the price, suffering a loss from the swap.

Second scenario

  1. Liquidator liquidates two loans X & Y using liquidate(), and set the minAssetAmount to be 1000 USDO.
  2. Loan X liquidated collateral is worth 1000 USDO and the swap is completely successful with zero slippage.
  3. we suppose Loan Y’s liquidated collateral is worth 300 USDO.

Now the minAssetAmount of 1000 USDO will be higher than the collateral, which is unlikely to be completed as it is higher than market price. That will revert the entire liquidate(), causing the liquidation of Loan X to fail as well.

Recommended Mitigation Steps

Update liquidate() to allow liquidator to pass in an array of minAssetAmount values that corresponding to the liquidated borrower.

An alternative, is to pass in the minimum expected price of the collateral and use that to compute the minAssetAmount.

0xRektora (Tapioca) confirmed via duplicate issue 122


The original report as published on Code4rena, by peakbolt. 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 liquidate function decodes a single minAssetAmount from dexData and applies it to every borrower's swap instead of using a per-borrower value.

Impact

The liquidator could receive a fraction of the expected reward and the protocol could suffer a loss from a worse-than-expected swap, or the entire liquidation could revert if the minimum is too high.

What the attacker needed

Liquidator calls liquidate() for multiple borrowers; dexData contains a single minAssetAmount

The fix

Update liquidate() to accept an array of minAssetAmount values corresponding to each borrower, or pass a minimum expected price and compute the minAssetAmount per borrower.

How it was found

  1. Liquidator calls liquidate() for two loans X and Y, setting minAssetAmount to 1000 USDO.
  2. Loan X's collateral is worth 1000 USDO and swaps successfully with zero slippage.
  3. Loan Y's collateral is worth 5000 USDO but due to low liquidity swaps at only 1000 USDO, the minAssetAmount.
  4. Alternatively, if Loan Y's collateral is worth 300 USDO, the minAssetAmount of 1000 USDO is higher than the collateral value, causing the swap to revert and the entire liquidate() call to fail, including Loan X's liquidation.

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 loan is liquidated, the collateral is swapped for another token. The liquidator can set a minimum amount they expect to receive from that swap to protect against bad prices. Here, the same minimum is applied to every loan in a batch, which can be too low for one loan or too high for another, leading to losses or a failed transaction.

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 peakbolt

View all

16 more disclosed reports, best first.

More from Tapioca Invitational

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.