IOP _ ThunderNFT 34659 - [Smart Contract - Low] Pool Balance Inflation

Pool Balance Inflation

Submitted on Mon Aug 19 2024 23:04:18 GMT-0400 (Atlantic Standard Time) by @Blockian for IOP | ThunderNFT

Report ID: #34659

Report type: Smart Contract

Report severity: Low

Target: https://github.com/ThunderFuel/smart-contracts/tree/main/contracts-v1/pool

Impacts:

  • Direct theft of any user funds, whether at-rest or in-motion, other than unclaimed yield

Description

Thunder Exchange

Pool Balance Inflation

Description

A issue exists where pool balances can be artificially inflated without any additional deposits. This flaw enables malicious actors to withdraw inflated funds, effectively stealing from the pool.

Root Cause

The vulnerability stems from the current implementation of the transfer function, as shown below:

The transfer process operates as follows:

  1. Read the from balance

  2. Read the to balance

  3. Update the from balance to from + amount

  4. Update the to balance to to + amount

However, if from and to are the same account, the update in step 3 is effectively overwritten by the update in step 4. As a result, the attacker retains their original balance plus the transferred amount.

Impact

This vulnerability allows the creation of inflated balances without corresponding funds. When a user with such an inflated balance withdraws from the pool, they can deplete the pool's reserves, leaving other users unable to withdraw their legitimate funds.

But, currently, the impact is classified as Low, since the transfer function can only be invoked by the ThunderExchange itself, which is not malicious.

Proposed fix

There are two primary solutions to address this issue:

  1. Ensure that from and to are not the same account.

  2. Adjust the order of operations as follows:

    1. Read the from balance

    2. Update the from balance to from + amount

    3. Read the to balance

    4. Update the to balance to to + amount

Proof of concept

Proof of Concept

There are some steps to follow:

  • Create forc.toml in contracts-v1 and add the below in the forc.toml:

  • Create 3 new folder called tests, test_user, and test_asset under the contracts-v1 directory:

  • In the each folder create a folder named src with a file called main.sw, and a forc.toml file. The folder tree will look like this:

tests folder

In the tests folder.

  • Add the below in the forc.toml:

  • Add the below in the main.sw:

test_user folder

In the test_user folder.

  • Add the below in the forc.toml:

  • Add the below in the main.sw:

test_asset folder

In the test_asset folder. The test asset is simply the Fuel Team SRC3 example

  • Add the below in the forc.toml:

  • Add the below in the main.sw:

Interfaces

Now we need to add the test_user interface to interact with. In the interfaces/src folder, in the lib.sw add the following line:

Additionally, create a file called test_user_interface.sw in the interfaces/src folder and add the following:

Run it all!

Simply run forc test in smart-contracts/contracts-v1.

Last updated

Was this helpful?