code-423n4 / 2024-05-canto-findings

0 stars 0 forks source link

DoS vulnerability in `coinswap` pool #20

Closed howlbot-integration[bot] closed 4 months ago

howlbot-integration[bot] commented 4 months ago

Lines of code

https://github.com/code-423n4/2024-05-canto/blob/d1d51b2293d4689f467b8b1c82bba84f8f7ea008/canto-main/x/coinswap/keeper/keeper.go#L168 https://github.com/code-423n4/2024-05-canto/blob/d1d51b2293d4689f467b8b1c82bba84f8f7ea008/canto-main/x/coinswap/keeper/keeper.go#L264-L265

Vulnerability details

Description

The balances are calculated from k.GetPoolBalances(ctx, pool.EscrowAddress) that actually calls the k.bk.GetAllBalances function.

This function loop through the balance of all the token in the loop. Ultimately, if the array is too large, the function can fail due to insufficient gas.

A malicious actor can create a large number of tokens using AddLiquidity for example.

He could then transfer these tokens to the target pool for an attack.

Impact

This can cause a DoS.

POC

func (k Keeper) GetPoolBalances(ctx sdk.Context, escrowAddress string) (coins sdk.Coins, err error) {
    address, err := sdk.AccAddressFromBech32(escrowAddress)
    if err != nil {
        return coins, err
    }
    acc := k.ak.GetAccount(ctx, address)
    if acc == nil {
        return nil, errorsmod.Wrap(types.ErrReservePoolNotExists, escrowAddress)
    }
    return k.bk.GetAllBalances(ctx, acc.GetAddress()), nil
}
func (k BaseViewKeeper) GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins {
    balances := sdk.NewCoins()
    k.IterateAccountBalances(ctx, addr, func(balance sdk.Coin) bool {
        balances = balances.Add(balance)
        return false
    })

    return balances.Sort()
}

Tools Used

Manual review

Recommended Mitigation

bk.GetAllBalances shouldn't be used to get the balances of all tokens. Instead, you need to have the balance of 1 token in the pool.

Assessed type

DoS

poorphd commented 4 months ago
3docSec commented 4 months ago

I find M to be appropriate for this group.

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

c4-judge commented 4 months ago

3docSec marked the issue as satisfactory

c4-judge commented 4 months ago

3docSec marked issue #28 as primary and marked this issue as a duplicate of 28