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

Wrong "fee" calculation can cause a user to receive zero amount #67

Open
hats-bug-reporter bot opened this issue Jan 31, 2024 · 5 comments
Open
Labels
duplicate This issue or pull request already exists

Comments

@hats-bug-reporter
Copy link

Github username: --
Twitter username: 97Sabit
Submission hash (on-chain): 0x3d7281573f7522cc131a8e44606498a2f7738e86c5a0d81018b020878d40b42e
Severity: high

Description:
Description
In the localSwap function, "fee" is calculated as follows:

uint256 fee = FixedPointMathLib.mulWadDown(amount, _vaultFee);

Here's a breakdown of what the above means:

  • Multiplying x * y
  • Dividing by the Wad unit (10^18)
  • Rounding down

And _vaultFee can only be <=1e18.

In a situation where a user wants to swap 100 tokens and sets minOut to zero. And _vault fee is set to 1e18, such a user will get zero as the output amount without being frontrun.

For example:

  • _vaultFee = 1e18
  • amount = 100 tokens
  • Let's assume tokens have 18 decimals like ether
  • So amount = 100 * 10^18 tokens

Calculating mulWadDown(amount, _vaultFee):

  • amount = 100 * 10^18
  • _vaultFee = 1e18
  • amount * _vaultFee = 100 * 10^18 * 1e18 = 100 * 10^36
  • 100 * 10^36 / 10^18 = 100 * 10^18
  • Round down = 100 * 10^18

The fee amount will be 100 tokens. And a user gets zero without being frontrun.

  1. Proof of Concept (PoC) File

https://github.com/catalystdao/catalyst/blob/27b4d0a2bca177aff00def8cd745623bfbf7cb6b/evm/src/CatalystVaultVolatile.sol#L572

https://github.com/catalystdao/catalyst/blob/27b4d0a2bca177aff00def8cd745623bfbf7cb6b/evm/src/CatalystVaultCommon.sol#L348

@hats-bug-reporter hats-bug-reporter bot added the bug Something isn't working label Jan 31, 2024
@reednaa
Copy link
Collaborator

reednaa commented Jan 31, 2024

Dublicate of #13 (and similar)

Minout can be used to protect against large fee.

@reednaa reednaa added the duplicate This issue or pull request already exists label Jan 31, 2024
@ololade97
Copy link

Dublicate of #13 (and similar)

Minout can be used to protect against large fee.

I checked the the #13 issue. The vulnerability isn't similar to this.

@reednaa reednaa removed the bug Something isn't working label Feb 13, 2024
@reednaa
Copy link
Collaborator

reednaa commented Feb 13, 2024

Please provide arguments.

@ololade97
Copy link

Ok.

  • _vaultFee is a percentage fee set by the contract (<= 100%)
  • amount is the number of tokens the user wants to swap
  • fee = amount * _vaultFee / 10^18 (rounding down)

So if:

  • _vaultFee = 100% (1e18)
  • amount = 100 tokens

Then:

fee = 100 * 1e18 / 10^18 = 100 tokens

( uint256 fee = FixedPointMathLib.mulWadDown(amount, _vaultFee);)

User gets 0 tokens from the swap

@ololade97
Copy link

I wanted to add. Unlike the frontrunning issue discussed in #13, it's legal for the protocol to set the fee to 1e18 from the start.

So here, I'm not talking about frontrunning. I've pointed out the legal way the protocol operates which could cause users to get 0 swap amount.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants