Skip to content
Business logic Audit finding Intermediate

Price formula in `TpdaLiquidationPair._computePrice()` does not account for a jump in liquidatable balance

The price formula in TpdaLiquidationPair._computePrice() decreases linearly over time and does not account for sudden increases in the vault's liquidatable balance, so the price can be much lower than the actual value. An attacker could exploit this by liquidating the vault's balance at an artificially low price, causing a loss of funds for the vault and its users.

0x73696d616f Follow with 0xSpearmint1 and MiloTruck

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-pooltogether-judging/issues/38

The protocol has acknowledged this issue.

Found by

0x73696d616f, 0xSpearmint1, MiloTruck

Summary

The linearly decreasing auction formula in TpdaLiquidationPair._computePrice() does not account for sudden increases in the vault's liquidatable balance, causing the price returned to be much lower than it should be.

Vulnerability Detail

The price paid by liquidation bots to liquidate the asset balance in a vault is calculated in TpdaLiquidationPair._computePrice() as shown:

TpdaLiquidationPair.sol#L191-L195

Solidity
        uint256 elapsedTime = block.timestamp - lastAuctionAt;
        if (elapsedTime == 0) {
            return type(uint192).max;
        }
        uint192 price = uint192((targetAuctionPeriod * lastAuctionPrice) / elapsedTime);

As seen from above, the price paid decreases linearly over time from targetAuctionPeriod * lastAuctionPrice to 0. This allows the liquidation pair to naturally find a price that liquidation bots are willing to pay for the current liquidatable balance in the vault, which is calculated as shown:

TpdaLiquidationPair.sol#L184-L186

Solidity
    function _availableBalance() internal returns (uint256) {
        return ((1e18 - smoothingFactor) * source.liquidatableBalanceOf(address(_tokenOut))) / 1e18;
    }

The vault's liquidatable balance is determined by liquidatableBalanceOf():

PrizeVault.sol#L687-L709

Solidity
    function liquidatableBalanceOf(address _tokenOut) external view returns (uint256) {
        uint256 _totalDebt = totalDebt();
        uint256 _maxAmountOut;
        if (_tokenOut == address(this)) {
            // Liquidation of vault shares is capped to the mint limit.
            _maxAmountOut = _mintLimit(_totalDebt);
        } else if (_tokenOut == address(_asset)) {
            // Liquidation of yield assets is capped at the max yield vault withdraw plus any latent balance.
            _maxAmountOut = _maxYieldVaultWithdraw() + _asset.balanceOf(address(this));
        } else {
            return 0;
        }

        // The liquid yield is limited by the max that can be minted or withdrawn, depending on
        // `_tokenOut`.
        uint256 _availableYield = _availableYieldBalance(totalPreciseAssets(), _totalDebt);
        uint256 _liquidYield = _availableYield >= _maxAmountOut ? _maxAmountOut : _availableYield;

        // The final balance is computed by taking the liquid yield and multiplying it by
        // (1 - yieldFeePercentage), rounding down, to ensure that enough yield is left for
        // the yield fee.
        return _liquidYield.mulDiv(FEE_PRECISION - yieldFeePercentage, FEE_PRECISION);
    }

Apart from the yield vault suddenly gaining yield, there are two reasons why the vault's liquidatable balance might suddenly increase:

  • (1) The liquidatable balance is no longer restricted by the mint limit after someone withdraws from the vault. For example:
    • Assume _tokenOut == address(this).
    • The vault is currently at the mint limit, so _maxAmountOut = _mintLimit(_totalDebt) = 0.
    • A user withdraws from the vault, causing the mint limit to decrease. Assume that _maxAmountOut is now 1000e18, which means 1000 more shares can be minted.
    • Since _liquidYield is restricted by _maxAmountOut, it jumps from 0 to 1000e18, causing a sudden increase in the liquidatable balance.
  • (2) The vault's owner calls setYieldFeePercentage() to decrease yieldFeePercentage. As seen from above, this will cause the returned value from liquidatableBalanceOf() to suddenly increase.

However, since the vault's liquidatable balance is not included in price calculation and the price decreases linearly, TpdaLiquidationPair._computePrice() cannot accurately account for instantaneous increases in liquidatableBalanceOf().

Using (1) to illustrate this:

  • Assume that the vault's token is USDT and the prize token is USDC.
  • Since the vault is currently at its mint limit, the liquidatable balance is 0 USDT.
  • Over time, the price calculated in _computePrice() decreases to near-zero to match the liquidatable balance.
  • A user suddenly withdraws, which increases the mint limit and causes the liquidatable balance to jump to 1000 USDT.
  • However, the price returned by _computePrice() will still remain close to 0 USDC.
  • This allows a liquidation bot to swap 1000 USDT in the vault for 0 USDC.

As seen from above, when a sudden increase in liquidatable balance occurs, the price in _computePrice() could be too low due to the linearly decreasing auction.

Impact

When liquidatableBalanceOf() suddenly increases due to an increase in the mint limit or a reduction in yieldFeePercentage, liquidation bots can swap the liquidatable balance in the vault for less than its actual value. This causes a loss of funds for the vault, and by extension, all users in the vault.

Code Snippet

https://github.com/sherlock-audit/2024-05-pooltogether/blob/1aa1b8c028b659585e4c7a6b9b652fb075f86db3/pt-v5-tpda-liquidator/src/TpdaLiquidationPair.sol#L191-L195

https://github.com/sherlock-audit/2024-05-pooltogether/blob/1aa1b8c028b659585e4c7a6b9b652fb075f86db3/pt-v5-tpda-liquidator/src/TpdaLiquidationPair.sol#L184-L186

https://github.com/sherlock-audit/2024-05-pooltogether/blob/1aa1b8c028b659585e4c7a6b9b652fb075f86db3/pt-v5-vault/src/PrizeVault.sol#L687-L709

Tool used

Manual Review

Recommendation

Consider using an alternative auction formula in _computePrice(), ideally one that takes into account the current liquidatable balance in the vault.

Discussion

sherlock-admin4

1 comment(s) were left on this issue during the judging contest.

infect3d commented:

that is the reason of the MIN_PRICE state variable

trmid

If the yield source is expected to have large jumps in yield (by design or by the prize vault owner's actions) the prize vault owner can either:

  1. set a suitable smoothingFactor on the liquidation pair to mitigate the effect within a reasonable efficiency target
  2. pause liquidations during times of anticipated flux and set a new liquidation pair when ready (this would be good practice if suddenly lowering the yield fee percentage).

0xjuaan

Escalate

This issue should be informational

This is clearly a design decision by the protocol

The sponsor has also suggested some ways the prizeVault owner can mitigate this in the vault setup

Even if there is the odd upward fluctuation of yield the current design of the auction system ensures it will correct itself for future liquidations

sherlock-admin3

Escalate

This issue should be informational

This is clearly a design decision by the protocol

The sponsor has also suggested some ways the prizeVault owner can mitigate this in the vault setup

Even if there is the odd upward fluctuation of yield the current design of the auction system ensures it will correct itself for future liquidations

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

MiloTruck

This is clearly a design decision by the protocol

The sponsor has also suggested some ways the prizeVault owner can mitigate this in the vault setup

I don't see how the auction mechanism being a design decision invalidates this issue. I've clearly pointed out how this design decision is incapable of handling upward yield fluctuations, which is an actual problem.

The sponsor has pointed out ways which, in my opinion, only partially mitigate the problem presented. The owner has to either has to temporarily stop liquidations or reduce the percentage of yield that can be liquidated to smooth out the upward fluctuation. Note that both ways are a form of owner intervention, and I don't believe it is documented anywhere on how the owner should deal with upward yield fluctuations.

Even if there is the odd upward fluctuation of yield the current design of the auction system ensures it will correct itself for future liquidations

The prize vault still loses funds for the current liquidation, so this doesn't invalidate anything.

nevillehuang

Agree with @MiloTruck comments.

WangSecurity

As the report says, the issue may cause loss of funds to users, hence, I believe design decision rule is not appropriate here. Planning to reject the escalation and leave the issue as it is.

WangSecurity

Result: Medium Has duplicates

sherlock-admin4

Escalations have been resolved successfully!

Escalation status:

The original report as published on Sherlock, by 0x73696d616f. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 price formula only considers elapsed time and the last auction price, ignoring the current liquidatable balance, so it cannot react to sudden increases in available assets.

Impact

Liquidation bots can swap the liquidatable balance in the vault for less than its actual value, causing a loss of funds for the vault and its users.

What the attacker needed

Vault at mint limit or high yield fee; sudden increase in liquidatable balance; liquidation bot ready to act

The fix

The recommendation is to use an alternative auction formula that takes into account the current liquidatable balance in the vault.

How it was found

  1. The vault is at its mint limit, so the liquidatable balance is zero.
  2. Over time, the price calculated in _computePrice() decreases to near-zero.
  3. A user withdraws from the vault, increasing the mint limit and causing the liquidatable balance to jump to a large amount.
  4. The price remains near-zero, allowing a liquidation bot to swap the large liquidatable balance for almost nothing.

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.

In this system, a vault holds assets and periodically sells some of them through an auction. The auction price starts high and goes down over time until someone buys. The problem is that if the amount of assets available to sell suddenly increases (for example, because a user withdraws and frees up space), the price does not adjust upward. So a buyer can purchase the newly available assets at the old, very low price, which is unfair to the vault and its users.

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 0x73696d616f

View all

172 more disclosed reports, best first.

More from PoolTogether: The Prize Layer for DeFi

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.