Skip to content
Business logic Audit finding Intermediate

`Vault._amount_per_base_lp_share` should also consider bad debt when `safety_module_lp_total_amount` is not enough

The vault's share price calculation for base liquidity providers ignores bad debt when the safety module cannot cover it, so early withdrawers can take more than their fair share and later withdrawers get nothing. An attacker could withdraw their full liquidity before others, leaving remaining providers with losses.

GimelSec Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-unstoppable-judging/issues/92

Found by

GimelSec

Summary

Unstoppable's vault consists of two types of pools. The safety module pool is designed to bear the first loss risk. So the bad debt is first covered by the safety module liquidity. https://github.com/sherlock-audit/2023-06-unstoppable-sces60107/tree/main/unstoppable-dex-audit#margin

Liquidity Providers have the choice to provide liquidity in the "Base LP" pool or the "Safety Module" pool. The Safety Module pool takes a first loss risk protecting the Base LP in exchange for a higher share of the trading and accruing interest.

However, if the safety module liquidity is insufficient, the base liquidity is also used to cover the bad debt. But Vault._amount_per_base_lp_share never take bad debt into consideration, leading to loss of funds.

Vulnerability Detail

When calling Vault.withdraw_liquidity, it uses Vault._account_for_withdraw_liquidity to calculate the withdraw share. https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L884

VYPER
def _account_for_withdraw_liquidity(
    _token: address, _amount: uint256, _is_safety_module: bool
):
    self._update_debt(_token)
    if _is_safety_module:
        shares: uint256 = self._amount_to_lp_shares(_token, _amount, True)
        assert (shares <= self.safety_module_lp_shares[_token][msg.sender]), "cannot withdraw more than you own"
        self.safety_module_lp_total_amount[_token] -= _amount
        self.safety_module_lp_total_shares[_token] -= shares
        self.safety_module_lp_shares[_token][msg.sender] -= shares

    else:
        shares: uint256 = self._amount_to_lp_shares(_token, _amount, False)
        assert (shares <= self.base_lp_shares[_token][msg.sender]), "cannot withdraw more than you own"
        self.base_lp_total_amount[_token] -= _amount
        self.base_lp_total_shares[_token] -= shares
        self.base_lp_shares[_token][msg.sender] -= shares

If the base pool is chosen, it calls self._amount_per_base_lp_share. And it directly uses self.base_lp_total_amount to calculate the shares without considering the bad debt. https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L910 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L946

VYPER
def _amount_to_lp_shares(
    _token: address, _amount: uint256, _is_safety_module: bool
) -> uint256:
    …

    else:  # base_lp
        # initial shares == wei
        if self.base_lp_total_shares[_token] == 0:
            return _amount * PRECISION

        wei_per_share: uint256 = self._amount_per_base_lp_share(_token)
        new_shares: uint256 = _amount * PRECISION * PRECISION / wei_per_share
        return new_shares

def _amount_per_base_lp_share(_token: address) -> uint256:
    return (
        self.base_lp_total_amount[_token]
        * PRECISION
        * PRECISION
        / self.base_lp_total_shares[_token]
    )

Suppose that base_lp_total_amount is 100 and safety_module_lp_total_amount is 100. The bad_debt is now 150 and total_debt_amount is 0. The available liquidity is 50. https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L989

VYPER
def _total_liquidity(_token: address) -> uint256:
    return (
        self.base_lp_total_amount[_token]
        + self.safety_module_lp_total_amount[_token]
        - self.bad_debt[_token]
    )

def _available_liquidity(_token: address) -> uint256:
    return self._total_liquidity(_token) - self.total_debt_amount[_token]

Consider the following situations.

  • The total share of base liquidity is 100 shares, with Alice and Bob each having 50 shares.
  • Alice calls withdraw_liquidity to withdraw 50 tokens from the base pool.
  • Bob also wants to withdraw the liquidity but he fails since the available liquidity is 0 now.

https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L863

VYPER
def withdraw_liquidity(_token: address, _amount: uint256, _is_safety_module: bool):
    …

    assert _amount <= self._available_liquidity(_token), "liquidity not available"

    …

Impact

If the amount of bad debt is more than safe module liquidity, the withdrawal of base liquidity becomes unfair. Early liquidity providers have the advantage of being able to withdraw the full amount, while other liquidity providers are unable to withdraw any liquidity, resulting in a loss of funds for them.

Code Snippet

https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L946 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L957 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L967 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L884 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L910 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L863 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L989

Tool used

Manual Review

Recommendation

If the bad debt exceeds the available safety module liquidity, _amount_per_base_lp_share should take bad debt into consideration.

Diff
def _amount_per_base_lp_share(_token: address) -> uint256:
    return (
-       self.base_lp_total_amount[_token]
+       self._base_total_amount(_token)
        * PRECISION
        * PRECISION
        / self.base_lp_total_shares[_token]
    )

+@internal
+@view
+def _base_total_amount(_token: address) -> uint256:
+   if self.bad_debt[_token] > self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token]:
+       return 0
+   if self.bad_debt[_token] <= self.safety_module_lp_total_amount[_token]:
+       return self.base_lp_total_amount[_token]
+   return self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token] - self.bad_debt[_token]

Discussion

Unstoppable-DeFi

https://github.com/Unstoppable-DeFi/unstoppable-dex-audit/pull/16

maarcweiss

Introduced a new view function to account for when the bad debt surpasses the amount covered by safety_module. Missing

Diff
+ if self.bad_debt[_token] > self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token]:
+       return 0

from watsons recommendation though. Seems good to me but I would like @Unstoppable-DeFi to re-check if letting this check out was intended

Unstoppable-DeFi

Yes, intended.

The definition of bad debt is the amount of liquidity that was borrowed to open a position and not recovered when closing the position (offset by the traders provided margin). Therefore the maximum amount of bad debt that could theoretically be accrued is the total amount of liquidity available (self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token]). There is no scenario where bad debt could be greater than this since closing a position cannot end in a negative return (i.e. is clamped to 0).

maarcweiss

Introduced a new view function to account for when the bad debt surpasses the amount covered by safety_module. Missing

Diff
+ if self.bad_debt[_token] > self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token]:
+       return 0

from watsons recommendation though. Seems good to me but I would like @Unstoppable-DeFi to re-check if letting this check out was intended

Following the sign-off, the previously explained snippet was not included because bad debt can't actually surpass: self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token] therefore the case is not accounted for in the contracts

The original report as published on Sherlock, by GimelSec. 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 share price function for base liquidity providers uses the raw base pool total without subtracting bad debt that exceeds the safety module balance.

Impact

If bad debt exceeds safety module liquidity, base liquidity providers who withdraw later may be unable to withdraw any funds, resulting in a loss.

What the attacker needed

Bad debt exceeds safety module liquidity; user holds base LP shares

The fix

The share price calculation should account for bad debt that exceeds the safety module balance, as was done in a later pull request.

How it was found

  1. Bad debt exceeds the safety module liquidity, so base liquidity must cover the remainder.
  2. Alice calls withdraw_liquidity to withdraw her base pool tokens.
  3. The share calculation ignores bad debt, so Alice receives the full amount.
  4. Bob tries to withdraw but fails because available liquidity is now 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 vault holds money from two groups: a safety pool that absorbs losses first, and a base pool. If losses exceed the safety pool, the base pool should shrink, but the code still values base pool shares as if no loss occurred. This lets the first person to withdraw take out too much, leaving later people with less or nothing.

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 GimelSec

View all

18 more disclosed reports, best first.

More from Unstoppable

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.