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:
Read the
frombalanceRead the
tobalanceUpdate the
frombalance tofrom + amountUpdate the
tobalance toto + 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:
Ensure that from and to are not the same account.
Adjust the order of operations as follows:
Read the
frombalanceUpdate the
frombalance tofrom + amountRead the
tobalanceUpdate the
tobalance toto + amount
Proof of concept
Proof of Concept
There are some steps to follow:
Create
forc.tomlincontracts-v1and add the below in theforc.toml:
Create 3 new folder called
tests,test_user, andtest_assetunder thecontracts-v1directory:In the each folder create a folder named
srcwith a file calledmain.sw, and aforc.tomlfile. 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?