Attackathon _ Fuel Network 33248 - [Smart Contract - High] Incorrect Implementation of IFP Floor and
Last updated
Was this helpful?
Last updated
Was this helpful?
Submitted on Mon Jul 15 2024 23:00:54 GMT-0400 (Atlantic Standard Time) by @Blockian for
Report ID: #33248
Report type: Smart Contract
Report severity: High
Target: https://github.com/FuelLabs/sway-libs/tree/0f47d33d6e5da25f782fc117d4be15b7b12d291b
Impacts:
Temporary freezing of funds for at least 1 hour
Direct theft of any user funds, whether at-rest or in-motion, other than unclaimed yield
Permanent freezing of funds
Incorrect math
The current implementation of the floor and ceil functions in sway-libs
for the signed Fixed Point numbers is flawed. The implementation returns the wrong number for every negative input.
The floor
function from the IFP64
implementation is as follows:
Similarly, the ceil
function is implemented as:
Several issues exist within these implementations, as detailed below:
Incorrect Usage of UFP32::from
: The from
function does not multiply the input number by the denominator
, resulting in UFP32::from(1u32)
being 0.0..01
instead of 1
.
Incorrect Subtraction in floor Function: The floor function attempts to subtract 1
even when the number is already rounded, so for negative numbers different from zero, it subtracts 0.0..01
(covered by the first issue). Regardless if it's rounded already
Unreachable Branch in ceil Function: The ceil function contains a branch that changes the sign of the number, which should not occur. Fortunately, this branch is unreachable.
NOTE: This error affects all IFP
types.
This issue affects every implementation of signed fixed point numbers in the Fuel ecosystem. Any project utilizing these implementations will encounter incorrect calculations.
In the crypto space, even minor errors, like off by one, can lead to substantial financial losses, underscoring the critical nature of this bug.
By examining the mathematical definitions of floor
and ceil
, we observe that for negative numbers, ceil
acts as floor
for positive numbers and vice versa. Thus, a simpler implementation can be adopted:
This revised implementation ensures correct handling of both positive and negative fixed-point numbers.
Run the POC's with forc test