The coinswap module's balance retrieval iterates over all token balances, so an attacker can send many token denominations to a pool's escrow address, causing gas exhaustion and failure of liquidity operations. This could disrupt swaps and liquidity removal, though the impact is limited to auto-swap during onboarding.
The balance calculations are initiated by calling k.GetPoolBalances(ctx, pool.EscrowAddress), which internally calls the k.bk.GetAllBalances function. This function iterates through all token balances in a loop. If the array of tokens is excessively large, the function may fail due to insufficient gas.
In essence, if an attacker introduces a large number of tokens, for instance through the AddLiquidity process, and subsequently transfers these tokens to a target pool, it can lead to an exploit. The attacker can strategically overload the array, causing significant gas consumption and ultimately causing the function to fail.
The process is as follows:
When the AddLiquidity or RemoveLiquidity functions are called within the coinswap module, the k.GetPoolBalances function retrieves the balance of all tokens in the pool. This function, k.GetPoolBalances, calls k.bk.GetAllBalances, which iterates through and aggregates all token balances before sorting them into an array.
Specifically, k.bk.GetAllBalances utilizes the following approach:
Here, sdk.NewCoins() returns an array of type Coins.
When an attacker exploits the AddLiquidity function in the coinswap module, they can create a pool using k.CreatePool(ctx, msg.MaxToken.Denom) if the pool does not already exist. By generating a large number of tokens and sending them to the target pool, the attacker causes the array of balances returned by GetPoolBalances to become excessively large. This leads to high gas consumption and potential transaction failure due to insufficient gas, thus disrupting the functionality of the coinswap module.
Reasoning: As raised in the issue, if an attacker sends tokens of various denoms to the reserved pool address, k.GetPoolBalances(ctx, pool.EscrowAddress) could invoke k.bk.GetAllBalances that internally uses iteration, leading to a situation where the operation could fail if the array becomes very large.
However, pool creation is only allowed for whitelisted denoms, so it is impossible to obtain new tokens through AddLiquidity as raised in the issue. (See here and here).
Severity: Mid → Low.
In the worst-case scenario, the swap or RemoveLiquidity in coinswap might fail, but this only affects the auto swap during onboarding and does not impact the essential functions of the chain.
Patch:
We will patch this before v0.50 upgrade.
Change k.GetPoolBalances(ctx, pool.EscrowAddress) so that it does not use k.bk.GetAllBalances and only queries and returns the balance of standard coin, counter party coin, and pool coin.
Make appropriate changes for GetPoolBalances callers.
// GetPoolBalances return the liquidity pool by the specified anotherCoinDenom
func (k Keeper) GetPoolBalances(ctx sdk.Context, pool types.Pool) (coins sdk.Coins, err error) {
address, err := sdk.AccAddressFromBech32(pool.EscrowAddress)
if err != nil {
return coins, err
}
acc := k.ak.GetAccount(ctx, address)
if acc == nil {
return nil, errorsmod.Wrap(types.ErrReservePoolNotExists, pool.EscrowAddress)
}
balances := sdk.NewCoins()
balances.Add(k.bk.GetBalance(ctx, acc.GetAddress(), pool.StandardDenom))
balances.Add(k.bk.GetBalance(ctx, acc.GetAddress(), pool.CounterpartyDenom))
balances.Add(k.bk.GetBalance(ctx, acc.GetAddress(), pool.LptDenom))
return balances, nil
}
Because Canto is connected to other Cosmos networks via IBC, an arbitrary number of token denominations can coexist (and be donated) to an existing pool to DoS its liquidity operations, without any privilege required for an attacker.
The original report as published on Code4rena, by 0x1771. 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the pool balance function retrieves all token balances instead of only the relevant ones, making it vulnerable to gas exhaustion when many token types are present.
Impact
In the worst case, swaps or RemoveLiquidity in coinswap might fail, but this only affects the auto swap during onboarding and does not impact the essential functions of the chain.
What the attacker needed
Ability to send tokens to the pool escrow address; many token denominations exist on the chain
The fix
The fix changes GetPoolBalances to query only the standard coin, counterparty coin, and pool coin balances instead of all balances.
How it was found
Send tokens of many different denominations to the pool's escrow address.
The pool's balance function iterates over all these tokens, consuming excessive gas.
Liquidity operations such as swaps or RemoveLiquidity fail due to insufficient gas.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A pool contract keeps track of all tokens it holds by looping over every token type. If someone sends many different token types to the pool, the loop becomes very long and runs out of gas, so normal operations like swapping or removing liquidity fail. This is a denial-of-service because the attacker doesn't steal anything but stops the pool from working.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.