IOP _ ThunderNFT 34964 - [Smart Contract - Low] Faulty Index out of Bounds

Faulty Index out of Bounds

Submitted on Sun Sep 01 2024 23:14:37 GMT-0400 (Atlantic Standard Time) by @Blockian for IOP | ThunderNFT

Report ID: #34964

Report type: Smart Contract

Report severity: Low

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

Impacts:

  • Contract fails to deliver promised returns, but doesn't lose value

Description

Thunder Exchange

Faulty Index out of Bounds

Description

An issue has been identified where the index out-of-bounds check is incorrectly implemented, potentially leading to incorrect error handling.

Root Cause

The problem originates from the current implementation of the getter functions for vector types in both the Asset Manager and Execution Manager modules.

Specifically, the checks for valid indices do not correctly account for the last index of vectors, leading to potential out-of-bound access. The problematic code is as follows:

The current check allows indices up to and including the vector length. However, since vector indices are zero-based, the valid range is from 0 to len - 1. This error could result in an out-of-bounds access attempt.

Impact

Luckily, the Fuel Storage Vector implementation includes a safeguard that verifies the index is within bounds during access. Therefore, the primary impact is the incorrect error log being generated, rather than any actual out-of-bound access or program failure, which is somewhere between an Insight and Low issue.

Proposed fix

Check the index is index < len

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 2 new folder called tests, 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_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:

Run it all!

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

Last updated

Was this helpful?