Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coinswap pool can be DoS #6

Closed
c4-bot-1 opened this issue Jun 15, 2024 · 2 comments
Closed

coinswap pool can be DoS #6

c4-bot-1 opened this issue Jun 15, 2024 · 2 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-28 🤖_02_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality

Comments

@c4-bot-1
Copy link
Contributor

Lines of code

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

Vulnerability details

Impact

DoS attack makes functions in coinswap unusable

Proof of Concept

In general, if anyone is running in the array and an attacker adds a large number of elements to the array, it will consume a large amount of gas while traversing the array, and the related function will fail to execute due to insufficient gas.

The AddLiquidity RemoveLiquidity calculateWithExactInput in coinswap will pass
k.GetPoolBalances The banlance of all tokens in the pool.

k.GetPoolBalances -> k.bk.GetAllBalances

Let's take a look at bk.GetAllBalances, which iterate through the balance(IterateAccountBalances) of all tokens in the pool and then add them to an array and sort them.

https://github.com/cosmos/cosmos-sdk/blob/d21620d1280538ddb1129af4979d62878850ff99/x/bank/keeper/view.go#L99

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()
}

sdk.NewCoins() returns Coins, Coins is an array:
https://github.com/cosmos/cosmos-sdk/blob/d21620d1280538ddb1129af4979d62878850ff99/types/coin.go#L191

When AddLiquidity is in the coinswap module, k.CreatePool is called to create a pool if the pool does not exist,

    pool = k.CreatePool(ctx, msg.MaxToken.Denom)

An attacker can create a large number of tokens with AddLiquidity (or perhaps in other ways)
and then send the balance to the target pool to be attacked.

When other users call functions in coinswap, the execution fails because the number of balances arrays returned by GetPoolBalances is too large, consumes too much gas, or the transaction execution failed because of insufficient gas.

Tools Used

vscode, manual

Recommended Mitigation Steps

Do not use bk.GetAllBalances to obtain the balances of all tokens, but only the balances of one token in the pool at a time.

Assessed type

DoS

@c4-bot-1 c4-bot-1 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Jun 15, 2024
c4-bot-9 added a commit that referenced this issue Jun 15, 2024
@c4-bot-11 c4-bot-11 added the 🤖_02_group AI based duplicate group recommendation label Jun 20, 2024
@howlbot-integration howlbot-integration bot added sufficient quality report This report is of sufficient quality duplicate-20 labels Jun 21, 2024
@c4-judge c4-judge added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value downgraded by judge Judge downgraded the risk level of this issue and removed 3 (High Risk) Assets can be stolen/lost/compromised directly labels Jun 26, 2024
@c4-judge
Copy link

3docSec changed the severity to 2 (Med Risk)

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jun 26, 2024
@c4-judge
Copy link

3docSec marked the issue as satisfactory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-28 🤖_02_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality
Projects
None yet
Development

No branches or pull requests

3 participants