Skip to content
Business logic Audit finding Intermediate

In edge cases, create_pool can either be reverted or allow user underpay fees

The pool creation fee validation uses a strict equality check and an 'any' check for token factory fees, so in edge cases a user can either underpay the token factory fee or the transaction reverts even when all fees are paid.

oakcobalt Follow with 0x1982us, Abdessamed and 1 other

Originally disclosed on Code4rena

Submitted by oakcobalt, also found by 0x1982us, Abdessamed, and Lambda

/contracts/pool-manager/src/helpers.rs#L563-L564

Finding description and impact

create_pool is permissionless. User will need to pay both pool creation fee and token factory fee (fees charged for creating lp token) when creating a pool.

The vulnerabilities are:

  1. In validate_fees_are_paid, a strict equality check is used between total paid fees in pool_creation token and required pool creation fee. paid_pool_fee_amount == pool_creation_fee.amount.
  2. denom_creation_fee.iter() uses .any instead of .all, which allows user to only pay one coin from denom_creation.
//contracts/pool-manager/src/helpers.rs
pub fn validate_fees_are_paid(
    pool_creation_fee: &Coin,
    denom_creation_fee: Vec<Coin>,
    info: &MessageInfo,
) -> Result<Vec<Coin>, ContractError> {
...
    // Check if the pool fee denom is found in the vector of the token factory possible fee denoms
    if let Some(tf_fee) = denom_creation_fee
        .iter()
        .find(|fee| &fee.denom == pool_fee_denom)
    {
        // If the token factory fee has only one option, check if the user paid the sum of the fees
        if denom_creation_fee.len() == 1usize {
...
        } else {
            // If the token factory fee has multiple options besides pool_fee_denom, check if the user paid the pool creation fee
            let paid_pool_fee_amount = get_paid_pool_fee_amount(info, pool_fee_denom)?;
            //@audit (1) strict equality check. When user is also required to pay denom_creation_fee in pool creation fee token, check will revert create_pool
            ensure!(
 |>             paid_pool_fee_amount == pool_creation_fee.amount,
                ContractError::InvalidPoolCreationFee {
                    amount: paid_pool_fee_amount,
                    expected: pool_creation_fee.amount,
                }
            );
...
            // Check if the user paid the token factory fee in any other of the allowed denoms
            //@audit (2) iter().any() only requires one of denom_creation_fee token to be paid.
|>          let tf_fee_paid = denom_creation_fee.iter().any(|fee| {
                let paid_fee_amount = info
                    .funds
                    .iter()
                    .filter(|fund| fund.denom == fee.denom)
                    .map(|fund| fund.amount)
                    .try_fold(Uint128::zero(), |acc, amount| acc.checked_add(amount))
                    .unwrap_or(Uint128::zero());

                total_fees.push(Coin {
                    denom: fee.denom.clone(),
                    amount: paid_fee_amount,
                });

                paid_fee_amount == fee.amount
            });
...

/contracts/pool-manager/src/helpers.rs#L577

Based on cosmwasm tokenfactory, denom creation fee (std.coins) can contain multiple coins and every coin needs to be paid.

//x/tokenfactory/simulation/operations.go
func SimulateMsgCreateDenom(tfKeeper TokenfactoryKeeper, ak types.AccountKeeper, bk BankKeeper) simtypes.Operation {
...
		// Check if sims account enough create fee
		createFee := tfKeeper.GetParams(ctx).DenomCreationFee
		balances := bk.GetAllBalances(ctx, simAccount.Address)
|>		_, hasNeg := balances.SafeSub(createFee) //@audit-info all denom creation fee tokens have to be paid
		if hasNeg {
			return simtypes.NoOpMsg(types.ModuleName, types.MsgCreateDenom{}.Type(), "Creator not enough creation fee"), nil, nil
		}
...

https://github.com/CosmWasm/token-factory/blob/47dc2d5ae36980bcc03cf746580f7cb3deabc39e/x/tokenfactory/simulation/operations.go#L359-L361

Flows: contracts/pool-manager/src/manager/commands::create_pool -> validate_fees_are_paid()

Impact

User can either underpay fees, or create_pool tx will revert.

Proof of Concept

Because pool creation fee and all denom creation fee tokens need to be paid. Consider this edge case: A. There are more than one token factory fee tokens required in denom_creation_fee. B. One of the denom*creation fee token is the pool creation fee token (fee.denom == pool*creation_fee.denom).

In this case, user is required to send = pool token amount (pool_creation_fee + denom_creation_fee) and all other denom creation token amount.

If user sends all the required fees, paid_pool_fee_amount == pool_creation_fee.amount check will fail because paid_pool_fee.amount > pool_creation_fee.amount. This reverts create_pool.

If user chooses to take advantage of denom_creation_fee.iter().any, user sent = pool_creation_fee + one of denom creation token amounts (not the pool token). This passes both the strict equality check and .any. However, the user only paid pool_creation_fee and underpaid denom_creation_fee.

Recommended mitigation steps

  1. Change .any() -> .all().
  2. Because this branch denom_creation_fee contains the pool_creation token, needs to add a control flow to handle the iteration of pool_creation token to check paid_fee_amount == fee.amount + pool_creation_fee.amount.

3docSec (judge) commented:

Looks to be intended behavior, as per comment L576.

jvr0x (MANTRA) disputed and commented:

pool_creation_fee is the fee for creating the pool while the vec. denom_creation_fee are the tokens that the user can pay for the token factory in. Only 1 is enough, no need to pay in all the denoms listed there.

3docSec (judge) commented:

Behavior is inconsistent with the MantraChain tokenfactory that collects all fees; okay for valid Medium.


The original report as published on Code4rena, by oakcobalt. 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

The code uses a strict equality check for the pool creation fee and an 'any' check for the token factory fees, which does not correctly handle cases where the pool fee token is also one of the token factory fee tokens.

What the attacker needed

Multiple token factory fee tokens; pool fee token is one of them

The fix

The code should be fixed by changing `.any()` to `.all()` and adding logic to handle the pool creation token in the token factory fee iteration.

How it was found

  1. Have more than one token factory fee token required in denom_creation_fee.
  2. Have one of the denom creation fee tokens be the same as the pool creation fee token.
  3. Send exactly the pool creation fee plus one other denom creation token amount (not the pool token) to pass both checks but underpay the token factory fee.
  4. Alternatively, send all required fees, which causes the strict equality check to fail and the transaction to revert.

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 you create a pool, you must pay a fee in a specific token and also a fee to create a new token. The code checks if you paid the exact pool fee and if you paid at least one of the token creation fees. In some cases, this lets you pay less than you should, or it blocks you from creating a pool even if you paid everything.

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 oakcobalt

View all

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